高级用法
在 快速入门 中,我们创建了一个简单的帖子模型并运行了一些查询。但是,Prisma 和 Go 客户端旨在与模型之间的关系一起使用。
我们已经创建了一个帖子模型,例如用于博客。假设我们想为帖子添加评论,并将这些模型以一种方式连接起来,这样我们就可以依赖 SQL 的外键和 Go 客户端处理关系的能力。
因此,让我们引入一个新的评论模型
model Comment {
id String @id @default(cuid())
createdAt DateTime @default(now())
content String
post Post @relation(fields: [postID], references: [id])
postID String
}
我们还需要从帖子模型添加一个关系,以便在这些模型之间建立 1:n 关系
model Post {
// ...
// add this to your post model
comments Comment[]
}
您的完整模式应如下所示
datasource db {
// could be postgresql or mysql
provider = "sqlite"
url = "file:dev.db"
}
generator db {
provider = "go run github.com/steebchen/prisma-client-go"
}
model Post {
id String @id @default(cuid())
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
title String
published Boolean
desc String?
comments Comment[]
}
model Comment {
id String @id @default(cuid())
createdAt DateTime @default(now())
content String
post Post @relation(fields: [postID], references: [id])
postID String
}
每当您对模型进行更改时,请迁移您的数据库并重新生成您的 prisma 代码
go run github.com/steebchen/prisma-client-go migrate dev --name add_comment_model
为了创建评论,我们首先需要创建一个帖子,然后在创建评论时引用该帖子。
post, err := client.Post.CreateOne(
db.Post.Title.Set("My new post"),
db.Post.Published.Set(true),
db.Post.Desc.Set("Hi there."),
db.Post.ID.Set("123"),
).Exec(ctx)
if err != nil {
return err
}
log.Printf("post: %+v", post)
// then create a comment
comments, err := client.Comment.CreateOne(
db.Comment.Content.Set("my description"),
// link the post we created before
db.Comment.Post.Link(
db.Post.ID.Equals(post.ID),
),
).Exec(ctx)
if err != nil {
return err
}
log.Printf("post: %+v", comments)
现在您已经创建了帖子和评论,您可以按照以下步骤查询它们
// return all published posts
posts, err := client.Post.FindMany(
db.Post.Published.Equals(true),
).Exec(ctx)
if err != nil {
return err
}
log.Printf("published posts: %+v", posts)
// insert a few new comments
_, err = client.Comment.CreateOne(
db.Comment.Content.Set("first comment"),
// link the post we created before
db.Comment.Post.Link(
db.Post.ID.Equals("123"),
),
).Exec(ctx)
if err != nil {
return err
}
_, err = client.Comment.CreateOne(
db.Comment.Content.Set("second comment"),
// link the post we created before
db.Comment.Post.Link(
db.Post.ID.Equals("123"),
),
).Exec(ctx)
if err != nil {
return err
}
// return all comments from a post with a given id
comments, err := client.Comment.FindMany(
db.Comment.Post.Where(
db.Post.ID.Equals("123"),
),
).Exec(ctx)
if err != nil {
return err
}
log.Printf("comments of post with id 123: %+v", comments)
// return the first two comments from a post with which contains a given title, and sort by descending date
orderedComments, err := client.Comment.FindMany(
db.Comment.Post.Where(
db.Post.ID.Equals("123"),
),
).Take(2).OrderBy(
db.Comment.CreatedAt.Order(db.SortOrderDesc),
).Exec(ctx)
if err != nil {
return err
}
log.Printf("ordered comments: %+v", orderedComments)
Prisma 还允许您一次获取多个内容。无需进行复杂的联接操作,您只需几行代码即可获取帖子及其部分评论,并且完全类型安全
// return a post by its id including 5 of its comments
post, err := client.Post.FindUnique(
db.Post.ID.Equals("123"),
).With(
// also fetch 3 this post's comments
db.Post.Comments.Fetch().Take(3),
).Exec(ctx)
// will log post and its comments
log.Printf("post: %+v", post)
API 参考
要探索所有查询功能,请查看 API 参考。