1
const a = prisma.user.create()
throw new Error() // some error happened
const b = prisma.user.create()
prisma.$transaction([a, b])

Since an error happened, prisma.$transaction was never called, so a inserted into the database.

skink
  • 5,133
  • 6
  • 37
  • 58
刘佳闻
  • 43
  • 1
  • 6

2 Answers2

0

a will never be inserted into the database as the prisma.user.create() command will only be called inside prisma.$transaction so you do not need to check for that.

Ryan
  • 5,229
  • 1
  • 20
  • 31
  • But in reality, ```a``` dose insert into database. – 刘佳闻 Mar 30 '21 at 08:48
  • That's not possible, Prisma's API is lazy by default so no insert will happen before the transaction. I can send you an example as well if possible. – Ryan Mar 31 '21 at 06:53
  • If i just use ```create()``` with no ```await``` and open the ```debug``` option of prisma, the console shows ```BEGIN; INSERT...; COMMIT;```, so it just auto commit my insert, is there an option can stop auto commit i don't know? @Ryan – 刘佳闻 Mar 31 '21 at 06:57
  • Maybe later i can create a small repo, i'll keep in touch. very much thinks. @Ryan – 刘佳闻 Mar 31 '21 at 07:05
  • Yes please do at [this link](https://github.com/prisma/prisma/discussions/new) so that I can reproduce :) – Ryan Mar 31 '21 at 12:32
  • Hi @Ryan, [look here](https://github.com/prisma/prisma/discussions/6355) – 刘佳闻 Apr 01 '21 at 06:08
0

How about

const [a, b] = await prisma.$transaction([prisma.user.create(), prisma.user.create()])
PoC
  • 140
  • 2
  • 10