@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.
@@ -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 the MongoDB connection is initialized and
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
- // Ensure DB is connected and `this.db` is set
21
- this.db = await this.getDb();
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.dbPromise = void 0;
33
- this.db = void 0;
34
- // Initialize the DB promise only once per connection string
35
- this.dbPromise = MongoConnectionSingleton.getInstance(context, dbConnectionString).then(conn => {
36
- context.info("✅ MongoDB initialized");
37
- return conn;
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("Failed to initialize MongoDB:", err);
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","db","getDb","apply","Initializers","constructor","context","dbConnectionString","dbPromise","MongoConnectionSingleton","getInstance","then","conn","info","catch","err","error","exports","instances","has","connPromise","mongoose","createConnection","asPromise","set","get","Map"],"sources":["../../../src/utils/initializers.ts"],"sourcesContent":["// packages/libs/src/db-initializers.ts\n\nimport mongoose, { Connection } from \"mongoose\";\nimport { InvocationContext } from \"@azure/functions\";\n\n/**\n * Decorator that ensures the MongoDB connection is initialized and\n * sets `this.db` before the method runs.\n *\n * Uses a TypedPropertyDescriptor so TypeScript understands the `this` context.\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 // Ensure DB is connected and `this.db` is set\n this.db = await this.getDb();\n // Execute the original method with correct `this`\n return originalMethod.apply(this, args);\n };\n}\n\nexport abstract class Initializers {\n protected dbPromise: Promise<Connection>;\n protected db!: Connection; // will be set by @WithDb\n\n constructor(\n protected context: InvocationContext,\n protected dbConnectionString: string\n ) {\n // Initialize the DB promise only once per connection string\n this.dbPromise = MongoConnectionSingleton.getInstance(\n context,\n dbConnectionString\n )\n .then(conn => {\n context.info(\"✅ MongoDB initialized\");\n return conn;\n })\n .catch(err => {\n context.error(\"Failed to initialize MongoDB:\", err);\n throw err;\n });\n }\n\n /**\n * Returns a promise that resolves to the MongoDB connection.\n */\n protected getDb(): Promise<Connection> {\n return this.dbPromise;\n }\n}\n\nclass MongoConnectionSingleton {\n private static instances: Map<string, Promise<Connection>> = new Map();\n\n static async getInstance(\n context: InvocationContext,\n dbConnectionString: string\n ): Promise<Connection> {\n if (!this.instances.has(dbConnectionString)) {\n context.info(\"[Mongo] connecting to MongoDB…\");\n const connPromise = mongoose\n .createConnection(dbConnectionString)\n .asPromise()\n .then(conn => {\n context.info(\"[Mongo] connected to MongoDB\");\n return conn;\n })\n .catch(err => {\n context.error(\"[Mongo] connection failed\", err);\n throw err;\n });\n this.instances.set(dbConnectionString, connPromise);\n }\n return this.instances.get(dbConnectionString)!;\n }\n}\n"],"mappings":";;;;;AAEA,IAAAA,SAAA,GAAAC,sBAAA,CAAAC,OAAA;AAAgD,SAAAD,uBAAAE,CAAA,WAAAA,CAAA,IAAAA,CAAA,CAAAC,UAAA,GAAAD,CAAA,KAAAE,OAAA,EAAAF,CAAA;AAFhD;;AAKA;AACA;AACA;AACA;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,IAAI,CAACC,EAAE,GAAG,MAAM,IAAI,CAACC,KAAK,CAAC,CAAC;IAC5B;IACA,OAAOH,cAAc,CAACI,KAAK,CAAC,IAAI,EAAEH,IAAI,CAAC;EACzC,CAAC;AACH;AAEO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BAAkB,EAAEa,WAAW,CAAC;IACrD;IACA,OAAO,IAAI,CAACF,SAAS,CAACO,GAAG,CAAClB,kBAAkB,CAAC;EAC/C;AACF;AAxBME,wBAAwB,CACbS,SAAS,GAAqC,IAAIQ,GAAG,CAAC,CAAC","ignoreList":[]}
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 the MongoDB connection is initialized and
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
- // Ensure DB is connected and `this.db` is set
15
- this.db = await this.getDb();
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.dbPromise = void 0;
27
- this.db = void 0;
28
- // Initialize the DB promise only once per connection string
29
- this.dbPromise = MongoConnectionSingleton.getInstance(context, dbConnectionString).then(conn => {
30
- context.info("✅ MongoDB initialized");
31
- return conn;
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("Failed to initialize MongoDB:", err);
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","db","getDb","apply","Initializers","constructor","context","dbConnectionString","dbPromise","MongoConnectionSingleton","getInstance","then","conn","info","catch","err","error","instances","has","connPromise","createConnection","asPromise","set","get","Map"],"sources":["../../../src/utils/initializers.ts"],"sourcesContent":["// packages/libs/src/db-initializers.ts\n\nimport mongoose, { Connection } from \"mongoose\";\nimport { InvocationContext } from \"@azure/functions\";\n\n/**\n * Decorator that ensures the MongoDB connection is initialized and\n * sets `this.db` before the method runs.\n *\n * Uses a TypedPropertyDescriptor so TypeScript understands the `this` context.\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 // Ensure DB is connected and `this.db` is set\n this.db = await this.getDb();\n // Execute the original method with correct `this`\n return originalMethod.apply(this, args);\n };\n}\n\nexport abstract class Initializers {\n protected dbPromise: Promise<Connection>;\n protected db!: Connection; // will be set by @WithDb\n\n constructor(\n protected context: InvocationContext,\n protected dbConnectionString: string\n ) {\n // Initialize the DB promise only once per connection string\n this.dbPromise = MongoConnectionSingleton.getInstance(\n context,\n dbConnectionString\n )\n .then(conn => {\n context.info(\"✅ MongoDB initialized\");\n return conn;\n })\n .catch(err => {\n context.error(\"Failed to initialize MongoDB:\", err);\n throw err;\n });\n }\n\n /**\n * Returns a promise that resolves to the MongoDB connection.\n */\n protected getDb(): Promise<Connection> {\n return this.dbPromise;\n }\n}\n\nclass MongoConnectionSingleton {\n private static instances: Map<string, Promise<Connection>> = new Map();\n\n static async getInstance(\n context: InvocationContext,\n dbConnectionString: string\n ): Promise<Connection> {\n if (!this.instances.has(dbConnectionString)) {\n context.info(\"[Mongo] connecting to MongoDB…\");\n const connPromise = mongoose\n .createConnection(dbConnectionString)\n .asPromise()\n .then(conn => {\n context.info(\"[Mongo] connected to MongoDB\");\n return conn;\n })\n .catch(err => {\n context.error(\"[Mongo] connection failed\", err);\n throw err;\n });\n this.instances.set(dbConnectionString, connPromise);\n }\n return this.instances.get(dbConnectionString)!;\n }\n}\n"],"mappings":"AAAA;;AAEA,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AAI,CAAC,gCAAgC,CAAC;MAC9C,MAAMM,WAAW,GAAG1B,QAAQ,CACzB2B,gBAAgB,CAACb,kBAAkB,CAAC,CACpCc,SAAS,CAAC,CAAC,CACXV,IAAI,CAACC,IAAI,IAAI;QACZN,OAAO,CAACO,IAAI,CAAC,8BAA8B,CAAC;QAC5C,OAAOD,IAAI;MACb,CAAC,CAAC,CACDE,KAAK,CAACC,GAAG,IAAI;QACZT,OAAO,CAACU,KAAK,CAAC,2BAA2B,EAAED,GAAG,CAAC;QAC/C,MAAMA,GAAG;MACX,CAAC,CAAC;MACJ,IAAI,CAACE,SAAS,CAACK,GAAG,CAACf,kBAAkB,EAAEY,WAAW,CAAC;IACrD;IACA,OAAO,IAAI,CAACF,SAAS,CAACM,GAAG,CAAChB,kBAAkB,CAAC;EAC/C;AACF;AAxBME,wBAAwB,CACbQ,SAAS,GAAqC,IAAIO,GAAG,CAAC,CAAC","ignoreList":[]}
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":[]}
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@culturefy/shared",
3
3
  "description": "Shared utilities for culturefy serverless services",
4
- "version": "1.0.13",
4
+ "version": "1.0.14",
5
5
  "main": "build/cjs/index.js",
6
6
  "module": "build/esm/index.js",
7
7
  "types": "build/src/index.d.ts",
@@ -1,20 +1,12 @@
1
- import { Connection } from "mongoose";
1
+ import mongoose from "mongoose";
2
2
  import { InvocationContext } from "@azure/functions";
3
3
  /**
4
- * Decorator that ensures the MongoDB connection is initialized and
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 dbPromise: Promise<Connection>;
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 the MongoDB connection is initialized and
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
- // Ensure DB is connected and `this.db` is set
21
- this.db = yield this.getDb();
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
- // Initialize the DB promise only once per connection string
32
- this.dbPromise = MongoConnectionSingleton.getInstance(context, dbConnectionString)
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("✅ MongoDB initialized");
35
- return conn;
32
+ context.info('✅ MongoDB connected');
33
+ return mongoose_1.default;
36
34
  })
37
35
  .catch(err => {
38
- context.error("Failed to initialize MongoDB:", err);
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":";AAAA,uCAAuC;;;AAWvC,wBAaC;;AAtBD,gEAAgD;AAGhD;;;;;GAKG;AACH,SAAgB,MAAM,CACpB,OAAY,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3B,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,7 +1,7 @@
1
1
  {
2
2
  "name": "@culturefy/shared",
3
3
  "description": "Shared utilities for culturefy serverless services",
4
- "version": "1.0.13",
4
+ "version": "1.0.14",
5
5
  "main": "build/cjs/index.js",
6
6
  "module": "build/esm/index.js",
7
7
  "types": "build/src/index.d.ts",
@@ -1,13 +1,8 @@
1
- // packages/libs/src/db-initializers.ts
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 the MongoDB connection is initialized and
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
- // Ensure DB is connected and `this.db` is set
21
- this.db = await this.getDb();
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 dbPromise: Promise<Connection>;
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
- // Initialize the DB promise only once per connection string
36
- this.dbPromise = MongoConnectionSingleton.getInstance(
37
- context,
38
- dbConnectionString
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("✅ MongoDB initialized");
42
- return conn;
34
+ context.info('✅ MongoDB connected');
35
+ return mongoose;
43
36
  })
44
37
  .catch(err => {
45
- context.error("Failed to initialize MongoDB:", err);
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
  }