Theo's Typesafe CultTTC
Theo's Typesafe Cult4y ago
7 replies
venus

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,
      },
    });
  })
Was this page helpful?