Files
evilspins/server/db/index.ts
valere ffd5c5d7eb
All checks were successful
Deploy App / build (push) Successful in 49s
Deploy App / deploy (push) Successful in 22s
try fix .env with NUXT_ prefix
2026-02-10 18:44:00 +01:00

32 lines
786 B
TypeScript

import { drizzle } from 'drizzle-orm/libsql'
import * as schema from './schema'
let _db: ReturnType<typeof drizzle> | null = null
export function useDB() {
if (_db) return _db
let dbPath = process.env.NUXT_PATH_DB
if (!dbPath) {
throw new Error('NUXT_PATH_DB is not configured')
}
// Convertir le chemin en URL file:// si ce n'est pas déjà une URL
if (!dbPath.startsWith('file:') && !dbPath.startsWith('libsql:') && !dbPath.startsWith('http')) {
// Si c'est un chemin relatif, le rendre absolu
if (!dbPath.startsWith('/')) {
dbPath = `file:${process.cwd()}/${dbPath}`
} else {
dbPath = `file:${dbPath}`
}
}
console.log('🗄️ Connexion à la DB:', dbPath)
_db = drizzle(dbPath, { schema })
return _db
}
export { schema }