|
|
|
@ -28,13 +28,21 @@ function applyDefaultPragmas(instance: any): void {
|
|
|
|
* - En Node (Vite dev SSR): better-sqlite3
|
|
|
|
* - En Node (Vite dev SSR): better-sqlite3
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
async function importSqliteDatabase(): Promise<any> {
|
|
|
|
async function importSqliteDatabase(): Promise<any> {
|
|
|
|
|
|
|
|
// En tests, forzar bun:sqlite para evitar mezclar engines con la conexión de tests
|
|
|
|
|
|
|
|
const nodeEnv = String(process.env.NODE_ENV || '').toLowerCase();
|
|
|
|
|
|
|
|
if (nodeEnv === 'test') {
|
|
|
|
|
|
|
|
const mod: any = await import('bun:sqlite');
|
|
|
|
|
|
|
|
return (mod as any).Database || (mod as any).default || mod;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// En desarrollo (Vite SSR), cargar better-sqlite3 vía require de Node para mantener el contexto CJS
|
|
|
|
// En desarrollo (Vite SSR), cargar better-sqlite3 vía require de Node para mantener el contexto CJS
|
|
|
|
if (import.meta.env.DEV) {
|
|
|
|
if (typeof import.meta !== 'undefined' && (import.meta as any).env?.DEV) {
|
|
|
|
const modModule: any = await import('node:module');
|
|
|
|
const modModule: any = await import('node:module');
|
|
|
|
const require = modModule.createRequire(import.meta.url);
|
|
|
|
const require = modModule.createRequire(import.meta.url);
|
|
|
|
const mod = require('better-sqlite3');
|
|
|
|
const mod = require('better-sqlite3');
|
|
|
|
return (mod as any).default || (mod as any).Database || mod;
|
|
|
|
return (mod as any).default || (mod as any).Database || mod;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// En producción (Bun en runtime), usar bun:sqlite nativo
|
|
|
|
// En producción (Bun en runtime), usar bun:sqlite nativo
|
|
|
|
const mod: any = await import('bun:sqlite');
|
|
|
|
const mod: any = await import('bun:sqlite');
|
|
|
|
return (mod as any).Database || (mod as any).default || mod;
|
|
|
|
return (mod as any).Database || (mod as any).default || mod;
|
|
|
|
@ -163,3 +171,16 @@ export async function getDb(filename: string = 'tasks.db'): Promise<any> {
|
|
|
|
_db = await openDb(filename);
|
|
|
|
_db = await openDb(filename);
|
|
|
|
return _db;
|
|
|
|
return _db;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* Cierra y resetea la instancia compartida (útil en tests para evitar manejar
|
|
|
|
|
|
|
|
* un descriptor abierto al borrar el archivo de la BD en disco).
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
export function closeDb(): void {
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
if (_db && typeof _db.close === 'function') {
|
|
|
|
|
|
|
|
_db.close();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
} catch {}
|
|
|
|
|
|
|
|
_db = null;
|
|
|
|
|
|
|
|
}
|
|
|
|
|