mongodb-ops 0.7.0 → 0.8.0
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/lib/mongodb-ops.js +35 -36
- package/package.json +1 -1
package/lib/mongodb-ops.js
CHANGED
|
@@ -33,35 +33,40 @@ class MongoDBOps {
|
|
|
33
33
|
getObjectId(id) { return new ObjectID(id); }
|
|
34
34
|
|
|
35
35
|
/**
|
|
36
|
-
* Static method -
|
|
36
|
+
* Static method - Get DB client - It supports multiple db client with different connection string. Active db client will be reused for better performance
|
|
37
37
|
*
|
|
38
38
|
* @param {string} connString Database connection string
|
|
39
39
|
* @param {number} [poolSize] Database client pool size
|
|
40
|
-
* @returns {promise}
|
|
40
|
+
* @returns {promise} Promise with db client
|
|
41
41
|
*/
|
|
42
|
-
static async
|
|
43
|
-
// dbClient will be at static level for reusability
|
|
44
|
-
//
|
|
45
|
-
if (MongoDBOps.dbClient && MongoDBOps.dbClient.topology.s.state === "connected") { return Promise.resolve(); }
|
|
42
|
+
static async getDbClient(connString, poolSize = DEFAULT_POOLSIZE) {
|
|
46
43
|
if (!connString) { throw new Error("missing-connection-string"); }
|
|
47
44
|
|
|
48
|
-
MongoDBOps.
|
|
45
|
+
if (!Array.isArray(MongoDBOps.dbClientList)) { MongoDBOps.dbClientList = []; }
|
|
46
|
+
|
|
47
|
+
for (let item of MongoDBOps.dbClientList) {
|
|
48
|
+
if (item.s.url === connString) {
|
|
49
|
+
if (item.topology.s.state !== "connected") {
|
|
50
|
+
item = await MongoClient.connect(connString, {
|
|
51
|
+
poolSize: poolSize !== DEFAULT_POOLSIZE ? poolSize : MongoDBOps.poolSize || DEFAULT_POOLSIZE,
|
|
52
|
+
useNewUrlParser: true,
|
|
53
|
+
useUnifiedTopology: true,
|
|
54
|
+
});
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
return Promise.resolve(item);
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
const newDbClient = await MongoClient.connect(connString, {
|
|
49
62
|
poolSize: poolSize !== DEFAULT_POOLSIZE ? poolSize : MongoDBOps.poolSize || DEFAULT_POOLSIZE,
|
|
50
63
|
useNewUrlParser: true,
|
|
51
64
|
useUnifiedTopology: true,
|
|
52
65
|
});
|
|
53
66
|
|
|
54
|
-
|
|
55
|
-
}
|
|
67
|
+
MongoDBOps.dbClientList.push(newDbClient);
|
|
56
68
|
|
|
57
|
-
|
|
58
|
-
* Instance method - Open database connection
|
|
59
|
-
*
|
|
60
|
-
* @param {number} [poolSize] Database client pool size
|
|
61
|
-
* @returns {promise}
|
|
62
|
-
*/
|
|
63
|
-
async openDBConn(poolSize = DEFAULT_POOLSIZE) {
|
|
64
|
-
return Promise.resolve(await MongoDBOps.openDBConn(this.connString, poolSize));
|
|
69
|
+
return Promise.resolve(newDbClient);
|
|
65
70
|
}
|
|
66
71
|
|
|
67
72
|
/**
|
|
@@ -69,7 +74,11 @@ class MongoDBOps {
|
|
|
69
74
|
* @returns {promise}
|
|
70
75
|
*/
|
|
71
76
|
static async closeDBConn() {
|
|
72
|
-
if (MongoDBOps.
|
|
77
|
+
if (Array.isArray(MongoDBOps.dbClientList)) {
|
|
78
|
+
for (const item of MongoDBOps.dbClientList) {
|
|
79
|
+
await item.close();
|
|
80
|
+
}
|
|
81
|
+
}
|
|
73
82
|
return Promise.resolve();
|
|
74
83
|
}
|
|
75
84
|
|
|
@@ -78,8 +87,7 @@ class MongoDBOps {
|
|
|
78
87
|
* @returns {promise}
|
|
79
88
|
*/
|
|
80
89
|
async closeDBConn() {
|
|
81
|
-
|
|
82
|
-
return Promise.resolve();
|
|
90
|
+
return Promise.resolve(await MongoDBOps.closeDBConn());
|
|
83
91
|
}
|
|
84
92
|
|
|
85
93
|
/**
|
|
@@ -97,10 +105,10 @@ class MongoDBOps {
|
|
|
97
105
|
* @param {string} connString Database connection string
|
|
98
106
|
* @returns {promise} Promise with object array
|
|
99
107
|
*/
|
|
100
|
-
static async getData(collectionName, queryExp, isAggregate = false, projection, sort, pagination, isGetCount = false, connString) {
|
|
101
|
-
|
|
102
|
-
let docs, db = MongoDBOps.dbClient.db(); // Create a new Db instance sharing the current socket connections
|
|
108
|
+
static async getData(collectionName, queryExp, isAggregate = false, projection, sort, pagination, isGetCount = false, connString) {
|
|
109
|
+
const db = (await MongoDBOps.getDbClient(connString)).db();
|
|
103
110
|
|
|
111
|
+
let docs;
|
|
104
112
|
if (isAggregate) { docs = await db.collection(collectionName).aggregate(queryExp).toArray(); }
|
|
105
113
|
else {
|
|
106
114
|
queryExp = queryExp || {};
|
|
@@ -162,11 +170,10 @@ class MongoDBOps {
|
|
|
162
170
|
* @returns {promise}
|
|
163
171
|
*/
|
|
164
172
|
static async writeData(type, collectionName, doc, filter, connString) {
|
|
165
|
-
if (!MongoDBOps.dbClient || (MongoDBOps.dbClient && MongoDBOps.dbClient.topology.s.state !== "connected")) { await MongoDBOps.openDBConn(connString); } // reopen the db connection if it is not connected
|
|
166
|
-
|
|
167
173
|
try {
|
|
168
|
-
|
|
174
|
+
const db = (await MongoDBOps.getDbClient(connString)).db();
|
|
169
175
|
|
|
176
|
+
let result;
|
|
170
177
|
switch(type) {
|
|
171
178
|
case "insertOne": result = await db.collection(collectionName).insertOne(doc); break;
|
|
172
179
|
case "replaceOne": result = await db.collection(collectionName).replaceOne(filter, doc); break;
|
|
@@ -217,11 +224,10 @@ class MongoDBOps {
|
|
|
217
224
|
* @returns {promise}
|
|
218
225
|
*/
|
|
219
226
|
static async writeBulkData(type, collectionName, docs, ordered = false, connString) {
|
|
220
|
-
if (!MongoDBOps.dbClient || (MongoDBOps.dbClient && MongoDBOps.dbClient.topology.s.state !== "connected")) { await MongoDBOps.openDBConn(connString); } // reopen the db connection if it is not connected
|
|
221
|
-
|
|
222
227
|
try {
|
|
223
|
-
|
|
228
|
+
const db = (await MongoDBOps.getDbClient(connString)).db();
|
|
224
229
|
|
|
230
|
+
let result;
|
|
225
231
|
switch(type) {
|
|
226
232
|
case "insertBulk":
|
|
227
233
|
for (let i = 0; i < docs.length; ++i) { docs[i] = { insertOne: { "document": docs[i] }}; }
|
|
@@ -247,13 +253,6 @@ class MongoDBOps {
|
|
|
247
253
|
// }
|
|
248
254
|
for (let i = 0; i < docs.length; ++i) { docs[i] = { updateOne: docs[i] }; }
|
|
249
255
|
break;
|
|
250
|
-
case "deleteBulk":
|
|
251
|
-
// doc = {
|
|
252
|
-
// "filter": <document>,
|
|
253
|
-
// "collation": <document>
|
|
254
|
-
// }
|
|
255
|
-
for (let i = 0; i < docs.length; ++i) { docs[i] = { deleteOne: docs[i] }; }
|
|
256
|
-
break;
|
|
257
256
|
case "allBulk":
|
|
258
257
|
// allowed bulkWrite operations include insertOne, replaceOne, updateOne, updateMany, deleteOne, deleteMany
|
|
259
258
|
break;
|