T
tRPC

Function to release context post-batching?

Function to release context post-batching?

MMiNiMAL11/2/2023
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
}
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?
AKAlex / KATT 🐱11/2/2023
Maybe you can do some lazy getter in async local storage? Oh yeah hear you I'll think about it, maybe there's something we could do You could revert this https://github.com/trpc/trpc/pull/2600
MMiNiMAL11/2/2023
Thanks, I'll peek at that. I'm also going to experiment with moving the db connection logic onto the express middleware layer Was there a reason for the removal of teardown? I'm happy to suggest reverting but lack the context.
AKAlex / KATT 🐱11/2/2023
I didn't think anyone was using it and we did a new major Just housekeeping Just make sure the teardown gets the context as an arg
MMiNiMAL11/2/2023
@Alex / KATT 🐱 I think I got this working just with express middleware. Somewhat hacky but...it's working!
app.use(async (req, res, next) => {
const client = await getClient()
req.client = client
next()
})

app.use('/trpc', async (req, res, next) => {
await createExpressMiddleware({
router: appRouter,
createContext,
})(req, res, next)
next()
})

app.use(async (req, res) => {
if (req.client) {
await req.client.release()
}
})
app.use(async (req, res, next) => {
const client = await getClient()
req.client = client
next()
})

app.use('/trpc', async (req, res, next) => {
await createExpressMiddleware({
router: appRouter,
createContext,
})(req, res, next)
next()
})

app.use(async (req, res) => {
if (req.client) {
await req.client.release()
}
})
and actually...this can be simplified into just one middleware for /trpc
app.use('/trpc', async (req, res, next) => {
const client = await getClient()
req.client = client

await createExpressMiddleware({
router: appRouter,
createContext
})(req, res, next)

if (req.client) {
await req.client.release()
}
})
app.use('/trpc', async (req, res, next) => {
const client = await getClient()
req.client = client

await createExpressMiddleware({
router: appRouter,
createContext
})(req, res, next)

if (req.client) {
await req.client.release()
}
})

Looking for more? Join the community!

T
tRPC

Function to release context post-batching?

Join Server
Recommended Posts
forwarding headers in solidjsI have a setup with solidstart, and im trying to forward the client headeres to ttrpc. Here's my seSupport @tanstack/react-query 5.4.3will it support 5.4.3 @tanstack/react-query when i install above version with my next.js it unable tHelp with inferring output```ts return ctx.prisma.product.findMany({ include: { price: { select: { Would you recommend tRPC’s usage in this case?So we have two applications, one existing, one about to be started. These two applications will talkHow to limit payload size?Is there a way to limit the payload size? E.g., if a particularly large JSON payload is sent to the Next.js app directoryHi. I have been able to set up tRPC with the app directory in Next.js. It was a frustrating experientrpc errorTrpc not working in next js 14 app router. Please help me to resolveFeature or happy accident: error message from `cause` being used as TRPC's error messageWhen a procedure throws a TRPC error with no message, it turns out that if the thrown error has a caWhen error in tRPC route, the error message is vaugeThere are no line numbers when a runtime error happens in a tRPC route. Surely I am missing somethinHow to have 2 separate trpc clients for http and websockets?I'm trying to have a setup where I can use trpc with 2 separate clients, one for normal HTTP requestHow to configure context for a standalone server?Are there any examples out there on how to set up context for a standalone server? The docs do incluDocs hard to follow with so much `next` codeNot having a great experience with the docs. There are many examples that use `next`, making it hardadding information to the QueryKey that is not part of the procedure inputwe have the situation that we have our trpc routes read some specific information from the browsesr How to make typed error responses in the context of a specific query?Each query has a typed (success) response, although it seems all queries have a shared type for the What's the negative code returned in error responses?What's the negative error code returned in errors responses and how is it meant to be used by the clTRPC File as InputHi can someone pls help me: https://stackoverflow.com/questions/77381516/nextjs-trpc-file-as-input`refetchOnWindowFocus` with RSC?Hi. Is there any way to have `refetchOnWindowFocus` work with server components? Seems like it onlyMulti useSubscription with trpc react -query integration failed to find second subusing trpc in nx monorepo, node version is 20 with yarn. backend is using trpc with express integratIs there any easy way to create a Type for a trpc routerI want to publish a set of trpc api client to npm, which will be used by my customer directly. But iTRPC with Nextjs 13 App RouterIm trying to implement TRPC with my already existing Nextjs 13 App to get typesafe API's. I've manag