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
{{ message }}
Repository navigation
How to query your database using Prisma with NestJS #113
Hi!
I am a Nest entusiastic and I've been trying to learn with your nestjs-prisma-starter template. Unfortunately I'm stuck in an error that is getting on me, and I can't find any others repositories with this stack, so I think I need your help!
First, I don't know why but prisma client and my nest models enums are not assignable (???) Type 'import("api/node_modules/.prisma/client/index").User[]' is not assignable to type 'import("api/src/models/user.model").User[]'. Type 'import("api/node_modules/.prisma/client/index").User' is not assignable to type 'import("api/src/models/user.model").User'. Types of property 'role' are incompatible. Type '"USER" | "ADMIN"' is not assignable to type 'Role'. Type '"USER"' is not assignable to type 'Role'.
And, my nested relationships are in loop! One model needs that the other one loads his relationship and it turned into a cycle! I can't find this in Prisma documentation...
Hope you could help me, this is my first project with Nest and Prisma and is for study only.
I tried your commented solution first. Then I wanted more control on the "connecting" attributed and followed this explicit schema of a relation table where I am stuck in creating a nice @ResolveField and also the mutation of creating the records building up a relation:
modelPost{idInt @id @default(autoincrement())titleStringcategoriesCategoriesOnPosts[]}modelCategory{idInt @id @default(autoincrement())nameStringpostsCategoriesOnPosts[]}modelCategoriesOnPosts{postPost @relation(fields: [postId],references: [id])postIdInt// relation scalar field (used in the `@relation` attribute above)categoryCategory @relation(fields: [categoryId],references: [id])categoryIdInt// relation scalar field (used in the `@relation` attribute above)createdAtDateTime @default(now())
@@id([postId,categoryId])}
@some-user123 when you work with Prisma you are defining the database model in the schema.prisma and also create for each database model a typescript model for exposing in your REST/GraphQL API. You are not directly exposing the autogeneratede class from Prisma.
Create Prisma database model and have a look at the preview feature native types
Thanks for your reply! this.prisma.product.findMany() would still return the autogenerated class, right? Then I manually need to trigger the transform to ProductEntity? Or is there a magical NestJS machenism to do so? (Not necessary for @ApiProperty of course, but for @Transform to be applied.)
Prisma returns your data as plain json objects. You have to return the correct values matching your Entity/Model of your response. Try it out with a simple response and you will see how it works.
Activity
Guiradi commentedon Aug 7, 2020
Hi!
I am a Nest entusiastic and I've been trying to learn with your nestjs-prisma-starter template. Unfortunately I'm stuck in an error that is getting on me, and I can't find any others repositories with this stack, so I think I need your help!
Could you please take a look at my code? https://github.com/Guiradi/Sempapp-API
My problems are 2:
First, I don't know why but prisma client and my nest models enums are not assignable (???)
Type 'import("api/node_modules/.prisma/client/index").User[]' is not assignable to type 'import("api/src/models/user.model").User[]'. Type 'import("api/node_modules/.prisma/client/index").User' is not assignable to type 'import("api/src/models/user.model").User'. Types of property 'role' are incompatible. Type '"USER" | "ADMIN"' is not assignable to type 'Role'. Type '"USER"' is not assignable to type 'Role'.And, my nested relationships are in loop! One model needs that the other one loads his relationship and it turned into a cycle! I can't find this in Prisma documentation...
Hope you could help me, this is my first project with Nest and Prisma and is for study only.
Great article by the way, it helped me a lot!
Thank you
Guilherme
marcjulian commentedon Aug 18, 2020
@Guiradi You are trying to assign the Prisma
Userto the GraphQLUserwhich is not working due to the following reasons.1. Prisma `type` != GraphQL `class`
Prisma
UserGraphql
User2. GraphQL Model contains relationships
Graphql
UserIt is similar for the
enum. Inspect the generated prisma client to see the differents between theenumtypes.I am not sure about the loop in nested relationships. Your code example is not available anymore for me to test.
Thanks for reaching out!
johsthlm commentedon Oct 13, 2020
Hi,
Thanks for the nestjs prisma 2 starter template. I am prototyping with it for graphql.
What puzzles me; how do I code an explicit many-to-many relation in the resolvers?
I have followed the guidance from prisma 2 for the explicit model:
https://www.prisma.io/docs/reference/tools-and-interfaces/prisma-schema/relations#many-to-many-relations
Cheers,
Johan
marcjulian commentedon Oct 13, 2020
Hi @johsthlm,
take the following
schema.prismaFor both models you create an entity class including the relation for the relation
Postentity:Categoryentity:Now you have two options to fetch the relationship in the resolver
includewhich will return the data forPosts andCategorysResolveFieldto resolve the relationshipHopefully this answer helps you, let me know otherwise create a demo repo with the problem.
Have a great day.
Marc
johsthlm commentedon Oct 13, 2020
Thanks Marc,
I tried your commented solution first. Then I wanted more control on the "connecting" attributed and followed this explicit schema of a relation table where I am stuck in creating a nice @ResolveField and also the mutation of creating the records building up a relation:
some-user123 commentedon Feb 8, 2021
I'm currently using Nestjs with Typeorm and in the model class I use
@Transformof class-transformer to transform the class to the actual output in the API and@ApiPropertyfrom nestjs/swagger to document the response object.Example:
How would I do this with Prisma where there is an autogenerated model class?
marcjulian commentedon Feb 9, 2021
@some-user123 when you work with Prisma you are defining the database model in the
schema.prismaand also create for each database model a typescript model for exposing in your REST/GraphQL API. You are not directly exposing the autogeneratede class from Prisma.Optional you can implement the generated type from Prisma on your entity like so
I wrote about the duplication between Prisma Model and GraphQL API which also applies for REST.
some-user123 commentedon Feb 10, 2021
Thanks for your reply!
this.prisma.product.findMany()would still return the autogenerated class, right? Then I manually need to trigger the transform toProductEntity? Or is there a magical NestJS machenism to do so? (Not necessary for@ApiPropertyof course, but for@Transformto be applied.)marcjulian commentedon Mar 1, 2021
Prisma returns your data as plain json objects. You have to return the correct values matching your Entity/Model of your response. Try it out with a simple response and you will see how it works.
garyhe0780 commentedon Aug 5, 2021
Nice article, one question thought: Is there an way to create two prisma client with
nestjs-prisma