Database wrapper with Task/Stream semantics.
Example 1
Example 1
// With a connector (recommended for production) const result = await DB.withConnection(myConnector, (db) => db.query("SELECT * FROM users") ).run(); // With a bare adapter (for testing or custom lifecycle) const db = new DB(adapter); const users = await db.query("SELECT * FROM users").run();
new
DB(adapter: DBAdapter)
withConnection<R,E,>(connector: DBConnector,fn: (db: DB) => Task<R, E>,): Task<R, E | ConnectionFailed>
Execute operations with a connection acquired from the connector. The connection is automatically released after the operation completes, whether successful or failed.
execute(sql: string,params?: unknown[],): Task<number, QueryFailed | ConstraintViolation>
Execute INSERT/UPDATE/DELETE and return affected row count.
query<T extends Record<string, any> = Record<string, any>>(sql: string,params?: unknown[],): Task<T[], QueryFailed | ConstraintViolation>
Execute a SELECT query and return rows.
stream<T extends Record<string, any> = Record<string, any>>(sql: string,params?: unknown[],): Source<T, QueryFailed>
Stream rows from a SELECT query for memory-efficient processing.
If the adapter supports cursor-based streaming (via the optional stream method), rows are yielded one at a time. Otherwise, the full result set is buffered in memory before streaming.
private
transaction(): Task<DBTransaction, TransactionFailed>
withTransaction<R>(fn: (tx: DBTransaction) => Promisable<R>): Task<R, >