import { DbContext, ISOLATION_LEVEL, TDbConnConf, TDbContextOption } from "@simplysm/sd-orm-common"; import { Type } from "@simplysm/sd-core-common"; import { NodeDbContextExecutor } from "./NodeDbContextExecutor"; export class SdOrm { constructor( readonly dbContextType: Type, readonly config: TDbConnConf, readonly dbContextOpt?: Partial, ) {} async connectAsync( callback: (conn: T) => Promise, isolationLevel?: ISOLATION_LEVEL, ): Promise { const db = new this.dbContextType(new NodeDbContextExecutor(this.config), { dialect: this.dbContextOpt?.dialect ?? this.config.dialect, database: this.dbContextOpt?.["database"] ?? this.config["database"], schema: this.dbContextOpt?.["schema"] ?? this.config["schema"], }); return await db.connectAsync(async () => await callback(db), isolationLevel); } async connectWithoutTransactionAsync(callback: (conn: T) => Promise): Promise { const db = new this.dbContextType(new NodeDbContextExecutor(this.config), { dialect: this.dbContextOpt?.dialect ?? this.config.dialect, database: this.dbContextOpt?.["database"] ?? this.config["database"], schema: this.dbContextOpt?.["schema"] ?? this.config["schema"], }); return await db.connectWithoutTransactionAsync(async () => await callback(db)); } }