do I need to do it raw sql?
# orm-help
s
do I need to do it raw sql?
1
t
You can do that with intaractiveTransaction, it’s prisma-experimentalFeatures. https://www.prisma.io/docs/concepts/components/prisma-client/transactions#interactive-transactions-in-preview
Copy code
await this.client.$transaction(async (prisma) => {
  const current = await prisma.user.findUnique({where: id})
await prisma.user.update({
      where: { id },
      data: { emailVerificationSendCount:  current.mailVerificationSendCount + 1 },
    });
})
But, this log count way has risk to lose data if you don’t handle errors to retry correctly. If you want accuracy, I recommend to create log records to another table for inserting logs when event to log happens and aggregates them.
s
what could cause a failure? losing connection and stuff?
t
The possibility may be not high, but deploying wrong code throws exception, or database connections occupied fully cause timeout against later reached requests and I often went through these in our production environment. Transaction generally limits to retry counts without cache server or task queue system or db records. So, I recommend to make harness by each way of them.
s
I think I'll just let that counter be slightly off, as it merely prevents spamming emails for verification purposes, but this will be useful to know for many many other use cases, thank you!
👍 1
n
Hi @Sebastian Gug 👋 You are looking for an increment operation, here’s a sample example on how you can use the increment operation. Let me know if this works for you.
🚀 1
t
@Nurul Amazing, prisma supports simple atomic operation. I didn’t know, too. Thank you.
🙏 1
n
Maybe we should start documenting it better, it seems many people were not aware of it 😄