Skip to content
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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 16 additions & 13 deletions callback-free-itx/script.ts
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>;
Expand All @@ -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;
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unnecessary function.

});

// a promise for controlling the transaction
Expand All @@ -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;
Copy link
Author

Choose a reason for hiding this comment

The 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, {
Expand Down Expand Up @@ -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();
Copy link
Author

Choose a reason for hiding this comment

The 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()
Expand Down