web icon indicating copy to clipboard operation
web copied to clipboard

Documentation about $allModels on model extensions is not clear

Open czanella opened this issue 3 years ago • 0 comments

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 a string and doing something like prisma[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 contains return {} as any is far from a good example.

  • Why does the validate function in https://www.prisma.io/docs/concepts/components/prisma-client/client-extensions/model#example-1 has only one argument while the customCall has two? Why the first argument in customCall appears to be a reference to the model it's being called on, and why does validate apparently doesn't receive this information? Again, having real code samples instead of just /* Generic validation logic */ or return {} as any would 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 customCall example doesn't explain what the x: Prisma.Exact<... argument is, nor how it should be used. It also fails to show that trying to access x.customProperty doesn'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 this x parameter 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.

czanella avatar Jun 04 '23 23:06 czanella