Let's say I have a model in my schema.prisma file:
model Post {
id Int #id #default(autoincrement())
author User #relation(fields: [authorId], references: [id])
authorId Int
}
Having a variable called model in my server containing a model name
const model: string = [model name generated dynamically]
Using this string I want to know every information about this model. For example if this variable model happens to be Post, I want to know that it has fields id, author, authorId and also information about each field separately, like in the case of author which field in which model does it reference, in this example the model User the field id.
I'm aware that prisma generates a type for each model and that way maybe I can access the fields that way but that't not enough for me, I want information about each fields as well.
I search the prisma docs, also googled something like 'get meta information about model in prisma2' but I didn't find any solution. Is there a way to achieve this?
Yes, you can get all the metadata for your entire schema in the following manner:
const prisma = new PrismaClient()
// #ts-ignore
console.log(prisma._dmmf)
This will give you all the details for the models and relations. The reason this is not documented is that this is highly experimental and will change with releases, which is why it's used for internal purposes only.
Related
The Prisma data model of my app has Posts, and each Post has a number of Authors. They can't have zero authors. As I understand it, the following model:
model Post {
id Int #id #default(autoincrement())
title String
authors Author[] #relation(references: [id])
}
allows a Post to have zero Authors. I'm new to Prisma. Can my data model be changed to reflect that a Post needs at least one Author?
Let's say, I am creating application using NestJS. I use MongoDB as a database and mongoose as ODM. So, NestJS has it's own way to validate data - ValidationPipe's. On the other side, there is mongoose built-in ways to validate data that being pushed into DB.
The question is - can I use only NestJS validation or do I need also second-check my data using mongoose validation tools?
Personally, I can't see why should I use additional layer of validation if I already have NestJS validation. But maybe there is best-practice or something?
Each database validates input data and emits an error if found. However, you must take into account your schema file. For instance, if you are using Prisma(It doesn't actually matter) and you have a User model like below
model User {
id String #id #default(auto()) #map("_id") #db.ObjectId
email String #unique
password String
name String
image String?
createdAt DateTime #default(now())
updatedAt DateTime #updatedAt
##map("user")
}
As you can see there is only one optional property "image". Whether you pass a value for image property or not, the database will insert the data as a row. On the other hand, the properties without "?" mark at the end of the type, will not be stored if you don't pass them and emit an error.
Therefore, if you modeled schema according to your business logic, you don't need to validate twice but only add an exception handling like the one below.
const user = await this.usersService.findOne('id...')
.catch((e) => new NotFoundException({ message: 'No user found.' }))
I am trying to create a prisma schema for a self relation. I would like to be able to show related posts for each post. Here is my post model:
model Post {
id Int #id #default(autoincrement())
title String
content String
relatedPosts Post[]
}
I am unsure on how to get this to work, the error message I am getting says that I need to define the other side of the relationship.
You are trying to create a many-to-many self relation. In such cases, you need to create two fields:
One field to represent the suggested posts for a given parent post (named relatedPosts in the example)
One field to represent the parent for a given suggested post (named relatedPostParent in the example)
This is what the syntax will look like
model Post {
id Int #id #default(autoincrement())
title String
content String
relatedPosts Post[] #relation("RelatedPosts", references: [id]) // child post (post that are suggested)
relatedPostParent Post[] #relation("RelatedPosts", references: [id]) // parent post (parent post of a suggested post)
}
Note that this is an implicit many-to-many relation. The article linked at the top also shows how to create an explicit many-to-many relation, if that is what you'd prefer.
I am trying to make a Hotel website backend that allows users to reserve rooms using GraphQL and prisma. I want to make sure that the reservation will be cancelled after the check-in time if no one has checked in. To do this I wanted to set up a ttl index that will delete the reservation.
If there is any alternative way of doing this you can help me.
take a look on this : https://www.prisma.io/docs/concepts/components/prisma-schema/indexes
model Post {
id Int #id
title String #db.VarChar(255)
content String #db.Text
##fulltext([title, content])
}
I am trying to create a prisma schema for a self relation. I would like to be able to show related posts for each post. Here is my post model:
model Post {
id Int #id #default(autoincrement())
title String
content String
relatedPosts Post[]
}
I am unsure on how to get this to work, the error message I am getting says that I need to define the other side of the relationship.
You are trying to create a many-to-many self relation. In such cases, you need to create two fields:
One field to represent the suggested posts for a given parent post (named relatedPosts in the example)
One field to represent the parent for a given suggested post (named relatedPostParent in the example)
This is what the syntax will look like
model Post {
id Int #id #default(autoincrement())
title String
content String
relatedPosts Post[] #relation("RelatedPosts", references: [id]) // child post (post that are suggested)
relatedPostParent Post[] #relation("RelatedPosts", references: [id]) // parent post (parent post of a suggested post)
}
Note that this is an implicit many-to-many relation. The article linked at the top also shows how to create an explicit many-to-many relation, if that is what you'd prefer.