1
0
mirror of https://github.com/Unleash/unleash.git synced 2025-01-25 00:07:47 +01:00

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
This commit is contained in:
Gastón Fournier 2023-10-17 12:30:44 +02:00 committed by GitHub
parent cf42a829f4
commit db04a1eaa8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 12 additions and 3 deletions

View File

@ -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<S> = (db: Knex) => S;
export type DeferredServiceFactory<S> = (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<S> = (
config: IUnleashConfig,
) => DeferredServiceFactory<S>;
export type WithTransactional<S> = S & {
transactional: <R>(fn: (service: S) => R) => Promise<R>;
};

View File

@ -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<ExportImportService> => {
): DeferredServiceFactory<ExportImportService> => {
return (db: Db) => {
const { eventBus, getLogger, flagResolver } = config;
const importTogglesStore = new ImportTogglesStore(db);