Integrating Effect into an Existing Codebase with Drizzle Transactions

Hi all, feels like a similar question to ^

New to effect, and trying to understand how to integrate it into an existing (large but not huge) codebase. So I was hoping to incrementally adopt effect.

A key pattern we have is we are using drizzle, and for RLS are almost always working with transactions. So I already have access to the existing db instance from drizzle.

Heres an example of typical code

export type Callback<T> = (
  txn: DrizzleTransactionScope,
  context: Context,
) => T | Promise<T>;

export async function txnWithContext<T>(
  context: Context,
  callback: Callback<T>,
): Promise<T> {
  return await db.transaction(async (txn) => {
    await txn.execute(
      sql`select set_config('rls.organisation_id', ${context.organisationId}, TRUE);`,
    );
    return callback(txn, context);
  });
}

async function createFoo(tx: DrizzleTransactionScope, values: {name:string, age:number}) {
  await tx.insert(schema.foo).values(values);
}

async function createBar(tx: DrizzleTransactionScope, values: {name:string}) {
  await tx.insert(schema.bar).values(values);
}

async function createUserService() {
  await txnWithContext({organisationId: 'test'}, async (tx, context) => {
    await createFoo(tx, {name: 'John Doe', age: 30});
    await createBar(tx, {name: 'Something Else'});
  });
}


... Now I kinda struggling on where to start.
Was this page helpful?