initializers.ts 901 B

1234567891011121314151617181920212223242526
  1. import { ConnectionOptions } from 'typeorm';
  2. import { TestDbInitializer } from './test-db-initializer';
  3. export type InitializerRegistry = { [type in ConnectionOptions['type']]?: TestDbInitializer<any> };
  4. const initializerRegistry: InitializerRegistry = {};
  5. /**
  6. * @description
  7. * Registers a {@link TestDbInitializer} for the given database type. Should be called before invoking
  8. * {@link createTestEnvironment}.
  9. *
  10. * @docsCategory testing
  11. */
  12. export function registerInitializer(type: ConnectionOptions['type'], initializer: TestDbInitializer<any>) {
  13. initializerRegistry[type] = initializer;
  14. }
  15. export function getInitializerFor(type: ConnectionOptions['type']): TestDbInitializer<any> {
  16. const initializer = initializerRegistry[type];
  17. if (!initializer) {
  18. throw new Error(`No initializer has been registered for the database type "${type}"`);
  19. }
  20. return initializer;
  21. }