Prisma(五)——深入CRUD

本文深入探讨了Prisma中集合间的关联关系,包括单向和双向关联的定义、部署及CRUD操作。通过具体示例,如users集合与Post集合的关联,详细展示了如何创建、读取、更新和删除关联数据。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

prisma(四)中是简单的CRUD,那么今天我们深入研究下CRUD,即集合之间的关联。

使用的集合

users、Post

先来看下users集合中关联Post集合。

datamodel

type User @db(name: "users") {
  _id: ID! @id
  age: Int
  email: String
  isUse: Boolean
  name: String
  post:Post@relation(link: INLINE)   //关联post集合
}
type Post{
  _id: ID! @id
  author:String
}

定义好datamodel后运行prisma deploy命令,这时候在generated文件夹下面的prisma-schema.js中会生成相应的方法,一起来看看吧:👇👇👇

prisma-schema.js

注意,这个文件会生成很多方法或类型,我大概挑几个使用的吧:

type Post {
  _id: ID!
  author: String
}

type User {
  _id: ID!
  age: Int
  email: String
  isUse: Boolean
  name: String
  post: Post
}

type Mutation {
  createUser(data: UserCreateInput!): User!
}


input UserCreateInput {
  _id: ID
  age: Int
  email: String
  isUse: Boolean
  name: String
  post: PostCreateOneInput
}

input PostCreateOneInput {
  create: PostCreateInput
  connect: PostWhereUniqueInput
}

type Query {
  users(where: UserWhereInput, orderBy: UserOrderByInput, skip: Int, after: String, before: String, first: Int, last: Int): [User]!
}

接下来我们去playground中进行相关操作。

C

  • 代码
    mutation{
      createUser(
        data:{
          name:"Anna",
          age:20,
          email:"23",
          isUse:true,
          post:{
            create:{
              author:"Anna"
            }
          }
        }
      ){
        _id,
        name,
        email,
        age,
        post{
          _id,
          author
        }
        
      }
    }
    
    
  • 效果图

R

  • 代码
    {
      users{
        _id,
        name,
        age,
        post{
          _id,
          author
        }
      }
    }
    
  • 效果

U

  • 代码

    mutation{
      updateUser(
        where:{_id:"5e1ece7df0c1310007bb2dc2"}
        data:{
          name:"Linda",
          age:20,
          email:"123@qq.com",
          isUse:true,
          post:{
            update:{
              author:"Linda"
            }
          }
        }
      ){
        _id,
        name,
        age,
        email,
        isUse,
        post{
          _id,
          author
        }
      }
    }
    
  • 效果图

D

  • 代码

    mutation{
      deleteUser(
        where:{_id:"5e1ece7df0c1310007bb2dc2"}
      ){
        _id,
        name,
        email,
        isUse,
        age,
        post{
          _id,
          author
        }
      }
    }
    
  • 效果图

如何删除关联的Post集合中的数据呢❓有待研究🤔🤔🤔

以上是users集合中关联Post,那如果这两个集合之间是双向关系呢❓该如何操作呢❓我们一起来看看吧👇👇👇

datamodel

type User @db(name: "users") {
  _id: ID! @id
  age: Int
  email: String
  isUse: Boolean
  name: String
  post:Post@relation(link: INLINE)
}
type Post{
  _id: ID! @id
  author:User      //上面author是String类型,此处来关联User类型
  name:String		//多加一个书的作者字段
}

prisma-schema.js

datamodel中定义好字段后继续运行prisma deploy来部署API,如下图(以createPost为例):

接下来打开playground来进行CRUD👇👇👇

C

  • 代码

    createPost(
        data:{
          name:"围城",
          author:{
            create:{
              name:"钱锺书",
              age:18,
              email:"12@qq.com",
              isUse:true
            }
          }
        }
      ){
        _id,
        name,
        author{
          _id,
          name,
          age,
          isUse,
          email
        }
      }
    }
    
  • 效果图

R

  • 代码

    {
     posts{
        _id,
        author{
          _id,
          name,
          age,
          email,
          isUse
        }
        name
      }
    }
    
  • 效果图

U

可以修改集合中的任何内容,要修改的内容放在data中,下面以修改作者的年龄为例:

  • 代码

    mutation{
     updatePost(
        where:{_id:"5e2308daf0c131000719ff4c"}
        data:{
          author:{
            update:{
              age:90
            }
          }
        }
      ){
        _id,
        author{
          _id,
          name,
          age,
          email,
          isUse
        }
        name
      }
    }
    
  • 效果图

D

  • 代码

    deletePost(
        where:{_id:"5e2308daf0c131000719ff4c"}
      ){
        _id,
        name,
        author{
          _id,
          name,
          age,
          email,
          isUse
        }
      }
    
  • 效果图

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值