UNPKG

typeorm-transactional-async-callbacks

Version:

A Transactional Method Decorator for typeorm that uses cls-hooked to handle and propagate transactions between different repositories and service methods. Inpired by Spring Trasnactional Annotation and Sequelize CLS

49 lines (48 loc) 2.6 kB
/// <reference types="node" /> import { DataSource, EntityManager } from 'typeorm'; import { EventEmitter } from 'events'; import { StorageDriver as StorageDriverEnum } from '../enums/storage-driver'; import { StorageDriver } from '../storage/driver/interface'; export declare type DataSourceName = string | 'default'; /** * Options to adjust and manage this library */ interface TypeormTransactionalOptions { /** * Controls how many hooks (`commit`, `rollback`, `complete`) can be used simultaneously. * If you exceed the number of hooks of same type, you get a warning. This is a useful to find possible memory leaks. * You can set this options to `0` or `Infinity` to indicate an unlimited number of listeners. */ maxHookHandlers: number; /** * Controls storage driver used for providing persistency during the async request timespan. * You can force any of the available drivers with this option. * By default, the modern AsyncLocalStorage will be preferred, if it is supported by your runtime. */ storageDriver: StorageDriverEnum; } interface AddTransactionalDataSourceInput { /** * Custom name for data source */ name?: DataSourceName; dataSource: DataSource; /** * Whether to "patch" some `DataSource` methods to support their usage in transactions (default `true`). * * If you don't need to use `DataSource` methods in transactions and you only work with `Repositories`, * you can set this flag to `false`. */ patch?: boolean; } export declare const getTransactionalContext: () => StorageDriver; export declare const getEntityManagerByDataSourceName: (context: StorageDriver, name: DataSourceName) => EntityManager | null; export declare const setEntityManagerByDataSourceName: (context: StorageDriver, name: DataSourceName, entityManager: EntityManager | null) => void; export declare const getTransactionalOptions: () => TypeormTransactionalOptions; export declare const initializeTransactionalContext: (options?: Partial<TypeormTransactionalOptions>) => StorageDriver; export declare const addTransactionalDataSource: (input: AddTransactionalDataSourceInput | DataSource) => DataSource; export declare const getDataSourceByName: (name: DataSourceName) => DataSource | undefined; export declare const deleteDataSourceByName: (name: DataSourceName) => boolean; export declare const getHookInContext: (context: StorageDriver | undefined) => EventEmitter | null; export declare const setHookInContext: (context: StorageDriver, emitter: EventEmitter | null) => void; export {};