UNPKG

@nestjs/typeorm

Version:

TypeORM for NestJS: connections and repositories through dependency injection, entities, transactions and multiple databases.

28 lines (27 loc) • 1.39 kB
import { EntitySchema, getMetadataArgsStorage } from 'typeorm'; import { getDataSourceToken, getRepositoryToken, } from './common/typeorm.utils.js'; export function createTypeOrmProviders(entities, dataSource) { return (entities || []).map((entity) => ({ provide: getRepositoryToken(entity, dataSource), useFactory: (dataSource) => { if (dataSource.options.type === 'mongodb') { return dataSource.getMongoRepository(entity); } const target = entity instanceof EntitySchema ? (entity.options.target ?? entity.options.name) : entity; const entityMetadata = dataSource.entityMetadatas.find((meta) => meta.target === target); const isTreeEntity = typeof entityMetadata?.treeType !== 'undefined'; return isTreeEntity ? dataSource.getTreeRepository(entity) : dataSource.getRepository(entity); }, inject: [getDataSourceToken(dataSource)], /** * Extra property to workaround dynamic modules serialisation issue * that occurs when "TypeOrm#forFeature()" method is called with the same number * of arguments and all entities share the same class names. */ targetEntitySchema: getMetadataArgsStorage().tables.find((item) => item.target === entity), })); }