-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.js
465 lines (446 loc) · 15.5 KB
/
utils.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
const MongoClient = require('mongodb').MongoClient;
const ObjectID = require('mongodb').ObjectID;
const _ = require('lodash');
const Promise = require('bluebird');
Promise.promisifyAll(MongoClient);
/**
* Method to insert documents in a given collection
* @param {string} collectionName Name of the collection
* @param {array} documents - Array of documents that will be inserted.
* @returns {object} A document with `acknowledged: true` and an array of successfully inserted _id's
*/
var insertIntoDb = function insertIntoDb(collectionName, documents) {
var dbHandleForShutDowns;
if (documents.length < 1) {
return Promise.resolve();
}
return MongoClient.connect(process.env.DB_URL, {promiseLibrary: Promise})
.then(function insertData(db) {
dbHandleForShutDowns = db;
return db.collection(collectionName).insertMany(documents, {w: 1})
.finally(db.close.bind(db));
})
.catch(function catchErrors(err) {
if (dbHandleForShutDowns) {
dbHandleForShutDowns.close();
}
throw err;
});
};
/**
* Method to update a document in a given collection based on _id.
* @param {string} collectionName Name of the collection
* @param {object} mutableEntity - Document to update in the collection
* @param {boolean} timeStamp - Default is false. If set to true it adds a key lastModifiedAt to the document with the current timestamp.
* @returns {*}
*/
var updateDocument = function updateDocument(collectionName, mutableEntity, timeStamp) {
var dbHandleForShutDowns;
if (timeStamp){
mutableEntity.lastModifiedAt = new Date().toISOString();
}
return MongoClient.connect(process.env.DB_URL, {promiseLibrary: Promise})
.then(function insertData(db) {
dbHandleForShutDowns = db;
return db.collection(collectionName).findOneAndReplace(
{
_id: mutableEntity._id
},
mutableEntity,
{
returnOriginal: false,
upsert: false
})
.finally(db.close.bind(db));
})
.catch(function catchErrors(err) {
if (dbHandleForShutDowns) {
dbHandleForShutDowns.close();
}
throw err;
});
};
/**
* Method to upsert a document in a given collection
* @param {string} collectionName Name of the collection
* @param {object} mutableEntity Properties that will be updated.
* @param {boolean} upsert Default is false, if set to true it will create or update the document with the given set of properties.
* @param {object} query Default is querying by _id but a custom query can be specified.
* @returns {*}
*/
var upsertDocument = function upsertDocument(collectionName, mutableEntity, upsert, query) {
var dbHandleForShutDowns;
if (!upsert){
upsert = false;
}
if (!query){
query = {
_id: mutableEntity._id
};
}
return MongoClient.connect(process.env.DB_URL, {promiseLibrary: Promise})
.then(function insertData(db) {
dbHandleForShutDowns = db;
return db.collection(collectionName).findOneAndUpdate(
query,
mutableEntity,
{
upsert: upsert
})
.finally(db.close.bind(db));
})
.catch(function catchErrors(err) {
if (dbHandleForShutDowns) {
dbHandleForShutDowns.close();
}
throw err;
});
};
/**
* Method to find one document based on a given query
* @param {string} collectionName Name of the collection
* @param {object} query Query
* @param {object} sort Sort, by default it sorts by _id.
* @returns {object} an document if a match is found based on the query.
*/
var findOneDocumentBasedOnQuery = function findOneDocumentBasedOnQuery(collectionName, query, sort) {
var dbHandleForShutDowns;
if(!sort){
sort = {
_id: 1
};
}
return MongoClient.connect(process.env.DB_URL, {promiseLibrary: Promise})
.then(function (db) {
dbHandleForShutDowns = db;
return db.collection(collectionName).find(query).sort(sort).limit(5)
.toArray()
.then(function(documents) {
return documents[0];
})
.catch(function (err) {
return Promise.reject(err);
})
.finally(db.close.bind(db));
})
.catch(function catchErrors(err) {
if (dbHandleForShutDowns) {
dbHandleForShutDowns.close();
}
throw err;
});
};
/**
* Method to find documents based on query
* @param {string} collectionName Name of the collection
* @param {object} query Query
* @param {number} limit Limit to the query. By default there's no limit until specified.
* @param {object} projection Query Projection
* @returns {array} an array of documents based on the query.
*/
var findDocumentsBasedOnQuery = function findDocumentsBasedOnQuery(collectionName, query, limit, projection) {
if (isEmpty(limit)) {
limit = 0; // A limit() value of 0 (i.e. .limit(0)) is equivalent to setting no limit.
}
if(!projection){
projection = {};
}
var dbHandleForShutDowns;
return MongoClient.connect(process.env.DB_URL, {promiseLibrary: Promise})
.then(function (db) {
dbHandleForShutDowns = db;
return db.collection(collectionName).find(query).project(projection).limit(limit).toArray()
.finally(db.close.bind(db));
})
.catch(function catchErrors(err) {
if (dbHandleForShutDowns) {
dbHandleForShutDowns.close();
}
throw err;
});
};
/**
* Method to count documents based on query
* @param {string} collectionName Name of the collection
* @param {object} query Query object
* @returns {number} the count of documents based on a given query
*/
var countDocumentsByQuery = function countDocumentsByQuery(collectionName, query) {
var dbHandleForShutDowns;
return MongoClient.connect(process.env.DB_URL, {promiseLibrary: Promise})
.then(function (db) {
dbHandleForShutDowns = db;
return db.collection(collectionName).find(query).count()
.finally(db.close.bind(db));
})
.catch(function catchErrors(err) {
if (dbHandleForShutDowns) {
dbHandleForShutDowns.close();
}
throw err;
});
};
/**
* Method to work on a collection page by page. PageSize can be even 1. Ideal if you want to work in batches.
* This method requires you to connect to the DB first by using connectDb(). \n \n
* Assumptions:
* a) sorts will happen by `_id` in this method
* b) `query._id` is overriden by this method
*
* @param {object} db
* @param {string} collectionName Name of the collection
* @param {object} query query object
* @param {object} projection fields to project
* @param {number} pageSize page size to return from the collection.
* @param {function} processPage pass a function to handle the pagedResults
* @param {array} processPageArgs additional arguments required by processPage
*/
var workOnItPageByPage = function workOnItPageByPage(db, collectionName, query, projection, pageSize, processPage, processPageArgs) {
projection = (projection) ? projection['_id'] = true : {'_id': true};
processPageArgs = processPageArgs || []; // as a fallback, these can be empty
return db
.collection(collectionName)
.find(query)
// .project(projection)
.sort({'_id': 1}).limit(pageSize)
.toArray() // cursor methods return promises: http://mongodb.github.io/node-mongodb-native/2.1/api/Cursor.html#toArray
.then(function processPagedResults(documents) {
if (!documents || documents.length < 1) {
// stop - no data left to traverse
return Promise.resolve();
}
else {
if (documents.length < pageSize) {
// stop - last page
return processPage(db, documents, ...processPageArgs); // process the results of the LAST page
}
else {
return processPage(db, documents, ...processPageArgs) // process the results of the current page
.then(function getNextPage(){ // then go get the next page
var last_id = documents[documents.length - 1]['_id'];
query['_id'] = {'$gt': last_id};
return workOnItPageByPage(db, collectionName, query, projection, pageSize, processPage, processPageArgs);
});
}
}
});
};
/**
* Method to connect to the db
* @returns {object} db connection
*/
var connectDb = function connectDb() {
var dbHandleForShutDowns;
return MongoClient.connect(process.env.DB_URL, {promiseLibrary: Promise})
.then(function (db) {
dbHandleForShutDowns = db;
return Promise.resolve(db);
})
.catch(function catchErrors(err) {
if (dbHandleForShutDowns) {
dbHandleForShutDowns.close();
}
throw err;
});
};
/**
* Method to create documents in bulk in a given collection.
* @param {object} db
* @param {string} collectionName - Name of the collection
* @param {array }documents Array of documents to be created
* @returns {*}
*/
var bulkCreate = function bulkCreate(db, collectionName, documents) {
if (!documents || documents.length === 0) return Promise.resolve();
// (1) Initialize the unordered Batch
var batch = db.collection(collectionName).initializeUnorderedBulkOp();
// (2) Add some operations to be executed
for (var i = 0; i < documents.length; i++) {
//console.log(documents[i]);
batch.insert(documents[i]);
}
// (3) Execute the operations
return batch.execute();
};
/**
* Method to update documents bulk in a given collection
* @param {object} db
* @param {string} collectionName Name of the collection
* @param {array} updates array of documents to update
* @param {object} omits Fields to omit while updating the documents in the collection
* @returns {*}
*/
var bulkUpdate = function bulkUpdate(db, collectionName, updates, omits) {
if (!updates || updates.length === 0) return Promise.resolve();
// (1) Initialize the unordered Batch
var batch = db.collection(collectionName).initializeUnorderedBulkOp();
// (2) Add some operations to be executed
for (var i = 0; i < updates.length; i++) {
var update;
(omits) ? update = _.omit(updates[i], omits.toString()) : update = updates[i];
var updateOp = {$set: update};
batch.find({'_id': new ObjectID(update._id)}).updateOne(updateOp);
}
// (3) Execute the operations
return batch.execute();
};
/**
* Idea came from https://medium.com/@gchudnov/trapping-signals-in-docker-containers-7a57fdda7d86
*
* @param {*} shutdown - the calling code user this method to control what actions to take as part of shutdown
*/
var registerForGracefulShutdown = function registerForGracefulShutdown(shutdown) {
var signals = {
'SIGINT': 2,
'SIGTERM': 15
};
Object.keys(signals).forEach(function (signal) {
process.on(signal, function () {
shutdown(signal, signals[signal]);
});
});
};
var isEmpty = function (input) {
if (_.isString(input)) {
return input === undefined || input === null || input.trim() === '';
}
else {
return input === undefined || input === null;
}
};
/**
* Method to insert a single document.
* @param {string} collectionName name of the collection
* @param {object} document document to insert
* @returns {*}
*/
var insertOne = function insertOne(collectionName, document) {
if (!document) {
return Promise.resolve();
}
var dbHandleForShutDowns;
return MongoClient.connect(process.env.DB_URL, {promiseLibrary: Promise})
.then(function insertData(db) {
dbHandleForShutDowns = db;
return db.collection(collectionName).insertOne(Object.assign({}, document))
.finally(db.close.bind(db));
})
.catch(function catchErrors(err) {
if (dbHandleForShutDowns) {
dbHandleForShutDowns.close();
}
throw err;
});
};
/**
* Method to drop a collection
* @param {string} name name of the collection to drop.
* @returns {*}
*/
var dropCollection = function dropCollection(name) {
var dbHandleForShutDowns;
return MongoClient.connect(process.env.DB_URL, {promiseLibrary: Promise})
.then(function dropDb(db) {
dbHandleForShutDowns = db;
return db.listCollections().toArray();
})
.then(function (collections) {
var collectionNames = collections.map(function (collection) {
return collection.name;
});
if(collectionNames.indexOf(name) > -1){
return dbHandleForShutDowns.collection(name).drop()
.finally(dbHandleForShutDowns.close.bind(dbHandleForShutDowns));
}
else {
return Promise.resolve(false);
}
})
.catch(function catchErrors(err) {
if (dbHandleForShutDowns) {
dbHandleForShutDowns.close();
}
throw err;
});
};
/**
* Method to find distinct documents in a collection
* @param {string} collectionName name of the collection
* @param {string} field - Distinct Field
* @returns {array} an array of field values that are in the collection
*/
var findDistinctDocuments = function findDistinctDocuments(collectionName, field) {
var dbHandleForShutDowns;
return MongoClient.connect(process.env.DB_URL, {promiseLibrary: Promise})
.then(function (db) {
dbHandleForShutDowns = db;
return db.collection(collectionName);
})
.then(function(collection){
return collection.distinct(field, {[field]: {$exists: true}})
.finally(dbHandleForShutDowns.close.bind(dbHandleForShutDowns));
})
.catch(function catchErrors(err) {
if (dbHandleForShutDowns) {
dbHandleForShutDowns.close();
}
throw err;
});
};
/**
* Method to process a batch of documents in a collection
* @param db - db instance connection
* @param collectionName - Name of the collection
* @param query - Query on the basis of which documents will be picked from a collection.
* @param batchSize - Size of the batch you'd want to process
* @param processBatch - Function/method to run once desired docs are fetched from the DB.
* @param processBatchArgs - Additional arguments that are required to be passed onto the processBatch method.
*/
var processABatchOfDocuments = function processABatchOfDocuments(db, collectionName, query, batchSize, processBatch, processBatchArgs){
processBatchArgs = processBatchArgs || [];
return db
.collection(collectionName)
.find(query)
.sort({'_id': 1}).limit(batchSize)
.toArray()
.then(function processPagedResults(documents) {
if (!documents || documents.length < 1) {
return Promise.resolve(false);
}
else {
return processBatch(db, documents, ...processBatchArgs);
}
});
};
/**
* Method to bulk update documents in a collection given a specific query.
* @param {object} db
* @param {string} collectionName Name of the collection
* @param {object} updates Values that will be updated. Can update multiple values or set new values too.
* @param {object} query Query to find the documents in the collection to update
* @returns {object}
*/
var bulkUpdateByQuery = function bulkUpdateByQuery(db, collectionName, updates, query) {
var bulk = db.collection(collectionName).initializeUnorderedBulkOp();
bulk.find(query).update({$set: updates});
return bulk.execute();
};
module.exports = {
bulkCreate: bulkCreate,
bulkUpdateByQuery: bulkUpdateByQuery,
insertOne: insertOne,
bulkUpdate: bulkUpdate,
findDistinctDocuments: findDistinctDocuments,
dropCollection: dropCollection,
findOneDocumentBasedOnQuery: findOneDocumentBasedOnQuery,
findDocumentsBasedOnQuery: findDocumentsBasedOnQuery,
insertIntoDb: insertIntoDb,
isEmpty: isEmpty,
countDocumentsByQuery: countDocumentsByQuery,
updateDocument: updateDocument,
connectDb: connectDb,
processABatchOfDocuments: processABatchOfDocuments,
registerForGracefulShutdown: registerForGracefulShutdown,
workOnItPageByPage: workOnItPageByPage,
upsertDocument: upsertDocument
};