UNPKG

kindagoose

Version:
101 lines 5.3 kB
"use strict"; var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; return c > 3 && r && Object.defineProperty(target, key, r), r; }; var __metadata = (this && this.__metadata) || function (k, v) { if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v); }; var __param = (this && this.__param) || function (paramIndex, decorator) { return function (target, key) { decorator(target, key, paramIndex); } }; var __rest = (this && this.__rest) || function (s, e) { var t = {}; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p]; if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) { if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) t[p[i]] = s[p[i]]; } return t; }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; var KindagooseCoreModule_1; Object.defineProperty(exports, "__esModule", { value: true }); exports.KindagooseCoreModule = void 0; const common_1 = require("@nestjs/common"); const core_1 = require("@nestjs/core"); const mongoose_1 = __importDefault(require("mongoose")); const rxjs_1 = require("rxjs"); const kindagoose_constants_1 = require("../constants/kindagoose.constants"); const get_connection_token_1 = require("../utils/get-connection-token"); const handle_retry_1 = require("../utils/handle-retry"); let KindagooseCoreModule = KindagooseCoreModule_1 = class KindagooseCoreModule { constructor(connectionName, moduleRef) { this.connectionName = connectionName; this.moduleRef = moduleRef; } static forRoot(uri, options) { const { retryAttempts, retryDelay, connectionName } = options, mongooseConnectOptions = __rest(options, ["retryAttempts", "retryDelay", "connectionName"]); const connectionToken = (0, get_connection_token_1.getConnectionToken)(connectionName); const connectionProvider = { provide: connectionToken, async useFactory() { return await (0, rxjs_1.lastValueFrom)((0, rxjs_1.defer)(async () => await mongoose_1.default.createConnection(uri, mongooseConnectOptions).asPromise()).pipe((0, handle_retry_1.handleRetry)(retryAttempts, retryDelay))); }, }; return { module: KindagooseCoreModule_1, providers: [connectionProvider, { provide: kindagoose_constants_1.KINDAGOOSE_CONNECTION_NAME, useValue: connectionToken }], exports: [connectionProvider], }; } static forRootAsync(options) { const connectionToken = (0, get_connection_token_1.getConnectionToken)(options.connectionName); const optionsProvider = { provide: kindagoose_constants_1.KINDAGOOSE_MODULE_OPTIONS, inject: options.inject, useFactory: options.useFactory, }; const connectionProvider = { provide: connectionToken, inject: [kindagoose_constants_1.KINDAGOOSE_MODULE_OPTIONS], async useFactory(connectOptions) { const { uri, retryDelay, retryAttempts } = connectOptions, mongooseConnectOptions = __rest(connectOptions, ["uri", "retryDelay", "retryAttempts"]); return await (0, rxjs_1.lastValueFrom)((0, rxjs_1.defer)(async () => await mongoose_1.default.createConnection(uri, mongooseConnectOptions).asPromise()).pipe((0, handle_retry_1.handleRetry)(retryAttempts, retryDelay))); }, }; return { module: KindagooseCoreModule_1, imports: options.imports, providers: [ optionsProvider, connectionProvider, { provide: kindagoose_constants_1.KINDAGOOSE_CONNECTION_NAME, useValue: connectionToken }, ], exports: [ optionsProvider, connectionProvider, { provide: kindagoose_constants_1.KINDAGOOSE_CONNECTION_NAME, useValue: connectionToken }, ], }; } async onApplicationShutdown() { const connection = this.moduleRef.get(this.connectionName); if (connection) await connection.close(); } }; exports.KindagooseCoreModule = KindagooseCoreModule; exports.KindagooseCoreModule = KindagooseCoreModule = KindagooseCoreModule_1 = __decorate([ (0, common_1.Global)(), (0, common_1.Module)({}), __param(0, (0, common_1.Inject)(kindagoose_constants_1.KINDAGOOSE_CONNECTION_NAME)), __metadata("design:paramtypes", [String, core_1.ModuleRef]) ], KindagooseCoreModule); //# sourceMappingURL=kindagoose-core.module.js.map