How to add Prisma Client extensions to create-t3-app?

Hi! I want to create a new prisma extension that is available inside my innerTrpcContext. (so I can acces it next to my context, and normal prisma client). I tried to just add it to innerTrpcContext like in this picture. I need to kind of create inside the context because I want to access logged user values to attach to the prisma extension. I tried like this, but it didn't work when I tried calling safeWsPrisma. (it was undefined at runtime) I guess it's because I need to do something with globalThis at the export file level, but I'm unsure how to do it. Does anyone have a way to add extensions to create-t3-app?
No description
1 Reply
Gabriel
GabrielOP15mo ago
I know people often need code in text format, so here we go:
const createInnerTRPCContext = (opts: CreateContextOptions) => {
const safeWsPrisma = prisma.$extends({
name: "safeWsPrisma", //Optional: name appears in errorLogs
query: {
$allModels: {
$allOperations: async ({ query, args }) => {
if ("where" in args && args.where) {
if ("workspaceId" in args.where) {
args.where = {
...args.where,
workspaceId: {
equals: opts.session?.user.activeWorkspaceId,
},
};
}
}

return query(args);
},
},
},
});
return {
session: opts.session,
safeWsPrisma,
prisma,
};
};
const createInnerTRPCContext = (opts: CreateContextOptions) => {
const safeWsPrisma = prisma.$extends({
name: "safeWsPrisma", //Optional: name appears in errorLogs
query: {
$allModels: {
$allOperations: async ({ query, args }) => {
if ("where" in args && args.where) {
if ("workspaceId" in args.where) {
args.where = {
...args.where,
workspaceId: {
equals: opts.session?.user.activeWorkspaceId,
},
};
}
}

return query(args);
},
},
},
});
return {
session: opts.session,
safeWsPrisma,
prisma,
};
};
Want results from more Discord servers?
Add your server