-
Notifications
You must be signed in to change notification settings - Fork 7
/
ProviderUtils.ts
55 lines (50 loc) · 1.62 KB
/
ProviderUtils.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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import { Connection } from 'typeorm';
import { getConnectionToken } from '@nestjs/typeorm';
export function components(...components: any[]) {
const results = [];
components.forEach(component => {
if (typeof component === 'function') {
results.push(component);
} else if (!!(typeof component === 'object' && component.provide)) {
results.push(component);
} else if (typeof component === 'object') {
for (const key in component) {
if (!component.hasOwnProperty(key)) {
continue;
}
results.push(component[key]);
}
}
});
return results;
}
export function repos(...repositories: any[]) {
const results = [];
repositories.forEach(repo => {
if (typeof repo === 'function') {
results.push(getCustomRepo(repo.name, repo));
} else if (typeof repo === 'object') {
for (const key in repo) {
if (!repo.hasOwnProperty(key)) {
continue;
}
results.push(getCustomRepo(repo[key].name, repo[key]));
}
}
});
return results;
}
export function getRepo(name: string, entity) {
return {
provide: name,
useFactory: (connection: Connection) => connection.getRepository(entity),
inject: [getConnectionToken()],
};
}
export function getCustomRepo(name: string, repo) {
return {
provide: name,
useFactory: (connection: Connection) => connection.getCustomRepository(repo),
inject: [getConnectionToken()],
};
}