unleash-server
Version:
Unleash is an enterprise ready feature flag service. It provides different strategies for handling feature flags.
44 lines • 2.6 kB
TypeScript
import type { Knex } from 'knex';
import type { IUnleashConfig } from '../types/index.ts';
export interface TransactionUserParams {
type: 'change-request' | 'transaction';
id: string;
}
export type KnexTransaction = Knex.Transaction;
export type MockTransaction = null;
export type UnleashTransaction = KnexTransaction | MockTransaction;
export type TransactionCreator<S> = <T>(scope: (trx: S) => void | Promise<T>) => Promise<T>;
export declare const createKnexTransactionStarter: (knex: Knex) => TransactionCreator<UnleashTransaction>;
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, transactionContext?: TransactionUserParams) => Promise<R>;
};
export type WithRollbackTransaction<S> = S & {
rollbackTransaction: <R>(fn: (service: S) => R) => Promise<R>;
};
/**
* @deprecated this is a temporary solution to deal with transactions at the store level.
* Ideally, we should handle transactions at the service level (each service method should be transactional).
* The controller should define the transactional scope as follows:
* https://github.com/Unleash/unleash/blob/cb034976b93abc799df774858d716a49f645d669/src/lib/features/export-import-toggles/export-import-controller.ts#L206-L208
*
* To be able to use .transactional method, services should be instantiated like this:
* https://github.com/Unleash/unleash/blob/cb034976b93abc799df774858d716a49f645d669/src/lib/services/index.ts#L282-L284
*
* This function makes sure that `fn` is executed in a transaction.
* If the db is already in a transaction, it will execute `fn` in that transactional scope.
*
* https://github.com/knex/knex/blob/bbbe4d4637b3838e4a297a457460cd2c76a700d5/lib/knex-builder/make-knex.js#L143C5-L144C88
*/
export declare function inTransaction<R>(db: Knex, fn: (db: Knex) => R): Promise<R>;
export declare function withTransactional<S>(serviceFactory: (db: Knex) => S, db: Knex): WithTransactional<S>;
export declare function withRollbackTransaction<S>(serviceFactory: (db: Knex) => S, db: Knex): WithRollbackTransaction<S>;
/** Just for testing purposes */
export declare function withFakeTransactional<S>(service: S): WithTransactional<S>;
//# sourceMappingURL=transaction.d.ts.map