Express integration with Drizzle ORM

Hey, I'm trying to use the express integration and Drizzle ORM for database, I need to wrap each request within a transaction, here is the code I've come to but I'm not really satisfied of the way it's done because I'm calling 2 time the run function I feel like it's wrong, any advice how I could achieve this in a better way ?

export class Express extends Context.Tag("Express")<
  Express,
  ReturnType<typeof express>
>() {
  private static request =
    (method: HttpMethod) =>
    <A, E, R>(
      path: string,
      body: (
        req: Request,
        res: Response,
        tx: DatabaseTx,
      ) => Effect.Effect<A, HttpError | E, R>,
    ) =>
      Effect.gen(function* () {
        const app = yield* Express
        const run = yield* FiberSet.makeRuntime<R>()
        const databaseClient = yield* DatabaseClient
        yield* Effect.logInfo(
          `[Express] Registered ${method.toUpperCase()} ${path}`,
        )
        app[method](path, (req, res) =>
          run(
            Effect.tryPromise(() =>
              databaseClient.transaction(async (tx) => run(body(req, res, tx))),
            ).pipe(
              Effect.andThen((data) => res.json(data)),
              HttpErrorCatcher(res),
              UnhandledErrorCatcher(res),
              Effect.catchAllDefect((e) => Console.log(e)),
            ),
          ),
        )
      })
Was this page helpful?