union type findMany
I want to be able to query in prisma using a variable which is a union type over mulitple model tables.
so instead of
I want to be able to do:
Since I have a really large schema and I do not want to repeat so many lines.
but, even though each model/schema table has the id field as the same type and they all have this same key, TS is complaining that the signatures of each of the
findMany
functions are different for each table so I cannot do this. .
I even tried using as const
in case there was a difference there, but that did not help either. I am not sure how I can do this to make my code cleaner and easier to maintain.
Here is an example in a codesandbox:
https://codesandbox.io/p/devbox/dark-night-vsqs739 Replies
Could you create a TS playground with the code ?
@snchmt did you figure it out?
Sorry, I was waiting for your modifications I forgot to check again
I think the issue here is that you are using a generic method that uses the generic type of an interface, but the generic types of the interfaces are not compatible, so the only way possible here is to cast somewhere or use a ts-ignore
I know. but then I would completely lose TS errors which I really do not want. I wish there was a way for prisma to allow this directly in their TS code gen.
The generic function doesn't / shouldn't care about Prisma.
https://codesandbox.io/p/devbox/prisma-errors-forked-z8hc44?file=%2Fsrc%2Findex.ts%3A35%2C34
i dont get the point of this. this doesnt solve my problem at all
What are you ultimately trying to achieve?
My assumption was that you want to create an abstraction over something you do often without knowing which model beforehand
I want to be able to make a defined operation that is long (find many where [x or y], select foo, include bar), over many different tables. but I want want to copy paste the code in case I need to change the query. so I want to be able to pass the table name as a variable and be type safe.
ya know. like what my original post said.....
So what is the issue with passing
prisma.modelName
instead of "modelName"
? You're making it very hard for yourself in terms of TS by wanting a string, especially since it has the same result.
No need for an attitude towards someone that is trying to help you, my guy