Skip to content

Commit 11716ce

Browse files
author
fangzhipeng
committed
fix when use custom repository, TypeOrm.forFeature also need entity to be add
1 parent 00e8c60 commit 11716ce

File tree

1 file changed

+38
-2
lines changed

1 file changed

+38
-2
lines changed

lib/typeorm.module.ts

+38-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
import { DynamicModule, Module } from '@nestjs/common';
2-
import { Connection, ConnectionOptions, EntitySchema } from 'typeorm';
2+
import {
3+
AbstractRepository,
4+
Connection,
5+
ConnectionOptions,
6+
EntitySchema,
7+
getMetadataArgsStorage,
8+
Repository,
9+
} from 'typeorm';
310
import { EntitiesMetadataStorage } from './entities-metadata.storage';
411
import { EntityClassOrSchema } from './interfaces/entity-class-or-schema.type';
512
import {
@@ -27,7 +34,36 @@ export class TypeOrmModule {
2734
| string = DEFAULT_CONNECTION_NAME,
2835
): DynamicModule {
2936
const providers = createTypeOrmProviders(entities, connection);
30-
EntitiesMetadataStorage.addEntitiesByConnection(connection, entities);
37+
let repEntities = [];
38+
for (let entity of entities) {
39+
if (
40+
entity instanceof Function &&
41+
(entity.prototype instanceof Repository ||
42+
entity.prototype instanceof AbstractRepository)
43+
) {
44+
const entityRepositoryMetadataArgs = getMetadataArgsStorage().entityRepositories.find(
45+
(repository) => {
46+
return (
47+
repository.target ===
48+
(entity instanceof Function
49+
? entity
50+
: (entity as any).constructor)
51+
);
52+
},
53+
);
54+
if (entityRepositoryMetadataArgs) {
55+
if (
56+
entities.indexOf(<any>entityRepositoryMetadataArgs.entity) === -1
57+
) {
58+
repEntities.push(entityRepositoryMetadataArgs.entity);
59+
}
60+
}
61+
}
62+
}
63+
EntitiesMetadataStorage.addEntitiesByConnection(connection, [
64+
...entities,
65+
...repEntities,
66+
]);
3167
return {
3268
module: TypeOrmModule,
3369
providers: providers,

0 commit comments

Comments
 (0)