I want to be able to query in prisma using a variable which is a union type over mulitple model tables.
so instead of
const model : 'user' | 'post' | ...;const id : number = 1;switch (model) { case 'user': prisma.user.findMany({where : id}) case 'post': prisma.post.findMany({where : id})}
const model : 'user' | 'post' | ...;const id : number = 1;switch (model) { case 'user': prisma.user.findMany({where : id}) case 'post': prisma.post.findMany({where : id})}
I want to be able to do:
const model : 'user' | 'post' | ...prisma[model].findMany({where : id} as const)
const model : 'user' | 'post' | ...prisma[model].findMany({where : id} as const)
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
findMany
functions are different for each table so I cannot do this. . I even tried using
as const
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.