Documentation about $allModels on model extensions is not clear
The documentation on model Prisma extensions (https://www.prisma.io/docs/concepts/components/prisma-client/client-extensions/model) is not very clear regarding how the $allModels resource should be used.
It's clear that it adds the same methods to all models in your Prisma scheme, but the documentation should have better examples on how these methods should be implemented, because there are issues that aren't addressed:
-
https://www.prisma.io/docs/concepts/components/prisma-client/client-extensions/model#add-a-custom-method-to-all-models-in-your-schema : inside the sample
validate(data: unknown)method, how can we know which model is the method being called on? Getting the model's name from the context (https://www.prisma.io/docs/concepts/components/prisma-client/client-extensions/model#get-the-current-model-name-at-runtime) is not a valid solution, because the name returned is astringand doing something likeprisma[name].findMany(...)will raise TypeScript errors. -
https://www.prisma.io/docs/concepts/components/prisma-client/client-extensions/model#advanced-type-safety-improve-the-type-safety-and-developer-experience-of-your-custom-model-methods : why are there so many Prisma tools for type safety (
Exact,Args, etc.) if inside the function we need to override type safety (return {} as any)? One can imagine the reasoning behind this, but it's not clear how the logic of a custom method should be implemented. A function that only containsreturn {} as anyis far from a good example. -
Why does the
validatefunction in https://www.prisma.io/docs/concepts/components/prisma-client/client-extensions/model#example-1 has only one argument while thecustomCallhas two? Why the first argument incustomCallappears to be a reference to the model it's being called on, and why doesvalidateapparently doesn't receive this information? Again, having real code samples instead of just/* Generic validation logic */orreturn {} as anywould be very helpful. -
https://www.prisma.io/docs/concepts/components/prisma-client/client-extensions/model#advanced-type-safety-improve-the-type-safety-and-developer-experience-of-your-custom-model-methods : the
customCallexample doesn't explain what thex: Prisma.Exact<...argument is, nor how it should be used. It also fails to show that trying to accessx.customPropertydoesn't work and causes a TS error:Property 'customProperty' does not exist on type 'string | number | bigint | boolean | [] | { [x: string]: string extends keyof A ? Exact<A[keyof A & string], any> : never; }'.. A working code sample that shows how thisxparameter should be used could also be very helpful.
I'd provide working samples for all the situations above, but I don't believe the workarounds I did to use $allModels on my project are the most appropriate ones.