Assistance Needed for Implementing Transactions with Drizzle-ORM and Postgres in Effect-TS
Hey guys. I was wondering if I could get some assistance / pointed in the right direction for an effect-drizzle improvement. Presently I am attempting to utilize drizzle’s transactions api while using a Postgres database. I’ve made a service derived from Anna | Devminer’s code that reads as follows:
import type { PgRemoteDatabase } from "drizzle-orm/pg-proxy";// Define the runInTx function as a serviceexport class RunInTx extends Effect.Service<{ runInTx: <A, E>( e: (tx: Parameters<PgRemoteDatabase<Record<string, never>>['transaction']>[0]) => Effect.Effect<A, E> ) => Effect.Effect<A, E | DrizzleDatabaseError>;}>()("app/RunInTx", { effect: Effect.gen(function*() { const db = yield* PgDrizzle; const runInTx = <A, E>( e: ( tx: Parameters<Parameters<typeof db.transaction>[0]>[0], ) => Effect.Effect<A, E>, ) => Effect.async<A, E | DrizzleDatabaseError>((resume) => { db.transaction((tx) => e(tx).pipe(Effect.runPromiseExit)) .then((result) => resume(result)) .catch((e) => resume(Effect.fail(new DrizzleDatabaseError({ inner: e }))), ); }); return { runInTx } as const; }), // Inject PgDrizzle layer dependency dependencies: [DatabaseLive],}) { }
import type { PgRemoteDatabase } from "drizzle-orm/pg-proxy";// Define the runInTx function as a serviceexport class RunInTx extends Effect.Service<{ runInTx: <A, E>( e: (tx: Parameters<PgRemoteDatabase<Record<string, never>>['transaction']>[0]) => Effect.Effect<A, E> ) => Effect.Effect<A, E | DrizzleDatabaseError>;}>()("app/RunInTx", { effect: Effect.gen(function*() { const db = yield* PgDrizzle; const runInTx = <A, E>( e: ( tx: Parameters<Parameters<typeof db.transaction>[0]>[0], ) => Effect.Effect<A, E>, ) => Effect.async<A, E | DrizzleDatabaseError>((resume) => { db.transaction((tx) => e(tx).pipe(Effect.runPromiseExit)) .then((result) => resume(result)) .catch((e) => resume(Effect.fail(new DrizzleDatabaseError({ inner: e }))), ); }); return { runInTx } as const; }), // Inject PgDrizzle layer dependency dependencies: [DatabaseLive],}) { }
Running this service results in the following:
“DrizzleDatabaseError: Transactions are not supported by the Postgres Proxy driver”
“DrizzleDatabaseError: Transactions are not supported by the Postgres Proxy driver”
Looking at drizzle-orm’s repos there are a few different Postgres drivers. Namely node-postgres and database-js
Seeing as both drizzle-orm's postgres-js driver and effect's PgClient both use the same “postgres-js” library I figured I’d ask how we can possibly compose a new effect-drizzle driver using the drizzle-orm postgres-js package. I could use a bit of help.