PrismaP
Prisma12mo ago
9 replies
ptrxyz

type-safely wrapping `findMany`

I am trying to wrap the findMany function in a separate function.

Can someone help me to get this right:

async function query<A, T = typeof prisma.assignment>(
    args: Prisma.Exact<A, Prisma.Args<T, 'findMany'>>
): Promise<Prisma.Result<T, A, 'findMany'>> {
    return prisma.assignment.findMany(args)
}

const params = {
    select: { id: true },
    foo: "bar"
}
const __ = await query(params)


So my trail of thought was to make it similar to a client extension (which is not what I want. I would like the function presented to be correctly typed) yet I do not have the this property to work with. So I ended using Prisma.Args to describe the shape of input args that I want and Prisma.Exact to narrow A down to Prisma.Args<T, 'findMany'>. This leads to args being rejected as valid for prisma.assignment.findMany

So I get this error if I use Prisma.Exact for args
Argument of type 'string | number | bigint | boolean | [] | { [K in keyof A]: Exact<A[K], any>; }' is not assignable to parameter of type '{ select?: Exact<AssignmentSelect<InternalArgs &  .... (more types follow)


As a 2nd problem, I can't get the return type right. the return prisma.assignment... statement has red squiqqly lines with TS complaining:

Type '{ id: string; created: Date; .... } is not assignable to type 'Result<T, A, "findMany">'


So apparently I get everything wrong and I can't figure out how this should work. Does anyone know how to make it so that my personal wrapper exactly type-safely mirrors the type/shape/behavior of prisma.assignment.findMany?
Was this page helpful?