refactor: quitar AsyncLocalStorage del locator y dejar currentDb simple

Co-authored-by: aider (openrouter/openai/gpt-5) <aider@aider.chat>
main
brobert 1 month ago
parent ac680ac467
commit 4c9f4d1439

@ -1,5 +1,4 @@
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.
@ -13,18 +12,11 @@ export class DbNotConfiguredError extends Error {
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.
* Se permite sobrescribir (útil en tests).
*/
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;
}
@ -32,13 +24,6 @@ export function setDb(db: Database): void {
* Obtiene la instancia global de DB o lanza si no está configurada.
*/
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;
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 {
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 {
currentDb = null;
// En tests no tocamos testScope para no invalidar contextos de otras suites en paralelo
}
/**

Loading…
Cancel
Save