Implementing Transactions in `drizzle-orm` with `Effect` Services

Probably a dumb question, but I'm using drizzle-orm with Effect and I've started implementing some services for each table, like this for example:

import { PgDrizzle } from "@effect/sql-drizzle/Pg";

const make = Effect.gen(function* () {
  const db = yield* PgDrizzle;
  yield* Effect.log("Profile effect initialized");

  const insert = (profile: Schema.Schema.Type<typeof InsertProfileSchema>) =>
    Effect.gen(function* () {
      //
    });

  return {
    insert,
  };
});

export class Profile extends Context.Tag("core/Profile")<Profile, Effect.Effect.Success<typeof make>>() {
  static Live = Layer.effect(Profile, make).pipe(Layer.provide(DatabaseLive));
}

The thing is, I want to use transactions, but since I'm providing the DatabaseLive service I'm not sure how to make the service use the transaction instead of the db directly. Obviously I can just do something like this, but would prefer using the service
await db.transaction(async (tx) => {
  await tx.update(accounts).set({ balance: sql`${accounts.balance} - 100.00` }).where(eq(users.name, 'Dan'));
  await tx.update(accounts).set({ balance: sql`${accounts.balance} + 100.00` }).where(eq(users.name, 'Andrew'));
});

For context the DatabaseLive is just this
const SqlLive = Pg.PgClient.layer({
  database: Config.succeed("db"),
});
const DrizzleLive = PgDrizzle.layer.pipe(Layer.provide(SqlLive));
export const DatabaseLive = Layer.mergeAll(SqlLive, DrizzleLive);
Was this page helpful?