@cocreate/mongodb 1.1.25 → 1.2.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.
Files changed (3) hide show
  1. package/CHANGELOG.md +14 -0
  2. package/package.json +1 -1
  3. package/src/index.js +203 -175
package/CHANGELOG.md CHANGED
@@ -1,3 +1,17 @@
1
+ ## [1.2.1](https://github.com/CoCreate-app/CoCreate-mongodb/compare/v1.2.0...v1.2.1) (2023-03-28)
2
+
3
+
4
+ ### Bug Fixes
5
+
6
+ * data.filter && data.filter.query ([bc3a0db](https://github.com/CoCreate-app/CoCreate-mongodb/commit/bc3a0db6d1fb6e7eed68031ed76a6af4cfcb1576))
7
+
8
+ # [1.2.0](https://github.com/CoCreate-app/CoCreate-mongodb/compare/v1.1.25...v1.2.0) (2023-03-19)
9
+
10
+
11
+ ### Features
12
+
13
+ * dynamically connect to user defined dbUrl ([343ab8e](https://github.com/CoCreate-app/CoCreate-mongodb/commit/343ab8e5a72683a3be74c24acfc4489198564c10))
14
+
1
15
  ## [1.1.25](https://github.com/CoCreate-app/CoCreate-mongodb/compare/v1.1.24...v1.1.25) (2023-02-01)
2
16
 
3
17
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cocreate/mongodb",
3
- "version": "1.1.25",
3
+ "version": "1.2.1",
4
4
  "description": "A simple mongodb component in vanilla javascript. Easily configured using HTML5 data-attributes and/or JavaScript API.",
5
5
  "keywords": [
6
6
  "mongodb",
package/src/index.js CHANGED
@@ -1,13 +1,14 @@
1
- const {MongoClient, ObjectId} = require('mongodb');
2
- const {dotNotationToObject, queryData, searchData, sortData} = require('@cocreate/utils')
1
+ const { MongoClient, ObjectId } = require('mongodb');
2
+ const { dotNotationToObject, queryData, searchData, sortData } = require('@cocreate/utils')
3
+ const clients = new Map()
3
4
 
4
- function mongoClient(dbUrl) {
5
+ async function mongoClient(dbUrl) {
5
6
  try {
6
7
  dbUrl = dbUrl || process.env.MONGO_URL;
7
8
  if (!dbUrl || !dbUrl.includes('mongodb'))
8
9
  console.log('CoCreate.config.js missing dbUrl')
9
- dbClient = MongoClient.connect(dbUrl, { useNewUrlParser: true, useUnifiedTopology: true });
10
- return dbClient;
10
+ const Client = MongoClient.connect(dbUrl, { useNewUrlParser: true, useUnifiedTopology: true });
11
+ return Client;
11
12
  } catch (error) {
12
13
  console.error(error)
13
14
  return {
@@ -16,13 +17,28 @@ function mongoClient(dbUrl) {
16
17
  }
17
18
  }
18
19
 
19
- let dbClient;
20
+ let platformClient;
20
21
  mongoClient().then(Client => {
21
- dbClient = Client
22
+ platformClient = Client
22
23
  });
23
24
 
25
+ async function dbClient(data) {
26
+ // ToDo: provide platform client only to specific collections ['permissions', 'metrics', 'organizations', 'users']
27
+ if (!data.dbs)
28
+ return platformClient
29
+ let client = clients.get(data.dbs)
30
+ if (!client) {
31
+ client = await mongoClient(data.dbs)
32
+ clients.set(data.dbs, client)
33
+ }
34
+ return client
35
+ }
36
+
24
37
  async function databaseStats(data) {
25
- let stats = await dbClient.db(data.organization_id).stats()
38
+ const client = await dbClient(data)
39
+ if (!client) return
40
+ let db = client.db(data.organization_id)
41
+ let stats = db.stats()
26
42
  return stats
27
43
  }
28
44
 
@@ -42,35 +58,39 @@ function deleteDatabase(data) {
42
58
  return database('deleteDatabase', data)
43
59
  }
44
60
 
45
- function database(action, data){
46
- return new Promise((resolve, reject) => {
61
+ function database(action, data) {
62
+ return new Promise(async (resolve, reject) => {
47
63
  let type = 'database'
48
64
  let databaseArray = []
49
-
65
+
50
66
  try {
67
+ const client = await dbClient(data)
68
+ if (!client) return
51
69
  if (action == 'readDatabase') {
52
- let db = dbClient.db().admin();
70
+ const db = client.db().admin();
53
71
 
54
72
  // List all the available databases
55
- db.listDatabases(function(err, dbs) {
56
-
57
- for (let database of dbs.databases){
58
- let isFilter = queryData(database, data.filter.query)
59
- if (isFilter)
60
- databaseArray.push({database, db: 'mongodb'})
73
+ db.listDatabases(function (err, dbs) {
74
+
75
+ for (let database of dbs.databases) {
76
+ if (data.filter && data.filter.query) {
77
+ let isFilter = queryData(database, data.filter.query)
78
+ if (isFilter)
79
+ databaseArray.push({ database, db: 'mongodb' })
80
+ } else
81
+ databaseArray.push({ database, db: 'mongodb' })
61
82
  }
62
-
63
- resolve(createData(data, databaseArray, type))
83
+
84
+ resolve(createData(data, databaseArray, type))
64
85
  })
65
-
66
86
  }
67
87
  if (action == 'deleteDatabase') {
68
- const db = dbClient.db(data.database);
88
+ const db = client.db(data.database);
69
89
  db.dropDatabase().then(response => {
70
90
  resolve(response)
71
91
  })
72
92
  }
73
- } catch(error) {
93
+ } catch (error) {
74
94
  errorHandler(data, error)
75
95
  console.log(action, 'error', error);
76
96
  resolve(data);
@@ -82,7 +102,7 @@ function database(action, data){
82
102
  }
83
103
 
84
104
 
85
- function createCollection(data){
105
+ function createCollection(data) {
86
106
  return collection('createCollection', data)
87
107
  }
88
108
 
@@ -98,94 +118,98 @@ function deleteCollection(data) {
98
118
  return collection('deleteCollection', data)
99
119
  }
100
120
 
101
- function collection(action, data){
102
- return new Promise((resolve, reject) => {
121
+ function collection(action, data) {
122
+ return new Promise(async (resolve, reject) => {
103
123
  let type = 'collection'
104
124
  let collectionArray = [];
105
-
125
+
106
126
  try {
127
+ const client = await dbClient(data)
128
+ if (!client) return
129
+
107
130
  if (data.request)
108
131
  data.collection = data.request
109
132
 
110
- let databases = data.database;
133
+ let databases = data.database;
111
134
  if (!Array.isArray(databases))
112
135
  databases = [databases]
113
136
 
114
137
  let databasesLength = databases.length
115
138
  for (let database of databases) {
116
- const db = dbClient.db(database);
139
+ const db = client.db(database);
140
+
117
141
  if (action == 'readCollection') {
118
142
 
119
- let {query, sort} = getFilters(data);
143
+ let { query, sort } = getFilters(data);
120
144
 
121
- db.listCollections().toArray(function(error, result) {
145
+ db.listCollections().toArray(function (error, result) {
122
146
  if (error)
123
147
  errorHandler(data, error, database)
124
-
148
+
125
149
  if (result) {
126
150
  for (let res of result) {
127
151
  let isFilter = queryData(res, data.filter.query)
128
152
  if (isFilter)
129
- collectionArray.push({name: res.name, database, db: 'mongodb'})
153
+ collectionArray.push({ name: res.name, database, db: 'mongodb' })
130
154
  }
131
155
  }
132
156
 
133
157
  databasesLength -= 1
134
158
  if (!databasesLength) {
135
159
  data = createData(data, collectionArray, type)
136
- resolve(data)
160
+ resolve(data)
137
161
  }
138
- })
162
+ })
139
163
  } else {
140
164
  let collections
141
165
  let value
142
166
  if (action == 'updateCollection')
143
167
  collections = Object.entries(data.collection)
144
168
  else
145
- collections = data.collection;
146
-
169
+ collections = data.collection;
170
+
147
171
  if (!Array.isArray(collections))
148
172
  collections = [collections]
149
173
 
150
174
  let collectionsLength = collections.length
151
175
  for (let collection of collections) {
152
-
176
+
153
177
  if (action == 'createCollection') {
154
- db.createCollection(collection, function(error, result) {
178
+ db.createCollection(collection, function (error, result) {
155
179
  if (error)
156
180
  errorHandler(data, error, database, collection)
157
181
 
158
182
  if (result)
159
- collectionArray.push({name: collection, database, db: 'mongodb'})
183
+ collectionArray.push({ name: collection, database, db: 'mongodb' })
160
184
 
161
- collectionsLength -= 1
185
+ collectionsLength -= 1
162
186
  if (!collectionsLength)
163
187
  databasesLength -= 1
164
-
188
+
165
189
  if (!databasesLength && !collectionsLength) {
166
190
  data = createData(data, collectionArray, type)
167
- resolve(data)
191
+ resolve(data)
168
192
  }
169
193
  })
170
194
  } else {
171
195
  if (action == 'updateCollection') {
172
196
  [collection, value] = collection
173
197
  }
174
-
198
+
175
199
  const collectionObj = db.collection(collection);
176
200
 
177
201
  if (action == 'updateCollection') {
178
- collectionObj.rename(value, function(error, result) {
202
+ collectionObj.rename(value, function (error, result) {
179
203
  if (error)
180
204
  errorHandler(data, error, database, collection)
181
-
205
+
182
206
  if (result)
183
- collectionArray.push({name: value, oldName: collection, database, db: 'mongodb'})
184
-
185
- collectionsLength -= 1
207
+ collectionArray.push({ name: value, oldName: collection, database, db: 'mongodb' })
208
+
209
+ collectionsLength -= 1
186
210
  if (!collectionsLength)
187
211
  databasesLength -= 1
188
-
212
+
189
213
  if (!databasesLength && !collectionsLength) {
190
214
  data = createData(data, collectionArray, type)
191
215
  resolve(data)
@@ -195,31 +219,32 @@ function collection(action, data){
195
219
  }
196
220
 
197
221
  if (action == 'deleteCollection') {
198
- collectionObj.drop( function(error, result) {
222
+ collectionObj.drop(function (error, result) {
199
223
  if (error)
200
224
  errorHandler(data, error, database, collection)
201
-
225
+
202
226
  if (result)
203
- collectionArray.push({name: collection, database, db: 'mongodb'})
204
-
205
- collectionsLength -= 1
227
+ collectionArray.push({ name: collection, database, db: 'mongodb' })
228
+
229
+ collectionsLength -= 1
206
230
  if (!collectionsLength)
207
231
  databasesLength -= 1
208
-
232
+
209
233
  if (!databasesLength && !collectionsLength) {
210
234
  data = createData(data, collectionArray, type)
211
235
  resolve(data)
212
236
  }
213
237
 
214
238
  })
215
-
239
+
216
240
  }
217
241
  }
218
242
 
219
243
  }
220
244
  }
221
245
  }
222
- } catch(error) {
246
+
247
+ } catch (error) {
223
248
  errorHandler(data, error)
224
249
  console.log(action, 'error', error);
225
250
  resolve(data);
@@ -230,7 +255,7 @@ function collection(action, data){
230
255
  }
231
256
 
232
257
 
233
- function createDocument(data){
258
+ function createDocument(data) {
234
259
  return document('createDocument', data)
235
260
  }
236
261
 
@@ -246,9 +271,12 @@ function deleteDocument(data) {
246
271
  return document('deleteDocument', data)
247
272
  }
248
273
 
249
- function document(action, data){
274
+ function document(action, data) {
250
275
  return new Promise(async (resolve, reject) => {
251
276
  try {
277
+ const client = await dbClient(data)
278
+ if (!client) return
279
+
252
280
  let type = 'document'
253
281
  let documents = [];
254
282
 
@@ -258,13 +286,13 @@ function document(action, data){
258
286
 
259
287
  if (!data['timeStamp'])
260
288
  data['timeStamp'] = new Date().toISOString()
261
-
289
+
262
290
 
263
291
  let isFilter
264
292
  if (data.filter && data.filter.query)
265
293
  isFilter = true
266
-
267
- let databases = data.database;
294
+
295
+ let databases = data.database;
268
296
  if (!Array.isArray(databases))
269
297
  databases = [databases]
270
298
 
@@ -276,10 +304,10 @@ function document(action, data){
276
304
 
277
305
  let collectionsLength = collections.length
278
306
  for (let collection of collections) {
279
- const db = dbClient.db(database);
307
+ const db = client.db(database);
280
308
  const collectionObj = db.collection(collection);
281
-
282
- let {query, sort} = getFilters(data);
309
+
310
+ let { query, sort } = getFilters(data);
283
311
  if (data['organization_id']) {
284
312
  query['organization_id'] = { $eq: data['organization_id'] }
285
313
  }
@@ -301,54 +329,54 @@ function document(action, data){
301
329
 
302
330
  if (!data[type][i]._id)
303
331
  data[type][i]._id = ObjectId()
304
- else
332
+ else
305
333
  data[type][i]._id = ObjectId(data[type][i]._id)
306
- data[type][i]['created'] = {on: data.timeStamp, by: data.user_id || data.clientId}
334
+ data[type][i]['created'] = { on: data.timeStamp, by: data.user_id || data.clientId }
307
335
  }
308
336
  if (action == 'readDocument' && data[type][i]._id) {
309
337
  _ids.push(ObjectId(data[type][i]._id))
310
338
  }
311
- if (action =='updateDocument') {
339
+ if (action == 'updateDocument') {
312
340
  if (data[type][i]._id)
313
- update_ids.push({_id: data[type][i]._id, updateDoc: data[type][i], updateType: '_id'})
314
-
341
+ update_ids.push({ _id: data[type][i]._id, updateDoc: data[type][i], updateType: '_id' })
342
+
315
343
  if (!data[type][i]._id)
316
- updateData = createUpdate({document: [data[type][i]]}, type)
344
+ updateData = createUpdate({ document: [data[type][i]] }, type)
317
345
 
318
- data[type][i]['modified'] = {on: data.timeStamp, by: data.user_id || data.clientId}
346
+ data[type][i]['modified'] = { on: data.timeStamp, by: data.user_id || data.clientId }
319
347
 
320
348
  }
321
- if (action =='deleteDocument') {
349
+ if (action == 'deleteDocument') {
322
350
  if (data[type][i]._id) {
323
351
  _ids.push(ObjectId(data[type][i]._id))
324
- documents.push({_id: data[type][i]._id, db: 'mongodb', database, collection})
352
+ documents.push({ _id: data[type][i]._id, db: 'mongodb', database, collection })
325
353
  }
326
354
  }
327
355
  }
328
356
  if (_ids.length == 1)
329
357
  query['_id'] = ObjectId(_ids[0])
330
358
  else if (_ids.length > 0)
331
- query['_id'] = {$in: _ids}
359
+ query['_id'] = { $in: _ids }
332
360
  }
333
-
361
+
334
362
 
335
363
  if (action == 'createDocument') {
336
- collectionObj.insertMany(data[type], function(error, result) {
364
+ collectionObj.insertMany(data[type], function (error, result) {
337
365
  if (error)
338
366
  errorHandler(data, error, database, collection)
339
-
367
+
340
368
  for (let i = 0; i < data[type].length; i++)
341
- documents.push({db: 'mongodb', database, collection, ...data[type][i]})
369
+ documents.push({ db: 'mongodb', database, collection, ...data[type][i] })
342
370
 
343
- collectionsLength -= 1
371
+ collectionsLength -= 1
344
372
  if (!collectionsLength)
345
373
  databasesLength -= 1
346
-
374
+
347
375
  if (!databasesLength && !collectionsLength) {
348
376
  data = createData(data, documents, type)
349
- resolve(data)
377
+ resolve(data)
350
378
  }
351
- });
379
+ });
352
380
  }
353
381
 
354
382
  if (action == 'readDocument') {
@@ -356,7 +384,7 @@ function document(action, data){
356
384
  if (data.filter) {
357
385
  const count = await collectionObj.estimatedDocumentCount()
358
386
  data.filter.count = count
359
-
387
+
360
388
  if (data.filter.startIndex)
361
389
  index = data.filter.startIndex
362
390
  if (data.filter.limit)
@@ -364,11 +392,11 @@ function document(action, data){
364
392
  if (limit)
365
393
  limit = index + limit;
366
394
  }
367
-
368
- collectionObj.find(query).limit(limit).sort(sort).toArray(function(error, result) {
395
+
396
+ collectionObj.find(query).limit(limit).sort(sort).toArray(function (error, result) {
369
397
  if (error)
370
398
  errorHandler(data, error, database, collection)
371
-
399
+
372
400
  if (result) {
373
401
  // ToDo: forEach at cursor
374
402
  for (let doc of result) {
@@ -389,37 +417,37 @@ function document(action, data){
389
417
  }
390
418
 
391
419
  if (data.returnDocument == false) {
392
-
393
- for (let item of data['data']) {
420
+
421
+ for (let item of data['data']) {
394
422
  let resp = {};
395
423
  resp['_id'] = tmp['_id']
396
424
  data[type].forEach((f) => resp[f] = item[f])
397
425
  documents.push(resp);
398
426
  }
399
-
427
+
400
428
  data['data'] = documents
401
429
  }
402
430
  }
403
431
 
404
- collectionsLength -= 1
432
+ collectionsLength -= 1
405
433
  if (!collectionsLength)
406
434
  databasesLength -= 1
407
-
435
+
408
436
  if (!databasesLength && !collectionsLength) {
409
437
  data = createData(data, documents, type)
410
- resolve(data)
438
+ resolve(data)
411
439
  }
412
440
  });
413
441
  }
414
442
 
415
443
  if (action == 'updateDocument' || action == 'deleteDocument') {
416
444
  const queryDocs = () => {
417
- return new Promise((resolve, reject) => {
418
-
419
- collectionObj.find(query).sort(sort).toArray(function(error, result) {
445
+ return new Promise(async (resolve, reject) => {
446
+
447
+ collectionObj.find(query).sort(sort).toArray(function (error, result) {
420
448
  if (error)
421
449
  errorHandler(data, error, database, collection)
422
-
450
+
423
451
  if (data.filter && data.filter.search) {
424
452
  let searchResult = []
425
453
 
@@ -436,7 +464,7 @@ function document(action, data){
436
464
  console.log(err);
437
465
  });
438
466
  }
439
-
467
+
440
468
  let Result, $update, update, projection;
441
469
 
442
470
  if (isFilter && data.returnDocument != false)
@@ -446,44 +474,44 @@ function document(action, data){
446
474
  if (Result) {
447
475
  for (let doc of Result) {
448
476
  if (action == 'deleteDocument')
449
- documents.push({_id: doc._id, db: 'mongodb', database, collection})
477
+ documents.push({ _id: doc._id, db: 'mongodb', database, collection })
450
478
  else
451
- doc['modified'] = {on: data.timeStamp, by: data.user_id || data.clientId}
452
-
479
+ doc['modified'] = { on: data.timeStamp, by: data.user_id || data.clientId }
480
+
453
481
  _ids.push(doc._id)
454
482
  }
455
- update_ids.push({updateType: 'filter'})
483
+ update_ids.push({ updateType: 'filter' })
456
484
  }
457
485
 
458
486
  if (action == 'updateDocument') {
459
487
  let docsLength = update_ids.length
460
- for (let {updateDoc, updateType} of update_ids) {
461
-
488
+ for (let { updateDoc, updateType } of update_ids) {
489
+
462
490
  if (updateType == '_id') {
463
491
  let update_id = updateDoc._id
464
492
  query['_id'] = ObjectId(update_id)
465
- $update = createUpdate({document: [updateDoc]}, type)
493
+ $update = createUpdate({ document: [updateDoc] }, type)
466
494
  update = $update.update
467
495
  projection = $update.projection
468
- documents.push({_id: update_id, db: 'mongodb', database, collection, ...update['$set']})
496
+ documents.push({ _id: update_id, db: 'mongodb', database, collection, ...update['$set'] })
469
497
  }
470
-
498
+
471
499
  if (updateType == 'filter') {
472
- query['_id'] = {$in: _ids}
500
+ query['_id'] = { $in: _ids }
473
501
  $update = updateData
474
502
  update = $update.update
475
503
  projection = $update.projection
476
504
  for (let _id of _ids)
477
- documents.push({_id, db: 'mongodb', database, collection, ...update['$set']})
505
+ documents.push({ _id, db: 'mongodb', database, collection, ...update['$set'] })
478
506
 
479
507
  }
480
508
 
481
509
  update['$set']['organization_id'] = data.organization_id
482
-
510
+
483
511
  collectionObj.updateMany(query, update, {
484
512
  upsert: data.upsert,
485
513
  projection
486
- }).then((result) => {
514
+ }).then((result) => {
487
515
 
488
516
  }).catch((error) => {
489
517
  errorHandler(data, error, database, collection)
@@ -491,29 +519,29 @@ function document(action, data){
491
519
  }).finally((error) => {
492
520
  docsLength -= 1
493
521
  if (!docsLength)
494
- collectionsLength -= 1
522
+ collectionsLength -= 1
495
523
 
496
524
  if (!collectionsLength)
497
525
  databasesLength -= 1
498
-
526
+
499
527
  if (!databasesLength && !collectionsLength) {
500
528
  data = createData(data, documents, type)
501
- resolve(data)
529
+ resolve(data)
502
530
  }
503
531
  })
504
- }
505
-
532
+ }
533
+
506
534
  if (!update_ids.length) {
507
535
  docsLength -= 1
508
536
  if (!docsLength)
509
- collectionsLength -= 1
537
+ collectionsLength -= 1
510
538
 
511
539
  if (!collectionsLength)
512
540
  databasesLength -= 1
513
-
541
+
514
542
  if (!databasesLength && !collectionsLength) {
515
543
  data = createData(data, documents, type)
516
- resolve(data)
544
+ resolve(data)
517
545
  }
518
546
  }
519
547
 
@@ -523,26 +551,26 @@ function document(action, data){
523
551
  if (_ids.length == 1)
524
552
  query['_id'] = ObjectId(_ids[0])
525
553
  else if (_ids.length > 0)
526
- query['_id'] = {$in: _ids}
527
- collectionObj.deleteMany(query, function(error, result) {
528
- collectionsLength -= 1
554
+ query['_id'] = { $in: _ids }
555
+ collectionObj.deleteMany(query, function (error, result) {
556
+ collectionsLength -= 1
529
557
  if (!collectionsLength)
530
558
  databasesLength -= 1
531
-
559
+
532
560
  if (!databasesLength && !collectionsLength) {
533
561
  data = createData(data, documents, type)
534
- resolve(data)
562
+ resolve(data)
535
563
  }
536
-
564
+
537
565
  })
538
566
  }
539
567
 
540
568
  }
541
-
569
+
542
570
  }
543
571
  }
544
572
 
545
- } catch(error) {
573
+ } catch (error) {
546
574
  errorHandler(data, error)
547
575
  console.log(action, 'error', error);
548
576
  resolve(data);
@@ -555,7 +583,7 @@ function document(action, data){
555
583
 
556
584
  function createUpdate(data, type) {
557
585
  let update = {}, projection = {};
558
- if (data[type][0]) {
586
+ if (data[type][0]) {
559
587
  update['$set'] = data[type][0]
560
588
  // update['$set']['organization_id'] = data['organization_id'];
561
589
  if (update['$set']['_id'])
@@ -564,18 +592,18 @@ function createUpdate(data, type) {
564
592
  projection[x] = 1
565
593
  })
566
594
  }
567
-
568
- if ( data['deleteName'] ) {
595
+
596
+ if (data['deleteName']) {
569
597
  update['$unset'] = replaceArray(data['deleteName']);
570
598
  }
571
-
572
- if ( data['updateName'] ) {
599
+
600
+ if (data['updateName']) {
573
601
  update['$rename'] = replaceArray(data['updateName'])
574
602
  for (const [key, value] of Object.entries(update['$rename'])) {
575
603
  if (/\.([0-9]*)/g.test(key) || /\[([0-9]*)\]/g.test(value)) {
576
604
  console.log('key is array', /\[([0-9]*)\]/g.test(value), /\.([0-9]*)/g.test(key))
577
605
  } else {
578
- let newValue = replaceArray({[value]: value})
606
+ let newValue = replaceArray({ [value]: value })
579
607
  let oldkey = key;
580
608
  for (const [key] of Object.entries(newValue)) {
581
609
  update['$rename'][oldkey] = key
@@ -584,7 +612,7 @@ function createUpdate(data, type) {
584
612
  }
585
613
  }
586
614
 
587
- return {update, projection}
615
+ return { update, projection }
588
616
 
589
617
  }
590
618
 
@@ -596,13 +624,13 @@ function createData(data, array, type) {
596
624
  data[type] = sortData(array, data.filter.sort)
597
625
  else
598
626
  data[type] = array
599
-
600
- if (data.returnLog){
627
+
628
+ if (data.returnLog) {
601
629
  if (!data.log)
602
630
  data.log = []
603
631
  data.log.push(...data[type])
604
632
  }
605
-
633
+
606
634
  return data
607
635
  }
608
636
 
@@ -626,14 +654,14 @@ function getFilters(data) {
626
654
  for (let i = 0; i < filter.sort.length; i++) {
627
655
  let direction = filter.sort[i].direction
628
656
  if (direction == 'desc' || direction == -1)
629
- direction = -1;
657
+ direction = -1;
630
658
  else
631
659
  direction = 1;
632
-
660
+
633
661
  sort[filter.sort[i].name] = filter.sort[i].direction
634
662
  }
635
663
  }
636
- return {query, sort}
664
+ return { query, sort }
637
665
  }
638
666
 
639
667
  // ToDo: create impved mongodb query to cover many cases
@@ -645,9 +673,9 @@ function createQuery(filters) {
645
673
 
646
674
  if (!item.name)
647
675
  continue
648
-
676
+
649
677
  if (item.name == "_id") {
650
- if (item.value)
678
+ if (item.value)
651
679
  item.value = ObjectId(item.value)
652
680
  else
653
681
  continue
@@ -657,23 +685,23 @@ function createQuery(filters) {
657
685
  if (!query[key]) {
658
686
  query[key] = {};
659
687
  }
660
-
688
+
661
689
  switch (item.operator) {
662
690
  case '$includes':
663
691
  case 'includes':
664
692
  query[key]['$regex'] = item.value;
665
693
  break;
666
-
694
+
667
695
  case '$range':
668
696
  if (item.value[0] !== null && item.value[1] !== null) {
669
- query[key] = {$gte: item.value[0], $lte: item.value[1]};
697
+ query[key] = { $gte: item.value[0], $lte: item.value[1] };
670
698
  } else if (item.value[0] !== null) {
671
- query[key] = {$gte: item.value[0]};
699
+ query[key] = { $gte: item.value[0] };
672
700
  } else if (item.value[1] !== null) {
673
- query[key] = {$lte: item.value[1]};
701
+ query[key] = { $lte: item.value[1] };
674
702
  }
675
703
  break;
676
-
704
+
677
705
  case 'equals':
678
706
  query[$eq][item.operator] = item.value;
679
707
  case '$eq':
@@ -690,8 +718,8 @@ function createQuery(filters) {
690
718
  // item.value.forEach(function(v) {
691
719
  // in_values.push(new RegExp(".*" + v + ".*", "i"));
692
720
  // });
693
-
694
- query[key] = {$in : item.value }
721
+
722
+ query[key] = { $in: item.value }
695
723
  break;
696
724
  case '$nin':
697
725
  query[key][item.operator] = item.value;
@@ -702,13 +730,13 @@ function createQuery(filters) {
702
730
  if (item.type) {
703
731
  query[key]['$geoWithin'] = {
704
732
  [item.type]: value
705
- }
733
+ }
706
734
  }
707
- } catch(e) {
735
+ } catch (e) {
708
736
  console.log('geowithin error');
709
737
  }
710
738
  break;
711
- }
739
+ }
712
740
  }
713
741
 
714
742
  //. global search
@@ -720,11 +748,11 @@ function createQuery(filters) {
720
748
  return query;
721
749
  }
722
750
 
723
- function errorHandler(data, error, database, collection){
751
+ function errorHandler(data, error, database, collection) {
724
752
  if (typeof error == 'object')
725
753
  error['db'] = 'mongodb'
726
754
  else
727
- error = {db: 'mongodb', message: error}
755
+ error = { db: 'mongodb', message: error }
728
756
 
729
757
  if (database)
730
758
  error['database'] = database
@@ -735,41 +763,41 @@ function errorHandler(data, error, database, collection){
735
763
  else
736
764
  data.error = [error]
737
765
  }
738
-
766
+
739
767
  function replaceArray(data) {
740
768
  let keys = Object.keys(data);
741
769
  let objectData = {};
742
-
770
+
743
771
  keys.forEach((k) => {
744
772
  let nk = k
745
773
  if (/\[([0-9]*)\]/g.test(k)) {
746
774
  nk = nk.replace(/\[/g, '.');
747
775
  if (nk.endsWith(']'))
748
- nk = nk.slice(0, -1)
776
+ nk = nk.slice(0, -1)
749
777
  nk = nk.replace(/\]./g, '.');
750
778
  nk = nk.replace(/\]/g, '.');
751
779
  }
752
- objectData[nk] = data[k];
780
+ objectData[nk] = data[k];
753
781
  });
754
-
782
+
755
783
  return objectData;
756
784
  }
757
785
 
758
786
 
759
787
  module.exports = {
760
788
  databaseStats,
761
- createDatabase,
762
- readDatabase,
763
- updateDatabase,
764
- deleteDatabase,
765
-
766
- createCollection,
767
- readCollection,
768
- updateCollection,
769
- deleteCollection,
770
-
771
- createDocument,
772
- readDocument,
773
- updateDocument,
774
- deleteDocument,
789
+ createDatabase,
790
+ readDatabase,
791
+ updateDatabase,
792
+ deleteDatabase,
793
+
794
+ createCollection,
795
+ readCollection,
796
+ updateCollection,
797
+ deleteCollection,
798
+
799
+ createDocument,
800
+ readDocument,
801
+ updateDocument,
802
+ deleteDocument,
775
803
  }