File tree Expand file tree Collapse file tree 3 files changed +47
-2
lines changed
Expand file tree Collapse file tree 3 files changed +47
-2
lines changed Original file line number Diff line number Diff line change 1+ import {
2+ AbstractRepository ,
3+ getMetadataArgsStorage ,
4+ Repository ,
5+ } from 'typeorm' ;
6+ import { EntityClassOrSchema } from '../interfaces/entity-class-or-schema.type' ;
7+
8+ export function getCustomRepositoryEntity (
9+ entities : EntityClassOrSchema [ ] ,
10+ ) : Array < EntityClassOrSchema > {
11+ const customRepositoryEntities = new Array < EntityClassOrSchema > ( ) ;
12+ const typeormEntityRepositories = getMetadataArgsStorage ( ) . entityRepositories ;
13+
14+ for ( const entity of entities ) {
15+ const isCustomRepository =
16+ entity instanceof Function &&
17+ ( entity . prototype instanceof Repository ||
18+ entity . prototype instanceof AbstractRepository ) ;
19+ if ( isCustomRepository ) {
20+ const entityRepositoryMetadataArgs = typeormEntityRepositories . find (
21+ ( repository ) => {
22+ return (
23+ repository . target ===
24+ ( entity instanceof Function ? entity : ( entity as any ) ?. constructor )
25+ ) ;
26+ } ,
27+ ) ;
28+ if ( entityRepositoryMetadataArgs ) {
29+ const targetEntity = entityRepositoryMetadataArgs . entity as EntityClassOrSchema ;
30+ const isEntityRegisteredAlready = entities . indexOf ( targetEntity ) !== - 1 ;
31+
32+ if ( ! isEntityRegisteredAlready ) {
33+ customRepositoryEntities . push ( targetEntity ) ;
34+ }
35+ }
36+ }
37+ }
38+ return customRepositoryEntities ;
39+ }
Original file line number Diff line number Diff line change 1+ export * from './get-custom-repository-entity' ;
Original file line number Diff line number Diff line change 11import { DynamicModule , Module } from '@nestjs/common' ;
2- import { Connection , ConnectionOptions , EntitySchema } from 'typeorm' ;
2+ import { Connection , ConnectionOptions } from 'typeorm' ;
33import { EntitiesMetadataStorage } from './entities-metadata.storage' ;
4+ import { getCustomRepositoryEntity } from './helpers/get-custom-repository-entity' ;
45import { EntityClassOrSchema } from './interfaces/entity-class-or-schema.type' ;
56import {
67 TypeOrmModuleAsyncOptions ,
@@ -27,7 +28,11 @@ export class TypeOrmModule {
2728 | string = DEFAULT_CONNECTION_NAME ,
2829 ) : DynamicModule {
2930 const providers = createTypeOrmProviders ( entities , connection ) ;
30- EntitiesMetadataStorage . addEntitiesByConnection ( connection , entities ) ;
31+ const customRepositoryEntities = getCustomRepositoryEntity ( entities ) ;
32+ EntitiesMetadataStorage . addEntitiesByConnection ( connection , [
33+ ...entities ,
34+ ...customRepositoryEntities ,
35+ ] ) ;
3136 return {
3237 module : TypeOrmModule ,
3338 providers : providers ,
You can’t perform that action at this time.
0 commit comments