mongodb-ops 0.5.0 → 0.8.1
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 +42 -29
- package/lib/mongodb-tool-set.js +111 -13
- 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,6 +253,13 @@ class MongoDBOps {
|
|
|
247
253
|
// }
|
|
248
254
|
for (let i = 0; i < docs.length; ++i) { docs[i] = { updateOne: docs[i] }; }
|
|
249
255
|
break;
|
|
256
|
+
case "deleteBulk":
|
|
257
|
+
// doc = {
|
|
258
|
+
// "filter": <document>,
|
|
259
|
+
// "collation": <document>
|
|
260
|
+
// }
|
|
261
|
+
for (let i = 0; i < docs.length; ++i) { docs[i] = { deleteOne: docs[i] }; }
|
|
262
|
+
break;
|
|
250
263
|
case "allBulk":
|
|
251
264
|
// allowed bulkWrite operations include insertOne, replaceOne, updateOne, updateMany, deleteOne, deleteMany
|
|
252
265
|
break;
|
package/lib/mongodb-tool-set.js
CHANGED
|
@@ -38,6 +38,32 @@ class MongoDBToolSet extends MongoDBOps {
|
|
|
38
38
|
*/
|
|
39
39
|
async getDataByID(id, projection) { return Promise.resolve(await MongoDBToolSet.getDataByID(this.collectionName, id, projection, this.connString)); }
|
|
40
40
|
|
|
41
|
+
/**
|
|
42
|
+
* Static method - Get data by filter
|
|
43
|
+
*
|
|
44
|
+
* @param {string} collectionName Collection name
|
|
45
|
+
* @param {object} filter Query filter {@link https://docs.mongodb.com/manual/core/document/#document-query-filter}
|
|
46
|
+
* @param {object} [projection] Projection {@link https://docs.mongodb.com/manual/reference/method/db.collection.find/#find-projection}
|
|
47
|
+
* @param {object} [sort] Sort filter {@link https://docs.mongodb.com/manual/reference/method/cursor.sort/#cursor.sort}
|
|
48
|
+
* @param {object} [pagination] Pagination `E.g., { startIndex: 11, endIndex: 20 }`
|
|
49
|
+
* @param {string} connString Database connection string
|
|
50
|
+
* @returns {promise} Promise with object array
|
|
51
|
+
*/
|
|
52
|
+
static async getDataByFilter(collectionName, filter, projection, sort, pagination, connString) {
|
|
53
|
+
return Promise.resolve(await MongoDBOps.getData(collectionName, filter, false, projection, sort, pagination, undefined, connString));
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* Instance method - Get data by filter
|
|
58
|
+
*
|
|
59
|
+
* @param {object} filter Query filter {@link https://docs.mongodb.com/manual/core/document/#document-query-filter}
|
|
60
|
+
* @param {object} [projection] Projection {@link https://docs.mongodb.com/manual/reference/method/db.collection.find/#find-projection}
|
|
61
|
+
* @param {object} [sort] Sort filter {@link https://docs.mongodb.com/manual/reference/method/cursor.sort/#cursor.sort}
|
|
62
|
+
* @param {object} [pagination] Pagination `E.g., { startIndex: 11, endIndex: 20 }`
|
|
63
|
+
* @returns {promise} Promise with object array
|
|
64
|
+
*/
|
|
65
|
+
async getDataByFilter(filter, projection, sort, pagination,) { return Promise.resolve(await MongoDBToolSet.getDataByFilter(this.collectionName, filter, projection, sort, pagination, this.connString)); }
|
|
66
|
+
|
|
41
67
|
/**
|
|
42
68
|
* Static method - Get data count by query
|
|
43
69
|
*
|
|
@@ -57,7 +83,7 @@ class MongoDBToolSet extends MongoDBOps {
|
|
|
57
83
|
* @returns {promise} Promise with data count
|
|
58
84
|
*/
|
|
59
85
|
async getDataCount(filter) {
|
|
60
|
-
return Promise.resolve(await
|
|
86
|
+
return Promise.resolve(await MongoDBToolSet.getDataCount(this.collectionName, filter, this.connString));
|
|
61
87
|
}
|
|
62
88
|
|
|
63
89
|
/**
|
|
@@ -100,7 +126,7 @@ class MongoDBToolSet extends MongoDBOps {
|
|
|
100
126
|
* @param {object} doc Data document
|
|
101
127
|
* @returns {promise}
|
|
102
128
|
*/
|
|
103
|
-
async insertOne(doc) { return Promise.resolve(await
|
|
129
|
+
async insertOne(doc) { return Promise.resolve(await MongoDBToolSet.insertOne(this.collectionName, doc, this.connString)); }
|
|
104
130
|
|
|
105
131
|
/**
|
|
106
132
|
* Static method - Insert multiple documents to database in ordered way
|
|
@@ -118,7 +144,7 @@ class MongoDBToolSet extends MongoDBOps {
|
|
|
118
144
|
* @param {Array} docs Data document array
|
|
119
145
|
* @returns {promise}
|
|
120
146
|
*/
|
|
121
|
-
async insertBulkOrdered(docs) { return Promise.resolve(await
|
|
147
|
+
async insertBulkOrdered(docs) { return Promise.resolve(await MongoDBToolSet.insertBulkOrdered(this.collectionName, docs, this.connString)); }
|
|
122
148
|
|
|
123
149
|
/**
|
|
124
150
|
* Static method - Insert multiple documents to database in unordered way
|
|
@@ -136,7 +162,7 @@ class MongoDBToolSet extends MongoDBOps {
|
|
|
136
162
|
* @param {Array} docs Data document array
|
|
137
163
|
* @returns {promise}
|
|
138
164
|
*/
|
|
139
|
-
async insertBulkUnOrdered(docs) { return Promise.resolve(await
|
|
165
|
+
async insertBulkUnOrdered(docs) { return Promise.resolve(await MongoDBToolSet.insertBulkUnOrdered(this.collectionName, docs, this.connString)); }
|
|
140
166
|
|
|
141
167
|
/**
|
|
142
168
|
* Static method - Replace one document to database
|
|
@@ -156,7 +182,7 @@ class MongoDBToolSet extends MongoDBOps {
|
|
|
156
182
|
* @param {object} filter Query filter {@link https://docs.mongodb.com/manual/core/document/#document-query-filter}
|
|
157
183
|
* @returns {promise}
|
|
158
184
|
*/
|
|
159
|
-
async replaceOne(doc, filter) { return Promise.resolve(await
|
|
185
|
+
async replaceOne(doc, filter) { return Promise.resolve(await MongoDBToolSet.replaceOne(this.collectionName, doc, filter, this.connString)); }
|
|
160
186
|
|
|
161
187
|
/**
|
|
162
188
|
* Static method - Replace multiple documents to database in ordered way
|
|
@@ -174,7 +200,7 @@ class MongoDBToolSet extends MongoDBOps {
|
|
|
174
200
|
* @param {Array} docs ReplaceOne object array
|
|
175
201
|
* @returns {promise}
|
|
176
202
|
*/
|
|
177
|
-
async replaceBulkOrdered(docs) { return Promise.resolve(await
|
|
203
|
+
async replaceBulkOrdered(docs) { return Promise.resolve(await MongoDBToolSet.replaceBulkOrdered(this.collectionName, docs, this.connString)); }
|
|
178
204
|
|
|
179
205
|
/**
|
|
180
206
|
* Static method - Replace multiple documents to database in unordered way
|
|
@@ -192,7 +218,7 @@ class MongoDBToolSet extends MongoDBOps {
|
|
|
192
218
|
* @param {Array} docs ReplaceOne object array
|
|
193
219
|
* @returns {promise}
|
|
194
220
|
*/
|
|
195
|
-
async replaceBulkUnOrdered(docs) { return Promise.resolve(await
|
|
221
|
+
async replaceBulkUnOrdered(docs) { return Promise.resolve(await MongoDBToolSet.replaceBulkUnOrdered(this.collectionName, docs, this.connString)); }
|
|
196
222
|
|
|
197
223
|
/**
|
|
198
224
|
* Static method - Update one document to database
|
|
@@ -212,7 +238,7 @@ class MongoDBToolSet extends MongoDBOps {
|
|
|
212
238
|
* @param {object} filter Query filter {@link https://docs.mongodb.com/manual/core/document/#document-query-filter}
|
|
213
239
|
* @returns {promise}
|
|
214
240
|
*/
|
|
215
|
-
async updateOne(doc, filter) { return Promise.resolve(await
|
|
241
|
+
async updateOne(doc, filter) { return Promise.resolve(await MongoDBToolSet.updateOne(this.collectionName, doc, filter, this.connString)); }
|
|
216
242
|
|
|
217
243
|
/**
|
|
218
244
|
* Static method - Update many documents to database
|
|
@@ -232,7 +258,7 @@ class MongoDBToolSet extends MongoDBOps {
|
|
|
232
258
|
* @param {object} filter Query filter {@link https://docs.mongodb.com/manual/core/document/#document-query-filter}
|
|
233
259
|
* @returns {promise}
|
|
234
260
|
*/
|
|
235
|
-
async updateMany(doc, filter) { return Promise.resolve(await
|
|
261
|
+
async updateMany(doc, filter) { return Promise.resolve(await MongoDBToolSet.updateMany(this.collectionName, doc, filter, this.connString)); }
|
|
236
262
|
|
|
237
263
|
/**
|
|
238
264
|
* Static method - Update multiple documents to database in ordered way
|
|
@@ -250,7 +276,7 @@ class MongoDBToolSet extends MongoDBOps {
|
|
|
250
276
|
* @param {Array} docs UpdateOne object array
|
|
251
277
|
* @returns {promise}
|
|
252
278
|
*/
|
|
253
|
-
async updateBulkOrdered(docs) { return Promise.resolve(await
|
|
279
|
+
async updateBulkOrdered(docs) { return Promise.resolve(await MongoDBToolSet.updateBulkOrdered(this.collectionName, docs, this.connString)); }
|
|
254
280
|
|
|
255
281
|
/**
|
|
256
282
|
* Static method - Update multiple documents to database in unordered way
|
|
@@ -268,7 +294,79 @@ class MongoDBToolSet extends MongoDBOps {
|
|
|
268
294
|
* @param {Array} docs UpdateOne object array
|
|
269
295
|
* @returns {promise}
|
|
270
296
|
*/
|
|
271
|
-
async updateBulkUnOrdered(docs) { return Promise.resolve(await
|
|
297
|
+
async updateBulkUnOrdered(docs) { return Promise.resolve(await MongoDBToolSet.updateBulkUnOrdered(this.collectionName, docs, this.connString)); }
|
|
298
|
+
|
|
299
|
+
/**
|
|
300
|
+
* Static method - Delete one document at database
|
|
301
|
+
*
|
|
302
|
+
* @param {string} collectionName Collection name
|
|
303
|
+
* @param {object} filter Query filter {@link https://docs.mongodb.com/manual/core/document/#document-query-filter}
|
|
304
|
+
* @param {string} connString Database connection string
|
|
305
|
+
* @returns {promise}
|
|
306
|
+
*/
|
|
307
|
+
static async deleteOne(collectionName, filter, connString) { return Promise.resolve(await MongoDBOps.writeData("deleteOne", collectionName, undefined, filter, connString)); }
|
|
308
|
+
|
|
309
|
+
/**
|
|
310
|
+
* Instance method - Delete one document at database
|
|
311
|
+
*
|
|
312
|
+
* @param {object} filter Query filter {@link https://docs.mongodb.com/manual/core/document/#document-query-filter}
|
|
313
|
+
* @returns {promise}
|
|
314
|
+
*/
|
|
315
|
+
async deleteOne(filter) { return Promise.resolve(await MongoDBToolSet.deleteOne(this.collectionName, filter, this.connString)); }
|
|
316
|
+
|
|
317
|
+
/**
|
|
318
|
+
* Static method - Delete many document at database
|
|
319
|
+
*
|
|
320
|
+
* @param {string} collectionName Collection name
|
|
321
|
+
* @param {object} filter Query filter {@link https://docs.mongodb.com/manual/core/document/#document-query-filter}
|
|
322
|
+
* @param {string} connString Database connection string
|
|
323
|
+
* @returns {promise}
|
|
324
|
+
*/
|
|
325
|
+
static async deleteMany(collectionName, filter, connString) { return Promise.resolve(await MongoDBOps.writeData("deleteMany", collectionName, undefined, filter, connString)); }
|
|
326
|
+
|
|
327
|
+
/**
|
|
328
|
+
* Instance method - Delete many document at database
|
|
329
|
+
*
|
|
330
|
+
* @param {object} filter Query filter {@link https://docs.mongodb.com/manual/core/document/#document-query-filter}
|
|
331
|
+
* @returns {promise}
|
|
332
|
+
*/
|
|
333
|
+
async deleteMany(filter) { return Promise.resolve(await MongoDBToolSet.deleteMany(this.collectionName, filter, this.connString)); }
|
|
334
|
+
|
|
335
|
+
/**
|
|
336
|
+
* Static method - Delete multiple documents to database in ordered way
|
|
337
|
+
*
|
|
338
|
+
* @param {string} collectionName Collection name
|
|
339
|
+
* @param {Array} docs DeleteOne object array
|
|
340
|
+
* @param {string} connString Database connection string
|
|
341
|
+
* @returns {promise}
|
|
342
|
+
*/
|
|
343
|
+
static async deleteBulkOrdered(collectionName, docs, connString) { return Promise.resolve(await MongoDBOps.writeBulkData("deleteBulk", collectionName, docs, true, connString)); }
|
|
344
|
+
|
|
345
|
+
/**
|
|
346
|
+
* Instance method - Delete multiple documents to database in ordered way
|
|
347
|
+
*
|
|
348
|
+
* @param {Array} docs DeleteOne object array
|
|
349
|
+
* @returns {promise}
|
|
350
|
+
*/
|
|
351
|
+
async deleteBulkOrdered(docs) { return Promise.resolve(await MongoDBToolSet.deleteBulkOrdered(this.collectionName, docs, this.connString)); }
|
|
352
|
+
|
|
353
|
+
/**
|
|
354
|
+
* Static method - Delete multiple documents to database in unordered way
|
|
355
|
+
*
|
|
356
|
+
* @param {string} collectionName Collection name
|
|
357
|
+
* @param {Array} docs DeleteOne object array
|
|
358
|
+
* @param {string} connString Database connection string
|
|
359
|
+
* @returns {promise}
|
|
360
|
+
*/
|
|
361
|
+
static async deleteBulkUnOrdered(collectionName, docs, connString) { return Promise.resolve(await MongoDBOps.writeBulkData("deleteBulk", collectionName, docs, false, connString)); }
|
|
362
|
+
|
|
363
|
+
/**
|
|
364
|
+
* Instance method - Delete multiple documents to database in unordered way
|
|
365
|
+
*
|
|
366
|
+
* @param {Array} docs DeleteOne object array
|
|
367
|
+
* @returns {promise}
|
|
368
|
+
*/
|
|
369
|
+
async deleteBulkUnOrdered(docs) { return Promise.resolve(await MongoDBToolSet.deleteBulkUnOrdered(this.collectionName, docs, this.connString)); }
|
|
272
370
|
|
|
273
371
|
/**
|
|
274
372
|
* Static method - BulkWrite operations to database in ordered way
|
|
@@ -286,7 +384,7 @@ class MongoDBToolSet extends MongoDBOps {
|
|
|
286
384
|
* @param {Array} docs BulkWrite object array
|
|
287
385
|
* @returns {promise}
|
|
288
386
|
*/
|
|
289
|
-
async allBulkOrdered(docs) { return Promise.resolve(await
|
|
387
|
+
async allBulkOrdered(docs) { return Promise.resolve(await MongoDBToolSet.allBulkOrdered(this.collectionName, docs, this.connString)); }
|
|
290
388
|
|
|
291
389
|
/**
|
|
292
390
|
* Static method - BulkWrite operations to database in unordered way
|
|
@@ -304,7 +402,7 @@ class MongoDBToolSet extends MongoDBOps {
|
|
|
304
402
|
* @param {Array} docs BulkWrite object array
|
|
305
403
|
* @returns {promise}
|
|
306
404
|
*/
|
|
307
|
-
async allBulkUnOrdered(docs) { return Promise.resolve(await
|
|
405
|
+
async allBulkUnOrdered(docs) { return Promise.resolve(await MongoDBToolSet.allBulkUnOrdered(this.collectionName, docs, this.connString)); }
|
|
308
406
|
}
|
|
309
407
|
|
|
310
408
|
module.exports = MongoDBToolSet;
|