MiNiMALM
tRPC3y ago
12 replies
MiNiMAL

Function to release context post-batching?

In our application every customer has their own schema, therefore we need a db client per request which I was hoping to leverage with TRPC context.

Ideally, we'd be able to:
- Connect to the database in context ✅
- Set the search_path to match the client's JWT in context ✅
- Run our procedures using that database connection from context ✅
- Release the database connection after all batching is finished ❌

Is this not currently possible with TRPC? Right now I think I can only connect to the database using a per procedure middleware.

const db = // connect to db

await db.execute('SET search_path=...')

try {
   return await next({ ctx: { ...opts.ctx, db } })
} catch () {
   // release connection
} finally {
   // release connection
}


This works, but it requires establishing a connection/search_path for every procedure in a batch, instead of just once in context and releasing after the batch. Is there a way to accomplish this?
Was this page helpful?