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