Add transactor.parentTransaction (#5567)

This commit is contained in:
Casey Foster 2023-11-29 13:51:44 -06:00 committed by GitHub
parent 770b2f20ac
commit d908f09d17
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 27 additions and 0 deletions

View File

@ -219,6 +219,10 @@ 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;
// If we've returned a "thenable" from the transaction container, assume

View File

@ -810,5 +810,27 @@ module.exports = function (knex) {
// closed it. (Ex: this was the case for OracleDB before fixing #3721)
});
});
it('exposes the parent transaction', async () => {
await knex.transaction(async (trx1) => {
expect(trx1.parentTransaction).to.equal(undefined);
await trx1.transaction(async (trx2) => {
expect(trx2.parentTransaction).to.equal(trx1);
});
await trx1.transaction(async (trx2) => {
expect(trx2.parentTransaction).to.equal(trx1);
await trx2.transaction(async (trx3) => {
expect(trx3.parentTransaction).to.equal(trx2);
});
await trx2.transaction(async (trx3) => {
expect(trx3.parentTransaction).to.equal(trx2);
});
});
});
});
});
};

1
types/index.d.ts vendored
View File

@ -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>(