I'm currently facing an issue while attempting to define a query in prisma dynamically. I encountered a roadblock right at the beginning of the process.
Here's a piece of code that works:
const items = await prisma.styleTags.findMany({
orderBy: {
name: 'asc',
}
});
However, when I try to define the query separately, I run into TypeScript errors.
const orderBy = {
cname: 'asc',
}
const items2 = await prisma.styleTags.findMany({
orderBy
});
On the surface, these two approaches should be identical. But somewhere within Prisma's complex web of auto-generated code...
Type '{ cname: string; priority: string; }' is not assignable to type 'Enumerable<StyleTagsOrderByWithRelationInput> | undefined'.
Type '{ cname: string; priority: string; }' is not assignable to type 'StyleTagsOrderByWithRelationInput'.
Types of property 'cname' are incompatible.
Type 'string' is not assignable to type 'SortOrder | undefined'.
The error message
Type 'string' is not assignable to type 'SortOrder | undefined'
prompted me to try passing orderBy: 'name'
, but that also failed.
For what it's worth, using @ts-ignore
makes the code work. However, if Prisma's typechecking needs to be overridden, then its purpose becomes somewhat redundant.
My plan next is to attempt dynamically composing the orderBy
based on passed parameters. But first, I need to resolve the issues mentioned above.
Can anyone shed light on why this typechecking issue with Prisma occurs?