opticedge-cloud-utils 1.1.9 → 1.1.11
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.d.ts +1 -0
- package/dist/db/mongo.js +13 -0
- package/dist/db/mongo2.d.ts +1 -0
- package/dist/db/mongo2.js +12 -0
- package/dist/db/mongo3.d.ts +1 -0
- package/dist/db/mongo3.js +13 -0
- package/dist/task.js +3 -2
- package/package.json +1 -1
- package/src/db/mongo.ts +14 -0
- package/src/db/mongo2.ts +13 -0
- package/src/db/mongo3.ts +16 -0
- package/src/task.ts +3 -2
- package/tests/db/mongo.test.ts +21 -1
- package/tests/db/mongo2.test.ts +34 -1
- package/tests/db/mongo3.test.ts +50 -1
package/dist/db/mongo.d.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
1
|
import { Collection, Document } from 'mongodb';
|
|
2
2
|
export declare function connectToMongo(projectId: string, uriSecret: string, dbName: string): Promise<void>;
|
|
3
|
+
export declare function connectToMongoWithUri(uri: string, dbName: string): Promise<void>;
|
|
3
4
|
export declare function getCollection<T extends Document = Document>(name: string): Collection<T>;
|
package/dist/db/mongo.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.connectToMongo = connectToMongo;
|
|
4
|
+
exports.connectToMongoWithUri = connectToMongoWithUri;
|
|
4
5
|
exports.getCollection = getCollection;
|
|
5
6
|
const mongodb_1 = require("mongodb");
|
|
6
7
|
const secrets_1 = require("../secrets");
|
|
@@ -20,6 +21,18 @@ async function connectToMongo(projectId, uriSecret, dbName) {
|
|
|
20
21
|
}
|
|
21
22
|
await connectPromise;
|
|
22
23
|
}
|
|
24
|
+
async function connectToMongoWithUri(uri, dbName) {
|
|
25
|
+
if (db)
|
|
26
|
+
return; // already connected
|
|
27
|
+
if (!connectPromise) {
|
|
28
|
+
connectPromise = (async () => {
|
|
29
|
+
client = new mongodb_1.MongoClient(uri);
|
|
30
|
+
await client.connect();
|
|
31
|
+
db = client.db(dbName);
|
|
32
|
+
})();
|
|
33
|
+
}
|
|
34
|
+
await connectPromise;
|
|
35
|
+
}
|
|
23
36
|
function getCollection(name) {
|
|
24
37
|
if (!db)
|
|
25
38
|
throw new Error('Mongo not initialized');
|
package/dist/db/mongo2.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { Db, Collection, Document } from 'mongodb';
|
|
2
2
|
export declare function connectToMongo2(projectId: string, uriSecret: string): Promise<void>;
|
|
3
|
+
export declare function connectToMongoWithUri2(uri: string): Promise<void>;
|
|
3
4
|
export declare function getDb2(dbName: string): Db;
|
|
4
5
|
export declare function getCollection2<T extends Document = Document>(dbName: string, collectionName: string): Collection<T>;
|
package/dist/db/mongo2.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.connectToMongo2 = connectToMongo2;
|
|
4
|
+
exports.connectToMongoWithUri2 = connectToMongoWithUri2;
|
|
4
5
|
exports.getDb2 = getDb2;
|
|
5
6
|
exports.getCollection2 = getCollection2;
|
|
6
7
|
// Multi DB safe util
|
|
@@ -20,6 +21,17 @@ async function connectToMongo2(projectId, uriSecret) {
|
|
|
20
21
|
}
|
|
21
22
|
await connectPromise;
|
|
22
23
|
}
|
|
24
|
+
async function connectToMongoWithUri2(uri) {
|
|
25
|
+
if (client)
|
|
26
|
+
return; // already connected
|
|
27
|
+
if (!connectPromise) {
|
|
28
|
+
connectPromise = (async () => {
|
|
29
|
+
client = new mongodb_1.MongoClient(uri);
|
|
30
|
+
await client.connect();
|
|
31
|
+
})();
|
|
32
|
+
}
|
|
33
|
+
await connectPromise;
|
|
34
|
+
}
|
|
23
35
|
function getDb2(dbName) {
|
|
24
36
|
if (!client)
|
|
25
37
|
throw new Error('Mongo not initialized');
|
package/dist/db/mongo3.d.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { MongoClient, Db, Collection, Document } from 'mongodb';
|
|
2
2
|
export declare function connectToMongo3(projectId: string, uriSecret: string): Promise<MongoClient>;
|
|
3
|
+
export declare function connectToMongoWithUri3(uri: string): Promise<MongoClient>;
|
|
3
4
|
export declare function getDb3(uriSecret: string, dbName: string): Db;
|
|
4
5
|
export declare function getCollection3<T extends Document = Document>(uriSecret: string, dbName: string, collectionName: string): Collection<T>;
|
|
5
6
|
export declare const __mongoClients: Map<string, MongoClient>;
|
package/dist/db/mongo3.js
CHANGED
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.__connectPromises = exports.__mongoClients = void 0;
|
|
4
4
|
exports.connectToMongo3 = connectToMongo3;
|
|
5
|
+
exports.connectToMongoWithUri3 = connectToMongoWithUri3;
|
|
5
6
|
exports.getDb3 = getDb3;
|
|
6
7
|
exports.getCollection3 = getCollection3;
|
|
7
8
|
// Multi cluster safe util
|
|
@@ -25,6 +26,18 @@ async function connectToMongo3(projectId, uriSecret) {
|
|
|
25
26
|
await connectPromises.get(uriSecret);
|
|
26
27
|
return clients.get(uriSecret);
|
|
27
28
|
}
|
|
29
|
+
async function connectToMongoWithUri3(uri) {
|
|
30
|
+
if (!connectPromises.has(uri)) {
|
|
31
|
+
const promise = (async () => {
|
|
32
|
+
const client = new mongodb_1.MongoClient(uri);
|
|
33
|
+
await client.connect();
|
|
34
|
+
clients.set(uri, client);
|
|
35
|
+
})();
|
|
36
|
+
connectPromises.set(uri, promise);
|
|
37
|
+
}
|
|
38
|
+
await connectPromises.get(uri);
|
|
39
|
+
return clients.get(uri);
|
|
40
|
+
}
|
|
28
41
|
function getDb3(uriSecret, dbName) {
|
|
29
42
|
const client = clients.get(uriSecret);
|
|
30
43
|
if (!client) {
|
package/dist/task.js
CHANGED
|
@@ -62,8 +62,9 @@ async function createTask(projectId, region, queueId, data, serviceAccount, audi
|
|
|
62
62
|
// retry options with sensible defaults; allow caller to override via opts.retryOptions
|
|
63
63
|
const retryOpts = {
|
|
64
64
|
retries: 3,
|
|
65
|
-
baseDelayMs:
|
|
66
|
-
maxDelayMs:
|
|
65
|
+
baseDelayMs: 300,
|
|
66
|
+
maxDelayMs: 2000,
|
|
67
|
+
timeoutMs: 15000,
|
|
67
68
|
isRetryable: isRetryableCloudTasks,
|
|
68
69
|
onRetry: (err, attempt, delay) => {
|
|
69
70
|
console.warn(`createTask retry #${attempt} in ${delay}ms — ${String(err?.message ?? err)}`);
|
package/package.json
CHANGED
package/src/db/mongo.ts
CHANGED
|
@@ -20,6 +20,20 @@ export async function connectToMongo(projectId: string, uriSecret: string, dbNam
|
|
|
20
20
|
await connectPromise
|
|
21
21
|
}
|
|
22
22
|
|
|
23
|
+
export async function connectToMongoWithUri(uri: string, dbName: string) {
|
|
24
|
+
if (db) return // already connected
|
|
25
|
+
|
|
26
|
+
if (!connectPromise) {
|
|
27
|
+
connectPromise = (async () => {
|
|
28
|
+
client = new MongoClient(uri)
|
|
29
|
+
await client.connect()
|
|
30
|
+
db = client.db(dbName)
|
|
31
|
+
})()
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
await connectPromise
|
|
35
|
+
}
|
|
36
|
+
|
|
23
37
|
export function getCollection<T extends Document = Document>(name: string): Collection<T> {
|
|
24
38
|
if (!db) throw new Error('Mongo not initialized')
|
|
25
39
|
return db.collection<T>(name)
|
package/src/db/mongo2.ts
CHANGED
|
@@ -19,6 +19,19 @@ export async function connectToMongo2(projectId: string, uriSecret: string) {
|
|
|
19
19
|
await connectPromise
|
|
20
20
|
}
|
|
21
21
|
|
|
22
|
+
export async function connectToMongoWithUri2(uri: string) {
|
|
23
|
+
if (client) return // already connected
|
|
24
|
+
|
|
25
|
+
if (!connectPromise) {
|
|
26
|
+
connectPromise = (async () => {
|
|
27
|
+
client = new MongoClient(uri)
|
|
28
|
+
await client.connect()
|
|
29
|
+
})()
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
await connectPromise
|
|
33
|
+
}
|
|
34
|
+
|
|
22
35
|
export function getDb2(dbName: string): Db {
|
|
23
36
|
if (!client) throw new Error('Mongo not initialized')
|
|
24
37
|
return client.db(dbName)
|
package/src/db/mongo3.ts
CHANGED
|
@@ -26,6 +26,22 @@ export async function connectToMongo3(projectId: string, uriSecret: string): Pro
|
|
|
26
26
|
return clients.get(uriSecret)!
|
|
27
27
|
}
|
|
28
28
|
|
|
29
|
+
export async function connectToMongoWithUri3(uri: string): Promise<MongoClient> {
|
|
30
|
+
if (!connectPromises.has(uri)) {
|
|
31
|
+
const promise = (async () => {
|
|
32
|
+
const client = new MongoClient(uri)
|
|
33
|
+
await client.connect()
|
|
34
|
+
clients.set(uri, client)
|
|
35
|
+
})()
|
|
36
|
+
|
|
37
|
+
connectPromises.set(uri, promise)
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
await connectPromises.get(uri)!
|
|
41
|
+
|
|
42
|
+
return clients.get(uri)!
|
|
43
|
+
}
|
|
44
|
+
|
|
29
45
|
export function getDb3(uriSecret: string, dbName: string): Db {
|
|
30
46
|
const client = clients.get(uriSecret)
|
|
31
47
|
if (!client) {
|
package/src/task.ts
CHANGED
|
@@ -85,8 +85,9 @@ export async function createTask(
|
|
|
85
85
|
// retry options with sensible defaults; allow caller to override via opts.retryOptions
|
|
86
86
|
const retryOpts: RetryOptions = {
|
|
87
87
|
retries: 3,
|
|
88
|
-
baseDelayMs:
|
|
89
|
-
maxDelayMs:
|
|
88
|
+
baseDelayMs: 300,
|
|
89
|
+
maxDelayMs: 2000,
|
|
90
|
+
timeoutMs: 15000,
|
|
90
91
|
isRetryable: isRetryableCloudTasks,
|
|
91
92
|
onRetry: (err, attempt, delay) => {
|
|
92
93
|
console.warn(`createTask retry #${attempt} in ${delay}ms — ${String(err?.message ?? err)}`)
|
package/tests/db/mongo.test.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { connectToMongo, getCollection } from '../../src/db/mongo'
|
|
1
|
+
import { connectToMongo, connectToMongoWithUri, getCollection } from '../../src/db/mongo'
|
|
2
2
|
import { MongoClient, Db } from 'mongodb'
|
|
3
3
|
import * as secretsModule from '../../src/secrets'
|
|
4
4
|
|
|
@@ -45,3 +45,23 @@ describe('Mongo Utils', () => {
|
|
|
45
45
|
})
|
|
46
46
|
})
|
|
47
47
|
})
|
|
48
|
+
|
|
49
|
+
describe('Mongo Utils 2', () => {
|
|
50
|
+
it('should connect to MongoDB and store client/db', async () => {
|
|
51
|
+
jest.isolateModules(async () => {
|
|
52
|
+
// eslint-disable-next-line @typescript-eslint/no-require-imports
|
|
53
|
+
const { connectToMongoWithUri } = require('../../src/db/mongo')
|
|
54
|
+
await connectToMongoWithUri('mongo-uri', 'test-db')
|
|
55
|
+
|
|
56
|
+
expect(mockConnect).toHaveBeenCalled()
|
|
57
|
+
expect(mockClient.db).toHaveBeenCalledWith('test-db')
|
|
58
|
+
})
|
|
59
|
+
})
|
|
60
|
+
|
|
61
|
+
it('should return a collection when db is initialized', async () => {
|
|
62
|
+
await connectToMongoWithUri('mongo-uri', 'test-db')
|
|
63
|
+
getCollection('users')
|
|
64
|
+
|
|
65
|
+
expect(mockDb.collection).toHaveBeenCalledWith('users')
|
|
66
|
+
})
|
|
67
|
+
})
|
package/tests/db/mongo2.test.ts
CHANGED
|
@@ -1,4 +1,9 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import {
|
|
2
|
+
connectToMongo2,
|
|
3
|
+
getDb2,
|
|
4
|
+
getCollection2,
|
|
5
|
+
connectToMongoWithUri2
|
|
6
|
+
} from '../../src/db/mongo2'
|
|
2
7
|
import { MongoClient, Db } from 'mongodb'
|
|
3
8
|
import * as secretsModule from '../../src/secrets'
|
|
4
9
|
|
|
@@ -61,3 +66,31 @@ describe('Mongo Utils', () => {
|
|
|
61
66
|
})
|
|
62
67
|
})
|
|
63
68
|
})
|
|
69
|
+
|
|
70
|
+
describe('Mongo Utils 2', () => {
|
|
71
|
+
it('should connect to MongoDB and store client', async () => {
|
|
72
|
+
jest.isolateModules(async () => {
|
|
73
|
+
// eslint-disable-next-line @typescript-eslint/no-require-imports
|
|
74
|
+
const { connectToMongoWithUri2 } = require('../../src/db/mongo2')
|
|
75
|
+
await connectToMongoWithUri2('mongo-uri')
|
|
76
|
+
|
|
77
|
+
expect(mockConnect).toHaveBeenCalled()
|
|
78
|
+
})
|
|
79
|
+
})
|
|
80
|
+
|
|
81
|
+
it('should return a Db instance when client is initialized', async () => {
|
|
82
|
+
await connectToMongoWithUri2('mongo-uri')
|
|
83
|
+
|
|
84
|
+
const db = getDb2('test-db')
|
|
85
|
+
expect(mockClient.db).toHaveBeenCalledWith('test-db')
|
|
86
|
+
expect(db).toBe(mockDb)
|
|
87
|
+
})
|
|
88
|
+
|
|
89
|
+
it('should return a collection when client is initialized', async () => {
|
|
90
|
+
await connectToMongoWithUri2('mongo-uri')
|
|
91
|
+
|
|
92
|
+
getCollection2('test-db', 'users')
|
|
93
|
+
expect(mockClient.db).toHaveBeenCalledWith('test-db')
|
|
94
|
+
expect(mockDb.collection).toHaveBeenCalledWith('users')
|
|
95
|
+
})
|
|
96
|
+
})
|
package/tests/db/mongo3.test.ts
CHANGED
|
@@ -3,7 +3,8 @@ import {
|
|
|
3
3
|
getDb3,
|
|
4
4
|
getCollection3,
|
|
5
5
|
__mongoClients,
|
|
6
|
-
__connectPromises
|
|
6
|
+
__connectPromises,
|
|
7
|
+
connectToMongoWithUri3
|
|
7
8
|
} from '../../src/db/mongo3'
|
|
8
9
|
import { MongoClient, Db } from 'mongodb'
|
|
9
10
|
import * as secretsModule from '../../src/secrets'
|
|
@@ -98,3 +99,51 @@ describe('MongoClientManager (multi-cluster)', () => {
|
|
|
98
99
|
})
|
|
99
100
|
})
|
|
100
101
|
})
|
|
102
|
+
|
|
103
|
+
describe('MongoClientManager (multi-cluster) 2', () => {
|
|
104
|
+
const clusterAUri = 'clusterA-uri'
|
|
105
|
+
const clusterBUri = 'clusterB-uri'
|
|
106
|
+
|
|
107
|
+
it('should connect and cache client for a single cluster', async () => {
|
|
108
|
+
jest.isolateModules(async () => {
|
|
109
|
+
// eslint-disable-next-line @typescript-eslint/no-require-imports
|
|
110
|
+
const { connectToMongoWithUri3, getDb3 } = require('../../src/db/mongo3')
|
|
111
|
+
await connectToMongoWithUri3(clusterAUri)
|
|
112
|
+
|
|
113
|
+
expect(mockConnect).toHaveBeenCalled()
|
|
114
|
+
|
|
115
|
+
const db = getDb3(clusterAUri, 'test-db')
|
|
116
|
+
expect(mockClient.db).toHaveBeenCalledWith('test-db')
|
|
117
|
+
expect(db).toBe(mockDb)
|
|
118
|
+
})
|
|
119
|
+
})
|
|
120
|
+
|
|
121
|
+
it('should reuse client if already connected', async () => {
|
|
122
|
+
jest.isolateModules(async () => {
|
|
123
|
+
// eslint-disable-next-line @typescript-eslint/no-require-imports
|
|
124
|
+
const { connectToMongoWithUri3 } = require('../../src/db/mongo3')
|
|
125
|
+
await connectToMongoWithUri3(clusterAUri)
|
|
126
|
+
await connectToMongoWithUri3(clusterAUri)
|
|
127
|
+
expect(mockConnect).toHaveBeenCalledTimes(1)
|
|
128
|
+
})
|
|
129
|
+
})
|
|
130
|
+
|
|
131
|
+
it('should connect and cache separate clients for multiple clusters', async () => {
|
|
132
|
+
jest.isolateModules(async () => {
|
|
133
|
+
// eslint-disable-next-line @typescript-eslint/no-require-imports
|
|
134
|
+
const { connectToMongoWithUri3 } = require('../../src/db/mongo3')
|
|
135
|
+
await connectToMongoWithUri3(clusterAUri)
|
|
136
|
+
await connectToMongoWithUri3(clusterBUri)
|
|
137
|
+
expect(mockConnect).toHaveBeenCalledTimes(2)
|
|
138
|
+
})
|
|
139
|
+
})
|
|
140
|
+
|
|
141
|
+
it('should return a collection when client is initialized', async () => {
|
|
142
|
+
await connectToMongoWithUri3(clusterAUri)
|
|
143
|
+
|
|
144
|
+
getCollection3(clusterAUri, 'test-db', 'users')
|
|
145
|
+
|
|
146
|
+
expect(mockClient.db).toHaveBeenCalledWith('test-db')
|
|
147
|
+
expect(mockDb.collection).toHaveBeenCalledWith('users')
|
|
148
|
+
})
|
|
149
|
+
})
|