opticedge-cloud-utils 1.1.30 → 1.1.31
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 +9 -0
- package/dist/db/mongo2.d.ts +1 -0
- package/dist/db/mongo2.js +8 -0
- package/dist/db/mongo3.d.ts +2 -0
- package/dist/db/mongo3.js +15 -0
- package/package.json +1 -1
- package/src/db/mongo.ts +12 -2
- package/src/db/mongo2.ts +10 -1
- package/src/db/mongo3.ts +18 -0
- package/tests/db/mongo.test.ts +32 -12
- package/tests/db/mongo2.test.ts +30 -15
- package/tests/db/mongo3.test.ts +37 -16
package/dist/db/mongo.d.ts
CHANGED
|
@@ -2,3 +2,4 @@ import { Db, Collection, Document } from 'mongodb';
|
|
|
2
2
|
export declare function connectToMongo(uri: string, dbName: string): Promise<void>;
|
|
3
3
|
export declare function getDb(): Db;
|
|
4
4
|
export declare function getCollection<T extends Document = Document>(name: string): Collection<T>;
|
|
5
|
+
export declare function closeMongo(): Promise<void>;
|
package/dist/db/mongo.js
CHANGED
|
@@ -3,6 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.connectToMongo = connectToMongo;
|
|
4
4
|
exports.getDb = getDb;
|
|
5
5
|
exports.getCollection = getCollection;
|
|
6
|
+
exports.closeMongo = closeMongo;
|
|
6
7
|
const mongodb_1 = require("mongodb");
|
|
7
8
|
let client;
|
|
8
9
|
let db;
|
|
@@ -29,3 +30,11 @@ function getCollection(name) {
|
|
|
29
30
|
throw new Error('Mongo not initialized');
|
|
30
31
|
return db.collection(name);
|
|
31
32
|
}
|
|
33
|
+
async function closeMongo() {
|
|
34
|
+
if (!client)
|
|
35
|
+
return;
|
|
36
|
+
await client.close();
|
|
37
|
+
client = undefined;
|
|
38
|
+
db = undefined;
|
|
39
|
+
connectPromise = undefined;
|
|
40
|
+
}
|
package/dist/db/mongo2.d.ts
CHANGED
|
@@ -2,3 +2,4 @@ import { Db, Collection, Document } from 'mongodb';
|
|
|
2
2
|
export declare function connectToMongo2(uri: string): Promise<void>;
|
|
3
3
|
export declare function getDb2(dbName: string): Db;
|
|
4
4
|
export declare function getCollection2<T extends Document = Document>(dbName: string, collectionName: string): Collection<T>;
|
|
5
|
+
export declare function closeMongo2(): Promise<void>;
|
package/dist/db/mongo2.js
CHANGED
|
@@ -3,6 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.connectToMongo2 = connectToMongo2;
|
|
4
4
|
exports.getDb2 = getDb2;
|
|
5
5
|
exports.getCollection2 = getCollection2;
|
|
6
|
+
exports.closeMongo2 = closeMongo2;
|
|
6
7
|
// Multi DB safe util
|
|
7
8
|
const mongodb_1 = require("mongodb");
|
|
8
9
|
let client;
|
|
@@ -26,3 +27,10 @@ function getDb2(dbName) {
|
|
|
26
27
|
function getCollection2(dbName, collectionName) {
|
|
27
28
|
return getDb2(dbName).collection(collectionName);
|
|
28
29
|
}
|
|
30
|
+
async function closeMongo2() {
|
|
31
|
+
if (!client)
|
|
32
|
+
return;
|
|
33
|
+
await client.close();
|
|
34
|
+
client = undefined;
|
|
35
|
+
connectPromise = undefined;
|
|
36
|
+
}
|
package/dist/db/mongo3.d.ts
CHANGED
|
@@ -2,5 +2,7 @@ import { MongoClient, Db, Collection, Document } from 'mongodb';
|
|
|
2
2
|
export declare function connectToMongo3(uri: string): Promise<MongoClient>;
|
|
3
3
|
export declare function getDb3(uri: string, dbName: string): Db;
|
|
4
4
|
export declare function getCollection3<T extends Document = Document>(uri: string, dbName: string, collectionName: string): Collection<T>;
|
|
5
|
+
export declare function closeMongo3(uri: string): Promise<void>;
|
|
6
|
+
export declare function closeAllMongo3(): Promise<void>;
|
|
5
7
|
export declare const __mongoClients: Map<string, MongoClient>;
|
|
6
8
|
export declare const __connectPromises: Map<string, Promise<void>>;
|
package/dist/db/mongo3.js
CHANGED
|
@@ -4,6 +4,8 @@ exports.__connectPromises = exports.__mongoClients = void 0;
|
|
|
4
4
|
exports.connectToMongo3 = connectToMongo3;
|
|
5
5
|
exports.getDb3 = getDb3;
|
|
6
6
|
exports.getCollection3 = getCollection3;
|
|
7
|
+
exports.closeMongo3 = closeMongo3;
|
|
8
|
+
exports.closeAllMongo3 = closeAllMongo3;
|
|
7
9
|
// Multi cluster safe util
|
|
8
10
|
const mongodb_1 = require("mongodb");
|
|
9
11
|
const clients = new Map();
|
|
@@ -30,5 +32,18 @@ function getDb3(uri, dbName) {
|
|
|
30
32
|
function getCollection3(uri, dbName, collectionName) {
|
|
31
33
|
return getDb3(uri, dbName).collection(collectionName);
|
|
32
34
|
}
|
|
35
|
+
async function closeMongo3(uri) {
|
|
36
|
+
const client = clients.get(uri);
|
|
37
|
+
if (client) {
|
|
38
|
+
await client.close();
|
|
39
|
+
}
|
|
40
|
+
clients.delete(uri);
|
|
41
|
+
connectPromises.delete(uri);
|
|
42
|
+
}
|
|
43
|
+
async function closeAllMongo3() {
|
|
44
|
+
await Promise.all(Array.from(clients.values()).map(client => client.close()));
|
|
45
|
+
clients.clear();
|
|
46
|
+
connectPromises.clear();
|
|
47
|
+
}
|
|
33
48
|
exports.__mongoClients = clients;
|
|
34
49
|
exports.__connectPromises = connectPromises;
|
package/package.json
CHANGED
package/src/db/mongo.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { MongoClient, Db, Collection, Document } from 'mongodb'
|
|
2
2
|
|
|
3
|
-
let client: MongoClient
|
|
4
|
-
let db: Db
|
|
3
|
+
let client: MongoClient | undefined
|
|
4
|
+
let db: Db | undefined
|
|
5
5
|
let connectPromise: Promise<void> | undefined
|
|
6
6
|
|
|
7
7
|
export async function connectToMongo(uri: string, dbName: string) {
|
|
@@ -27,3 +27,13 @@ export function getCollection<T extends Document = Document>(name: string): Coll
|
|
|
27
27
|
if (!db) throw new Error('Mongo not initialized')
|
|
28
28
|
return db.collection<T>(name)
|
|
29
29
|
}
|
|
30
|
+
|
|
31
|
+
export async function closeMongo(): Promise<void> {
|
|
32
|
+
if (!client) return
|
|
33
|
+
|
|
34
|
+
await client.close()
|
|
35
|
+
|
|
36
|
+
client = undefined
|
|
37
|
+
db = undefined
|
|
38
|
+
connectPromise = undefined
|
|
39
|
+
}
|
package/src/db/mongo2.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
// Multi DB safe util
|
|
2
2
|
import { MongoClient, Db, Collection, Document } from 'mongodb'
|
|
3
3
|
|
|
4
|
-
let client: MongoClient
|
|
4
|
+
let client: MongoClient | undefined
|
|
5
5
|
let connectPromise: Promise<void> | undefined
|
|
6
6
|
|
|
7
7
|
export async function connectToMongo2(uri: string) {
|
|
@@ -28,3 +28,12 @@ export function getCollection2<T extends Document = Document>(
|
|
|
28
28
|
): Collection<T> {
|
|
29
29
|
return getDb2(dbName).collection<T>(collectionName)
|
|
30
30
|
}
|
|
31
|
+
|
|
32
|
+
export async function closeMongo2(): Promise<void> {
|
|
33
|
+
if (!client) return
|
|
34
|
+
|
|
35
|
+
await client.close()
|
|
36
|
+
|
|
37
|
+
client = undefined
|
|
38
|
+
connectPromise = undefined
|
|
39
|
+
}
|
package/src/db/mongo3.ts
CHANGED
|
@@ -37,5 +37,23 @@ export function getCollection3<T extends Document = Document>(
|
|
|
37
37
|
return getDb3(uri, dbName).collection<T>(collectionName)
|
|
38
38
|
}
|
|
39
39
|
|
|
40
|
+
export async function closeMongo3(uri: string): Promise<void> {
|
|
41
|
+
const client = clients.get(uri)
|
|
42
|
+
|
|
43
|
+
if (client) {
|
|
44
|
+
await client.close()
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
clients.delete(uri)
|
|
48
|
+
connectPromises.delete(uri)
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export async function closeAllMongo3(): Promise<void> {
|
|
52
|
+
await Promise.all(Array.from(clients.values()).map(client => client.close()))
|
|
53
|
+
|
|
54
|
+
clients.clear()
|
|
55
|
+
connectPromises.clear()
|
|
56
|
+
}
|
|
57
|
+
|
|
40
58
|
export const __mongoClients = clients
|
|
41
59
|
export const __connectPromises = connectPromises
|
package/tests/db/mongo.test.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { connectToMongo, getCollection, getDb } from '../../src/db/mongo'
|
|
1
|
+
import { connectToMongo, getCollection, getDb, closeMongo } from '../../src/db/mongo'
|
|
2
2
|
import { MongoClient, Db } from 'mongodb'
|
|
3
3
|
|
|
4
4
|
jest.mock('mongodb')
|
|
@@ -8,12 +8,17 @@ const mockDb = {
|
|
|
8
8
|
} as unknown as Db
|
|
9
9
|
|
|
10
10
|
const mockConnect = jest.fn()
|
|
11
|
+
const mockClose = jest.fn()
|
|
12
|
+
|
|
11
13
|
const mockClient = {
|
|
12
14
|
connect: mockConnect,
|
|
15
|
+
close: mockClose,
|
|
13
16
|
db: jest.fn().mockReturnValue(mockDb)
|
|
14
17
|
} as unknown as MongoClient
|
|
15
18
|
|
|
16
|
-
beforeEach(() => {
|
|
19
|
+
beforeEach(async () => {
|
|
20
|
+
await closeMongo()
|
|
21
|
+
|
|
17
22
|
jest.clearAllMocks()
|
|
18
23
|
;(MongoClient as unknown as jest.Mock).mockImplementation(() => mockClient)
|
|
19
24
|
})
|
|
@@ -22,10 +27,19 @@ describe('Mongo Utils', () => {
|
|
|
22
27
|
it('should connect to MongoDB and store client/db', async () => {
|
|
23
28
|
await connectToMongo('mongo-uri', 'test-db')
|
|
24
29
|
|
|
30
|
+
expect(MongoClient).toHaveBeenCalledWith('mongo-uri')
|
|
25
31
|
expect(mockConnect).toHaveBeenCalled()
|
|
26
32
|
expect(mockClient.db).toHaveBeenCalledWith('test-db')
|
|
27
33
|
})
|
|
28
34
|
|
|
35
|
+
it('should not reconnect if db is already initialized', async () => {
|
|
36
|
+
await connectToMongo('mongo-uri', 'test-db')
|
|
37
|
+
await connectToMongo('mongo-uri', 'test-db')
|
|
38
|
+
|
|
39
|
+
expect(MongoClient).toHaveBeenCalledTimes(1)
|
|
40
|
+
expect(mockConnect).toHaveBeenCalledTimes(1)
|
|
41
|
+
})
|
|
42
|
+
|
|
29
43
|
it('should return a collection when db is initialized', async () => {
|
|
30
44
|
await connectToMongo('mongo-uri', 'test-db')
|
|
31
45
|
getCollection('users')
|
|
@@ -35,17 +49,23 @@ describe('Mongo Utils', () => {
|
|
|
35
49
|
|
|
36
50
|
it('getDb should return the initialized Db instance', async () => {
|
|
37
51
|
await connectToMongo('mongo-uri', 'test-db')
|
|
38
|
-
|
|
39
|
-
expect(
|
|
52
|
+
|
|
53
|
+
expect(getDb()).toBe(mockDb)
|
|
54
|
+
})
|
|
55
|
+
|
|
56
|
+
it('should throw error if db is not initialized', async () => {
|
|
57
|
+
await closeMongo()
|
|
58
|
+
|
|
59
|
+
expect(() => getCollection('users')).toThrow(/Mongo not initialized/)
|
|
60
|
+
expect(() => getDb()).toThrow(/Mongo not initialized/)
|
|
40
61
|
})
|
|
41
62
|
|
|
42
|
-
it('should
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
})
|
|
63
|
+
it('should close Mongo and reset state', async () => {
|
|
64
|
+
await connectToMongo('mongo-uri', 'test-db')
|
|
65
|
+
await closeMongo()
|
|
66
|
+
|
|
67
|
+
expect(mockClose).toHaveBeenCalled()
|
|
68
|
+
|
|
69
|
+
expect(() => getDb()).toThrow(/Mongo not initialized/)
|
|
50
70
|
})
|
|
51
71
|
})
|
package/tests/db/mongo2.test.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { connectToMongo2, getDb2, getCollection2 } from '../../src/db/mongo2'
|
|
1
|
+
import { connectToMongo2, getDb2, getCollection2, closeMongo2 } from '../../src/db/mongo2'
|
|
2
2
|
import { MongoClient, Db } from 'mongodb'
|
|
3
3
|
|
|
4
4
|
jest.mock('mongodb')
|
|
@@ -8,12 +8,17 @@ const mockDb = {
|
|
|
8
8
|
} as unknown as Db
|
|
9
9
|
|
|
10
10
|
const mockConnect = jest.fn()
|
|
11
|
+
const mockClose = jest.fn()
|
|
12
|
+
|
|
11
13
|
const mockClient = {
|
|
12
14
|
connect: mockConnect,
|
|
15
|
+
close: mockClose,
|
|
13
16
|
db: jest.fn().mockReturnValue(mockDb)
|
|
14
17
|
} as unknown as MongoClient
|
|
15
18
|
|
|
16
|
-
beforeEach(() => {
|
|
19
|
+
beforeEach(async () => {
|
|
20
|
+
await closeMongo2()
|
|
21
|
+
|
|
17
22
|
jest.clearAllMocks()
|
|
18
23
|
;(MongoClient as unknown as jest.Mock).mockImplementation(() => mockClient)
|
|
19
24
|
})
|
|
@@ -22,13 +27,23 @@ describe('Mongo Utils', () => {
|
|
|
22
27
|
it('should connect to MongoDB and store client', async () => {
|
|
23
28
|
await connectToMongo2('mongo-uri')
|
|
24
29
|
|
|
25
|
-
expect(
|
|
30
|
+
expect(MongoClient).toHaveBeenCalledWith('mongo-uri')
|
|
31
|
+
expect(mockConnect).toHaveBeenCalledTimes(1)
|
|
32
|
+
})
|
|
33
|
+
|
|
34
|
+
it('should not reconnect if client is already initialized', async () => {
|
|
35
|
+
await connectToMongo2('mongo-uri')
|
|
36
|
+
await connectToMongo2('mongo-uri')
|
|
37
|
+
|
|
38
|
+
expect(MongoClient).toHaveBeenCalledTimes(1)
|
|
39
|
+
expect(mockConnect).toHaveBeenCalledTimes(1)
|
|
26
40
|
})
|
|
27
41
|
|
|
28
42
|
it('should return a Db instance when client is initialized', async () => {
|
|
29
43
|
await connectToMongo2('mongo-uri')
|
|
30
44
|
|
|
31
45
|
const db = getDb2('test-db')
|
|
46
|
+
|
|
32
47
|
expect(mockClient.db).toHaveBeenCalledWith('test-db')
|
|
33
48
|
expect(db).toBe(mockDb)
|
|
34
49
|
})
|
|
@@ -37,23 +52,23 @@ describe('Mongo Utils', () => {
|
|
|
37
52
|
await connectToMongo2('mongo-uri')
|
|
38
53
|
|
|
39
54
|
getCollection2('test-db', 'users')
|
|
55
|
+
|
|
40
56
|
expect(mockClient.db).toHaveBeenCalledWith('test-db')
|
|
41
57
|
expect(mockDb.collection).toHaveBeenCalledWith('users')
|
|
42
58
|
})
|
|
43
59
|
|
|
44
|
-
it('should throw error if client is not initialized
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
})
|
|
60
|
+
it('should throw error if client is not initialized', async () => {
|
|
61
|
+
await closeMongo2()
|
|
62
|
+
|
|
63
|
+
expect(() => getDb2('test-db')).toThrow('Mongo not initialized')
|
|
64
|
+
expect(() => getCollection2('test-db', 'users')).toThrow('Mongo not initialized')
|
|
50
65
|
})
|
|
51
66
|
|
|
52
|
-
it('should
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
67
|
+
it('should close Mongo and reset state', async () => {
|
|
68
|
+
await connectToMongo2('mongo-uri')
|
|
69
|
+
await closeMongo2()
|
|
70
|
+
|
|
71
|
+
expect(mockClose).toHaveBeenCalledTimes(1)
|
|
72
|
+
expect(() => getDb2('test-db')).toThrow('Mongo not initialized')
|
|
58
73
|
})
|
|
59
74
|
})
|
package/tests/db/mongo3.test.ts
CHANGED
|
@@ -2,6 +2,8 @@ import {
|
|
|
2
2
|
connectToMongo3,
|
|
3
3
|
getDb3,
|
|
4
4
|
getCollection3,
|
|
5
|
+
closeMongo3,
|
|
6
|
+
closeAllMongo3,
|
|
5
7
|
__mongoClients,
|
|
6
8
|
__connectPromises
|
|
7
9
|
} from '../../src/db/mongo3'
|
|
@@ -14,12 +16,17 @@ const mockDb = {
|
|
|
14
16
|
} as unknown as Db
|
|
15
17
|
|
|
16
18
|
const mockConnect = jest.fn()
|
|
19
|
+
const mockClose = jest.fn()
|
|
20
|
+
|
|
17
21
|
const mockClient = {
|
|
18
22
|
connect: mockConnect,
|
|
23
|
+
close: mockClose,
|
|
19
24
|
db: jest.fn().mockReturnValue(mockDb)
|
|
20
25
|
} as unknown as MongoClient
|
|
21
26
|
|
|
22
|
-
beforeEach(() => {
|
|
27
|
+
beforeEach(async () => {
|
|
28
|
+
await closeAllMongo3()
|
|
29
|
+
|
|
23
30
|
jest.clearAllMocks()
|
|
24
31
|
__mongoClients.clear()
|
|
25
32
|
__connectPromises.clear()
|
|
@@ -33,9 +40,11 @@ describe('MongoClientManager (multi-cluster)', () => {
|
|
|
33
40
|
it('should connect and cache client for a single cluster', async () => {
|
|
34
41
|
await connectToMongo3(clusterAUri)
|
|
35
42
|
|
|
36
|
-
expect(
|
|
43
|
+
expect(MongoClient).toHaveBeenCalledWith(clusterAUri)
|
|
44
|
+
expect(mockConnect).toHaveBeenCalledTimes(1)
|
|
37
45
|
|
|
38
46
|
const db = getDb3(clusterAUri, 'test-db')
|
|
47
|
+
|
|
39
48
|
expect(mockClient.db).toHaveBeenCalledWith('test-db')
|
|
40
49
|
expect(db).toBe(mockDb)
|
|
41
50
|
})
|
|
@@ -44,6 +53,7 @@ describe('MongoClientManager (multi-cluster)', () => {
|
|
|
44
53
|
await connectToMongo3(clusterAUri)
|
|
45
54
|
await connectToMongo3(clusterAUri)
|
|
46
55
|
|
|
56
|
+
expect(MongoClient).toHaveBeenCalledTimes(1)
|
|
47
57
|
expect(mockConnect).toHaveBeenCalledTimes(1)
|
|
48
58
|
})
|
|
49
59
|
|
|
@@ -51,6 +61,7 @@ describe('MongoClientManager (multi-cluster)', () => {
|
|
|
51
61
|
await connectToMongo3(clusterAUri)
|
|
52
62
|
await connectToMongo3(clusterBUri)
|
|
53
63
|
|
|
64
|
+
expect(MongoClient).toHaveBeenCalledTimes(2)
|
|
54
65
|
expect(mockConnect).toHaveBeenCalledTimes(2)
|
|
55
66
|
})
|
|
56
67
|
|
|
@@ -63,21 +74,31 @@ describe('MongoClientManager (multi-cluster)', () => {
|
|
|
63
74
|
expect(mockDb.collection).toHaveBeenCalledWith('users')
|
|
64
75
|
})
|
|
65
76
|
|
|
66
|
-
it('should throw error if client not initialized
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
77
|
+
it('should throw error if client not initialized', () => {
|
|
78
|
+
expect(() => getDb3('unknown-uri', 'some-db')).toThrow('Mongo client not initialized')
|
|
79
|
+
expect(() => getCollection3('unknown-uri', 'some-db', 'users')).toThrow(
|
|
80
|
+
'Mongo client not initialized'
|
|
81
|
+
)
|
|
82
|
+
})
|
|
83
|
+
|
|
84
|
+
it('should close one Mongo client and reset only that uri', async () => {
|
|
85
|
+
await connectToMongo3(clusterAUri)
|
|
86
|
+
await closeMongo3(clusterAUri)
|
|
87
|
+
|
|
88
|
+
expect(mockClose).toHaveBeenCalledTimes(1)
|
|
89
|
+
expect(__mongoClients.has(clusterAUri)).toBe(false)
|
|
90
|
+
expect(__connectPromises.has(clusterAUri)).toBe(false)
|
|
91
|
+
expect(() => getDb3(clusterAUri, 'test-db')).toThrow('Mongo client not initialized')
|
|
72
92
|
})
|
|
73
93
|
|
|
74
|
-
it('should
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
94
|
+
it('should close all Mongo clients and reset state', async () => {
|
|
95
|
+
await connectToMongo3(clusterAUri)
|
|
96
|
+
await connectToMongo3(clusterBUri)
|
|
97
|
+
|
|
98
|
+
await closeAllMongo3()
|
|
99
|
+
|
|
100
|
+
expect(mockClose).toHaveBeenCalledTimes(2)
|
|
101
|
+
expect(__mongoClients.size).toBe(0)
|
|
102
|
+
expect(__connectPromises.size).toBe(0)
|
|
82
103
|
})
|
|
83
104
|
})
|