felixmosh/knex-mock-client

Doesn't support transactions

Closed this issue · 3 comments

When using transaction Knex throws an error. And the transaction rollback isn't handled as well so we get a second error.

Transaction error:

Error: Transaction query already complete, run with DEBUG=knex:tx for more info
    at completedError (/node_modules/knex/lib/execution/transaction.js:396:9)
    at /node_modules/knex/lib/execution/transaction.js:362:24
    at new Promise (<anonymous>)
    at MockClient.trxClient.query (/node_modules/knex/lib/execution/transaction.js:358:12)
    at Runner.query (/node_modules/knex/lib/execution/runner.js:130:36)
    at ensureConnectionCallback (/node_modules/knex/lib/execution/internal/ensure-connection-callback.js:13:17)
    at Runner.ensureConnection (/node_modules/knex/lib/execution/runner.js:272:20)
    at Runner.run (/node_modules/knex/lib/execution/runner.js:30:19)

Rollback error:

Error: ROLLBACK - No mock handler found

    at Timeout._onTimeout (/node_modules/knex-mock-client/dist/Tracker.js:50:24)
    at listOnTimeout (node:internal/timers:557:17)
    at processTimers (node:internal/timers:500:7)

Transaction example:

       db.transaction(async (trx) => {
            try {
                const [myId] = await trx('example')
                    .insert({ example:0 })
                    .returning(['my_id']);
                    
                await trx('example2')
                    .insert({ example: myId });
            } catch (e) {
                await trx.rollback(e);
                throw e;
            }
        });

Right now we could fix the transaction so it doesn't error and trigger the rollback and in the future we could add transactions and rollbacks to the tracker history.

Let me know if there is a workaround.

Thank for reporting the issue 🙏🏼
I will check it ASAP

I've checked it, you should not call to trx.rollback by yourself, it is called for you in a case of error.

db.transaction(async (trx) => {
    const [myId] = await trx('example').insert({ example: 0 }).returning(['my_id']);
    await trx('example2').insert({ example: myId });
});

I've released v1.5.2 with support of ROLLBACK command