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 = DB.from(adapter); const users = await db.query("SELECT * FROM users").run();
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.
executeBatch(sql: string,paramsArray: unknown[][]): Task<number[], QueryFailed | ConstraintViolation>
Execute multiple commands in a batch. Leverages optimized adapter method if available.
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.
withTransaction<R>(fn: (tx: DBTransaction) => Promisable<R>): Task<R, TransactionFailed | QueryFailed | ConstraintViolation>
Execute a callback within a transaction. Supports nested transactions via SQL savepoints.