-
Notifications
You must be signed in to change notification settings - Fork 15
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix: rollback called explicitly working. #47
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
import { Prisma, PrismaClient } from "@prisma/client"; | ||
import { PrismaPromise } from '@prisma/client/runtime/library' | ||
|
||
type FlatTransactionClient = Prisma.TransactionClient & { | ||
$commit: () => Promise<void>; | ||
|
@@ -17,7 +18,7 @@ const prisma = new PrismaClient({ log: ["query"] }).$extends({ | |
|
||
// a promise for getting the tx inner client | ||
const txClient = new Promise<Prisma.TransactionClient>((res) => { | ||
setTxClient = (txClient) => res(txClient); | ||
setTxClient = res; | ||
}); | ||
|
||
// a promise for controlling the transaction | ||
|
@@ -33,12 +34,13 @@ const prisma = new PrismaClient({ log: ["query"] }).$extends({ | |
) { | ||
const tx = prisma.$transaction((txClient) => { | ||
setTxClient(txClient as unknown as Prisma.TransactionClient); | ||
|
||
return txPromise.catch((e) => { | ||
if (e === ROLLBACK) return; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this swallows the exception that actually triggers the rollback. |
||
throw e; | ||
}); | ||
}); | ||
return txPromise; | ||
}).catch((e) => { | ||
if (e === ROLLBACK) { | ||
return; | ||
} | ||
throw e; | ||
}) | ||
|
||
// return a proxy TransactionClient with `$commit` and `$rollback` methods | ||
return new Proxy(await txClient, { | ||
|
@@ -69,17 +71,18 @@ async function main() { | |
const tx = await prisma.$begin(); | ||
const user = await tx.user.findFirstOrThrow(); | ||
|
||
const tx2 = await prisma.$begin(); | ||
await tx2.user.findMany(); | ||
// const tx2 = await prisma.$begin(); | ||
// await tx2.user.findMany(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. to me the important part to show in transactions is rollback, maybe we should show both. |
||
|
||
await tx.user.update({ | ||
where: { id: user.id }, | ||
data: { firstName: `${user.firstName} II` }, | ||
data: { firstName: `${user.firstName} Update 2` }, | ||
}); | ||
await tx.$commit(); | ||
await tx.$rollback(); | ||
const tx2 = await prisma.$begin(); | ||
|
||
console.dir(await tx2.user.findFirstOrThrow({ where: { id: user.id } })); | ||
|
||
await tx2.user.count(); | ||
await tx2.$commit(); | ||
} | ||
|
||
main() | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
unnecessary function.