method DB.withConnection
DB.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.

Examples

Example 1

const result = await DB.withConnection(postgresConnector, (db) =>
  db.withTransaction(async (tx) => {
    await tx.execute("INSERT INTO orders (user_id) VALUES (?)", [userId]).run();
    return tx.query("SELECT last_insert_rowid()").run();
  })
).run();

Type Parameters

Parameters

connector: DBConnector
fn: (db: DB) => Task<R, E>

Return Type

Usage

import { DB } from ".";