UNPKG

1.33 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.handleRetry = exports.getConnectionToken = exports.getModelToken = void 0;
4const common_1 = require("@nestjs/common");
5const operators_1 = require("rxjs/operators");
6const mongoose_constants_1 = require("../mongoose.constants");
7function getModelToken(model, connectionName) {
8 if (connectionName === undefined) {
9 return `${model}Model`;
10 }
11 return `${getConnectionToken(connectionName)}/${model}Model`;
12}
13exports.getModelToken = getModelToken;
14function getConnectionToken(name) {
15 return name && name !== mongoose_constants_1.DEFAULT_DB_CONNECTION
16 ? `${name}Connection`
17 : mongoose_constants_1.DEFAULT_DB_CONNECTION;
18}
19exports.getConnectionToken = getConnectionToken;
20function 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}
30exports.handleRetry = handleRetry;