@culturefy/shared 1.0.18 → 1.0.19
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 +37 -12
- package/build/cjs/utils/initializers.js.map +1 -1
- package/build/esm/utils/initializers.js +37 -12
- package/build/esm/utils/initializers.js.map +1 -1
- package/build/package.json +1 -1
- package/build/src/utils/initializers.d.ts +3 -1
- package/build/src/utils/initializers.js +41 -14
- package/build/src/utils/initializers.js.map +1 -1
- package/package.json +1 -1
- package/src/utils/initializers.ts +47 -20
|
@@ -12,27 +12,52 @@ function WithDb(_target, _propertyKey, descriptor) {
|
|
|
12
12
|
if (!descriptor.value) return;
|
|
13
13
|
const originalMethod = descriptor.value;
|
|
14
14
|
descriptor.value = async function (...args) {
|
|
15
|
-
|
|
16
|
-
await this.dbConnected;
|
|
15
|
+
await this.ensureConnection();
|
|
17
16
|
return originalMethod.apply(this, args);
|
|
18
17
|
};
|
|
19
18
|
}
|
|
20
19
|
class Initializers {
|
|
21
20
|
constructor(context, dbConnectionString) {
|
|
21
|
+
this.context = void 0;
|
|
22
|
+
this.dbConnectionString = void 0;
|
|
23
|
+
this.connectionInitialized = false;
|
|
22
24
|
this.context = context;
|
|
23
25
|
this.dbConnectionString = dbConnectionString;
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
serverSelectionTimeoutMS: 10000
|
|
28
|
-
}).then(conn => {
|
|
29
|
-
context.info('✅ MongoDB connected');
|
|
26
|
+
}
|
|
27
|
+
async ensureConnection() {
|
|
28
|
+
if (_mongoose.default.connection.readyState === 1) {
|
|
30
29
|
return _mongoose.default;
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
30
|
+
}
|
|
31
|
+
if (!Initializers.connectionPromise) {
|
|
32
|
+
this.context.info('Initializing database connection...');
|
|
33
|
+
Initializers.connectionPromise = _mongoose.default.connect(this.dbConnectionString, {
|
|
34
|
+
serverSelectionTimeoutMS: 10000,
|
|
35
|
+
connectTimeoutMS: 10000,
|
|
36
|
+
socketTimeoutMS: 45000
|
|
37
|
+
}).then(conn => {
|
|
38
|
+
this.context.info('✅ MongoDB connected successfully');
|
|
39
|
+
this.connectionInitialized = true;
|
|
40
|
+
return _mongoose.default;
|
|
41
|
+
}).catch(err => {
|
|
42
|
+
this.context.error('❌ MongoDB connection error:', {
|
|
43
|
+
message: err.message,
|
|
44
|
+
name: err.name,
|
|
45
|
+
code: err.code,
|
|
46
|
+
stack: err.stack
|
|
47
|
+
});
|
|
48
|
+
Initializers.connectionPromise = null;
|
|
49
|
+
this.connectionInitialized = false;
|
|
50
|
+
throw new Error(`Failed to connect to MongoDB: ${err.message}`);
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
try {
|
|
54
|
+
return await Initializers.connectionPromise;
|
|
55
|
+
} catch (error) {
|
|
56
|
+
this.connectionInitialized = false;
|
|
57
|
+
throw error;
|
|
58
|
+
}
|
|
35
59
|
}
|
|
36
60
|
}
|
|
37
61
|
exports.Initializers = Initializers;
|
|
62
|
+
Initializers.connectionPromise = null;
|
|
38
63
|
//# 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","ensureConnection","apply","Initializers","constructor","context","dbConnectionString","connectionInitialized","mongoose","connection","readyState","connectionPromise","info","connect","serverSelectionTimeoutMS","connectTimeoutMS","socketTimeoutMS","then","conn","catch","err","error","message","name","code","stack","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 this.ensureConnection();\n return originalMethod.apply(this, args);\n };\n}\n\nexport abstract class Initializers {\n private static connectionPromise: Promise<typeof mongoose> | null = null;\n protected context: InvocationContext;\n protected dbConnectionString: string;\n private connectionInitialized: boolean = false;\n\n constructor(context: InvocationContext, dbConnectionString: string) {\n this.context = context;\n this.dbConnectionString = dbConnectionString;\n }\n\n protected async ensureConnection(): Promise<typeof mongoose> {\n if (mongoose.connection.readyState === 1) {\n return mongoose;\n }\n\n if (!Initializers.connectionPromise) {\n this.context.info('Initializing database connection...');\n Initializers.connectionPromise = mongoose\n .connect(this.dbConnectionString, {\n serverSelectionTimeoutMS: 10000,\n connectTimeoutMS: 10000,\n socketTimeoutMS: 45000,\n })\n .then(conn => {\n this.context.info('✅ MongoDB connected successfully');\n this.connectionInitialized = true;\n return mongoose;\n })\n .catch(err => {\n this.context.error('❌ MongoDB connection error:', {\n message: err.message,\n name: err.name,\n code: err.code,\n stack: err.stack\n });\n Initializers.connectionPromise = null;\n this.connectionInitialized = false;\n throw new Error(`Failed to connect to MongoDB: ${err.message}`);\n });\n }\n\n try {\n return await Initializers.connectionPromise;\n } catch (error) {\n this.connectionInitialized = false;\n throw error;\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,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L,GAAG,CAACK;QACb,CAAC,CAAC;QACFtB,YAAY,CAACQ,iBAAiB,GAAG,IAAI;QACrC,IAAI,CAACJ,qBAAqB,GAAG,KAAK;QAClC,MAAM,IAAImB,KAAK,CAAC,iCAAiCN,GAAG,CAACE,OAAO,EAAE,CAAC;MACjE,CAAC,CAAC;IACN;IAEA,IAAI;MACF,OAAO,MAAMnB,YAAY,CAACQ,iBAAiB;IAC7C,CAAC,CAAC,OAAOU,KAAK,EAAE;MACd,IAAI,CAACd,qBAAqB,GAAG,KAAK;MAClC,MAAMc,KAAK;IACb;EACF;AACF;AAACM,OAAA,CAAAxB,YAAA,GAAAA,YAAA;AAjDqBA,YAAY,CACjBQ,iBAAiB,GAAoC,IAAI","ignoreList":[]}
|
|
@@ -6,26 +6,51 @@ export function WithDb(_target, _propertyKey, descriptor) {
|
|
|
6
6
|
if (!descriptor.value) return;
|
|
7
7
|
const originalMethod = descriptor.value;
|
|
8
8
|
descriptor.value = async function (...args) {
|
|
9
|
-
|
|
10
|
-
await this.dbConnected;
|
|
9
|
+
await this.ensureConnection();
|
|
11
10
|
return originalMethod.apply(this, args);
|
|
12
11
|
};
|
|
13
12
|
}
|
|
14
13
|
export class Initializers {
|
|
15
14
|
constructor(context, dbConnectionString) {
|
|
15
|
+
this.context = void 0;
|
|
16
|
+
this.dbConnectionString = void 0;
|
|
17
|
+
this.connectionInitialized = false;
|
|
16
18
|
this.context = context;
|
|
17
19
|
this.dbConnectionString = dbConnectionString;
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
serverSelectionTimeoutMS: 10000
|
|
22
|
-
}).then(conn => {
|
|
23
|
-
context.info('✅ MongoDB connected');
|
|
20
|
+
}
|
|
21
|
+
async ensureConnection() {
|
|
22
|
+
if (mongoose.connection.readyState === 1) {
|
|
24
23
|
return mongoose;
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
24
|
+
}
|
|
25
|
+
if (!Initializers.connectionPromise) {
|
|
26
|
+
this.context.info('Initializing database connection...');
|
|
27
|
+
Initializers.connectionPromise = mongoose.connect(this.dbConnectionString, {
|
|
28
|
+
serverSelectionTimeoutMS: 10000,
|
|
29
|
+
connectTimeoutMS: 10000,
|
|
30
|
+
socketTimeoutMS: 45000
|
|
31
|
+
}).then(conn => {
|
|
32
|
+
this.context.info('✅ MongoDB connected successfully');
|
|
33
|
+
this.connectionInitialized = true;
|
|
34
|
+
return mongoose;
|
|
35
|
+
}).catch(err => {
|
|
36
|
+
this.context.error('❌ MongoDB connection error:', {
|
|
37
|
+
message: err.message,
|
|
38
|
+
name: err.name,
|
|
39
|
+
code: err.code,
|
|
40
|
+
stack: err.stack
|
|
41
|
+
});
|
|
42
|
+
Initializers.connectionPromise = null;
|
|
43
|
+
this.connectionInitialized = false;
|
|
44
|
+
throw new Error(`Failed to connect to MongoDB: ${err.message}`);
|
|
45
|
+
});
|
|
46
|
+
}
|
|
47
|
+
try {
|
|
48
|
+
return await Initializers.connectionPromise;
|
|
49
|
+
} catch (error) {
|
|
50
|
+
this.connectionInitialized = false;
|
|
51
|
+
throw error;
|
|
52
|
+
}
|
|
29
53
|
}
|
|
30
54
|
}
|
|
55
|
+
Initializers.connectionPromise = null;
|
|
31
56
|
//# 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","ensureConnection","apply","Initializers","constructor","context","dbConnectionString","connectionInitialized","connection","readyState","connectionPromise","info","connect","serverSelectionTimeoutMS","connectTimeoutMS","socketTimeoutMS","then","conn","catch","err","error","message","name","code","stack","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 this.ensureConnection();\n return originalMethod.apply(this, args);\n };\n}\n\nexport abstract class Initializers {\n private static connectionPromise: Promise<typeof mongoose> | null = null;\n protected context: InvocationContext;\n protected dbConnectionString: string;\n private connectionInitialized: boolean = false;\n\n constructor(context: InvocationContext, dbConnectionString: string) {\n this.context = context;\n this.dbConnectionString = dbConnectionString;\n }\n\n protected async ensureConnection(): Promise<typeof mongoose> {\n if (mongoose.connection.readyState === 1) {\n return mongoose;\n }\n\n if (!Initializers.connectionPromise) {\n this.context.info('Initializing database connection...');\n Initializers.connectionPromise = mongoose\n .connect(this.dbConnectionString, {\n serverSelectionTimeoutMS: 10000,\n connectTimeoutMS: 10000,\n socketTimeoutMS: 45000,\n })\n .then(conn => {\n this.context.info('✅ MongoDB connected successfully');\n this.connectionInitialized = true;\n return mongoose;\n })\n .catch(err => {\n this.context.error('❌ MongoDB connection error:', {\n message: err.message,\n name: err.name,\n code: err.code,\n stack: err.stack\n });\n Initializers.connectionPromise = null;\n this.connectionInitialized = false;\n throw new Error(`Failed to connect to MongoDB: ${err.message}`);\n });\n }\n\n try {\n return await Initializers.connectionPromise;\n } catch (error) {\n this.connectionInitialized = false;\n throw error;\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,MAAM,IAAI,CAACC,gBAAgB,CAAC,CAAC;IAC7B,O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,MAAM,IAAIkB,KAAK,CAAC,iCAAiCN,GAAG,CAACE,OAAO,EAAE,CAAC;MACjE,CAAC,CAAC;IACN;IAEA,IAAI;MACF,OAAO,MAAMlB,YAAY,CAACO,iBAAiB;IAC7C,CAAC,CAAC,OAAOU,KAAK,EAAE;MACd,IAAI,CAACb,qBAAqB,GAAG,KAAK;MAClC,MAAMa,KAAK;IACb;EACF;AACF;AAjDsBjB,YAAY,CACjBO,iBAAiB,GAAoC,IAAI","ignoreList":[]}
|
package/build/package.json
CHANGED
|
@@ -5,8 +5,10 @@ import { InvocationContext } from "@azure/functions";
|
|
|
5
5
|
*/
|
|
6
6
|
export declare function WithDb(_target: any, _propertyKey: string | symbol, descriptor: TypedPropertyDescriptor<(...args: any[]) => Promise<any>>): void;
|
|
7
7
|
export declare abstract class Initializers {
|
|
8
|
+
private static connectionPromise;
|
|
8
9
|
protected context: InvocationContext;
|
|
9
10
|
protected dbConnectionString: string;
|
|
10
|
-
|
|
11
|
+
private connectionInitialized;
|
|
11
12
|
constructor(context: InvocationContext, dbConnectionString: string);
|
|
13
|
+
protected ensureConnection(): Promise<typeof mongoose>;
|
|
12
14
|
}
|
|
@@ -13,30 +13,57 @@ function WithDb(_target, _propertyKey, descriptor) {
|
|
|
13
13
|
const originalMethod = descriptor.value;
|
|
14
14
|
descriptor.value = function (...args) {
|
|
15
15
|
return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
|
16
|
-
|
|
17
|
-
yield this.dbConnected;
|
|
16
|
+
yield this.ensureConnection();
|
|
18
17
|
return originalMethod.apply(this, args);
|
|
19
18
|
});
|
|
20
19
|
};
|
|
21
20
|
}
|
|
22
21
|
class Initializers {
|
|
23
22
|
constructor(context, dbConnectionString) {
|
|
23
|
+
this.connectionInitialized = false;
|
|
24
24
|
this.context = context;
|
|
25
25
|
this.dbConnectionString = dbConnectionString;
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
26
|
+
}
|
|
27
|
+
ensureConnection() {
|
|
28
|
+
return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
|
29
|
+
if (mongoose_1.default.connection.readyState === 1) {
|
|
30
|
+
return mongoose_1.default;
|
|
31
|
+
}
|
|
32
|
+
if (!Initializers.connectionPromise) {
|
|
33
|
+
this.context.info('Initializing database connection...');
|
|
34
|
+
Initializers.connectionPromise = mongoose_1.default
|
|
35
|
+
.connect(this.dbConnectionString, {
|
|
36
|
+
serverSelectionTimeoutMS: 10000,
|
|
37
|
+
connectTimeoutMS: 10000,
|
|
38
|
+
socketTimeoutMS: 45000,
|
|
39
|
+
})
|
|
40
|
+
.then(conn => {
|
|
41
|
+
this.context.info('✅ MongoDB connected successfully');
|
|
42
|
+
this.connectionInitialized = true;
|
|
43
|
+
return mongoose_1.default;
|
|
44
|
+
})
|
|
45
|
+
.catch(err => {
|
|
46
|
+
this.context.error('❌ MongoDB connection error:', {
|
|
47
|
+
message: err.message,
|
|
48
|
+
name: err.name,
|
|
49
|
+
code: err.code,
|
|
50
|
+
stack: err.stack
|
|
51
|
+
});
|
|
52
|
+
Initializers.connectionPromise = null;
|
|
53
|
+
this.connectionInitialized = false;
|
|
54
|
+
throw new Error(`Failed to connect to MongoDB: ${err.message}`);
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
try {
|
|
58
|
+
return yield Initializers.connectionPromise;
|
|
59
|
+
}
|
|
60
|
+
catch (error) {
|
|
61
|
+
this.connectionInitialized = false;
|
|
62
|
+
throw error;
|
|
63
|
+
}
|
|
38
64
|
});
|
|
39
65
|
}
|
|
40
66
|
}
|
|
41
67
|
exports.Initializers = Initializers;
|
|
68
|
+
Initializers.connectionPromise = null;
|
|
42
69
|
//# sourceMappingURL=initializers.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"initializers.js","sourceRoot":"","sources":["../../../src/utils/initializers.ts"],"names":[],"mappings":";;;AAMA,
|
|
1
|
+
{"version":3,"file":"initializers.js","sourceRoot":"","sources":["../../../src/utils/initializers.ts"],"names":[],"mappings":";;;AAMA,wBAWC;;AAjBD,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,MAAM,IAAI,CAAC,gBAAgB,EAAE,CAAC;YAC9B,OAAO,cAAc,CAAC,KAAK,CAAC,IAAI,EAAE,IAAI,CAAC,CAAC;QAC1C,CAAC;KAAA,CAAC;AACJ,CAAC;AAED,MAAsB,YAAY;IAMhC,YAAY,OAA0B,EAAE,kBAA0B;QAF1D,0BAAqB,GAAY,KAAK,CAAC;QAG7C,IAAI,CAAC,OAAO,GAAG,OAAO,CAAC;QACvB,IAAI,CAAC,kBAAkB,GAAG,kBAAkB,CAAC;IAC/C,CAAC;IAEe,gBAAgB;;YAC9B,IAAI,kBAAQ,CAAC,UAAU,CAAC,UAAU,KAAK,CAAC,EAAE,CAAC;gBACzC,OAAO,kBAAQ,CAAC;YAClB,CAAC;YAED,IAAI,CAAC,YAAY,CAAC,iBAAiB,EAAE,CAAC;gBACpC,IAAI,CAAC,OAAO,CAAC,IAAI,CAAC,qCAAqC,CAAC,CAAC;gBACzD,YAAY,CAAC,iBAAiB,GAAG,kBAAQ;qBACtC,OAAO,CAAC,IAAI,CAAC,kBAAkB,EAAE;oBAChC,wBAAwB,EAAE,KAAK;oBAC/B,gBAAgB,EAAE,KAAK;oBACvB,eAAe,EAAE,KAAK;iBACvB,CAAC;qBACD,IAAI,CAAC,IAAI,CAAC,EAAE;oBACX,IAAI,CAAC,OAAO,CAAC,IAAI,CAAC,kCAAkC,CAAC,CAAC;oBACtD,IAAI,CAAC,qBAAqB,GAAG,IAAI,CAAC;oBAClC,OAAO,kBAAQ,CAAC;gBAClB,CAAC,CAAC;qBACD,KAAK,CAAC,GAAG,CAAC,EAAE;oBACX,IAAI,CAAC,OAAO,CAAC,KAAK,CAAC,6BAA6B,EAAE;wBAChD,OAAO,EAAE,GAAG,CAAC,OAAO;wBACpB,IAAI,EAAE,GAAG,CAAC,IAAI;wBACd,IAAI,EAAE,GAAG,CAAC,IAAI;wBACd,KAAK,EAAE,GAAG,CAAC,KAAK;qBACjB,CAAC,CAAC;oBACH,YAAY,CAAC,iBAAiB,GAAG,IAAI,CAAC;oBACtC,IAAI,CAAC,qBAAqB,GAAG,KAAK,CAAC;oBACnC,MAAM,IAAI,KAAK,CAAC,iCAAiC,GAAG,CAAC,OAAO,EAAE,CAAC,CAAC;gBAClE,CAAC,CAAC,CAAC;YACP,CAAC;YAED,IAAI,CAAC;gBACH,OAAO,MAAM,YAAY,CAAC,iBAAiB,CAAC;YAC9C,CAAC;YAAC,OAAO,KAAK,EAAE,CAAC;gBACf,IAAI,CAAC,qBAAqB,GAAG,KAAK,CAAC;gBACnC,MAAM,KAAK,CAAC;YACd,CAAC;QACH,CAAC;KAAA;;AAhDH,oCAiDC;AAhDgB,8BAAiB,GAAoC,IAAI,AAAxC,CAAyC"}
|
package/package.json
CHANGED
|
@@ -12,31 +12,58 @@ export function WithDb(
|
|
|
12
12
|
if (!descriptor.value) return;
|
|
13
13
|
const originalMethod = descriptor.value;
|
|
14
14
|
descriptor.value = async function (this: Initializers, ...args: any[]) {
|
|
15
|
-
|
|
16
|
-
await this.dbConnected;
|
|
15
|
+
await this.ensureConnection();
|
|
17
16
|
return originalMethod.apply(this, args);
|
|
18
17
|
};
|
|
19
18
|
}
|
|
20
19
|
|
|
21
20
|
export abstract class Initializers {
|
|
22
|
-
|
|
21
|
+
private static connectionPromise: Promise<typeof mongoose> | null = null;
|
|
22
|
+
protected context: InvocationContext;
|
|
23
|
+
protected dbConnectionString: string;
|
|
24
|
+
private connectionInitialized: boolean = false;
|
|
23
25
|
|
|
24
|
-
constructor(
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
26
|
+
constructor(context: InvocationContext, dbConnectionString: string) {
|
|
27
|
+
this.context = context;
|
|
28
|
+
this.dbConnectionString = dbConnectionString;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
protected async ensureConnection(): Promise<typeof mongoose> {
|
|
32
|
+
if (mongoose.connection.readyState === 1) {
|
|
33
|
+
return mongoose;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
if (!Initializers.connectionPromise) {
|
|
37
|
+
this.context.info('Initializing database connection...');
|
|
38
|
+
Initializers.connectionPromise = mongoose
|
|
39
|
+
.connect(this.dbConnectionString, {
|
|
40
|
+
serverSelectionTimeoutMS: 10000,
|
|
41
|
+
connectTimeoutMS: 10000,
|
|
42
|
+
socketTimeoutMS: 45000,
|
|
43
|
+
})
|
|
44
|
+
.then(conn => {
|
|
45
|
+
this.context.info('✅ MongoDB connected successfully');
|
|
46
|
+
this.connectionInitialized = true;
|
|
47
|
+
return mongoose;
|
|
48
|
+
})
|
|
49
|
+
.catch(err => {
|
|
50
|
+
this.context.error('❌ MongoDB connection error:', {
|
|
51
|
+
message: err.message,
|
|
52
|
+
name: err.name,
|
|
53
|
+
code: err.code,
|
|
54
|
+
stack: err.stack
|
|
55
|
+
});
|
|
56
|
+
Initializers.connectionPromise = null;
|
|
57
|
+
this.connectionInitialized = false;
|
|
58
|
+
throw new Error(`Failed to connect to MongoDB: ${err.message}`);
|
|
59
|
+
});
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
try {
|
|
63
|
+
return await Initializers.connectionPromise;
|
|
64
|
+
} catch (error) {
|
|
65
|
+
this.connectionInitialized = false;
|
|
66
|
+
throw error;
|
|
67
|
+
}
|
|
41
68
|
}
|
|
42
69
|
}
|