|
|
|
@ -1,5 +1,4 @@
|
|
|
|
import type { Database } from 'bun:sqlite';
|
|
|
|
import type { Database } from 'bun:sqlite';
|
|
|
|
import { AsyncLocalStorage } from 'node:async_hooks';
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* Error específico cuando se intenta acceder a la DB sin haberla configurado.
|
|
|
|
* Error específico cuando se intenta acceder a la DB sin haberla configurado.
|
|
|
|
@ -13,18 +12,11 @@ export class DbNotConfiguredError extends Error {
|
|
|
|
|
|
|
|
|
|
|
|
let currentDb: Database | null = null;
|
|
|
|
let currentDb: Database | null = null;
|
|
|
|
|
|
|
|
|
|
|
|
// AsyncLocalStorage para aislar DB por contexto en tests (paralelismo seguro)
|
|
|
|
|
|
|
|
let testScope: AsyncLocalStorage<Database> | null = null;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* Establece la instancia global de DB.
|
|
|
|
* Establece la instancia global de DB.
|
|
|
|
* Se permite sobrescribir (útil en tests).
|
|
|
|
* Se permite sobrescribir (útil en tests).
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
export function setDb(db: Database): void {
|
|
|
|
export function setDb(db: Database): void {
|
|
|
|
if (process.env.NODE_ENV === 'test') {
|
|
|
|
|
|
|
|
if (!testScope) testScope = new AsyncLocalStorage<Database>();
|
|
|
|
|
|
|
|
try { testScope.enterWith(db); } catch {}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
currentDb = db;
|
|
|
|
currentDb = db;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@ -32,13 +24,6 @@ export function setDb(db: Database): void {
|
|
|
|
* Obtiene la instancia global de DB o lanza si no está configurada.
|
|
|
|
* Obtiene la instancia global de DB o lanza si no está configurada.
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
export function getDb(): Database {
|
|
|
|
export function getDb(): Database {
|
|
|
|
if (process.env.NODE_ENV === 'test') {
|
|
|
|
|
|
|
|
if (!testScope) testScope = new AsyncLocalStorage<Database>();
|
|
|
|
|
|
|
|
const scoped = testScope.getStore();
|
|
|
|
|
|
|
|
if (scoped) return scoped;
|
|
|
|
|
|
|
|
// En tests, no hacemos fallback a currentDb para evitar contaminación entre suites
|
|
|
|
|
|
|
|
throw new DbNotConfiguredError('Database has not been configured. Call setDb(db) before using getDb().');
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if (currentDb) return currentDb;
|
|
|
|
if (currentDb) return currentDb;
|
|
|
|
throw new DbNotConfiguredError('Database has not been configured. Call setDb(db) before using getDb().');
|
|
|
|
throw new DbNotConfiguredError('Database has not been configured. Call setDb(db) before using getDb().');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
@ -48,7 +33,6 @@ export function getDb(): Database {
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
export function resetDb(): void {
|
|
|
|
export function resetDb(): void {
|
|
|
|
currentDb = null;
|
|
|
|
currentDb = null;
|
|
|
|
// En tests no tocamos testScope para no invalidar contextos de otras suites en paralelo
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
@ -56,7 +40,6 @@ export function resetDb(): void {
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
export function clearDb(): void {
|
|
|
|
export function clearDb(): void {
|
|
|
|
currentDb = null;
|
|
|
|
currentDb = null;
|
|
|
|
// En tests no tocamos testScope para no invalidar contextos de otras suites en paralelo
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
|