1 | "use strict";
|
2 | var __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 | };
|
8 | var __metadata = (this && this.__metadata) || function (k, v) {
|
9 | if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
10 | };
|
11 | var __param = (this && this.__param) || function (paramIndex, decorator) {
|
12 | return function (target, key) { decorator(target, key, paramIndex); }
|
13 | };
|
14 | var MongooseCoreModule_1;
|
15 | Object.defineProperty(exports, "__esModule", { value: true });
|
16 | exports.MongooseCoreModule = void 0;
|
17 | const common_1 = require("@nestjs/common");
|
18 | const core_1 = require("@nestjs/core");
|
19 | const mongoose = require("mongoose");
|
20 | const rxjs_1 = require("rxjs");
|
21 | const operators_1 = require("rxjs/operators");
|
22 | const mongoose_utils_1 = require("./common/mongoose.utils");
|
23 | const mongoose_constants_1 = require("./mongoose.constants");
|
24 | let MongooseCoreModule = exports.MongooseCoreModule = MongooseCoreModule_1 = class MongooseCoreModule {
|
25 | constructor(connectionName, moduleRef) {
|
26 | this.connectionName = connectionName;
|
27 | this.moduleRef = moduleRef;
|
28 | }
|
29 | static forRoot(uri, options = {}) {
|
30 | const { retryAttempts, retryDelay, connectionName, connectionFactory, connectionErrorFactory, lazyConnection, ...mongooseOptions } = options;
|
31 | const mongooseConnectionFactory = connectionFactory || ((connection) => connection);
|
32 | const mongooseConnectionError = connectionErrorFactory || ((error) => error);
|
33 | const mongooseConnectionName = (0, mongoose_utils_1.getConnectionToken)(connectionName);
|
34 | const mongooseConnectionNameProvider = {
|
35 | provide: mongoose_constants_1.MONGOOSE_CONNECTION_NAME,
|
36 | useValue: mongooseConnectionName,
|
37 | };
|
38 | const connectionProvider = {
|
39 | provide: mongooseConnectionName,
|
40 | useFactory: async () => await (0, rxjs_1.lastValueFrom)((0, rxjs_1.defer)(async () => mongooseConnectionFactory(await this.createMongooseConnection(uri, mongooseOptions, lazyConnection), mongooseConnectionName)).pipe((0, mongoose_utils_1.handleRetry)(retryAttempts, retryDelay), (0, operators_1.catchError)((error) => {
|
41 | throw mongooseConnectionError(error);
|
42 | }))),
|
43 | };
|
44 | return {
|
45 | module: MongooseCoreModule_1,
|
46 | providers: [connectionProvider, mongooseConnectionNameProvider],
|
47 | exports: [connectionProvider],
|
48 | };
|
49 | }
|
50 | static forRootAsync(options) {
|
51 | const mongooseConnectionName = (0, mongoose_utils_1.getConnectionToken)(options.connectionName);
|
52 | const mongooseConnectionNameProvider = {
|
53 | provide: mongoose_constants_1.MONGOOSE_CONNECTION_NAME,
|
54 | useValue: mongooseConnectionName,
|
55 | };
|
56 | const connectionProvider = {
|
57 | provide: mongooseConnectionName,
|
58 | useFactory: async (mongooseModuleOptions) => {
|
59 | const { retryAttempts, retryDelay, uri, connectionFactory, connectionErrorFactory, lazyConnection, ...mongooseOptions } = mongooseModuleOptions;
|
60 | const mongooseConnectionFactory = connectionFactory || ((connection) => connection);
|
61 | const mongooseConnectionError = connectionErrorFactory || ((error) => error);
|
62 | return await (0, rxjs_1.lastValueFrom)((0, rxjs_1.defer)(async () => mongooseConnectionFactory(await this.createMongooseConnection(uri, mongooseOptions, lazyConnection), mongooseConnectionName)).pipe((0, mongoose_utils_1.handleRetry)(retryAttempts, retryDelay), (0, operators_1.catchError)((error) => {
|
63 | throw mongooseConnectionError(error);
|
64 | })));
|
65 | },
|
66 | inject: [mongoose_constants_1.MONGOOSE_MODULE_OPTIONS],
|
67 | };
|
68 | const asyncProviders = this.createAsyncProviders(options);
|
69 | return {
|
70 | module: MongooseCoreModule_1,
|
71 | imports: options.imports,
|
72 | providers: [
|
73 | ...asyncProviders,
|
74 | connectionProvider,
|
75 | mongooseConnectionNameProvider,
|
76 | ],
|
77 | exports: [connectionProvider],
|
78 | };
|
79 | }
|
80 | static createAsyncProviders(options) {
|
81 | if (options.useExisting || options.useFactory) {
|
82 | return [this.createAsyncOptionsProvider(options)];
|
83 | }
|
84 | const useClass = options.useClass;
|
85 | return [
|
86 | this.createAsyncOptionsProvider(options),
|
87 | {
|
88 | provide: useClass,
|
89 | useClass,
|
90 | },
|
91 | ];
|
92 | }
|
93 | static createAsyncOptionsProvider(options) {
|
94 | if (options.useFactory) {
|
95 | return {
|
96 | provide: mongoose_constants_1.MONGOOSE_MODULE_OPTIONS,
|
97 | useFactory: options.useFactory,
|
98 | inject: options.inject || [],
|
99 | };
|
100 | }
|
101 | const inject = [
|
102 | (options.useClass || options.useExisting),
|
103 | ];
|
104 | return {
|
105 | provide: mongoose_constants_1.MONGOOSE_MODULE_OPTIONS,
|
106 | useFactory: async (optionsFactory) => await optionsFactory.createMongooseOptions(),
|
107 | inject,
|
108 | };
|
109 | }
|
110 | static async createMongooseConnection(uri, mongooseOptions, lazyConnection) {
|
111 | const connection = mongoose.createConnection(uri, mongooseOptions);
|
112 | if (lazyConnection) {
|
113 | return connection;
|
114 | }
|
115 | return connection.asPromise();
|
116 | }
|
117 | async onApplicationShutdown() {
|
118 | const connection = this.moduleRef.get(this.connectionName);
|
119 | connection && (await connection.close());
|
120 | }
|
121 | };
|
122 | exports.MongooseCoreModule = MongooseCoreModule = MongooseCoreModule_1 = __decorate([
|
123 | (0, common_1.Global)(),
|
124 | (0, common_1.Module)({}),
|
125 | __param(0, (0, common_1.Inject)(mongoose_constants_1.MONGOOSE_CONNECTION_NAME)),
|
126 | __metadata("design:paramtypes", [String, core_1.ModuleRef])
|
127 | ], MongooseCoreModule);
|