Skip to content

Commit afa8bec

Browse files
Merge branch 'Paul0523-feature/fix-custom-repository-nometadata-error'
2 parents 731c031 + 216b496 commit afa8bec

File tree

3 files changed

+47
-2
lines changed

3 files changed

+47
-2
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
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+
}

lib/helpers/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from './get-custom-repository-entity';

lib/typeorm.module.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { DynamicModule, Module } from '@nestjs/common';
2-
import { Connection, ConnectionOptions, EntitySchema } from 'typeorm';
2+
import { Connection, ConnectionOptions } from 'typeorm';
33
import { EntitiesMetadataStorage } from './entities-metadata.storage';
4+
import { getCustomRepositoryEntity } from './helpers/get-custom-repository-entity';
45
import { EntityClassOrSchema } from './interfaces/entity-class-or-schema.type';
56
import {
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,

0 commit comments

Comments
 (0)