1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | exports.handleRetry = exports.getConnectionToken = exports.getModelToken = void 0;
|
4 | const common_1 = require("@nestjs/common");
|
5 | const operators_1 = require("rxjs/operators");
|
6 | const mongoose_constants_1 = require("../mongoose.constants");
|
7 | function getModelToken(model, connectionName) {
|
8 | if (connectionName === undefined) {
|
9 | return `${model}Model`;
|
10 | }
|
11 | return `${getConnectionToken(connectionName)}/${model}Model`;
|
12 | }
|
13 | exports.getModelToken = getModelToken;
|
14 | function getConnectionToken(name) {
|
15 | return name && name !== mongoose_constants_1.DEFAULT_DB_CONNECTION
|
16 | ? `${name}Connection`
|
17 | : mongoose_constants_1.DEFAULT_DB_CONNECTION;
|
18 | }
|
19 | exports.getConnectionToken = getConnectionToken;
|
20 | function handleRetry(retryAttempts = 9, retryDelay = 3000) {
|
21 | const logger = new common_1.Logger('MongooseModule');
|
22 | return (source) => source.pipe((0, operators_1.retryWhen)((e) => e.pipe((0, operators_1.scan)((errorCount, error) => {
|
23 | logger.error(`Unable to connect to the database. Retrying (${errorCount + 1})...`, '');
|
24 | if (errorCount + 1 >= retryAttempts) {
|
25 | throw error;
|
26 | }
|
27 | return errorCount + 1;
|
28 | }, 0), (0, operators_1.delay)(retryDelay))));
|
29 | }
|
30 | exports.handleRetry = handleRetry;
|