Skip to content

Commit

Permalink
Use parentTransaction instead of baseExecutionPromise
Browse files Browse the repository at this point in the history
  • Loading branch information
caseywebdev committed Nov 29, 2023
1 parent 2fd15ba commit 7c318f7
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 15 deletions.
8 changes: 4 additions & 4 deletions lib/execution/transaction.js
Original file line number Diff line number Diff line change
Expand Up @@ -219,11 +219,11 @@ class Transaction extends EventEmitter {
return makeTransactor(this, connection, trxClient);
})
.then((transactor) => {
this.transactor = transactor;
if (this.outerTx) {
transactor.parentTransaction = this.outerTx.transactor;
}
transactor.executionPromise = executionPromise;
transactor.baseExecutionPromise = this.baseExecutionPromise = this
.outerTx
? this.outerTx.baseExecutionPromise
: executionPromise;

// If we've returned a "thenable" from the transaction container, assume
// the rollback and commit are chained to this object's success / failure.
Expand Down
25 changes: 14 additions & 11 deletions test/integration/execution/transaction.js
Original file line number Diff line number Diff line change
Expand Up @@ -811,23 +811,26 @@ module.exports = function (knex) {
});
});

it('exposes baseExecutionPromise', async () => {
let committed = false;
it('exposes the parent transaction', async () => {
await knex.transaction(async (trx1) => {
expect(trx1.baseExecutionPromise).to.equal(trx1.executionPromise);
expect(trx1.parentTransaction).to.equal(undefined);

await trx1.transaction(async (trx2) => {
expect(trx2.baseExecutionPromise).to.equal(trx1.executionPromise);
expect(trx2.parentTransaction).to.equal(trx1);
});

await trx1.transaction(async (trx2) => {
expect(trx2.parentTransaction).to.equal(trx1);

await trx2.transaction(async (trx3) => {
expect(trx3.baseExecutionPromise).to.equal(trx1.executionPromise);
trx3.baseExecutionPromise.then(() => {
committed = true;
});
expect(trx3.parentTransaction).to.equal(trx2);
});

await trx2.transaction(async (trx3) => {
expect(trx3.parentTransaction).to.equal(trx2);
});
expect(committed).to.be.false;
});
expect(committed).to.be.false;
});
expect(committed).to.be.true;
});
});
};
1 change: 1 addition & 0 deletions types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2323,6 +2323,7 @@ declare namespace Knex {
interface Transaction<TRecord extends {} = any, TResult = any[]>
extends Knex<TRecord, TResult> {
executionPromise: Promise<TResult>;
parentTransaction?: Transaction;
isCompleted: () => boolean;

query<TRecord extends {} = any, TResult = void>(
Expand Down

0 comments on commit 7c318f7

Please sign in to comment.