简体   繁体   English

使用 Prisma 进行深度嵌套事务写入

[英]Deeply nested transaction writes with Prisma

One of the advantages of Sequelize is its ability to just pass in a already structured request.body with nested relations data using includes and it'll create the relationship for you or use Mixins. Sequelize 的优点之一是它能够使用includes传递一个已经结构化的 request.body 和嵌套关系数据,它会为你创建关系或使用 Mixins。

I have been using Prisma for a month now and currently i am trying to do transactions with Nested Writes , and i am finding it hard to accomplish.我已经使用Prisma一个月了,目前我正在尝试使用Nested Writes进行交易,但我发现它很难完成。

I would have posted my question here, but it is too long.我会在这里发布我的问题,但它太长了。

Because atm i have to pull apart and build each property from the request.Body and pass it into the create for every relationship when creating with deeply nested writes.因为 atm 我必须从 request.Body 中分离并构建每个属性,并在使用深层嵌套写入创建时将其传递到每个关系的create中。

Doing it this way, can lead to creating wrong relationships when creating.这样做会导致在创建时创建错误的关系。

What i want to ask is how to accomplish Nested Writes similar to Sequelize's includes?我想问的是如何实现类似于Sequelize的includes的Nested Writes? and also并且

Can someone please help with how they accomplish DEEPLY (like 3 or 4 levels) nested writes?有人可以帮助他们如何完成DEEPLY (如 3 或 4 级)嵌套写入吗?

For anyone landing here searching for how to work with deeply nested relations (like me,), there is an example at Prisma.io that I missed that shows how to do deeply nested reads and writes (nest the includes and create keys respectively):对于登陆这里寻找如何处理深度嵌套关系的任何人(像我一样),我错过了Prisma.io中的一个示例,它展示了如何进行深度嵌套的读取和写入(分别嵌套includescreate键):

const user = await prisma.user.create({
  data: {
    email: 'yvette@prisma.io',
    name: 'Yvette',
    posts: {
      create: [
        {
          title: 'How to make an omelette',
          categories: {
            create: {
              name: 'Easy cooking',
            },
          },
        },
        { title: 'How to eat an omelette' },
      ],
    },
  },
  include: {
    // Include posts
    posts: {
      include: {
        categories: true, // Include post categories
      },
    },
  },
})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM