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 +13 -7
- package/package.json +1 -1
- package/src/db/mongo.ts +13 -6
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");
|
|
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 (
|
|
11
|
-
return;
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
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
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'
|
|
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 (
|
|
9
|
+
if (db) return // already connected
|
|
9
10
|
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
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> {
|