opticedge-cloud-utils 1.0.8 → 1.0.9

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/dist/db/mongo.js CHANGED
@@ -3,16 +3,22 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.connectToMongo = connectToMongo;
4
4
  exports.getCollection = getCollection;
5
5
  const mongodb_1 = require("mongodb");
6
- const secrets_1 = require("../utils/secrets"); // reuse your secret manager util
6
+ const secrets_1 = require("../utils/secrets");
7
7
  let client;
8
8
  let db;
9
+ let connectPromise;
9
10
  async function connectToMongo(projectId, uriSecret, dbName) {
10
- if (client)
11
- return;
12
- const uri = await (0, secrets_1.getSecret)(projectId, uriSecret);
13
- client = new mongodb_1.MongoClient(uri);
14
- await client.connect();
15
- db = client.db(dbName); // or pass DB name if needed
11
+ if (db)
12
+ return; // already connected
13
+ if (!connectPromise) {
14
+ connectPromise = (async () => {
15
+ const uri = await (0, secrets_1.getSecret)(projectId, uriSecret);
16
+ client = new mongodb_1.MongoClient(uri);
17
+ await client.connect();
18
+ db = client.db(dbName);
19
+ })();
20
+ }
21
+ await connectPromise;
16
22
  }
17
23
  function getCollection(name) {
18
24
  if (!db)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "opticedge-cloud-utils",
3
- "version": "1.0.8",
3
+ "version": "1.0.9",
4
4
  "description": "Common utilities for cloud functions",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
package/src/db/mongo.ts CHANGED
@@ -1,16 +1,23 @@
1
1
  import { MongoClient, Db, Collection, Document } from 'mongodb'
2
- import { getSecret } from '../utils/secrets' // reuse your secret manager util
2
+ import { getSecret } from '../utils/secrets'
3
3
 
4
4
  let client: MongoClient
5
5
  let db: Db
6
+ let connectPromise: Promise<void> | undefined
6
7
 
7
8
  export async function connectToMongo(projectId: string, uriSecret: string, dbName: string) {
8
- if (client) return
9
+ if (db) return // already connected
9
10
 
10
- const uri = await getSecret(projectId, uriSecret)
11
- client = new MongoClient(uri)
12
- await client.connect()
13
- db = client.db(dbName) // or pass DB name if needed
11
+ if (!connectPromise) {
12
+ connectPromise = (async () => {
13
+ const uri = await getSecret(projectId, uriSecret)
14
+ client = new MongoClient(uri)
15
+ await client.connect()
16
+ db = client.db(dbName)
17
+ })()
18
+ }
19
+
20
+ await connectPromise
14
21
  }
15
22
 
16
23
  export function getCollection<T extends Document = Document>(name: string): Collection<T> {