Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Duplication between prisma schema and models#446

Answeredbymarcjulian
Skona27 asked this question inQ&A
Discussion options

Hi!
First of all thanks for this starter. It is really awesome 😃

I've noticed that some of the code is duplicated between prisma schema definitions and nest js models.
Inprisma.schema we have aUser definition, which looks like below:

model User {  id        String   @id @default(cuid())  createdAt DateTime @default(now())  updatedAt DateTime @updatedAt  email     String   @unique  password  String  firstname String?  lastname  String?  posts     Post[]  role      Role}

And also inuser.model we define the same 'shape' but with typescript:

@ObjectType()export class User extends BaseModel {  email: string;  firstname?: string;  lastname?: string;  role: Role;  @HideField()  password: string;}

Why is that necessary? Can't we somehow generate schema from models, or other way around?

You must be logged in to vote

@Skona27 Hi, thanks 👍 I hope I can answer your question.

Yes currently the code requires duplicates between the prisma schema and the graphql schema.

  1. Database schema:schema.prisma describes the model and thus the table created for this model.
  2. GraphQL schema:user.model.ts and any other models describes the response value of youruser type for your GraphQL queries

How to resolve the duplicates for now?
It is currentlynecessary to have duplicate code once inschema.prisma and once for your GraphQL schema.

One idea would be to implementUser from prisma client for your graphql model to always keep those prisma and graphql models in sync.

import{User}from'@prisma/client';import{P…

Replies: 4 comments 2 replies

Comment options

@Skona27 Hi, thanks 👍 I hope I can answer your question.

Yes currently the code requires duplicates between the prisma schema and the graphql schema.

  1. Database schema:schema.prisma describes the model and thus the table created for this model.
  2. GraphQL schema:user.model.ts and any other models describes the response value of youruser type for your GraphQL queries

How to resolve the duplicates for now?
It is currentlynecessary to have duplicate code once inschema.prisma and once for your GraphQL schema.

One idea would be to implementUser from prisma client for your graphql model to always keep those prisma and graphql models in sync.

import{User}from'@prisma/client';import{PostModel}from'../post/post.model.ts';@ObjectType('User')exportclassUserModelextendsBaseModelimplementsUser{email:string;  @Field({nullable:true})firstname:string;  @Field({nullable:true})lastname:string;role:Role;  @HideField()password:string;posts:PostModel[];}

Another option might be in the featuretypegraphql-prisma

And in the near feature - when Prisma SDK will be ready - the typegraphql-prisma integration will also allow to use a code-first approach to build a schema.prisma and GraphQL schema at once, using classes with decorators as a single source of truth. Stay tuned! 💪

There is also an typegraphql integration for nestjstypegraphql-nestjs with somecaveats.

You must be logged in to vote
1 reply
@marcjulian
Comment options

Anothre option could behttps://github.com/unlight/prisma-nestjs-graphql which is similar tohttps://github.com/MichalLytek/typegraphql-prisma, but works with NestJS GraphQL Module.

Answer selected bymarcjulian
Comment options

@marcjulian
Thanks for that answer! Everything is clear now

You must be logged in to vote
0 replies
Comment options

its been a while since asked. is there now a way to not have duplicate data ?

i thought it be easier that classes generate prisma DSL but it does not seem to be a thing.

You must be logged in to vote
1 reply
@lucassith
Comment options

Yes, there a way to avoid duplicated data, just don't use prisma with NestJS. I know this is probably the most disappointing answer, but it seems like the technology isn't really compatible with each other and it's just getting exclusive set.

TypeORM seems to be more fitting package (but less hyped atm).

Comment options

With prisma, I use the generated models(types) from prisma client. Why needto generate models again?
On Wed, 23 Nov 2022, 8:24 pm Łukasz Sitarski, ***@***.***> wrote: Yes, there a way to avoid duplicated data, just don't use prisma with NestJS. I know this is probably the most disappointing answer, but it seems like the technology isn't really compatible with each other and it's just getting exclusive set. TypeORM seems to be more fitting package (but less hyped atm). — Reply to this email directly, view it on GitHub <#446 (reply in thread)>, or unsubscribe <https://github.com/notifications/unsubscribe-auth/AIMV5QNBTY5XOZATKSFAI4TWJZHMVANCNFSM44FFJAEA> . You are receiving this because you are subscribed to this thread.Message ID: <notiz-dev/nestjs-prisma-starter/repo-discussions/446/comments/4218694 @github.com>
You must be logged in to vote
0 replies
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Category
Q&A
Labels
None yet
5 participants
@Skona27@orenmizr@lucassith@marcjulian@hmiiro
Converted from issue

This discussion was converted from issue #427 on May 04, 2021 16:18.


[8]ページ先頭

©2009-2025 Movatter.jp