drizzle-team / drizzle-orm

Headless TypeScript ORM with a head. Runs on Node, Bun and Deno. Lives on the Edge and yes, it's a JavaScript ORM too 😅
https://orm.drizzle.team
Apache License 2.0
24.39k stars 635 forks source link

Improve transactions docs: do exceptions cause rollback inside transaction block? #1450

Closed IbrahimMohammed47 closed 11 months ago

IbrahimMohammed47 commented 1 year ago

I think the docs should clearly mention that a transaction would rollback when an exception is thrown without catching it inside the transaction block

masterbater commented 1 year ago

Thats how things work and dont even need to be mention in docs.

IbrahimMohammed47 commented 1 year ago

@masterbater I think you based this on assumptions from other libraries.. which is good that you have this intuition.. but it's not the case that everyone is like you or have your same experience. someone who knows only about transactions will firstly ask about what commits it and what rolls it back.

Sequelize - for example - did their homework: image

masterbater commented 1 year ago

@masterbater I think you based this on assumptions from other libraries.. which is good that you have this intuition.. but it's not the case that everyone is like you or have your same experience. someone who knows only about transactions will firstly ask about what commits it and what rolls it back.

Sequelize - for example - did their homework: image

I understand that, you are right if their first orm is drizzle, but you can infer it by their sample transaction in the docs even mention transaction are A transaction in its entirety can commit to a database as a single logical unit or rollback (become undone) as a single logical unit. https://orm.drizzle.team/docs/transactions

I also check they dont support manual controls of transaction(when to start, rollback, or commit) at least with their helper db.transaction, like prisma and laravel eloquent or any orm

IbrahimMohammed47 commented 1 year ago

@masterbater I think you based this on assumptions from other libraries.. which is good that you have this intuition.. but it's not the case that everyone is like you or have your same experience. someone who knows only about transactions will firstly ask about what commits it and what rolls it back. Sequelize - for example - did their homework: image

I understand that, you are right if their first orm is drizzle, but you can infer it by their sample transaction in the docs even mention transaction are A transaction in its entirety can commit to a database as a single logical unit or rollback (become undone) as a single logical unit. https://orm.drizzle.team/docs/transactions

I also check they dont support manual controls of transaction(when to start, rollback, or commit) at least with their helper db.transaction, like prisma and laravel eloquent or any orm

Actually they do support manual control of rolling back which made me a little confused in the first place, this is right from the docs:

const db = drizzle(...)
await db.transaction(async (tx) => {
  const [account] = await tx.select({ balance: accounts.balance }).from(accounts).where(eq(users.name, 'Dan'));
  if (account.balance < 100) {
    await tx.rollback()
    return
  }
  await tx.update(accounts).set({ balance: sql`${accounts.balance} - 100.00` }).where(eq(users.name, 'Dan'));
  await tx.update(accounts).set({ balance: sql`${accounts.balance} + 100.00` }).where(eq(users.name, 'Andrew'));
}); 
masterbater commented 1 year ago

Right I just scan it, you are right they support it, my mistake

On Fri, Nov 3, 2023, 5:19 PM Ibrahim Mohammed @.***> wrote:

@masterbater https://github.com/masterbater I think you based this on assumptions from other libraries.. which is good that you have this intuition.. but it's not the case that everyone is like you or have your same experience. someone who knows only about transactions will firstly ask about what commits it and what rolls it back. Sequelize - for example - did their homework: [image: image] https://user-images.githubusercontent.com/25140638/279986446-58d2d83f-0949-47e2-ada9-4c126e380b3c.png

I understand that, you are right if their first orm is drizzle, but you can infer it by their sample transaction in the docs even mention transaction are A transaction in its entirety can commit to a database as a single logical unit or rollback (become undone) as a single logical unit. https://orm.drizzle.team/docs/transactions

I also check they dont support manual controls of transaction(when to start, rollback, or commit) at least with their helper db.transaction, like prisma and laravel eloquent or any orm

Actually they do support manual control of rolling back which made me a little confused in the first place, this is right from the docs:

const db = drizzle(...)await db.transaction(async (tx) => { const [account] = await tx.select({ balance: accounts.balance }).from(accounts).where(eq(users.name, 'Dan')); if (account.balance < 100) { await tx.rollback() return } await tx.update(accounts).set({ balance: sql${accounts.balance} - 100.00 }).where(eq(users.name, 'Dan')); await tx.update(accounts).set({ balance: sql${accounts.balance} + 100.00 }).where(eq(users.name, 'Andrew'));});

— Reply to this email directly, view it on GitHub https://github.com/drizzle-team/drizzle-orm/issues/1450#issuecomment-1792102319, or unsubscribe https://github.com/notifications/unsubscribe-auth/AGI6VFAZ4ADHXQY5QAU4TMTYCSZJPAVCNFSM6AAAAAA6YNLSOWVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMYTOOJSGEYDEMZRHE . You are receiving this because you were mentioned.Message ID: @.***>

ngregrichardson commented 12 months ago

They support manual rollbacks, but all tx.rollback() does is throw an error, so it's just a nice helper function