-
Notifications
You must be signed in to change notification settings - Fork 203
/
typeorm.utils.ts
143 lines (134 loc) Β· 4.94 KB
/
typeorm.utils.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
import { Logger, Type } from '@nestjs/common';
import { Observable } from 'rxjs';
import { delay, retryWhen, scan } from 'rxjs/operators';
import {
AbstractRepository,
Connection,
ConnectionOptions,
EntityManager,
Repository,
} from 'typeorm';
import { isNullOrUndefined } from 'util';
import { v4 as uuid } from 'uuid';
import { CircularDependencyException } from '../exceptions/circular-dependency.exception';
import { DEFAULT_CONNECTION_NAME } from '../typeorm.constants';
const logger = new Logger('TypeOrmModule');
/**
* This function generates an injection token for an Entity or Repository
* @param {Function} This parameter can either be an Entity or Repository
* @param {string} [connection='default'] Connection name
* @returns {string} The Entity | Repository injection token
*/
export function getRepositoryToken(
entity: Function,
connection: Connection | ConnectionOptions | string = DEFAULT_CONNECTION_NAME,
) {
if (isNullOrUndefined(entity)) {
throw new CircularDependencyException('@InjectRepository()');
}
const connectionPrefix = getConnectionPrefix(connection);
if (
entity.prototype instanceof Repository ||
entity.prototype instanceof AbstractRepository
) {
return `${connectionPrefix}${getCustomRepositoryToken(entity)}`;
}
return `${connectionPrefix}${entity.name}Repository`;
}
/**
* This function generates an injection token for an Entity or Repository
* @param {Function} This parameter can either be an Entity or Repository
* @returns {string} The Repository injection token
*/
export function getCustomRepositoryToken(repository: Function) {
if (isNullOrUndefined(repository)) {
throw new CircularDependencyException('@InjectRepository()');
}
return repository.name;
}
/**
* This function returns a Connection injection token for the given Connection, ConnectionOptions or connection name.
* @param {Connection | ConnectionOptions | string} [connection='default'] This optional parameter is either
* a Connection, or a ConnectionOptions or a string.
* @returns {string | Function} The Connection injection token.
*/
export function getConnectionToken(
connection: Connection | ConnectionOptions | string = DEFAULT_CONNECTION_NAME,
): string | Function | Type<Connection> {
return DEFAULT_CONNECTION_NAME === connection
? Connection
: 'string' === typeof connection
? `${connection}Connection`
: DEFAULT_CONNECTION_NAME === connection.name || !connection.name
? Connection
: `${connection.name}Connection`;
}
/**
* This function returns a Connection prefix based on the connection name
* @param {Connection | ConnectionOptions | string} [connection='default'] This optional parameter is either
* a Connection, or a ConnectionOptions or a string.
* @returns {string | Function} The Connection injection token.
*/
export function getConnectionPrefix(
connection: Connection | ConnectionOptions | string = DEFAULT_CONNECTION_NAME,
): string {
if (connection === DEFAULT_CONNECTION_NAME) {
return '';
}
if (typeof connection === 'string') {
return connection + '_';
}
if (connection.name === DEFAULT_CONNECTION_NAME || !connection.name) {
return '';
}
return connection.name + '_';
}
/**
* This function returns an EntityManager injection token for the given Connection, ConnectionOptions or connection name.
* @param {Connection | ConnectionOptions | string} [connection='default'] This optional parameter is either
* a Connection, or a ConnectionOptions or a string.
* @returns {string | Function} The EntityManager injection token.
*/
export function getEntityManagerToken(
connection: Connection | ConnectionOptions | string = DEFAULT_CONNECTION_NAME,
): string | Function {
return DEFAULT_CONNECTION_NAME === connection
? EntityManager
: 'string' === typeof connection
? `${connection}EntityManager`
: DEFAULT_CONNECTION_NAME === connection.name || !connection.name
? EntityManager
: `${connection.name}EntityManager`;
}
export function handleRetry(
retryAttempts = 9,
retryDelay = 3000,
verboseRetryLog = false,
): <T>(source: Observable<T>) => Observable<T> {
return <T>(source: Observable<T>) =>
source.pipe(
retryWhen((e) =>
e.pipe(
scan((errorCount, error: Error) => {
const message = verboseRetryLog
? `Unable to connect to the database. Message: ${
error.message
}. Retrying (${errorCount + 1})...`
: `Unable to connect to the database. Retrying (${
errorCount + 1
})...`;
logger.error(message, error.stack);
if (errorCount + 1 >= retryAttempts) {
throw error;
}
return errorCount + 1;
}, 0),
delay(retryDelay),
),
),
);
}
export function getConnectionName(options: ConnectionOptions) {
return options && options.name ? options.name : DEFAULT_CONNECTION_NAME;
}
export const generateString = () => uuid();