You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Attempting to migrate to Prisma and having a tough time wrapping any features generically due to the (apparent?) lack of umbrella exposing the applicable types for a given model. Not sure if I'm just doing this wrong, or if this is a legitimate request you may hopefully consider.
My ultimate goal is to be able to use generics to abstract some of the redundant code away from lifecycle operations in my project. For some of my models, I want to have an audit log of changes. When records are created/updated/deleted, I log these events and record the changes in a separate table.
In my first attempt to accomplish this with Prisma, I read into the Client Extensions preview feature and wrote several extensions. Unfortunately, this route didn't seem to be a good fit for my overall objective. After reading through #15074 and the comments thereof, I found myself in agreement with the opposition comments to the feature. I think what you do with Prisma is simple, focused, and effective and perhaps the CEs would be better implemented in other was in our own projects. Accordingly, I set out to write a new layer of code to sit between the rest of my project and Prisma. In addition to my auditing objective, I also have a myriad of computed fields and such on various models, so I opted to write a class for each model, to serve my goals.
Unfortunately, I'm having a really tough time being able to abstract anything from my classes due to the typings available in Prisma. If I write everything in one class for a given model, it works great; however, this means I have to copy/paste quite a bit of redundant code between several other models, which is a recipe for failure as the code evolves. What I would really like to be able to do is have an abstract, generic base class that can take a few type parameters and retain a lot of the reusable code and then extend this class with each model. I can't find any way to do anything effective with the abstract class, though, because there doesn't seem to be any generic way to refer to the various model types generated by prisma generate: they're all independent of each other, with no base extension or type map. The Prisma.TypeMap has been hugely helpful in many regards, but it doesn't help with exposing models' associated types. For example, I can't find a way to dynamically access a given model's GetPayload or Input types. I've been able to type the appropriate Prisma model from a generic using the code below, but the signature required is very redundant and I still haven't been able to dynamically access the other necessary types in order to type the generic methods I want to write.
import{Prisma,PrismaClient}from"@prisma/client";exportabstractclassModel<ClientextendsPrisma.TypeMap['meta']['modelProps'],Payloadextends{id: number}>{protectedabstract_modelName: Prisma.ModelName;protectedabstract_client: PrismaClient[Client];protected_values: Payload;protectedconstructor(data: Payload){this._values=data;}publicstaticasynccreate(data: HowToTypeThis){// this seems to be a dead end}}
I've been brainstorming for a few days, so perhaps I am going about this all wrong and would certainly appreciate an alternative suggestion if I'm just lost in the woods and not finding trees.
Suggested solution
What would be helpful is a map similar to Prisma.TypeMap that, rather than aggregating types by operations, aggregated the various Input, Args, Output, etc types under a key for each model. Also, an association between the camelCase and PascalCase namings for each type, so that we can dynamically retrieve the appropriate Prisma Client and its types.
Additional context
Here's some rough pseudocode I put together as an example of what I think should be a lot more easily possible.
// lib/ModelWithAuditing.class.tsimport{Prisma,PrismaClient}from'@prisma/client'import{Model}from'./Model.class';exportabstractclassModelWithAudit<NameextendsPrisma.ModelName,PayloadextendsPrisma.Models[Name]['payloads']['allIncludes']>extendsModel<Name,Payload>{asynconCreate(){// make entry into audit log}asynconUpdate(){// make entry into audit log}asynconDelete(){// make entry into audit log}}
Problem
Attempting to migrate to Prisma and having a tough time wrapping any features generically due to the (apparent?) lack of umbrella exposing the applicable types for a given model. Not sure if I'm just doing this wrong, or if this is a legitimate request you may hopefully consider.
My ultimate goal is to be able to use generics to abstract some of the redundant code away from lifecycle operations in my project. For some of my models, I want to have an audit log of changes. When records are created/updated/deleted, I log these events and record the changes in a separate table.
In my first attempt to accomplish this with Prisma, I read into the Client Extensions preview feature and wrote several extensions. Unfortunately, this route didn't seem to be a good fit for my overall objective. After reading through #15074 and the comments thereof, I found myself in agreement with the opposition comments to the feature. I think what you do with Prisma is simple, focused, and effective and perhaps the CEs would be better implemented in other was in our own projects. Accordingly, I set out to write a new layer of code to sit between the rest of my project and Prisma. In addition to my auditing objective, I also have a myriad of computed fields and such on various models, so I opted to write a class for each model, to serve my goals.
Unfortunately, I'm having a really tough time being able to abstract anything from my classes due to the typings available in Prisma. If I write everything in one class for a given model, it works great; however, this means I have to copy/paste quite a bit of redundant code between several other models, which is a recipe for failure as the code evolves. What I would really like to be able to do is have an abstract, generic base class that can take a few type parameters and retain a lot of the reusable code and then extend this class with each model. I can't find any way to do anything effective with the abstract class, though, because there doesn't seem to be any generic way to refer to the various model types generated by
prisma generate
: they're all independent of each other, with no base extension or type map. ThePrisma.TypeMap
has been hugely helpful in many regards, but it doesn't help with exposing models' associated types. For example, I can't find a way to dynamically access a given model'sGetPayload
orInput
types. I've been able to type the appropriate Prisma model from a generic using the code below, but the signature required is very redundant and I still haven't been able to dynamically access the other necessary types in order to type the generic methods I want to write.I've been brainstorming for a few days, so perhaps I am going about this all wrong and would certainly appreciate an alternative suggestion if I'm just lost in the woods and not finding trees.
Suggested solution
What would be helpful is a map similar to
Prisma.TypeMap
that, rather than aggregating types by operations, aggregated the various Input, Args, Output, etc types under a key for each model. Also, an association between thecamelCase
andPascalCase
namings for each type, so that we can dynamically retrieve the appropriate Prisma Client and its types.Additional context
Here's some rough pseudocode I put together as an example of what I think should be a lot more easily possible.
The text was updated successfully, but these errors were encountered: