How to properly handle Prima selects/includes with tRPC?

Hello, my understanding of optimizing procedures to be the most efficient would be to enable one procedure to handle different use-cases. So I'd like a procedure like getById to take a list that allows all possible attributes from the prisma models and optionally a list of models to include. Currently, I define a list of all attributes on a global level for each router, that includes all includable Fields (I'd then do the same for the selectable fields). But that - even with just the includable fields - ends in this madness:
const includableFields = z.enum([
"comments",
"readingProgress",
"novelInsights",
"author",
"userLists",
// ...
]);

export const novelRouter = createTRPCRouter({
getById: privateProcedure
.input(
z.object({
id: z.string(),
include: z.array(includableFields).optional(),
})
)
.query(async ({ ctx, input }) => {
return await ctx.db.novel.findUnique({
where: {
id: input.id,
},
include: {
comments: input.include?.includes("comments") ?? false,
readingProgress:
input.include?.includes("readingProgress") ?? false,
novelInsights:
input.include?.includes("novelInsights") ?? false,
author: input.include?.includes("author") ?? false,
userLists: input.include?.includes("userLists") ?? false,
chapters: input.include?.includes("chapters") ?? false,
genre: input.include?.includes("genre") ?? false,
customInsights:
input.include?.includes("customInsights") ?? false,
},
});
}),
});
const includableFields = z.enum([
"comments",
"readingProgress",
"novelInsights",
"author",
"userLists",
// ...
]);

export const novelRouter = createTRPCRouter({
getById: privateProcedure
.input(
z.object({
id: z.string(),
include: z.array(includableFields).optional(),
})
)
.query(async ({ ctx, input }) => {
return await ctx.db.novel.findUnique({
where: {
id: input.id,
},
include: {
comments: input.include?.includes("comments") ?? false,
readingProgress:
input.include?.includes("readingProgress") ?? false,
novelInsights:
input.include?.includes("novelInsights") ?? false,
author: input.include?.includes("author") ?? false,
userLists: input.include?.includes("userLists") ?? false,
chapters: input.include?.includes("chapters") ?? false,
genre: input.include?.includes("genre") ?? false,
customInsights:
input.include?.includes("customInsights") ?? false,
},
});
}),
});
The problem I have with this, is that it looks horrendous and needs to be manually adjusted everytime the schema changes
Solution:
```ts const includableFields = z.enum([ "comments", "readingProgress", "novelInsights",...
F
Finn51d ago
So, my question basically is, how you usually do this. Do you just allow all strings to be passed and try-catch possible errors being thrown, or am I severly misunderstanding how to use tRPC.
N
NeoBean50d ago
why not make items of includableFields booleans ?
J
julius50d ago
yea i'd keep the include an object and just passthrough
Solution
J
julius50d ago
const includableFields = z.enum([
"comments",
"readingProgress",
"novelInsights",
"author",
"userLists",
// ...
]);

export const novelRouter = createTRPCRouter({
getById: privateProcedure
.input(
z.object({
id: z.string(),
include: z.record(includableFields, z.boolean()).default({}),
})
)
.query(async ({ ctx, input }) => {
return await ctx.db.novel.findUnique({
where: {
id: input.id,
},
include: input.include,
});
}),
});
const includableFields = z.enum([
"comments",
"readingProgress",
"novelInsights",
"author",
"userLists",
// ...
]);

export const novelRouter = createTRPCRouter({
getById: privateProcedure
.input(
z.object({
id: z.string(),
include: z.record(includableFields, z.boolean()).default({}),
})
)
.query(async ({ ctx, input }) => {
return await ctx.db.novel.findUnique({
where: {
id: input.id,
},
include: input.include,
});
}),
});
F
Finn21d ago
Sorry it took me so long to respond. I've read your answer after you've posted it, but I completely forget about it because I was immersed into something else. Just wanted to say thanks.
More Posts
onSuccess with createTRPCProxyClientWhat is the best way to setup onSuccess with createTRPCProxyClient?trpcNext from client for testingHi guys, I have created a client that uses a mock database for testing. Can I create a context and tMissing content type header for mutations leads to 405Im migrating my app from Next Pages Router to App Router. I implemented the new trpc wrapper as explIs there any way to temporarily disable mutations?My goal is to move into a read-only mode temporarily. I'm thinking anything could be fine as a shoAfter upgrading to 11.0.0-next-beta.300, every request throws zod errorI didn't do anything but upgrading the trpc, and the error throws: ```json {"issues":[{"code":"invaSetting up tRPC for next.js with edge AND serverless functionstldr: I am trying to build an app that uses both edge AND serverless functions by creating separate Hello i am using trpc open api but there is an issue regarding using z.optional() however in the libHello i am using trpc open api but there is an issue regarding using z.optional() however in the libCreateCaller with React-queryHey guys. Is it possible to combine createCaller approach with React-query? I figured out that reactBeginner: form field error handling ?hey guys, I have a question regarding error handling with trpc. How could I specify a form field iZod.enum makes my tRPC explodeI'm having an issue with what appears to be circular dependencies. My problem is that I'm not sure wReact type errors because of backendHello, I'm using tRPC in React and it's working fine generally. However, WebPack always finds somethValue attached to all queriesIs it possible to add a value the all queries so its preset within every procedure call on the clienInvalidating query cache irrespective of query paramsHey is it possible to invalidate a query cache without providing parameters? I have a query for exaSentry integrationHello. I'm using trpc + fastify approach. I need a proper error loggin so I added fastify sentry, whIn my NEXTJS app can read value next-auth session in localhost ,but not when deployed to VercelI have several procedures in my TRPC router (https://github.com/Teodorant1/nighthawk_repair/blob/masWebSocket Authentication: Cookie missingHi community 👋 I'm currently trying to integrate the wsLink, and I've noticed that cookies are no