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, onConnectionCreate, ...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, {
|
41 | lazyConnection,
|
42 | onConnectionCreate,
|
43 | }), mongooseConnectionName)).pipe((0, mongoose_utils_1.handleRetry)(retryAttempts, retryDelay), (0, operators_1.catchError)((error) => {
|
44 | throw mongooseConnectionError(error);
|
45 | }))),
|
46 | };
|
47 | return {
|
48 | module: MongooseCoreModule_1,
|
49 | providers: [connectionProvider, mongooseConnectionNameProvider],
|
50 | exports: [connectionProvider],
|
51 | };
|
52 | }
|
53 | static forRootAsync(options) {
|
54 | const mongooseConnectionName = (0, mongoose_utils_1.getConnectionToken)(options.connectionName);
|
55 | const mongooseConnectionNameProvider = {
|
56 | provide: mongoose_constants_1.MONGOOSE_CONNECTION_NAME,
|
57 | useValue: mongooseConnectionName,
|
58 | };
|
59 | const connectionProvider = {
|
60 | provide: mongooseConnectionName,
|
61 | useFactory: async (mongooseModuleOptions) => {
|
62 | const { retryAttempts, retryDelay, uri, connectionFactory, connectionErrorFactory, lazyConnection, onConnectionCreate, ...mongooseOptions } = mongooseModuleOptions;
|
63 | const mongooseConnectionFactory = connectionFactory || ((connection) => connection);
|
64 | const mongooseConnectionError = connectionErrorFactory || ((error) => error);
|
65 | return await (0, rxjs_1.lastValueFrom)((0, rxjs_1.defer)(async () => mongooseConnectionFactory(await this.createMongooseConnection(uri, mongooseOptions, { lazyConnection, onConnectionCreate }), mongooseConnectionName)).pipe((0, mongoose_utils_1.handleRetry)(retryAttempts, retryDelay), (0, operators_1.catchError)((error) => {
|
66 | throw mongooseConnectionError(error);
|
67 | })));
|
68 | },
|
69 | inject: [mongoose_constants_1.MONGOOSE_MODULE_OPTIONS],
|
70 | };
|
71 | const asyncProviders = this.createAsyncProviders(options);
|
72 | return {
|
73 | module: MongooseCoreModule_1,
|
74 | imports: options.imports,
|
75 | providers: [
|
76 | ...asyncProviders,
|
77 | connectionProvider,
|
78 | mongooseConnectionNameProvider,
|
79 | ],
|
80 | exports: [connectionProvider],
|
81 | };
|
82 | }
|
83 | static createAsyncProviders(options) {
|
84 | if (options.useExisting || options.useFactory) {
|
85 | return [this.createAsyncOptionsProvider(options)];
|
86 | }
|
87 | const useClass = options.useClass;
|
88 | return [
|
89 | this.createAsyncOptionsProvider(options),
|
90 | {
|
91 | provide: useClass,
|
92 | useClass,
|
93 | },
|
94 | ];
|
95 | }
|
96 | static createAsyncOptionsProvider(options) {
|
97 | if (options.useFactory) {
|
98 | return {
|
99 | provide: mongoose_constants_1.MONGOOSE_MODULE_OPTIONS,
|
100 | useFactory: options.useFactory,
|
101 | inject: options.inject || [],
|
102 | };
|
103 | }
|
104 | const inject = [
|
105 | (options.useClass || options.useExisting),
|
106 | ];
|
107 | return {
|
108 | provide: mongoose_constants_1.MONGOOSE_MODULE_OPTIONS,
|
109 | useFactory: async (optionsFactory) => await optionsFactory.createMongooseOptions(),
|
110 | inject,
|
111 | };
|
112 | }
|
113 | static async createMongooseConnection(uri, mongooseOptions, factoryOptions) {
|
114 | const connection = mongoose.createConnection(uri, mongooseOptions);
|
115 | if (factoryOptions?.lazyConnection) {
|
116 | return connection;
|
117 | }
|
118 | factoryOptions?.onConnectionCreate?.(connection);
|
119 | return connection.asPromise();
|
120 | }
|
121 | async onApplicationShutdown() {
|
122 | const connection = this.moduleRef.get(this.connectionName);
|
123 | connection && (await connection.close());
|
124 | }
|
125 | };
|
126 | exports.MongooseCoreModule = MongooseCoreModule = MongooseCoreModule_1 = __decorate([
|
127 | (0, common_1.Global)(),
|
128 | (0, common_1.Module)({}),
|
129 | __param(0, (0, common_1.Inject)(mongoose_constants_1.MONGOOSE_CONNECTION_NAME)),
|
130 | __metadata("design:paramtypes", [String, core_1.ModuleRef])
|
131 | ], MongooseCoreModule);
|