简体   繁体   中英

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.

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.

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.

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? and also

Can someone please help with how they accomplish DEEPLY (like 3 or 4 levels) nested writes?

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):

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
      },
    },
  },
})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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