JavascriptMickJ
tRPC3y 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....
  }  
};

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,
  }
}),

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

or will subsequent calls to routes, re-invoke createContext and re-load the user from the database?
Was this page helpful?