(另,该开源笔记也有不可忽略问题如:成书较久远,时效性欠佳,对一些库的 api 调用属于历史废弃写法,当下已经发生变更。所以在阅读时,需自行匹配库最新版本进行修改,比如有对: @tanstack/react-query,next-connect 的 api 封装或调用;但是整体质量是有的,仍然推荐阅读)
// This is your Prisma schema file,
// learn more about it in the docs: https://pris.ly/d/prisma-schema
generator client {
provider = "prisma-client-js"
}
datasource db {
provider = "sqlite"
url = "file:../database/db.sqlite"
}
model User {
id String @id @default(nanoid(10))
name String?
avatarLocal Int @default(1) @map(name: "avatar_local")
email String @unique
password String
createdAt DateTime @default(now()) @map(name: "created_at")
posts Post[] @relation(name: "user_posts")
comments Comment[] @relation(name: "user_comments")
@@map(name: "user")
}
model Post {
id Int @id @default(autoincrement())
title String
content String
published Boolean @default(true)
creatorUid String @map(name: "creator_uid")
creator User @relation(name: "user_posts", fields: [creatorUid], references: [id])
comments Comment[] @relation(name: "post_comments")
tags Tag[] @relation(name: "posts_tags")
createdAt DateTime @default(now()) @map(name: "created_at")
updatedAt DateTime @updatedAt @map(name: "updated_at")
@@map(name: "post")
}
model Comment {
id Int @id @default(autoincrement())
content String
postId Int // TODO: @map(name: "post_id")
post Post @relation(name: "post_comments", fields: [postId], references: [id])
creatorUid String @map(name: "creator_uid")
creator User @relation(name: "user_comments", fields: [creatorUid], references: [id])
createdAt DateTime @default(now()) @map(name: "created_at")
@@map(name: "comment")
}
model Tag {
id Int @id @default(autoincrement())
label String @unique
posts Post[] @relation(name: "posts_tags")
createdAt DateTime @default(now()) @map(name: "created_at")
@@map(name: "tag")
}
上面的 schema 文件声明,对应“表”之间的关系如下图:(由 Prisma 官方在线工具生成)
Important: You need to re-run the prisma generate command after every change that's made to your Prisma schema to update the generated Prisma Client code.