question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Possibility to merge & extend prisma models ?

See original GitHub issue

As there is most likely a parser for .prisma files (See #92) coming soon, it would be nice to have the possibility to merge & extend existing models, like:

post.prisma

model Post {
  title String @pg.varchar(42)
  body  String
}

core.prisma

import "./post.prisma"

model User {
  posts Post[]
}

Use case: let’s assume core.prisma & post.prisma are a package dependency and I’d like to extend and overwrite the Post model:

schema.prisma

import "dependency/prisma/core.prisma"

extend model Post {
  title String @pg.varchar(50)
  slug String
}

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:44
  • Comments:10 (3 by maintainers)

github_iconTop GitHub Comments

19reactions
pantharshit00commented, May 14, 2021

Pasting your suggested API here:

snippet AuditRecords {
  createdAt     DateTime   @default(now())
  createdBy     String
  updatedAt     DateTime   @updatedAt
  updatedBy     String
}

snippet StandardId {
  id          String   @default(cuid()) @id
}

Model User {
  @snippet['StandardId']
  name String
  email String
  @snippet['AuditRecords']
}

Model SomethingElse {
  id         Int       @id @default(autoincrement())
  name String
  email String
  @snippet['AuditRecords']
}

The API suggested in the first post of this issue is not final or anything, it is just to showcase the idea but we appreciate your suggestion 😃

12reactions
Gomahcommented, Sep 10, 2020

Yup, the interface type would be great

Can you describe why you’d like to override a field?

If multiple models are treated as a dependency, being able to extend & override its original value would be ideal, e.g:

# dependency/user.prisma
model User {
  id      String   @default(cuid()) @id
  email   String @unique
}
# dependency/post.prisma
model Post {
  id       String   @default(cuid()) @id
  title    String    @pg.varchar(42)
  body     String
}

Vendor applications:

import "package/user.prisma"
import "package/post.prisma"

extend model User {
  posts  Post[]
}

extend model Post {
  title    String @pg.varchar(50)
  user     User @relation(fields: [userId], references: [id])
  userId   String 
}

That way, I can easily compose my prisma schema from existing datamodels.

I came across an interesting project that temporarily solves this issue: https://github.com/amplication/prisma-schema-dsl

Read more comments on GitHub >

github_iconTop Results From Across the Web

Custom models - Prisma
This page explains how to wrap Prisma Client in custom models. ... We suggest either wrapping a model in a class or extending...
Read more >
Prisma Client extensions (Preview)
When you combine two or more extensions into a single extended client, then the last extension that you declare takes precedence in any...
Read more >
Prisma Client API (Reference)
With $extends , you can create and use Prisma Client extensions to add functionality to Prisma Client in the following ways: model :...
Read more >
Prisma Client extensions: model component (Preview)
You can use the model Prisma Client extensions component type to add custom methods to your models. We introduced this feature in version...
Read more >
Data model (Reference) - Prisma
Learn about the concepts for building your data model with Prisma: Models, scalar types, enums, attributes, functions, IDs, default values and more.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found