mongodb-ops 0.4.2 → 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.
@@ -33,35 +33,40 @@ class MongoDBOps {
33
33
  getObjectId(id) { return new ObjectID(id); }
34
34
 
35
35
  /**
36
- * Static method - Open database connection
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 openDBConn(connString, poolSize = DEFAULT_POOLSIZE) {
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.dbClient = await MongoClient.connect(connString, {
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
- return Promise.resolve();
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.dbClient) { await MongoDBOps.dbClient.close(); }
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
- if (MongoDBOps.dbClient) { await MongoDBOps.dbClient.close(); }
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
- if (!MongoDBOps.dbClient || (MongoDBOps.dbClient && MongoDBOps.dbClient.topology.s.state !== "connected")) { await MongoDBOps.openDBConn(connString); } // reopen the db connection if it is not connected
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
- let result, db = MongoDBOps.dbClient.db(); // Create a new Db instance sharing the current socket connections
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
- let result, db = MongoDBOps.dbClient.db(); // Create a new Db instance sharing the current socket connections
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] }}; }
@@ -38,6 +38,54 @@ 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
+
67
+ /**
68
+ * Static method - Get data count by query
69
+ *
70
+ * @param {string} collectionName Collection name
71
+ * @param {object} filter Query filter {@link https://docs.mongodb.com/manual/core/document/#document-query-filter}
72
+ * @param {string} connString Database connection string
73
+ * @returns {promise} Promise with data count
74
+ */
75
+ static async getDataCount(collectionName, filter, connString) {
76
+ return Promise.resolve(await MongoDBOps.getData(collectionName, filter, false, undefined, undefined, undefined, true, connString));
77
+ }
78
+
79
+ /**
80
+ * Instance method - Get data count by query
81
+ *
82
+ * @param {object} filter Query filter {@link https://docs.mongodb.com/manual/core/document/#document-query-filter}
83
+ * @returns {promise} Promise with data count
84
+ */
85
+ async getDataCount(filter) {
86
+ return Promise.resolve(await MongoDBToolSet.getDataCount(this.collectionName, filter, this.connString));
87
+ }
88
+
41
89
  /**
42
90
  * Static method - Get all data
43
91
  *
@@ -78,7 +126,7 @@ class MongoDBToolSet extends MongoDBOps {
78
126
  * @param {object} doc Data document
79
127
  * @returns {promise}
80
128
  */
81
- async insertOne(doc) { return Promise.resolve(await super.writeData("insertOne", this.collectionName, doc, this.connString)); }
129
+ async insertOne(doc) { return Promise.resolve(await MongoDBToolSet.insertOne(this.collectionName, doc, this.connString)); }
82
130
 
83
131
  /**
84
132
  * Static method - Insert multiple documents to database in ordered way
@@ -96,7 +144,7 @@ class MongoDBToolSet extends MongoDBOps {
96
144
  * @param {Array} docs Data document array
97
145
  * @returns {promise}
98
146
  */
99
- async insertBulkOrdered(docs) { return Promise.resolve(await super.writeBulkData("insertBulk", this.collectionName, docs, true, this.connString)); }
147
+ async insertBulkOrdered(docs) { return Promise.resolve(await MongoDBToolSet.insertBulkOrdered(this.collectionName, docs, this.connString)); }
100
148
 
101
149
  /**
102
150
  * Static method - Insert multiple documents to database in unordered way
@@ -114,7 +162,7 @@ class MongoDBToolSet extends MongoDBOps {
114
162
  * @param {Array} docs Data document array
115
163
  * @returns {promise}
116
164
  */
117
- async insertBulkUnOrdered(docs) { return Promise.resolve(await super.writeBulkData("insertBulk", this.collectionName, docs, false, this.connString)); }
165
+ async insertBulkUnOrdered(docs) { return Promise.resolve(await MongoDBToolSet.insertBulkUnOrdered(this.collectionName, docs, this.connString)); }
118
166
 
119
167
  /**
120
168
  * Static method - Replace one document to database
@@ -134,7 +182,7 @@ class MongoDBToolSet extends MongoDBOps {
134
182
  * @param {object} filter Query filter {@link https://docs.mongodb.com/manual/core/document/#document-query-filter}
135
183
  * @returns {promise}
136
184
  */
137
- async replaceOne(doc, filter) { return Promise.resolve(await super.writeData("replaceOne", this.collectionName, doc, filter, this.connString)); }
185
+ async replaceOne(doc, filter) { return Promise.resolve(await MongoDBToolSet.replaceOne(this.collectionName, doc, filter, this.connString)); }
138
186
 
139
187
  /**
140
188
  * Static method - Replace multiple documents to database in ordered way
@@ -152,7 +200,7 @@ class MongoDBToolSet extends MongoDBOps {
152
200
  * @param {Array} docs ReplaceOne object array
153
201
  * @returns {promise}
154
202
  */
155
- async replaceBulkOrdered(docs) { return Promise.resolve(await super.writeBulkData("replaceBulk", this.collectionName, docs, true, this.connString)); }
203
+ async replaceBulkOrdered(docs) { return Promise.resolve(await MongoDBToolSet.replaceBulkOrdered(this.collectionName, docs, this.connString)); }
156
204
 
157
205
  /**
158
206
  * Static method - Replace multiple documents to database in unordered way
@@ -170,7 +218,7 @@ class MongoDBToolSet extends MongoDBOps {
170
218
  * @param {Array} docs ReplaceOne object array
171
219
  * @returns {promise}
172
220
  */
173
- async replaceBulkUnOrdered(docs) { return Promise.resolve(await super.writeBulkData("replaceBulk", this.collectionName, docs, false, this.connString)); }
221
+ async replaceBulkUnOrdered(docs) { return Promise.resolve(await MongoDBToolSet.replaceBulkUnOrdered(this.collectionName, docs, this.connString)); }
174
222
 
175
223
  /**
176
224
  * Static method - Update one document to database
@@ -190,7 +238,7 @@ class MongoDBToolSet extends MongoDBOps {
190
238
  * @param {object} filter Query filter {@link https://docs.mongodb.com/manual/core/document/#document-query-filter}
191
239
  * @returns {promise}
192
240
  */
193
- async updateOne(doc, filter) { return Promise.resolve(await super.writeData("updateOne", this.collectionName, doc, filter, this.connString)); }
241
+ async updateOne(doc, filter) { return Promise.resolve(await MongoDBToolSet.updateOne(this.collectionName, doc, filter, this.connString)); }
194
242
 
195
243
  /**
196
244
  * Static method - Update many documents to database
@@ -210,7 +258,7 @@ class MongoDBToolSet extends MongoDBOps {
210
258
  * @param {object} filter Query filter {@link https://docs.mongodb.com/manual/core/document/#document-query-filter}
211
259
  * @returns {promise}
212
260
  */
213
- async updateMany(doc, filter) { return Promise.resolve(await super.writeData("updateMany", this.collectionName, doc, filter, this.connString)); }
261
+ async updateMany(doc, filter) { return Promise.resolve(await MongoDBToolSet.updateMany(this.collectionName, doc, filter, this.connString)); }
214
262
 
215
263
  /**
216
264
  * Static method - Update multiple documents to database in ordered way
@@ -228,7 +276,7 @@ class MongoDBToolSet extends MongoDBOps {
228
276
  * @param {Array} docs UpdateOne object array
229
277
  * @returns {promise}
230
278
  */
231
- async updateBulkOrdered(docs) { return Promise.resolve(await super.writeBulkData("updateBulk", this.collectionName, docs, true, this.connString)); }
279
+ async updateBulkOrdered(docs) { return Promise.resolve(await MongoDBToolSet.updateBulkOrdered(this.collectionName, docs, this.connString)); }
232
280
 
233
281
  /**
234
282
  * Static method - Update multiple documents to database in unordered way
@@ -246,7 +294,79 @@ class MongoDBToolSet extends MongoDBOps {
246
294
  * @param {Array} docs UpdateOne object array
247
295
  * @returns {promise}
248
296
  */
249
- async updateBulkUnOrdered(docs) { return Promise.resolve(await super.writeBulkData("updateBulk", this.collectionName, docs, false, this.connString)); }
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)); }
250
370
 
251
371
  /**
252
372
  * Static method - BulkWrite operations to database in ordered way
@@ -264,7 +384,7 @@ class MongoDBToolSet extends MongoDBOps {
264
384
  * @param {Array} docs BulkWrite object array
265
385
  * @returns {promise}
266
386
  */
267
- async allBulkOrdered(docs) { return Promise.resolve(await super.writeBulkData("allBulk", this.collectionName, docs, true, this.connString)); }
387
+ async allBulkOrdered(docs) { return Promise.resolve(await MongoDBToolSet.allBulkOrdered(this.collectionName, docs, this.connString)); }
268
388
 
269
389
  /**
270
390
  * Static method - BulkWrite operations to database in unordered way
@@ -282,7 +402,7 @@ class MongoDBToolSet extends MongoDBOps {
282
402
  * @param {Array} docs BulkWrite object array
283
403
  * @returns {promise}
284
404
  */
285
- async allBulkUnOrdered(docs) { return Promise.resolve(await super.writeBulkData("allBulk", this.collectionName, docs, false, this.connString)); }
405
+ async allBulkUnOrdered(docs) { return Promise.resolve(await MongoDBToolSet.allBulkUnOrdered(this.collectionName, docs, this.connString)); }
286
406
  }
287
407
 
288
408
  module.exports = MongoDBToolSet;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mongodb-ops",
3
- "version": "0.4.2",
3
+ "version": "0.8.0",
4
4
  "description": "Read and write ops for MongoDB",
5
5
  "main": "index.js",
6
6
  "scripts": {