how to upsert many fields in prisma ORM

Viewed 2553

How can I upsert many fields in prisma ORM with one query?

I don't want to use upsert fields one by one. Can I upsert all of them with one query?

1 Answers

You can't do it right now in Prisma.

Most efficient way if you need to handle lots of data would probably be something like that:

prisma.$transaction([
  prisma.posts.deleteMany({ where: { userId: 1 } }),
  prisma.posts.createMany({
    { id: 1, title: 'first',  userId: 1 },
    { id: 2, title: 'second', userId: 1 },
    { id: 3, title: 'third',  userId: 1 },
  }),
]);

So you delete existing records and then recreate them again inside of transaction.

Related