From db04a1eaa837f9ef157875a8f92ac157cb221610 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gast=C3=B3n=20Fournier?= Date: Tue, 17 Oct 2023 12:30:44 +0200 Subject: [PATCH] chore: introduce type to prevent potential issues (#5066) ## About the changes This small improvement aims to help developers when instantiating services. They need to be constructed without injecting services or stores created elsewhere so they can be bound to the same transactional scope. This suggests that you need to create the services and stores on your own --- src/lib/db/transaction.ts | 11 ++++++++++- .../createExportImportService.ts | 4 ++-- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/src/lib/db/transaction.ts b/src/lib/db/transaction.ts index 574126966b..b26231356b 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 00ee8da651..01d6155f44 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);