Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: introduce type to prevent potential issues #5066

Merged
merged 1 commit into from
Oct 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion src/lib/db/transaction.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Knex } from 'knex';
import { IUnleashConfig } from 'lib/server-impl';

export type KnexTransaction = Knex.Transaction;

Expand Down Expand Up @@ -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>;
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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);
Expand Down
Loading