@culturefy/shared 1.0.13 → 1.0.14
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/build/cjs/utils/initializers.js +11 -42
- package/build/cjs/utils/initializers.js.map +1 -1
- package/build/esm/utils/initializers.js +11 -42
- package/build/esm/utils/initializers.js.map +1 -1
- package/build/package.json +1 -1
- package/build/src/utils/initializers.d.ts +3 -11
- package/build/src/utils/initializers.js +11 -42
- package/build/src/utils/initializers.js.map +1 -1
- package/package.json +1 -1
- package/src/utils/initializers.ts +13 -53
|
@@ -5,65 +5,34 @@ exports.Initializers = void 0;
|
|
|
5
5
|
exports.WithDb = WithDb;
|
|
6
6
|
var _mongoose = _interopRequireDefault(require("mongoose"));
|
|
7
7
|
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
|
|
8
|
-
// packages/libs/src/db-initializers.ts
|
|
9
|
-
|
|
10
8
|
/**
|
|
11
|
-
* Decorator that ensures
|
|
12
|
-
* sets `this.db` before the method runs.
|
|
13
|
-
*
|
|
14
|
-
* Uses a TypedPropertyDescriptor so TypeScript understands the `this` context.
|
|
9
|
+
* Decorator that ensures MongoDB is connected before the method runs.
|
|
15
10
|
*/
|
|
16
11
|
function WithDb(_target, _propertyKey, descriptor) {
|
|
17
12
|
if (!descriptor.value) return;
|
|
18
13
|
const originalMethod = descriptor.value;
|
|
19
14
|
descriptor.value = async function (...args) {
|
|
20
|
-
//
|
|
21
|
-
|
|
22
|
-
// Execute the original method with correct `this`
|
|
15
|
+
// Await the shared connection promise
|
|
16
|
+
await this.dbConnected;
|
|
23
17
|
return originalMethod.apply(this, args);
|
|
24
18
|
};
|
|
25
19
|
}
|
|
26
20
|
class Initializers {
|
|
27
|
-
// will be set by @WithDb
|
|
28
|
-
|
|
29
21
|
constructor(context, dbConnectionString) {
|
|
30
22
|
this.context = context;
|
|
31
23
|
this.dbConnectionString = dbConnectionString;
|
|
32
|
-
this.
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
24
|
+
this.dbConnected = void 0;
|
|
25
|
+
// Connect once to the default mongoose instance
|
|
26
|
+
this.dbConnected = _mongoose.default.connect(dbConnectionString, {
|
|
27
|
+
serverSelectionTimeoutMS: 10000
|
|
28
|
+
}).then(conn => {
|
|
29
|
+
context.info('✅ MongoDB connected');
|
|
30
|
+
return _mongoose.default;
|
|
38
31
|
}).catch(err => {
|
|
39
|
-
context.error(
|
|
32
|
+
context.error('❌ MongoDB connection error', err);
|
|
40
33
|
throw err;
|
|
41
34
|
});
|
|
42
35
|
}
|
|
43
|
-
|
|
44
|
-
/**
|
|
45
|
-
* Returns a promise that resolves to the MongoDB connection.
|
|
46
|
-
*/
|
|
47
|
-
getDb() {
|
|
48
|
-
return this.dbPromise;
|
|
49
|
-
}
|
|
50
36
|
}
|
|
51
37
|
exports.Initializers = Initializers;
|
|
52
|
-
class MongoConnectionSingleton {
|
|
53
|
-
static async getInstance(context, dbConnectionString) {
|
|
54
|
-
if (!this.instances.has(dbConnectionString)) {
|
|
55
|
-
context.info("[Mongo] connecting to MongoDB…");
|
|
56
|
-
const connPromise = _mongoose.default.createConnection(dbConnectionString).asPromise().then(conn => {
|
|
57
|
-
context.info("[Mongo] connected to MongoDB");
|
|
58
|
-
return conn;
|
|
59
|
-
}).catch(err => {
|
|
60
|
-
context.error("[Mongo] connection failed", err);
|
|
61
|
-
throw err;
|
|
62
|
-
});
|
|
63
|
-
this.instances.set(dbConnectionString, connPromise);
|
|
64
|
-
}
|
|
65
|
-
return this.instances.get(dbConnectionString);
|
|
66
|
-
}
|
|
67
|
-
}
|
|
68
|
-
MongoConnectionSingleton.instances = new Map();
|
|
69
38
|
//# sourceMappingURL=initializers.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"initializers.js","names":["_mongoose","_interopRequireDefault","require","e","__esModule","default","WithDb","_target","_propertyKey","descriptor","value","originalMethod","args","
|
|
1
|
+
{"version":3,"file":"initializers.js","names":["_mongoose","_interopRequireDefault","require","e","__esModule","default","WithDb","_target","_propertyKey","descriptor","value","originalMethod","args","dbConnected","apply","Initializers","constructor","context","dbConnectionString","mongoose","connect","serverSelectionTimeoutMS","then","conn","info","catch","err","error","exports"],"sources":["../../../src/utils/initializers.ts"],"sourcesContent":["import mongoose from \"mongoose\";\nimport { InvocationContext } from \"@azure/functions\";\n\n/**\n * Decorator that ensures MongoDB is connected before the method runs.\n */\nexport function WithDb(\n _target: any,\n _propertyKey: string | symbol,\n descriptor: TypedPropertyDescriptor<(...args: any[]) => Promise<any>>\n): void {\n if (!descriptor.value) return;\n const originalMethod = descriptor.value;\n descriptor.value = async function (this: Initializers, ...args: any[]) {\n // Await the shared connection promise\n await this.dbConnected;\n return originalMethod.apply(this, args);\n };\n}\n\nexport abstract class Initializers {\n protected dbConnected: Promise<typeof mongoose>;\n\n constructor(\n protected context: InvocationContext,\n protected dbConnectionString: string\n ) {\n // Connect once to the default mongoose instance\n this.dbConnected = mongoose\n .connect(dbConnectionString, {\n serverSelectionTimeoutMS: 10000\n })\n .then(conn => {\n context.info('✅ MongoDB connected');\n return mongoose;\n })\n .catch(err => {\n context.error('❌ MongoDB connection error', err);\n throw err;\n });\n }\n}\n"],"mappings":";;;;;AAAA,IAAAA,SAAA,GAAAC,sBAAA,CAAAC,OAAA;AAAgC,SAAAD,uBAAAE,CAAA,WAAAA,CAAA,IAAAA,CAAA,CAAAC,UAAA,GAAAD,CAAA,KAAAE,OAAA,EAAAF,CAAA;AAGhC;AACA;AACA;AACO,SAASG,MAAMA,CACpBC,OAAY,EACZC,YAA6B,EAC7BC,UAAqE,EAC/D;EACN,IAAI,CAACA,UAAU,CAACC,KAAK,EAAE;EACvB,MAAMC,cAAc,GAAGF,UAAU,CAACC,KAAK;EACvCD,UAAU,CAACC,KAAK,GAAG,gBAAoC,GAAGE,IAAW,EAAE;IACrE;IACA,MAAM,IAAI,CAACC,WAAW;IACtB,OAAOF,cAAc,CAACG,KAAK,CAAC,IAAI,EAAEF,IAAI,CAAC;EACzC,CAAC;AACH;AAEO,MAAeG,YAAY,CAAC;EAGjCC,WAAWA,CACCC,OAA0B,EAC1BC,kBAA0B,EACpC;IAAA,KAFUD,OAA0B,GAA1BA,OAA0B;IAAA,KAC1BC,kBAA0B,GAA1BA,kBAA0B;IAAA,KAJ5BL,WAAW;IAMnB;IACA,IAAI,CAACA,WAAW,GAAGM,iBAAQ,CACxBC,OAAO,CAACF,kBAAkB,EAAE;MAC3BG,wBAAwB,EAAE;IAC5B,CAAC,CAAC,CACDC,IAAI,CAACC,IAAI,IAAI;MACZN,OAAO,CAACO,IAAI,CAAC,qBAAqB,CAAC;MACnC,OAAOL,iBAAQ;IACjB,CAAC,CAAC,CACDM,KAAK,CAACC,GAAG,IAAI;MACZT,OAAO,CAACU,KAAK,CAAC,4BAA4B,EAAED,GAAG,CAAC;MAChD,MAAMA,GAAG;IACX,CAAC,CAAC;EACN;AACF;AAACE,OAAA,CAAAb,YAAA,GAAAA,YAAA","ignoreList":[]}
|
|
@@ -1,62 +1,31 @@
|
|
|
1
|
-
// packages/libs/src/db-initializers.ts
|
|
2
|
-
|
|
3
1
|
import mongoose from "mongoose";
|
|
4
2
|
/**
|
|
5
|
-
* Decorator that ensures
|
|
6
|
-
* sets `this.db` before the method runs.
|
|
7
|
-
*
|
|
8
|
-
* Uses a TypedPropertyDescriptor so TypeScript understands the `this` context.
|
|
3
|
+
* Decorator that ensures MongoDB is connected before the method runs.
|
|
9
4
|
*/
|
|
10
5
|
export function WithDb(_target, _propertyKey, descriptor) {
|
|
11
6
|
if (!descriptor.value) return;
|
|
12
7
|
const originalMethod = descriptor.value;
|
|
13
8
|
descriptor.value = async function (...args) {
|
|
14
|
-
//
|
|
15
|
-
|
|
16
|
-
// Execute the original method with correct `this`
|
|
9
|
+
// Await the shared connection promise
|
|
10
|
+
await this.dbConnected;
|
|
17
11
|
return originalMethod.apply(this, args);
|
|
18
12
|
};
|
|
19
13
|
}
|
|
20
14
|
export class Initializers {
|
|
21
|
-
// will be set by @WithDb
|
|
22
|
-
|
|
23
15
|
constructor(context, dbConnectionString) {
|
|
24
16
|
this.context = context;
|
|
25
17
|
this.dbConnectionString = dbConnectionString;
|
|
26
|
-
this.
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
18
|
+
this.dbConnected = void 0;
|
|
19
|
+
// Connect once to the default mongoose instance
|
|
20
|
+
this.dbConnected = mongoose.connect(dbConnectionString, {
|
|
21
|
+
serverSelectionTimeoutMS: 10000
|
|
22
|
+
}).then(conn => {
|
|
23
|
+
context.info('✅ MongoDB connected');
|
|
24
|
+
return mongoose;
|
|
32
25
|
}).catch(err => {
|
|
33
|
-
context.error(
|
|
26
|
+
context.error('❌ MongoDB connection error', err);
|
|
34
27
|
throw err;
|
|
35
28
|
});
|
|
36
29
|
}
|
|
37
|
-
|
|
38
|
-
/**
|
|
39
|
-
* Returns a promise that resolves to the MongoDB connection.
|
|
40
|
-
*/
|
|
41
|
-
getDb() {
|
|
42
|
-
return this.dbPromise;
|
|
43
|
-
}
|
|
44
|
-
}
|
|
45
|
-
class MongoConnectionSingleton {
|
|
46
|
-
static async getInstance(context, dbConnectionString) {
|
|
47
|
-
if (!this.instances.has(dbConnectionString)) {
|
|
48
|
-
context.info("[Mongo] connecting to MongoDB…");
|
|
49
|
-
const connPromise = mongoose.createConnection(dbConnectionString).asPromise().then(conn => {
|
|
50
|
-
context.info("[Mongo] connected to MongoDB");
|
|
51
|
-
return conn;
|
|
52
|
-
}).catch(err => {
|
|
53
|
-
context.error("[Mongo] connection failed", err);
|
|
54
|
-
throw err;
|
|
55
|
-
});
|
|
56
|
-
this.instances.set(dbConnectionString, connPromise);
|
|
57
|
-
}
|
|
58
|
-
return this.instances.get(dbConnectionString);
|
|
59
|
-
}
|
|
60
30
|
}
|
|
61
|
-
MongoConnectionSingleton.instances = new Map();
|
|
62
31
|
//# sourceMappingURL=initializers.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"initializers.js","names":["mongoose","WithDb","_target","_propertyKey","descriptor","value","originalMethod","args","
|
|
1
|
+
{"version":3,"file":"initializers.js","names":["mongoose","WithDb","_target","_propertyKey","descriptor","value","originalMethod","args","dbConnected","apply","Initializers","constructor","context","dbConnectionString","connect","serverSelectionTimeoutMS","then","conn","info","catch","err","error"],"sources":["../../../src/utils/initializers.ts"],"sourcesContent":["import mongoose from \"mongoose\";\nimport { InvocationContext } from \"@azure/functions\";\n\n/**\n * Decorator that ensures MongoDB is connected before the method runs.\n */\nexport function WithDb(\n _target: any,\n _propertyKey: string | symbol,\n descriptor: TypedPropertyDescriptor<(...args: any[]) => Promise<any>>\n): void {\n if (!descriptor.value) return;\n const originalMethod = descriptor.value;\n descriptor.value = async function (this: Initializers, ...args: any[]) {\n // Await the shared connection promise\n await this.dbConnected;\n return originalMethod.apply(this, args);\n };\n}\n\nexport abstract class Initializers {\n protected dbConnected: Promise<typeof mongoose>;\n\n constructor(\n protected context: InvocationContext,\n protected dbConnectionString: string\n ) {\n // Connect once to the default mongoose instance\n this.dbConnected = mongoose\n .connect(dbConnectionString, {\n serverSelectionTimeoutMS: 10000\n })\n .then(conn => {\n context.info('✅ MongoDB connected');\n return mongoose;\n })\n .catch(err => {\n context.error('❌ MongoDB connection error', err);\n throw err;\n });\n }\n}\n"],"mappings":"AAAA,OAAOA,QAAQ,MAAM,UAAU;AAG/B;AACA;AACA;AACA,OAAO,SAASC,MAAMA,CACpBC,OAAY,EACZC,YAA6B,EAC7BC,UAAqE,EAC/D;EACN,IAAI,CAACA,UAAU,CAACC,KAAK,EAAE;EACvB,MAAMC,cAAc,GAAGF,UAAU,CAACC,KAAK;EACvCD,UAAU,CAACC,KAAK,GAAG,gBAAoC,GAAGE,IAAW,EAAE;IACrE;IACA,MAAM,IAAI,CAACC,WAAW;IACtB,OAAOF,cAAc,CAACG,KAAK,CAAC,IAAI,EAAEF,IAAI,CAAC;EACzC,CAAC;AACH;AAEA,OAAO,MAAeG,YAAY,CAAC;EAGjCC,WAAWA,CACCC,OAA0B,EAC1BC,kBAA0B,EACpC;IAAA,KAFUD,OAA0B,GAA1BA,OAA0B;IAAA,KAC1BC,kBAA0B,GAA1BA,kBAA0B;IAAA,KAJ5BL,WAAW;IAMnB;IACA,IAAI,CAACA,WAAW,GAAGR,QAAQ,CACxBc,OAAO,CAACD,kBAAkB,EAAE;MAC3BE,wBAAwB,EAAE;IAC5B,CAAC,CAAC,CACDC,IAAI,CAACC,IAAI,IAAI;MACZL,OAAO,CAACM,IAAI,CAAC,qBAAqB,CAAC;MACnC,OAAOlB,QAAQ;IACjB,CAAC,CAAC,CACDmB,KAAK,CAACC,GAAG,IAAI;MACZR,OAAO,CAACS,KAAK,CAAC,4BAA4B,EAAED,GAAG,CAAC;MAChD,MAAMA,GAAG;IACX,CAAC,CAAC;EACN;AACF","ignoreList":[]}
|
package/build/package.json
CHANGED
|
@@ -1,20 +1,12 @@
|
|
|
1
|
-
import
|
|
1
|
+
import mongoose from "mongoose";
|
|
2
2
|
import { InvocationContext } from "@azure/functions";
|
|
3
3
|
/**
|
|
4
|
-
* Decorator that ensures
|
|
5
|
-
* sets `this.db` before the method runs.
|
|
6
|
-
*
|
|
7
|
-
* Uses a TypedPropertyDescriptor so TypeScript understands the `this` context.
|
|
4
|
+
* Decorator that ensures MongoDB is connected before the method runs.
|
|
8
5
|
*/
|
|
9
6
|
export declare function WithDb(_target: any, _propertyKey: string | symbol, descriptor: TypedPropertyDescriptor<(...args: any[]) => Promise<any>>): void;
|
|
10
7
|
export declare abstract class Initializers {
|
|
11
8
|
protected context: InvocationContext;
|
|
12
9
|
protected dbConnectionString: string;
|
|
13
|
-
protected
|
|
14
|
-
protected db: Connection;
|
|
10
|
+
protected dbConnected: Promise<typeof mongoose>;
|
|
15
11
|
constructor(context: InvocationContext, dbConnectionString: string);
|
|
16
|
-
/**
|
|
17
|
-
* Returns a promise that resolves to the MongoDB connection.
|
|
18
|
-
*/
|
|
19
|
-
protected getDb(): Promise<Connection>;
|
|
20
12
|
}
|
|
@@ -1,15 +1,11 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
// packages/libs/src/db-initializers.ts
|
|
3
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
3
|
exports.Initializers = void 0;
|
|
5
4
|
exports.WithDb = WithDb;
|
|
6
5
|
const tslib_1 = require("tslib");
|
|
7
6
|
const mongoose_1 = tslib_1.__importDefault(require("mongoose"));
|
|
8
7
|
/**
|
|
9
|
-
* Decorator that ensures
|
|
10
|
-
* sets `this.db` before the method runs.
|
|
11
|
-
*
|
|
12
|
-
* Uses a TypedPropertyDescriptor so TypeScript understands the `this` context.
|
|
8
|
+
* Decorator that ensures MongoDB is connected before the method runs.
|
|
13
9
|
*/
|
|
14
10
|
function WithDb(_target, _propertyKey, descriptor) {
|
|
15
11
|
if (!descriptor.value)
|
|
@@ -17,9 +13,8 @@ function WithDb(_target, _propertyKey, descriptor) {
|
|
|
17
13
|
const originalMethod = descriptor.value;
|
|
18
14
|
descriptor.value = function (...args) {
|
|
19
15
|
return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
|
20
|
-
//
|
|
21
|
-
|
|
22
|
-
// Execute the original method with correct `this`
|
|
16
|
+
// Await the shared connection promise
|
|
17
|
+
yield this.dbConnected;
|
|
23
18
|
return originalMethod.apply(this, args);
|
|
24
19
|
});
|
|
25
20
|
};
|
|
@@ -28,46 +23,20 @@ class Initializers {
|
|
|
28
23
|
constructor(context, dbConnectionString) {
|
|
29
24
|
this.context = context;
|
|
30
25
|
this.dbConnectionString = dbConnectionString;
|
|
31
|
-
//
|
|
32
|
-
this.
|
|
26
|
+
// Connect once to the default mongoose instance
|
|
27
|
+
this.dbConnected = mongoose_1.default
|
|
28
|
+
.connect(dbConnectionString, {
|
|
29
|
+
serverSelectionTimeoutMS: 10000
|
|
30
|
+
})
|
|
33
31
|
.then(conn => {
|
|
34
|
-
context.info(
|
|
35
|
-
return
|
|
32
|
+
context.info('✅ MongoDB connected');
|
|
33
|
+
return mongoose_1.default;
|
|
36
34
|
})
|
|
37
35
|
.catch(err => {
|
|
38
|
-
context.error(
|
|
36
|
+
context.error('❌ MongoDB connection error', err);
|
|
39
37
|
throw err;
|
|
40
38
|
});
|
|
41
39
|
}
|
|
42
|
-
/**
|
|
43
|
-
* Returns a promise that resolves to the MongoDB connection.
|
|
44
|
-
*/
|
|
45
|
-
getDb() {
|
|
46
|
-
return this.dbPromise;
|
|
47
|
-
}
|
|
48
40
|
}
|
|
49
41
|
exports.Initializers = Initializers;
|
|
50
|
-
class MongoConnectionSingleton {
|
|
51
|
-
static getInstance(context, dbConnectionString) {
|
|
52
|
-
return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
|
53
|
-
if (!this.instances.has(dbConnectionString)) {
|
|
54
|
-
context.info("[Mongo] connecting to MongoDB…");
|
|
55
|
-
const connPromise = mongoose_1.default
|
|
56
|
-
.createConnection(dbConnectionString)
|
|
57
|
-
.asPromise()
|
|
58
|
-
.then(conn => {
|
|
59
|
-
context.info("[Mongo] connected to MongoDB");
|
|
60
|
-
return conn;
|
|
61
|
-
})
|
|
62
|
-
.catch(err => {
|
|
63
|
-
context.error("[Mongo] connection failed", err);
|
|
64
|
-
throw err;
|
|
65
|
-
});
|
|
66
|
-
this.instances.set(dbConnectionString, connPromise);
|
|
67
|
-
}
|
|
68
|
-
return this.instances.get(dbConnectionString);
|
|
69
|
-
});
|
|
70
|
-
}
|
|
71
|
-
}
|
|
72
|
-
MongoConnectionSingleton.instances = new Map();
|
|
73
42
|
//# sourceMappingURL=initializers.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"initializers.js","sourceRoot":"","sources":["../../../src/utils/initializers.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"initializers.js","sourceRoot":"","sources":["../../../src/utils/initializers.ts"],"names":[],"mappings":";;;AAMA,wBAYC;;AAlBD,gEAAgC;AAGhC;;GAEG;AACH,SAAgB,MAAM,CACpB,OAAY,EACZ,YAA6B,EAC7B,UAAqE;IAErE,IAAI,CAAC,UAAU,CAAC,KAAK;QAAE,OAAO;IAC9B,MAAM,cAAc,GAAG,UAAU,CAAC,KAAK,CAAC;IACxC,UAAU,CAAC,KAAK,GAAG,UAAoC,GAAG,IAAW;;YACnE,sCAAsC;YACtC,MAAM,IAAI,CAAC,WAAW,CAAC;YACvB,OAAO,cAAc,CAAC,KAAK,CAAC,IAAI,EAAE,IAAI,CAAC,CAAC;QAC1C,CAAC;KAAA,CAAC;AACJ,CAAC;AAED,MAAsB,YAAY;IAGhC,YACY,OAA0B,EAC1B,kBAA0B;QAD1B,YAAO,GAAP,OAAO,CAAmB;QAC1B,uBAAkB,GAAlB,kBAAkB,CAAQ;QAEpC,gDAAgD;QAChD,IAAI,CAAC,WAAW,GAAG,kBAAQ;aACxB,OAAO,CAAC,kBAAkB,EAAE;YAC3B,wBAAwB,EAAE,KAAK;SAChC,CAAC;aACD,IAAI,CAAC,IAAI,CAAC,EAAE;YACX,OAAO,CAAC,IAAI,CAAC,qBAAqB,CAAC,CAAC;YACpC,OAAO,kBAAQ,CAAC;QAClB,CAAC,CAAC;aACD,KAAK,CAAC,GAAG,CAAC,EAAE;YACX,OAAO,CAAC,KAAK,CAAC,4BAA4B,EAAE,GAAG,CAAC,CAAC;YACjD,MAAM,GAAG,CAAC;QACZ,CAAC,CAAC,CAAC;IACP,CAAC;CACF;AArBD,oCAqBC"}
|
package/package.json
CHANGED
|
@@ -1,13 +1,8 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
import mongoose, { Connection } from "mongoose";
|
|
1
|
+
import mongoose from "mongoose";
|
|
4
2
|
import { InvocationContext } from "@azure/functions";
|
|
5
3
|
|
|
6
4
|
/**
|
|
7
|
-
* Decorator that ensures
|
|
8
|
-
* sets `this.db` before the method runs.
|
|
9
|
-
*
|
|
10
|
-
* Uses a TypedPropertyDescriptor so TypeScript understands the `this` context.
|
|
5
|
+
* Decorator that ensures MongoDB is connected before the method runs.
|
|
11
6
|
*/
|
|
12
7
|
export function WithDb(
|
|
13
8
|
_target: any,
|
|
@@ -17,66 +12,31 @@ export function WithDb(
|
|
|
17
12
|
if (!descriptor.value) return;
|
|
18
13
|
const originalMethod = descriptor.value;
|
|
19
14
|
descriptor.value = async function (this: Initializers, ...args: any[]) {
|
|
20
|
-
//
|
|
21
|
-
|
|
22
|
-
// Execute the original method with correct `this`
|
|
15
|
+
// Await the shared connection promise
|
|
16
|
+
await this.dbConnected;
|
|
23
17
|
return originalMethod.apply(this, args);
|
|
24
18
|
};
|
|
25
19
|
}
|
|
26
20
|
|
|
27
21
|
export abstract class Initializers {
|
|
28
|
-
protected
|
|
29
|
-
protected db!: Connection; // will be set by @WithDb
|
|
22
|
+
protected dbConnected: Promise<typeof mongoose>;
|
|
30
23
|
|
|
31
24
|
constructor(
|
|
32
25
|
protected context: InvocationContext,
|
|
33
26
|
protected dbConnectionString: string
|
|
34
27
|
) {
|
|
35
|
-
//
|
|
36
|
-
this.
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
28
|
+
// Connect once to the default mongoose instance
|
|
29
|
+
this.dbConnected = mongoose
|
|
30
|
+
.connect(dbConnectionString, {
|
|
31
|
+
serverSelectionTimeoutMS: 10000
|
|
32
|
+
})
|
|
40
33
|
.then(conn => {
|
|
41
|
-
context.info(
|
|
42
|
-
return
|
|
34
|
+
context.info('✅ MongoDB connected');
|
|
35
|
+
return mongoose;
|
|
43
36
|
})
|
|
44
37
|
.catch(err => {
|
|
45
|
-
context.error(
|
|
38
|
+
context.error('❌ MongoDB connection error', err);
|
|
46
39
|
throw err;
|
|
47
40
|
});
|
|
48
41
|
}
|
|
49
|
-
|
|
50
|
-
/**
|
|
51
|
-
* Returns a promise that resolves to the MongoDB connection.
|
|
52
|
-
*/
|
|
53
|
-
protected getDb(): Promise<Connection> {
|
|
54
|
-
return this.dbPromise;
|
|
55
|
-
}
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
class MongoConnectionSingleton {
|
|
59
|
-
private static instances: Map<string, Promise<Connection>> = new Map();
|
|
60
|
-
|
|
61
|
-
static async getInstance(
|
|
62
|
-
context: InvocationContext,
|
|
63
|
-
dbConnectionString: string
|
|
64
|
-
): Promise<Connection> {
|
|
65
|
-
if (!this.instances.has(dbConnectionString)) {
|
|
66
|
-
context.info("[Mongo] connecting to MongoDB…");
|
|
67
|
-
const connPromise = mongoose
|
|
68
|
-
.createConnection(dbConnectionString)
|
|
69
|
-
.asPromise()
|
|
70
|
-
.then(conn => {
|
|
71
|
-
context.info("[Mongo] connected to MongoDB");
|
|
72
|
-
return conn;
|
|
73
|
-
})
|
|
74
|
-
.catch(err => {
|
|
75
|
-
context.error("[Mongo] connection failed", err);
|
|
76
|
-
throw err;
|
|
77
|
-
});
|
|
78
|
-
this.instances.set(dbConnectionString, connPromise);
|
|
79
|
-
}
|
|
80
|
-
return this.instances.get(dbConnectionString)!;
|
|
81
|
-
}
|
|
82
42
|
}
|