Skip to content

Is there a way to transform knex transactions into kysely transactions #475

@flobbun

Description

@flobbun

I'm working on a gigantic project, trying to progressively migrate from knex to kysely, but there's an issue: we have long-running transactions that are being passed through multiple functions across the entire codebase, which forces us to make huge refactors in order to use kysely. Is there any way to transform a running knex transaction into a kysely transaction?

I imagine something like this:

function knexToKyselyTransaction(knexTransaction: KnexTransaction): Kysely<DB> {
   return new Kysely<DB>({
    dialect: new KyselyKnexDialect({ knex: knexTransaction, kyselySubDialect  }),
  });
}

function someLegacyFunction(knexTx: KnexTransaction) {
    const testEmail1 = 'knex-test@example.com';
    const testEmail2 = 'kysely-test@example.com';
    const testEmail3 = 'knex-update@example.com';
    try {
        const kyselyTx = knexToKyselyTransaction(knexTx);

        // Perform operations with Knex
        await knexTx('users').insert({
          email: testEmail1,
          password: 'password1',
          name: 'Knex User',
        });

        // Perform operations with Kysely
        await kyselyTx
          .insertInto('users')
          .values({
            email: testEmail2,
            password: 'password2',
            name: 'Kysely User',
          })
          .execute();

        // Perform another Knex operation
        await knexTx('users')
          .insert({
            email: testEmail3,
            password: 'password3',
            name: 'Knex Update User',
          })
          .returning('id');

        // Update with Kysely
        await kyselyTx
          .updateTable('users')
          .set({ name: 'Updated Knex User' })
          .where('email', '=', testEmail3)
          .execute();

        // Verify data exists within both Knex and Kysely transactions (before commit)
        const usersInKnexTransaction = await knexTx('users').whereIn('email', [testEmail1, testEmail2, testEmail3]);
        const usersInKyselyTransaction = await kyselyTx.selectFrom('users').selectAll().where('email', 'in', [testEmail1, testEmail2, testEmail3]).execute();

        expect(usersInKnexTransaction).to.have.length(3);
        expect(usersInKyselyTransaction).to.have.length(3);

        throw new Error('Intentional error to test rollback');
    } catch (error) {
      expect(error).to.be.instanceOf(Error);
      expect((error as Error).message).to.equal('Intentional error to test rollback');
    }

  const usersAfterRollback = await kysely.selectFrom('users').selectAll().where('email', 'in', [testEmail1, testEmail2, testEmail3]).execute();
  expect(usersAfterRollback).to.have.length(0, 'All changes should have been rolled back');
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions