UNPKG

@nestjs/mongoose

Version:

Nest - modern, fast, powerful node.js web framework (@mongoose)

134 lines 6.34 kB
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 MongooseCoreModule_1; import { Global, Inject, Module, } from '@nestjs/common'; import { ModuleRef } from '@nestjs/core'; import * as mongoose from 'mongoose'; import { defer, lastValueFrom } from 'rxjs'; import { catchError } from 'rxjs/operators'; import { getConnectionToken, handleRetry } from './common/mongoose.utils.js'; import { MONGOOSE_CONNECTION_NAME, MONGOOSE_MODULE_OPTIONS, } from './mongoose.constants.js'; let MongooseCoreModule = MongooseCoreModule_1 = class MongooseCoreModule { connectionName; moduleRef; constructor(connectionName, moduleRef) { this.connectionName = connectionName; this.moduleRef = moduleRef; } static forRoot(uri, options = {}) { const { retryAttempts, retryDelay, connectionName, connectionFactory, connectionErrorFactory, lazyConnection, onConnectionCreate, verboseRetryLog, ...mongooseOptions } = options; const mongooseConnectionFactory = connectionFactory || ((connection) => connection); const mongooseConnectionError = connectionErrorFactory || ((error) => error); const mongooseConnectionName = getConnectionToken(connectionName); const mongooseConnectionNameProvider = { provide: MONGOOSE_CONNECTION_NAME, useValue: mongooseConnectionName, }; const connectionProvider = { provide: mongooseConnectionName, useFactory: async () => await lastValueFrom(defer(async () => mongooseConnectionFactory(await this.createMongooseConnection(uri, mongooseOptions, { lazyConnection, onConnectionCreate, }), mongooseConnectionName)).pipe(handleRetry(retryAttempts, retryDelay, verboseRetryLog), catchError((error) => { throw mongooseConnectionError(error); }))), }; return { module: MongooseCoreModule_1, providers: [connectionProvider, mongooseConnectionNameProvider], exports: [connectionProvider], }; } static forRootAsync(options) { const mongooseConnectionName = getConnectionToken(options.connectionName); const mongooseConnectionNameProvider = { provide: MONGOOSE_CONNECTION_NAME, useValue: mongooseConnectionName, }; const connectionProvider = { provide: mongooseConnectionName, useFactory: async (mongooseModuleOptions) => { const { retryAttempts, retryDelay, uri, connectionFactory, connectionErrorFactory, lazyConnection, onConnectionCreate, verboseRetryLog, ...mongooseOptions } = mongooseModuleOptions; const mongooseConnectionFactory = connectionFactory || ((connection) => connection); const mongooseConnectionError = connectionErrorFactory || ((error) => error); return await lastValueFrom(defer(async () => mongooseConnectionFactory(await this.createMongooseConnection(uri, mongooseOptions, { lazyConnection, onConnectionCreate }), mongooseConnectionName)).pipe(handleRetry(retryAttempts, retryDelay, verboseRetryLog), catchError((error) => { throw mongooseConnectionError(error); }))); }, inject: [MONGOOSE_MODULE_OPTIONS], }; const asyncProviders = this.createAsyncProviders(options); return { module: MongooseCoreModule_1, imports: options.imports, providers: [ ...asyncProviders, connectionProvider, mongooseConnectionNameProvider, ], exports: [connectionProvider], }; } static createAsyncProviders(options) { if (options.useExisting || options.useFactory) { return [this.createAsyncOptionsProvider(options)]; } const useClass = options.useClass; return [ this.createAsyncOptionsProvider(options), { provide: useClass, useClass, }, ]; } static createAsyncOptionsProvider(options) { if (options.useFactory) { return { provide: MONGOOSE_MODULE_OPTIONS, useFactory: options.useFactory, inject: options.inject || [], }; } const inject = [ (options.useClass || options.useExisting), ]; return { provide: MONGOOSE_MODULE_OPTIONS, useFactory: async (optionsFactory) => await optionsFactory.createMongooseOptions(), inject, }; } static async createMongooseConnection(uri, mongooseOptions, factoryOptions) { const connection = mongoose.createConnection(uri, mongooseOptions); if (factoryOptions?.lazyConnection) { return connection; } factoryOptions?.onConnectionCreate?.(connection); return connection.asPromise(); } async onApplicationShutdown() { const connection = this.moduleRef.get(this.connectionName); if (connection) { await connection.close(); } } }; MongooseCoreModule = MongooseCoreModule_1 = __decorate([ Global(), Module({}), __param(0, Inject(MONGOOSE_CONNECTION_NAME)), __metadata("design:paramtypes", [String, ModuleRef]) ], MongooseCoreModule); export { MongooseCoreModule }; //# sourceMappingURL=mongoose-core.module.js.map