Zod - sort schema

Hi. I am adding sort option to prisma findMany query. Since I don't know zod very well, I could not think of any possible type-safe solution. Sort format should be: columnFieldName:orderType, so for example: firstName:desc. I made my types from @prisma/client, but don't know how to add them to tRPC input. Is it even possible?
type ColumnField = keyof Astronaut;
type Order = "desc" | "asc";

getAll: t.procedure
.input(z.object({ sort: z.string().optional() }))
.query(({ ctx, input }) => {
const [col, order] = input.sort?.split(":") as any;

return ctx.prisma.astronaut.findMany({
orderBy: {
[col]: order,
},
});
})
type ColumnField = keyof Astronaut;
type Order = "desc" | "asc";

getAll: t.procedure
.input(z.object({ sort: z.string().optional() }))
.query(({ ctx, input }) => {
const [col, order] = input.sort?.split(":") as any;

return ctx.prisma.astronaut.findMany({
orderBy: {
[col]: order,
},
});
})
4 Replies
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
venus
venus2y ago
I just realized maybe putting it all in one string isn't best solution... I might add two properties like: sortBy along with order Sure, I thought it so. Thanks I think one of the possible solutions would be to use regex
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
venus
venus2y ago
okay ^^
Want results from more Discord servers?
Add your server