-
Notifications
You must be signed in to change notification settings - Fork 203
/
get-custom-repository-entity.ts
39 lines (36 loc) 路 1.28 KB
/
get-custom-repository-entity.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import {
AbstractRepository,
getMetadataArgsStorage,
Repository,
} from 'typeorm';
import { EntityClassOrSchema } from '../interfaces/entity-class-or-schema.type';
export function getCustomRepositoryEntity(
entities: EntityClassOrSchema[],
): Array<EntityClassOrSchema> {
const customRepositoryEntities = new Array<EntityClassOrSchema>();
const typeormEntityRepositories = getMetadataArgsStorage().entityRepositories;
for (const entity of entities) {
const isCustomRepository =
entity instanceof Function &&
(entity.prototype instanceof Repository ||
entity.prototype instanceof AbstractRepository);
if (isCustomRepository) {
const entityRepositoryMetadataArgs = typeormEntityRepositories.find(
(repository) => {
return (
repository.target ===
(entity instanceof Function ? entity : (entity as any)?.constructor)
);
},
);
if (entityRepositoryMetadataArgs) {
const targetEntity = entityRepositoryMetadataArgs.entity as EntityClassOrSchema;
const isEntityRegisteredAlready = entities.indexOf(targetEntity) !== -1;
if (!isEntityRegisteredAlready) {
customRepositoryEntities.push(targetEntity);
}
}
}
}
return customRepositoryEntities;
}