P
Prisma•2mo ago
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)
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)
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">'
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?
6 Replies
Prisma AI Help
Prisma AI Help•2mo ago
You chose to debug with a human. They'll tinker with your query soon. If you get curious meanwhile, hop into #ask-ai for a quick spin!
ptrxyz
ptrxyzOP•2mo ago
Does anyone have an idea about this? I am still wondering how to do it 😄
RaphaelEtim
RaphaelEtim•2mo ago
Hi @ptrxyz Can you try this code?
async function query<T extends Prisma.AssignmentFindManyArgs>(
args: Prisma.SelectSubset<T, Prisma.AssignmentFindManyArgs>
) {
return prisma.assignment.findMany(args);
}
async function query<T extends Prisma.AssignmentFindManyArgs>(
args: Prisma.SelectSubset<T, Prisma.AssignmentFindManyArgs>
) {
return prisma.assignment.findMany(args);
}
This approach should resolve both of the issues you mentioned: 1. It should accept the args parameter without type errors. 2. The return type should be correctly inferred based on the input arguments.
ptrxyz
ptrxyzOP•2mo ago
I will give it a try in a few! thanks so far let me see if it works
RaphaelEtim
RaphaelEtim•2mo ago
you can use it like this
async function main() {

const params = {
select: { id: true },
where: { /* add your conditions here */ }
}
const assignments = await query(params)

console.log(assignments)

// You can now use the assignments with proper type inference
assignments.forEach(assignment => {
console.log(assignment.id)
// TypeScript will know that only 'id' is available
})
}
async function main() {

const params = {
select: { id: true },
where: { /* add your conditions here */ }
}
const assignments = await query(params)

console.log(assignments)

// You can now use the assignments with proper type inference
assignments.forEach(assignment => {
console.log(assignment.id)
// TypeScript will know that only 'id' is available
})
}
ptrxyz
ptrxyzOP•2mo ago
yes, this seems to be what i need. one thing about the args, i had to change SelectSubset to Prisma.Exact to make it work, but it does what it should now! Thanks a bunch!

Did you find this page helpful?