From 0d4211b924a32aba13f3efe8fb89af2a10cf557b Mon Sep 17 00:00:00 2001 From: Arjun Komath Date: Thu, 16 Oct 2025 16:01:58 +1100 Subject: [PATCH] Move db to TRPC context --- trpc/init.ts | 26 ++++++++++++-------------- 1 file changed, 12 insertions(+), 14 deletions(-) diff --git a/trpc/init.ts b/trpc/init.ts index 9737b8c..13287b4 100644 --- a/trpc/init.ts +++ b/trpc/init.ts @@ -8,11 +8,15 @@ import { database } from "@/lib/utils/useDatabase"; import { getOwner, getTimezone } from "@/lib/utils/useOwner"; export const createTRPCContext = cache(async () => { - /** - * @see: https://trpc.io/docs/server/context - */ + const timezone = await getTimezone(); + const { orgSlug, ownerId } = await getOwner(); + const db = await database(); + return { - timezone: await getTimezone(), + timezone, + db, + ownerId, + orgSlug, }; }); @@ -49,29 +53,23 @@ export const createTRPCRouter = t.router; export const createCallerFactory = t.createCallerFactory; export const publicProcedure = t.procedure; -export const protectedProcedure = t.procedure.use(async ({ next }) => { +export const protectedProcedure = t.procedure.use(async ({ ctx, next }) => { const { sessionId, has } = await auth(); if (!sessionId) { throw new TRPCError({ code: "UNAUTHORIZED" }); } - const { orgSlug, ownerId, orgId, userId } = await getOwner(); + const { orgId, userId } = await getOwner(); - // For personal accounts (no orgId), user is always admin - // For organizations, check if user has org:admin role const isOrgAdmin = !orgId ? true : has({ role: "org:admin" }); - - const db = await database(); - const search = new SearchService(ownerId, orgSlug); + const search = new SearchService(ctx.ownerId, ctx.orgSlug); return next({ ctx: { + ...ctx, sessionId, userId, orgId, - orgSlug, - ownerId, - db, search, isOrgAdmin, },