santi
santi9mo ago

Create client that is used in every request, without re-creating client

Hi all, Consider the following
export const createContextInner = async ({
req,
res,
}: {
req?: Context['req']
res?: Context['res']
}): Promise<Context> => {
const app = await NestFactory.createApplicationContext(AppModule)
return {
app,
prisma,
req,
res,
supabaseServerClient: null,
user: null,
}
}
export const createContextInner = async ({
req,
res,
}: {
req?: Context['req']
res?: Context['res']
}): Promise<Context> => {
const app = await NestFactory.createApplicationContext(AppModule)
return {
app,
prisma,
req,
res,
supabaseServerClient: null,
user: null,
}
}
This is annoying in local development because it needs to create the nest app for every request. The same thing with the prisma client. We'd like to create this once and use in every request. Is this possible?
3 Replies
BeBoRE
BeBoRE9mo ago
Move it outside of the context creator?
santi
santiOP9mo ago
To where though? It'd be ideal here
export const t = initTRPC.context<Context>().create({
transformer: superjson,
errorFormatter({ shape }) {
return shape
},
})
export const t = initTRPC.context<Context>().create({
transformer: superjson,
errorFormatter({ shape }) {
return shape
},
})
BeBoRE
BeBoRE9mo ago
The global context