class DB

Database wrapper with Task/Stream semantics.

Examples

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();

Constructors

new
DB(adapter: DBAdapter)

Static Methods

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.

Methods

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.