Hi friends is an upsertMany possible? right now i ...
# prisma-client
k
Hi friends is an upsertMany possible? right now i can onlything i would have to do multiple upserts using transaction?
1
Copy code
const response = await PrismaService.$transaction(
            payload.map((p) =>
                PrismaService.contract.upsert({
                    where: { address: p.address },
                    update: p,
                    create: p,
                })
            )
        );
n
Hey Kay 👋 unfortunately there’s no native
upsertMany
query in Prisma Client yet. We already have an open feature request for that: https://github.com/prisma/prisma/issues/4134 If you think that would be useful, it would be great if you could leave a 👍 on the GitHub issue and ideally a comment with your use case as well to help us prioritize this in the future 🙂
k
So would you reccomend doing it like i have done above?
n
Yes, I think that’s a good workaround for the time being. You could also consider using the “batch” version of
$transaction
(instead of the “interactive” one), where you submit non-resolved Prisma promises as an array, e.g.:
Copy code
const id = 9 // User to be deleted

const deletePosts = prisma.post.deleteMany({
  where: {
    userId: id,
  },
})

const deleteMessages = prisma.privateMessage.deleteMany({
  where: {
    userId: id,
  },
})

const deleteUser = prisma.user.delete({
  where: {
    id: id,
  },
})

await prisma.$transaction([deletePosts, deleteMessages, deleteUser]) // Operations succeed or fail together
More info about this in the docs 🙂 https://www.prisma.io/docs/guides/performance-and-optimization/prisma-client-transactions-guide#when-to-use-the-transaction-api