UNPKG

effect-sql-kysely

Version:

A full-featured integration between `@effect/sql` and `Kysely` that provides type-safe database operations with Effect's powerful error handling and resource management.

31 lines (26 loc) 685 B
import type { Kysely } from "kysely"; import { DeferredPromise } from "./DeferredPromise.js"; export async function beginConnection<DB>(db: Kysely<DB>) { const connection = new DeferredPromise<Kysely<DB>>(); const result = new DeferredPromise<unknown>(); // Do NOT await this line. const transaction = db .connection() .execute((trx) => { connection.resolve(trx); return result.promise; }) .catch(() => null); const conn = await connection.promise; return { conn, success() { result.resolve(null); return transaction; }, fail() { result.reject(new Error("failure")); return transaction; }, }; }