All files / module rdb.module.ts

64.28% Statements 27/42
77.77% Branches 7/9
50% Functions 1/2
64.28% Lines 27/42

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53  1x     1x       1x       1x 1x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x                                 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 1x  
import { DynamicModule } from '@nestjs/common';
import { TypeOrmModule, TypeOrmModuleOptions } from '@nestjs/typeorm';
import { EntityClassOrSchema } from '@nestjs/typeorm/dist/interfaces/entity-class-or-schema.type';
 
import { AppConfigPersistanceRDB, ConfigProviderService, loadDynamicModules } from '@node-c/core';
 
import { RDBModuleOptions } from './rdb.module.definitions';
 
import { SQLQueryBuilderModule } from '../sqlQueryBuilder';
 
// Note: the services here do not have a wrapping module intentionally - because of how TypeORM imports them,
// we want to have all of them in a single module, to be used as an array in TypeOrmModule.forRootAsync
export class RDBModule {
  static register(options: RDBModuleOptions): DynamicModule {
    const { connectionName, folderData, imports: additionalImports, moduleClass, moduleName } = options;
    const { atEnd: importsAtEnd, postORM: importsPostORM, preORM: importsPreORM } = additionalImports || {};
    const { entities, modules } = loadDynamicModules(folderData);
    return {
      global: true,
      module: moduleClass as DynamicModule['module'],
      imports: [
        ...(importsPreORM || []),
        TypeOrmModule.forRootAsync({
          useFactory: (configProvider: ConfigProviderService) => {
            const persistanceConfig = configProvider.config.persistance;
            // example : configProvider.config.persistance.db
            const { database, host, password, port, type, user } = persistanceConfig[
              moduleName as keyof typeof persistanceConfig
            ] as AppConfigPersistanceRDB;
            return {
              database,
              entities: entities as EntityClassOrSchema[],
              host,
              name: connectionName,
              password,
              port,
              type,
              username: user
            } as TypeOrmModuleOptions;
          },
          inject: [ConfigProviderService]
        }),
        SQLQueryBuilderModule.register({ persistanceModuleName: moduleName }),
        ...(importsPostORM || []),
        ...(modules || []),
        ...(importsAtEnd || [])
      ],
      providers: [...(options.providers || [])],
      exports: [...(modules || []), ...(options.exports || [])]
    };
  }
}