drizzle-team / drizzle-team/drizzle-orm
[FEATURE]: Allow tx.rollback() to provide custom error or return
- Dominant language
- TypeScript
- Stars
- 35.8k
- Forks
- 1.6k
- Avg merge
- 2d 7h
- Merged PRs (30d)
- 4
Description
### Describe what you want
Suppose we have the following code:
```ts
db.transaction(async (tx) => {
// a lot of operations with tx
if (!someCondition) {
await tx.rollback();
}
// some more operations
if (!otherCondition) {
await tx.rollback();
}
})
```
The transaction would throw the same error for both rollbacks. We couldn't get any info about what the cause was, and how far the transaction had done (even though it will be discarded).
I believe there should be a way to put the reason/cause of a rollback from happening.
The first time I use drizzle, I thought `tx.rollback()` would just do a regular sql `ROLLBACK` and still allows us to run the code inside the transaction (seeing the `return` statement after `tx.rollback()` in the examples from docs). I thought I could return an error or something on that `return` statement.
I was trying to do this:
```ts
const res = db.transaction(async (tx) => {
if (...) {
await tx.rollback();
return { status: "error" as const, msg: "Something didn't exist" }
}
});
if (res.status === "error") {
// actually throw it
throw new TRPCError({ ... });
}
```
But apparently it's not possible with how `tx.rollback()` actually works.
Maybe you guys could implement a feature similar to that? Perhaps treat `throw new AnyError()` inside a transaction as a rollback as well? Thanks!
Contributor guide
Assessment
This issue has not been assessed yet.