diff --git a/src/lib/db/transaction.ts b/src/lib/db/transaction.ts index 574126966bd8..b26231356b13 100644 --- a/src/lib/db/transaction.ts +++ b/src/lib/db/transaction.ts @@ -1,4 +1,5 @@ import { Knex } from 'knex'; +import { IUnleashConfig } from 'lib/server-impl'; export type KnexTransaction = Knex.Transaction; @@ -26,7 +27,15 @@ export const createKnexTransactionStarter = ( return transaction; }; -export type DbServiceFactory = (db: Knex) => S; +export type DeferredServiceFactory = (db: Knex) => S; +/** + * Services need to be instantiated with a knex instance on a per-transaction basis. + * Limiting the input parameters, makes sure we don't inject already instantiated services + * that might be bound to a different transaction. + */ +export type ServiceFactory = ( + config: IUnleashConfig, +) => DeferredServiceFactory; export type WithTransactional = S & { transactional: (fn: (service: S) => R) => Promise; }; diff --git a/src/lib/features/export-import-toggles/createExportImportService.ts b/src/lib/features/export-import-toggles/createExportImportService.ts index 00ee8da651c1..01d6155f4454 100644 --- a/src/lib/features/export-import-toggles/createExportImportService.ts +++ b/src/lib/features/export-import-toggles/createExportImportService.ts @@ -43,7 +43,7 @@ import { createFakePrivateProjectChecker, createPrivateProjectChecker, } from '../private-project/createPrivateProjectChecker'; -import { DbServiceFactory } from 'lib/db/transaction'; +import { DeferredServiceFactory } from 'lib/db/transaction'; import { DependentFeaturesReadModel } from '../dependent-features/dependent-features-read-model'; import { FakeDependentFeaturesReadModel } from '../dependent-features/fake-dependent-features-read-model'; import { @@ -149,7 +149,7 @@ export const createFakeExportImportTogglesService = ( export const deferredExportImportTogglesService = ( config: IUnleashConfig, -): DbServiceFactory => { +): DeferredServiceFactory => { return (db: Db) => { const { eventBus, getLogger, flagResolver } = config; const importTogglesStore = new ImportTogglesStore(db);