UNPKG

7.37 kBJavaScriptView Raw
1"use strict";
2var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
3 var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
4 if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
5 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;
6 return c > 3 && r && Object.defineProperty(target, key, r), r;
7};
8var __metadata = (this && this.__metadata) || function (k, v) {
9 if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
10};
11var __param = (this && this.__param) || function (paramIndex, decorator) {
12 return function (target, key) { decorator(target, key, paramIndex); }
13};
14var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
15 function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
16 return new (P || (P = Promise))(function (resolve, reject) {
17 function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
18 function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
19 function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
20 step((generator = generator.apply(thisArg, _arguments || [])).next());
21 });
22};
23var __rest = (this && this.__rest) || function (s, e) {
24 var t = {};
25 for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
26 t[p] = s[p];
27 if (s != null && typeof Object.getOwnPropertySymbols === "function")
28 for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
29 if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
30 t[p[i]] = s[p[i]];
31 }
32 return t;
33};
34var MongooseCoreModule_1;
35Object.defineProperty(exports, "__esModule", { value: true });
36exports.MongooseCoreModule = void 0;
37const common_1 = require("@nestjs/common");
38const core_1 = require("@nestjs/core");
39const mongoose = require("mongoose");
40const rxjs_1 = require("rxjs");
41const mongoose_utils_1 = require("./common/mongoose.utils");
42const mongoose_constants_1 = require("./mongoose.constants");
43let MongooseCoreModule = MongooseCoreModule_1 = class MongooseCoreModule {
44 constructor(connectionName, moduleRef) {
45 this.connectionName = connectionName;
46 this.moduleRef = moduleRef;
47 }
48 static forRoot(uri, options = {}) {
49 const { retryAttempts, retryDelay, connectionName, connectionFactory } = options, mongooseOptions = __rest(options, ["retryAttempts", "retryDelay", "connectionName", "connectionFactory"]);
50 const mongooseConnectionFactory = connectionFactory || ((connection) => connection);
51 const mongooseConnectionName = (0, mongoose_utils_1.getConnectionToken)(connectionName);
52 const mongooseConnectionNameProvider = {
53 provide: mongoose_constants_1.MONGOOSE_CONNECTION_NAME,
54 useValue: mongooseConnectionName,
55 };
56 const connectionProvider = {
57 provide: mongooseConnectionName,
58 useFactory: () => __awaiter(this, void 0, void 0, function* () {
59 return yield (0, rxjs_1.lastValueFrom)((0, rxjs_1.defer)(() => __awaiter(this, void 0, void 0, function* () {
60 return mongooseConnectionFactory(mongoose.createConnection(uri, mongooseOptions), mongooseConnectionName);
61 })).pipe((0, mongoose_utils_1.handleRetry)(retryAttempts, retryDelay)));
62 }),
63 };
64 return {
65 module: MongooseCoreModule_1,
66 providers: [connectionProvider, mongooseConnectionNameProvider],
67 exports: [connectionProvider],
68 };
69 }
70 static forRootAsync(options) {
71 const mongooseConnectionName = (0, mongoose_utils_1.getConnectionToken)(options.connectionName);
72 const mongooseConnectionNameProvider = {
73 provide: mongoose_constants_1.MONGOOSE_CONNECTION_NAME,
74 useValue: mongooseConnectionName,
75 };
76 const connectionProvider = {
77 provide: mongooseConnectionName,
78 useFactory: (mongooseModuleOptions) => __awaiter(this, void 0, void 0, function* () {
79 const { retryAttempts, retryDelay, connectionName, uri, connectionFactory } = mongooseModuleOptions, mongooseOptions = __rest(mongooseModuleOptions, ["retryAttempts", "retryDelay", "connectionName", "uri", "connectionFactory"]);
80 const mongooseConnectionFactory = connectionFactory || ((connection) => connection);
81 return yield (0, rxjs_1.lastValueFrom)((0, rxjs_1.defer)(() => __awaiter(this, void 0, void 0, function* () {
82 return mongooseConnectionFactory(mongoose.createConnection(mongooseModuleOptions.uri, mongooseOptions), mongooseConnectionName);
83 })).pipe((0, mongoose_utils_1.handleRetry)(mongooseModuleOptions.retryAttempts, mongooseModuleOptions.retryDelay)));
84 }),
85 inject: [mongoose_constants_1.MONGOOSE_MODULE_OPTIONS],
86 };
87 const asyncProviders = this.createAsyncProviders(options);
88 return {
89 module: MongooseCoreModule_1,
90 imports: options.imports,
91 providers: [
92 ...asyncProviders,
93 connectionProvider,
94 mongooseConnectionNameProvider,
95 ],
96 exports: [connectionProvider],
97 };
98 }
99 onApplicationShutdown() {
100 return __awaiter(this, void 0, void 0, function* () {
101 const connection = this.moduleRef.get(this.connectionName);
102 connection && (yield connection.close());
103 });
104 }
105 static createAsyncProviders(options) {
106 if (options.useExisting || options.useFactory) {
107 return [this.createAsyncOptionsProvider(options)];
108 }
109 const useClass = options.useClass;
110 return [
111 this.createAsyncOptionsProvider(options),
112 {
113 provide: useClass,
114 useClass,
115 },
116 ];
117 }
118 static createAsyncOptionsProvider(options) {
119 if (options.useFactory) {
120 return {
121 provide: mongoose_constants_1.MONGOOSE_MODULE_OPTIONS,
122 useFactory: options.useFactory,
123 inject: options.inject || [],
124 };
125 }
126 const inject = [
127 (options.useClass || options.useExisting),
128 ];
129 return {
130 provide: mongoose_constants_1.MONGOOSE_MODULE_OPTIONS,
131 useFactory: (optionsFactory) => __awaiter(this, void 0, void 0, function* () { return yield optionsFactory.createMongooseOptions(); }),
132 inject,
133 };
134 }
135};
136MongooseCoreModule = MongooseCoreModule_1 = __decorate([
137 (0, common_1.Global)(),
138 (0, common_1.Module)({}),
139 __param(0, (0, common_1.Inject)(mongoose_constants_1.MONGOOSE_CONNECTION_NAME)),
140 __metadata("design:paramtypes", [String, core_1.ModuleRef])
141], MongooseCoreModule);
142exports.MongooseCoreModule = MongooseCoreModule;