Typically called schema.prisma
and consists of the following parts:
- Data source: Specifies your database connection (via an environment variable)
- Generator: Indicates that you want to generate Prisma Client
- Data model: Defines your application models
// Data source
datasource db {
provider = "postgresql"
url = env("DATABASE_URL") // DATABASE_URL="mysql://root:@mysql:3306/nestjs_prisma"
}
// Generator
generator client {
provider = "prisma-client-js"
}
// Data model
model User {
id Int @id @default(autoincrement())
email String @unique
name String
profileId Int @map("profile_id")
createdAt DateTime @default(now()) @map("created_at")
updatedAt DateTime @default(now()) @map("update_at")
posts Post[]
profile Profile @relation(fields: [profileId], references: [id])
@@map("users")
}
model Profile {
id Int @id @default(autoincrement())
birthdate DateTime?
nickname String?
createdAt DateTime @default(now()) @map("created_at")
updatedAt DateTime @default(now()) @map("update_at")
user User?
@@map("profiles")
}
model Post {
id Int @id @default(autoincrement())
authorId Int @map("author_id")
content String @db.LongText
createdAt DateTime @default(now()) @map("created_at")
updatedAt DateTime @default(now()) @map("update_at")
author User @relation(fields: [authorId], references: [id])
categories CategoryPost[]
@@map("posts")
}
model Category {
id Int @id @default(autoincrement())
name String
createdAt DateTime @default(now()) @map("created_at")
updatedAt DateTime @default(now()) @map("update_at")
posts CategoryPost[]
@@map("categories")
}
model CategoryPost {
id Int @id @default(autoincrement())
categoryId Int @map("category_id")
postId Int @map("post_id")
createdAt DateTime @default(now()) @map("created_at")
updatedAt DateTime @default(now()) @map("update_at")
category Category @relation(fields: [categoryId], references: [id])
post Post @relation(fields: [postId], references: [id])
@@unique([postId, categoryId])
@@map("category_post")
}
The properties of a model are called fields, which consist of:
- A field name. Such as:
id
,name
,email
. - A field type. Such as:
Int
,String
,Post
,Profile
. Type has two categories: Scalar type and Model types.- Scalar type (includes enums) that map to columns. Such as:
String
,Int
. Complete list of scalar field types. - Model types map to relations. Such as:
Post
,Comment
.
- Scalar type (includes enums) that map to columns. Such as:
- Optional type modifiers: The type of a field can be modified by appending either of two modifiers:
[]
Make a field a list.?
Make a field optional.
- Optional attributes, including native database type attributes. Such as:
@id
,@default(autoincrement())
,@unique
,@db.VarChar(200)
,@db.TinyInt
.
Optional attributes, including native database type attributes
Name | Type | Scalar vs Relation | Type modifier | Attributes |
---|---|---|---|---|
id |
Int |
Scalar | - | @id and @default(autoincrement()) |
email |
String |
Scalar | - | @unique |
name |
String |
Scalar | ? |
- |
role |
Role |
Scalar (enum ) |
- | @default(USER) |
posts |
Post |
Relation (Prisma-level field) | [] |
- |
profile |
Profile |
Relation (Prisma-level field) | ? |
- |
- Prisma doesn't automatically trip field that doesn't exist in database but in DTO object. See #pullrequestreview-745869322
[Nest] 89398 - 09/03/2021, 3:28:32 PM ERROR [ExceptionsHandler]
Invalid `prisma.user.create()` invocation:
{
data: {
email: 'linh@gmail.com',
name: 'Linh Pham',
nickname: 'Nick name',
~~~~~~~~
profile: {
create: {
nickname: 'Nick name'
}
}
}
}
Unknown arg `nickname` in data.nickname for type UserCreateInput. Did you mean `name`? Available args:
type UserCreateInput {
email: String
name: String
createdAt?: DateTime
updatedAt?: DateTime
posts?: PostCreateNestedManyWithoutAuthorInput
profile: ProfileCreateNestedOneWithoutUserInput
}
- Handle exception
if ('name' in exception && exception.name === 'NotFoundError') {
return super.catch(new NotFoundException('Resource not found'), host);
}
- Type will be generated by Prisma. Such as:
Prisma.UserCreateInput
const userPayload: Prisma.UserCreateInput = {
email: createUser.email,
name: createUser.name,
profile: {
create: {
nickname: createUser.nickname,
},
},
};