Search code examples
javascriptnode.jstypescriptormprisma

Is there a way to create abstract models in prisma ORM?


I have few prisma models and each of these models have few common fields, for eg. audit fields like created_at, updated_at.

model User {
  id         Int       @id @default(autoincrement())
  created_at DateTime  @default(now())
  updated_at DateTime  @updatedAt
  email      String    @unique
  username   String    @unique
  password   String?
  firstName  String?
  lastName   String?
  isActive   Boolean
  deletedAt  DateTime?
  blogs      Blog[]
  profile    Profile?
}

model Profile {
  id         Int      @id @default(autoincrement())
  created_at DateTime @default(now())
  updated_at DateTime @updatedAt
  bio        String?
  user       User     @relation(fields: [userId], references: [id])
  userId     Int      @unique
}

model Blog {
  id         Int      @id @default(autoincrement())
  created_at DateTime @default(now())
  updated_at DateTime @updatedAt
  author     User     @relation(fields: [authorId], references: [id])
  authorId   Int
  title      String
  content    String

  @@unique([authorId, title])
  @@index([id, authorId])
}

I would like to define these all fields in one abstract base model and then extend all other models using that base model so that the repetitive code can be avoided and the risk of missing to add those fields in newer models will be lowered.

I tried to find the solution in prisma doc but I'm not able to find anything there. I can see some solutions online that are doing some hacks or using third party tools to achieve similar results but I would prefer something out of the box, if available.

For eg, In Django, we define base model and inherit other models from same. If anyone know how to achieve similar thing with prisma or if you are using some other way to manage massive number of models in production then that would be of great help. Thanks.


Solution

  • In short: No there is such thing as abstract models or inheritance in general implemented in Prisma as of now.

    However there is an open Github issue proposal which describes how interfaces could be used for that kind of abstraction. https://github.com/prisma/prisma/issues/2506. Unfortunately there hasn't been made any real progress on the issue since 2020.


    Thus people started to implement it by themselves. A custom library called ZenStack built on top of Prisma offers abstract models. You can define .zmodel model files which are compiled to the normal prisma.schema.

    An example from the ZenStack documentation:

    abstract model Basic {
        id String @id
        createdAt DateTime @default(now())
        updatedAt DateTime @updatedAt
    }
    
    model User extends Basic {
        name String 
    }
    

    The generated prisma file only contains one User model:

    model User {
        id String @id
        createdAt DateTime @default(now())
        updatedAt DateTime @updatedAt
        name String @id
    }