UNPKG

7.46 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(yield mongoose.createConnection(uri, mongooseOptions).asPromise(), 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(yield mongoose
83 .createConnection(mongooseModuleOptions.uri, mongooseOptions)
84 .asPromise(), mongooseConnectionName);
85 })).pipe((0, mongoose_utils_1.handleRetry)(mongooseModuleOptions.retryAttempts, mongooseModuleOptions.retryDelay)));
86 }),
87 inject: [mongoose_constants_1.MONGOOSE_MODULE_OPTIONS],
88 };
89 const asyncProviders = this.createAsyncProviders(options);
90 return {
91 module: MongooseCoreModule_1,
92 imports: options.imports,
93 providers: [
94 ...asyncProviders,
95 connectionProvider,
96 mongooseConnectionNameProvider,
97 ],
98 exports: [connectionProvider],
99 };
100 }
101 onApplicationShutdown() {
102 return __awaiter(this, void 0, void 0, function* () {
103 const connection = this.moduleRef.get(this.connectionName);
104 connection && (yield connection.close());
105 });
106 }
107 static createAsyncProviders(options) {
108 if (options.useExisting || options.useFactory) {
109 return [this.createAsyncOptionsProvider(options)];
110 }
111 const useClass = options.useClass;
112 return [
113 this.createAsyncOptionsProvider(options),
114 {
115 provide: useClass,
116 useClass,
117 },
118 ];
119 }
120 static createAsyncOptionsProvider(options) {
121 if (options.useFactory) {
122 return {
123 provide: mongoose_constants_1.MONGOOSE_MODULE_OPTIONS,
124 useFactory: options.useFactory,
125 inject: options.inject || [],
126 };
127 }
128 const inject = [
129 (options.useClass || options.useExisting),
130 ];
131 return {
132 provide: mongoose_constants_1.MONGOOSE_MODULE_OPTIONS,
133 useFactory: (optionsFactory) => __awaiter(this, void 0, void 0, function* () { return yield optionsFactory.createMongooseOptions(); }),
134 inject,
135 };
136 }
137};
138MongooseCoreModule = MongooseCoreModule_1 = __decorate([
139 (0, common_1.Global)(),
140 (0, common_1.Module)({}),
141 __param(0, (0, common_1.Inject)(mongoose_constants_1.MONGOOSE_CONNECTION_NAME)),
142 __metadata("design:paramtypes", [String, core_1.ModuleRef])
143], MongooseCoreModule);
144exports.MongooseCoreModule = MongooseCoreModule;