tRPCttRPC
Powered by
JavascriptMickJ
tRPC•3y ago•
6 replies
JavascriptMick

is context cached?

If I put an object on the context that represents the User record from my database...
export async function createContext(event: H3Event){
  if (!dbUser) {
    const userService = new UserService();
    dbUser = await userService.getUser(user_id);
  }
  
  return {
    dbUser,
    blah....
  }  
};
export async function createContext(event: H3Event){
  if (!dbUser) {
    const userService = new UserService();
    dbUser = await userService.getUser(user_id);
  }
  
  return {
    dbUser,
    blah....
  }  
};

And then I have a router function which changes important details of the user....
changeAccountLevel: protectedProcedure
.input(z.object({ user_id: z.number(), level: z.number() }))
.query(async ({ ctx, input }) => {
  const userService = new UserService();
  const user = await userService.changeAccountLevel(input.user_id, input.level);
  return {
    user,
  }
}),
changeAccountLevel: protectedProcedure
.input(z.object({ user_id: z.number(), level: z.number() }))
.query(async ({ ctx, input }) => {
  const userService = new UserService();
  const user = await userService.changeAccountLevel(input.user_id, input.level);
  return {
    user,
  }
}),

Do I need to 'mutate' the dbUser on the context....
ctx.dbUser = user
ctx.dbUser = user

or will subsequent calls to routes, re-invoke createContext and re-load the user from the database?
tRPCJoin
Move Fast & Break Nothing. End-to-end typesafe APIs made easy.
5,015Members
Resources
Recent Announcements

Similar Threads

Was this page helpful?

Similar Threads

Context is not being passed correctly
CodexCCodex / ❓-help
2y ago
Global context or shared context.
wleistraWwleistra / ❓-help
4y ago