@cocreate/mongodb 1.9.0 → 1.10.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.
package/CHANGELOG.md CHANGED
@@ -1,3 +1,25 @@
1
+ # [1.10.0](https://github.com/CoCreate-app/CoCreate-mongodb/compare/v1.9.1...v1.10.0) (2023-10-09)
2
+
3
+
4
+ ### Bug Fixes
5
+
6
+ * ObjectId generated by db to maintain integrity and atomicity of message_log ([63a1d4a](https://github.com/CoCreate-app/CoCreate-mongodb/commit/63a1d4a0f7387cf5a7ea242de8c5047572377313))
7
+
8
+
9
+ ### Features
10
+
11
+ * $currentDate operator to add timestamp in ([057e067](https://github.com/CoCreate-app/CoCreate-mongodb/commit/057e067ee6e359a901736f43955883935330436d))
12
+ * isValidDate imported from utils to check key values for date strings and convert to date object ([16f6695](https://github.com/CoCreate-app/CoCreate-mongodb/commit/16f6695a67e43232a5feccee70e19b826bc43b3a))
13
+ * Support for $each update operator ([d955bd6](https://github.com/CoCreate-app/CoCreate-mongodb/commit/d955bd6b4f15db5d745ed1d392637aa4348d1aa5))
14
+
15
+ ## [1.9.1](https://github.com/CoCreate-app/CoCreate-mongodb/compare/v1.9.0...v1.9.1) (2023-09-18)
16
+
17
+
18
+ ### Bug Fixes
19
+
20
+ * Add path and pathname ([7ae3b2d](https://github.com/CoCreate-app/CoCreate-mongodb/commit/7ae3b2d7b57fdfbbfe26782ca260e5ad01b13cd0))
21
+ * Update dCoCreate dpendencies to latest versions ([b583adc](https://github.com/CoCreate-app/CoCreate-mongodb/commit/b583adc423db188577fe8e1fbac6304dabf8c86a))
22
+
1
23
  # [1.9.0](https://github.com/CoCreate-app/CoCreate-mongodb/compare/v1.8.0...v1.9.0) (2023-09-17)
2
24
 
3
25
 
@@ -8,15 +8,15 @@ module.exports = {
8
8
  "object": {
9
9
  "_id": "60145dc49f64ba1680b86693",
10
10
  "name": "index.html",
11
- "path": "/docs/mongodb/index.html",
11
+ "path": "/docs/mongodb",
12
+ "pathname": "/docs/mongodb/index.html",
12
13
  "src": "{{./docs/index.html}}",
13
14
  "host": [
14
15
  "*"
15
16
  ],
16
- "directory": "/docs/mongodb",
17
+ "directory": "mongodb",
17
18
  "content-type": "text/html",
18
- "public": "true",
19
- "website_id": "5ffbceb7f11d2d00103c4535"
19
+ "public": "true"
20
20
  }
21
21
  }
22
22
  ]
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cocreate/mongodb",
3
- "version": "1.9.0",
3
+ "version": "1.10.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",
@@ -45,7 +45,7 @@
45
45
  },
46
46
  "main": "./src/index.js",
47
47
  "dependencies": {
48
- "@cocreate/utils": "^1.24.0",
48
+ "@cocreate/utils": "^1.24.2",
49
49
  "mongodb": "^4.12.1"
50
50
  }
51
51
  }
package/src/index.js CHANGED
@@ -1,5 +1,5 @@
1
1
  const { MongoClient, ObjectId } = require('mongodb');
2
- const { dotNotationToObject, queryData, searchData, sortData } = require('@cocreate/utils')
2
+ const { dotNotationToObject, queryData, searchData, sortData, isValidDate } = require('@cocreate/utils')
3
3
  const clients = new Map()
4
4
 
5
5
 
@@ -249,7 +249,9 @@ function object(action, data) {
249
249
  data[type] = data.request
250
250
 
251
251
  if (!data['timeStamp'])
252
- data['timeStamp'] = new Date().toISOString()
252
+ data['timeStamp'] = new Date()
253
+ else
254
+ data['timeStamp'] = new Date(data['timeStamp'])
253
255
 
254
256
  let databases = data.database;
255
257
  if (!Array.isArray(databases))
@@ -265,20 +267,21 @@ function object(action, data) {
265
267
  const arrayObj = db.collection(array);
266
268
  const reference = { $storage: data.storageName, $database: database, $array: array }
267
269
 
270
+ if (!data[type])
271
+ data[type] = []
268
272
  if (data[type] && !Array.isArray(data[type]))
269
273
  data[type] = [data[type]]
270
274
 
271
275
  let isFilter
272
- if (data.$filter && data.$filter.query)
276
+ if (data.$filter)
273
277
  isFilter = true
278
+ if (isFilter && !data[type].length)
279
+ data[type] = [{}]
274
280
 
275
281
  let filter = await createFilter(data, arrayObj);
276
282
 
277
283
  let projections = {}, projection = {}, update = {}, options = {}
278
284
 
279
- if (isFilter && (!data[type] || !data[type].length))
280
- data[type] = [{}]
281
-
282
285
  if (action === 'updateObject')
283
286
  createUpdate(update, options, data, true)
284
287
 
@@ -294,7 +297,6 @@ function object(action, data) {
294
297
  if (action === 'createObject') {
295
298
  data[type][i] = replaceArray(data[type][i])
296
299
  data[type][i] = dotNotationToObject(data[type][i])
297
- data[type][i]._id = ObjectId(data[type][i]._id)
298
300
  data[type][i]['organization_id'] = data['organization_id'];
299
301
  data[type][i]['created'] = { on: data.timeStamp, by: data.user_id || data.clientId }
300
302
  } else if (action === 'readObject') {
@@ -305,25 +307,47 @@ function object(action, data) {
305
307
  createUpdate(update, options, data[type][i])
306
308
  }
307
309
 
308
- if (data[type][i]._id) {
309
- try {
310
- if (action !== 'createObject') {
311
- query._id = ObjectId(data[type][i]._id)
310
+ if (data[type][i]._id || action === 'createObject') {
311
+ if (action !== 'createObject') {
312
+ try {
313
+ query._id = new ObjectId(data[type][i]._id);
314
+ } catch (error) {
315
+ if (action === 'updateObject' && options.upsert) {
316
+ data[type][i]._id = ObjectId()
317
+ query._id = data[type][i]._id;
318
+ } else {
319
+ errorHandler(data, error, database, array)
320
+ continue;
321
+ }
312
322
  }
323
+ }
324
+
325
+ try {
313
326
 
314
327
  dataTransferedOut += getBytes({ query, update, projection, options })
315
328
 
316
329
  let result
317
330
  if (action === 'createObject') {
318
- let _id = data[type][i]._id.toString()
331
+ if (data[type][i]._id) {
332
+ try {
333
+ data[type][i]._id = new ObjectId(data[type][i]._id);
334
+ } catch (error) {
335
+ delete data[type][i]._id
336
+ }
337
+ }
338
+ result = await arrayObj.insertOne(data[type][i]);
319
339
  // TODO: type error occuring when pushing the item pushes but throws an error
320
- documents.push({ ...data[type][i], ...reference, _id })
340
+ data[type][i]._id = result.insertedId.toString()
341
+ documents.push({ ...data[type][i], ...reference })
321
342
  } else if (action === 'readObject') {
322
343
  result = await arrayObj.findOne(query, projection);
323
344
  result._id = result._id.toString()
324
345
  documents.push({ ...result, ...reference })
325
346
  } else if (action === 'updateObject') {
326
347
  result = await arrayObj.updateOne(query, update, options);
348
+
349
+ // TODO: handle upsert false and id does not exist
350
+ data[type][i]._id = query._id.toString()
327
351
  documents.push({ ...data[type][i], ...reference })
328
352
  } else if (action === 'deleteObject') {
329
353
  result = await arrayObj.deleteOne(query);
@@ -347,6 +371,7 @@ function object(action, data) {
347
371
  const cursor = arrayObj.find(query, projection).sort(sort).skip(index).limit(limit);
348
372
  if (!(await cursor.hasNext()) && action === 'updateObject' && data.upsert)
349
373
  document = { _id: ObjectId(data[type][i]._id) }
374
+
350
375
  while (await cursor.hasNext() || document) {
351
376
  if (!document)
352
377
  document = await cursor.next();
@@ -366,32 +391,52 @@ function object(action, data) {
366
391
 
367
392
  let result
368
393
  if (action === 'updateObject') {
369
- result = await arrayObj.updateOne({ _id: document._id }, update, options);
394
+ if (options.returnNewDocument) {
395
+ let object = await arrayObj.findOneAndUpdate({ _id: document._id }, update, options);
396
+ for (let key of Object.keys(object)) {
397
+ if (key === '_id')
398
+ continue
399
+ let newArrayKey = options.newArray[key]
400
+ // TODO: get index based on $operator
401
+ let index = object[key].length - 1
402
+ if (index >= 0)
403
+ data[type][i][newArrayKey.replace('[]', `[${index}]`)] = data[type][i][newArrayKey]
404
+ }
405
+ } else
406
+ result = await arrayObj.updateOne({ _id: document._id }, update, options);
407
+ // TODO: if update.$push or update.$each read document with projection to get index and update the keys [] to include index
370
408
  } else if (action === 'deleteObject') {
371
409
  result = await arrayObj.deleteOne({ _id: document._id });
372
410
  }
373
411
 
374
412
  dataTransferedIn += getBytes(result)
375
413
  documents.push({ ...data[type][i], ...reference, _id: document._id.toString() })
414
+ data[type].push({ ...data[type][i], ...reference, _id: document._id.toString() })
415
+
376
416
  }
377
417
  document = ''
378
418
  }
379
419
  } catch (error) {
380
420
  errorHandler(data, error, database, array)
381
421
  }
382
- }
383
- }
384
422
 
385
- if (action === 'createObject') {
386
- try {
387
- dataTransferedOut += getBytes(data[type])
388
- const result = await arrayObj.insertMany(data[type]);
389
- dataTransferedIn += getBytes(result)
390
- } catch (error) {
391
- errorHandler(data, error, database, array)
392
423
  }
393
424
  }
394
425
 
426
+ // if (action === 'createObject') {
427
+ // try {
428
+ // dataTransferedOut += getBytes(data[type])
429
+ // const result = await arrayObj.insertMany(data[type]);
430
+ // dataTransferedIn += getBytes(result)
431
+
432
+ // for (let i = 0; i < data[type].length; i++) {
433
+ // data[type][i]._id = data[type][i]._id.toString()
434
+ // }
435
+ // } catch (error) {
436
+ // errorHandler(data, error, database, array)
437
+ // }
438
+ // }
439
+
395
440
  }
396
441
  }
397
442
 
@@ -418,6 +463,7 @@ function createUpdate(update, options, data, isGlobal) {
418
463
  Object.keys(data).forEach(key => {
419
464
  if (isGlobal && !key.startsWith('$') || key === '_id')
420
465
  return
466
+ data[key] = isValidDate(data[key])
421
467
 
422
468
  let operator
423
469
  if (key.endsWith(']')) {
@@ -440,7 +486,20 @@ function createUpdate(update, options, data, isGlobal) {
440
486
  let originalKey = key
441
487
  key = key.replace(/\[(\d+)\]/g, '.$1');
442
488
 
443
- let operators = ['$rename', '$inc', '$push', '$splice', '$unset', '$delete', '$slice', '$pop']
489
+ if (originalKey.endsWith('[]')) {
490
+ if (!options.projection) {
491
+ options.projection = {}
492
+ options.arrayKey = {}
493
+ options.returnNewDocument = true
494
+ } else {
495
+ options.projection[key.replace(operator + '.', '')] = 1
496
+ options.arrayKey[key.replace(operator + '.', '')] = originalKey
497
+ }
498
+ if (!key.startsWith('$'))
499
+ operator = '$push'
500
+ }
501
+
502
+ let operators = ['$rename', '$inc', '$push', '$each', '$splice', '$unset', '$delete', '$slice', '$pop', '$shift', '$addToSet', '$pull', '$currentDate']
444
503
  if (!operators.includes(operator) && typeof index !== 'number') {
445
504
  if (!isGlobal)
446
505
  update['$set'][key] = data[originalKey]
@@ -467,21 +526,21 @@ function createUpdate(update, options, data, isGlobal) {
467
526
  key = arrayKey
468
527
  updates[key] = index || 1
469
528
  } else if (operator === '$addToSet' || operator === '$pull') {
470
- key = arrayKey
471
529
  updates[key] = data[originalKey]
472
- } else if (operator === '$push' || typeof index === 'number') {
473
- if (!Array.isArray(data[originalKey]))
474
- updates[key] = [data[originalKey]]
475
- else
476
- updates[key] = data[originalKey]
530
+ } else if (operator === '$push' || operator === '$each' || typeof index === 'number') {
531
+ updates[key] = data[originalKey]
532
+ if (typeof index === 'number' && index >= 0) {
533
+ if (operator === '$push')
534
+ updates[key] = [data[originalKey]]
477
535
 
478
- let insert = { $each: updates[key] }
479
- if (typeof index === 'number' && index >= 0)
536
+ let insert = { $each: updates[key] }
480
537
  insert.$postion = index
481
-
482
- updates[key] = insert
538
+ updates[key] = insert
539
+ }
483
540
  } else if (operator === '$inc') {
484
541
  updates[key] = data[originalKey]
542
+ } else if (operator === '$currentDate') {
543
+ updates[key] = data[originalKey]
485
544
  }
486
545
 
487
546
  if (!update[operator])
@@ -490,7 +549,7 @@ function createUpdate(update, options, data, isGlobal) {
490
549
  if (key === operator)
491
550
  update[operator] = { ...update[operator], ...replaceArray(updates[key]) }
492
551
  else
493
- update[operator][key.replace(operator, '')] = updates[key]
552
+ update[operator][key.replace(operator + '.', '')] = updates[key]
494
553
  })
495
554
  }
496
555
 
@@ -501,7 +560,6 @@ async function createFilter(data, arrayObj) {
501
560
  if (data.$filter.query)
502
561
  query = createQuery(data.$filter.query);
503
562
 
504
-
505
563
  if (data.$filter.sort) {
506
564
  for (let i = 0; i < data.$filter.sort.length; i++) {
507
565
  let direction = data.$filter.sort[i].direction
@@ -545,11 +603,17 @@ function createQuery(queries) {
545
603
 
546
604
  if (item.key == "_id") {
547
605
  if (item.value)
548
- item.value = ObjectId(item.value)
606
+ try {
607
+ item.value = ObjectId(item.value)
608
+ } catch (error) {
609
+ continue
610
+ }
549
611
  else
550
612
  continue
551
613
  }
552
614
 
615
+ item.value = isValidDate(item.value)
616
+
553
617
  let key = item.key;
554
618
  if (!query[key]) {
555
619
  query[key] = {};
@@ -631,7 +695,7 @@ function createProjection(data) {
631
695
  return projection;
632
696
  }
633
697
 
634
- function replaceArray(data) {
698
+ function replaceArray(data = {}) {
635
699
  let object = {}
636
700
 
637
701
  Object.keys(data).forEach((key) => {