UNPKG

@nestjs/typeorm

Version:

Nest - modern, fast, powerful node.js web framework (@typeorm)

24 lines (23 loc) 1.21 kB
import { 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) => { const entityMetadata = dataSource.entityMetadatas.find((meta) => meta.target === entity); const isTreeEntity = typeof entityMetadata?.treeType !== 'undefined'; return isTreeEntity ? dataSource.getTreeRepository(entity) : dataSource.options.type === 'mongodb' ? dataSource.getMongoRepository(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), })); }