@cocreate/mongodb 1.11.0 → 1.12.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.
package/CHANGELOG.md CHANGED
@@ -1,3 +1,26 @@
1
+ ## [1.12.1](https://github.com/CoCreate-app/CoCreate-mongodb/compare/v1.12.0...v1.12.1) (2023-11-12)
2
+
3
+
4
+ ### Bug Fixes
5
+
6
+ * bump dependencies for latest features ([704750a](https://github.com/CoCreate-app/CoCreate-mongodb/commit/704750af769d95e78535221ed9bf92a2aa20b1fd))
7
+ * removed limit = index + limit ([c085917](https://github.com/CoCreate-app/CoCreate-mongodb/commit/c085917d06c6017298b6c09f974015b22341a404))
8
+
9
+ # [1.12.0](https://github.com/CoCreate-app/CoCreate-mongodb/compare/v1.11.0...v1.12.0) (2023-11-09)
10
+
11
+
12
+ ### Bug Fixes
13
+
14
+ * $storage, $database, $array are arrays ([9cda13a](https://github.com/CoCreate-app/CoCreate-mongodb/commit/9cda13ad88eff2639da6b1c5a60dc901a54d0518))
15
+ * meta name typo ([c7c8e1d](https://github.com/CoCreate-app/CoCreate-mongodb/commit/c7c8e1d7182f5992a5a1567e2ff7edf678cefbde))
16
+ * update date queries ([3f8103d](https://github.com/CoCreate-app/CoCreate-mongodb/commit/3f8103db89f0037f566e930928742a20825e24cd))
17
+ * update method names ([936254d](https://github.com/CoCreate-app/CoCreate-mongodb/commit/936254d850536e55daaaf2d7862930a3068e8aee))
18
+
19
+
20
+ ### Features
21
+
22
+ * merge data[types] with same _id for type objects and name for all other types ([1da51a8](https://github.com/CoCreate-app/CoCreate-mongodb/commit/1da51a8c0600ecbe84af41fcc5c77f1949d36c60))
23
+
1
24
  # [1.11.0](https://github.com/CoCreate-app/CoCreate-mongodb/compare/v1.10.6...v1.11.0) (2023-11-03)
2
25
 
3
26
 
package/docs/index.html CHANGED
@@ -11,10 +11,10 @@
11
11
  sizes="32x32"
12
12
  href="https://cocreate.app/images/favicon.ico" />
13
13
  <meta
14
- key="description"
14
+ name="description"
15
15
  content="A simple HTML5 and pure javascript component. Easy configuration using data-attributes and highly styleable." />
16
16
  <meta
17
- key="keywords"
17
+ name="keywords"
18
18
  content="helper classes, utility classes, css framework, css library, inline style classes" />
19
19
  <meta name="robots" content="index,follow" />
20
20
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cocreate/mongodb",
3
- "version": "1.11.0",
3
+ "version": "1.12.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",
@@ -45,7 +45,7 @@
45
45
  },
46
46
  "main": "./src/index.js",
47
47
  "dependencies": {
48
- "@cocreate/utils": "^1.26.2",
48
+ "@cocreate/utils": "^1.27.1",
49
49
  "mongodb": "^4.12.1"
50
50
  }
51
51
  }
package/src/index.js CHANGED
@@ -27,17 +27,15 @@ async function dbClient(data) {
27
27
 
28
28
  function send(data) {
29
29
  let [type, method] = data.method.split('.')
30
- // let action = data.method.replace(/\.([a-z])/g, (_, match) => match.toUpperCase());
31
-
32
30
  if (type === 'database')
33
- return database(method + type.charAt(0).toUpperCase() + type.slice(1), data)
31
+ return database(method, data)
34
32
  if (type === 'array')
35
- return array(method + type.charAt(0).toUpperCase() + type.slice(1), data)
33
+ return array(method, data)
36
34
  if (type === 'object')
37
- return object(method + type.charAt(0).toUpperCase() + type.slice(1), data)
35
+ return object(method, data)
38
36
  }
39
37
 
40
- function database(action, data) {
38
+ function database(method, data) {
41
39
  return new Promise(async (resolve, reject) => {
42
40
  let type = 'database'
43
41
  let databaseArray = []
@@ -48,7 +46,7 @@ function database(action, data) {
48
46
  const client = await dbClient(data)
49
47
  if (!client || client.status === false)
50
48
  return data
51
- if (action == 'readDatabase') {
49
+ if (method == 'read') {
52
50
  const db = client.db().admin();
53
51
  // TODO: support if a database name is defined then return the database details and stats
54
52
  // else apply filter and return dbs for which each should have stats and details
@@ -73,7 +71,7 @@ function database(action, data) {
73
71
  resolve(createData(data, databaseArray, type))
74
72
  })
75
73
  }
76
- if (action == 'deleteDatabase') {
74
+ if (method == 'delete') {
77
75
  const db = client.db(data.database);
78
76
  db.dropDatabase().then(restult => {
79
77
  dataTransferedIn += getBytes(restult)
@@ -82,7 +80,7 @@ function database(action, data) {
82
80
  }
83
81
  } catch (error) {
84
82
  errorHandler(data, error)
85
- console.log(action, 'error', error);
83
+ console.log(method, 'error', error);
86
84
  resolve(data);
87
85
  }
88
86
 
@@ -91,7 +89,7 @@ function database(action, data) {
91
89
  });
92
90
  }
93
91
 
94
- function array(action, data) {
92
+ function array(method, data) {
95
93
  return new Promise(async (resolve, reject) => {
96
94
  let type = 'array'
97
95
  let arrayArray = [];
@@ -114,7 +112,7 @@ function array(action, data) {
114
112
  for (let database of databases) {
115
113
  const db = client.db(database);
116
114
 
117
- if (action == 'readCollection') {
115
+ if (method == 'read') {
118
116
 
119
117
  let { query, sort } = await createFilter(data);
120
118
 
@@ -144,7 +142,7 @@ function array(action, data) {
144
142
  } else {
145
143
  let arrays
146
144
  let value
147
- if (action == 'updateCollection')
145
+ if (method == 'update')
148
146
  arrays = Object.entries(data.array)
149
147
  else
150
148
  arrays = data.array;
@@ -155,7 +153,7 @@ function array(action, data) {
155
153
  let arraysLength = arrays.length
156
154
  for (let array of arrays) {
157
155
 
158
- if (action == 'createCollection') {
156
+ if (method == 'create') {
159
157
  dataTransferedOut += getBytes(array)
160
158
  db.createCollection(array, function (error, result) {
161
159
  if (error)
@@ -176,13 +174,13 @@ function array(action, data) {
176
174
  }
177
175
  })
178
176
  } else {
179
- if (action == 'updateCollection') {
177
+ if (method == 'update') {
180
178
  [array, value] = array
181
179
  }
182
180
 
183
181
  const arrayObj = db.collection(array);
184
182
 
185
- if (action == 'updateCollection') {
183
+ if (method == 'update') {
186
184
  dataTransferedOut += getBytes(result)
187
185
  arrayObj.rename(value, function (error, result) {
188
186
  if (error)
@@ -204,7 +202,7 @@ function array(action, data) {
204
202
  })
205
203
  }
206
204
 
207
- if (action == 'deleteCollection') {
205
+ if (method == 'delete') {
208
206
  arrayObj.drop(function (error, result) {
209
207
  if (error)
210
208
  errorHandler(data, error, database, array)
@@ -234,7 +232,7 @@ function array(action, data) {
234
232
 
235
233
  } catch (error) {
236
234
  errorHandler(data, error)
237
- console.log(action, 'error', error);
235
+ console.log(method, 'error', error);
238
236
  resolve(data);
239
237
  }
240
238
  }, (error) => {
@@ -242,7 +240,7 @@ function array(action, data) {
242
240
  });
243
241
  }
244
242
 
245
- function object(action, data) {
243
+ function object(method, data) {
246
244
  return new Promise(async (resolve, reject) => {
247
245
  try {
248
246
  const client = await dbClient(data)
@@ -255,13 +253,8 @@ function object(action, data) {
255
253
  let type = 'object'
256
254
  let documents = [];
257
255
 
258
- // if (data.request)
259
- // data[type] = data.request
260
-
261
256
  if (!data['timeStamp'])
262
- data['timeStamp'] = new Date()
263
- else
264
- data['timeStamp'] = new Date(data['timeStamp'])
257
+ data['timeStamp'] = new Date().toISOString();
265
258
 
266
259
  let databases = data.database;
267
260
  if (!Array.isArray(databases))
@@ -292,15 +285,22 @@ function object(action, data) {
292
285
 
293
286
  let projections = {}, projection = {}, update = {}, options = {}
294
287
 
295
- if (action === 'updateObject')
288
+ if (method === 'update')
296
289
  createUpdate(update, options, data, true)
297
290
 
298
291
  for (let i = 0; i < data[type].length; i++) {
292
+ let $storage = data[type][i].$storage || []
293
+ let $database = data[type][i].$database || []
294
+ let $array = data[type][i].$array || []
295
+ $storage.push(data.storageName)
296
+ $database.push(database)
297
+ $array.push(array)
298
+
299
299
  delete data[type][i].$storage
300
300
  delete data[type][i].$database
301
301
  delete data[type][i].$array
302
302
 
303
- if (action !== 'createObject' && data[type][i].$filter) {
303
+ if (method !== 'create' && data[type][i].$filter) {
304
304
  isFilter = true
305
305
  reference['$filter'] = data[type][i].$filter
306
306
  filter = await createFilter({ $filter: data[type][i].$filter }, arrayObj)
@@ -308,25 +308,29 @@ function object(action, data) {
308
308
 
309
309
  let { query, sort, index, limit } = filter
310
310
 
311
- if (action === 'createObject') {
311
+ if (method === 'create') {
312
312
  data[type][i] = replaceArray(data[type][i])
313
313
  data[type][i] = dotNotationToObject(data[type][i])
314
314
  data[type][i]['organization_id'] = data['organization_id'];
315
- data[type][i]['created'] = { on: data.timeStamp, by: data.user_id || data.clientId }
316
- } else if (action === 'readObject') {
315
+ data[type][i]['created'] = { on: new Date(data.timeStamp), by: data.user_id || data.clientId }
316
+ } else if (method === 'read') {
317
317
  projection = createProjection(data[type][i])
318
- } else if (action === 'updateObject') {
319
- data[type][i].modified = { on: data.timeStamp, by: data.user_id || data.clientId }
318
+ } else if (method === 'update') {
319
+ if (!data[type][i].modified)
320
+ data[type][i].modified = { on: new Date(data.timeStamp), by: data.user_id || data.clientId }
321
+ else
322
+ data[type][i].modified.on = new Date(data[type][i].modified.on)
323
+
320
324
  data[type][i].organization_id = data.organization_id
321
325
  createUpdate(update, options, data[type][i])
322
326
  }
323
327
 
324
- if (data[type][i]._id || action === 'createObject') {
325
- if (action !== 'createObject') {
328
+ if (data[type][i]._id || method === 'create') {
329
+ if (method !== 'create') {
326
330
  try {
327
331
  query._id = new ObjectId(data[type][i]._id);
328
332
  } catch (error) {
329
- if (action === 'updateObject' && options.upsert) {
333
+ if (method === 'update' && options.upsert) {
330
334
  data[type][i]._id = ObjectId()
331
335
  query._id = data[type][i]._id;
332
336
  } else {
@@ -341,7 +345,7 @@ function object(action, data) {
341
345
  dataTransferedOut += getBytes({ query, update, projection, options })
342
346
 
343
347
  let result
344
- if (action === 'createObject') {
348
+ if (method === 'create') {
345
349
  if (data[type][i]._id) {
346
350
  try {
347
351
  data[type][i]._id = new ObjectId(data[type][i]._id);
@@ -353,7 +357,7 @@ function object(action, data) {
353
357
  // TODO: type error occuring when pushing the item pushes but throws an error
354
358
  data[type][i]._id = result.insertedId.toString()
355
359
  // documents.push({ ...data[type][i], ...reference })
356
- } else if (action === 'readObject') {
360
+ } else if (method === 'read') {
357
361
  result = await arrayObj.findOne(query, projection);
358
362
  result._id = result._id.toString()
359
363
 
@@ -369,20 +373,20 @@ function object(action, data) {
369
373
  data[type][i] = { ...data[type][i], ...result }
370
374
  } else
371
375
  data[type][i] = { ...data[type][i], ...result }
372
- } else if (action === 'updateObject') {
376
+ } else if (method === 'update') {
373
377
  result = await arrayObj.updateOne(query, update, options);
374
378
 
375
379
  // TODO: handle upsert false and id does not exist
376
380
  data[type][i]._id = query._id.toString()
377
381
  // documents.push({ ...data[type][i], ...reference })
378
- } else if (action === 'deleteObject') {
382
+ } else if (method === 'delete') {
379
383
  result = await arrayObj.deleteOne(query);
380
384
  // documents.push({ ...reference, _id: data[type][i]._id })
381
385
  }
382
386
 
383
- data[type][i].$storage = 'mongodb'
384
- data[type][i].$database = database
385
- data[type][i].$array = array
387
+ data[type][i].$storage = $storage
388
+ data[type][i].$database = $database
389
+ data[type][i].$array = $array
386
390
 
387
391
  dataTransferedIn += getBytes(result)
388
392
 
@@ -391,16 +395,14 @@ function object(action, data) {
391
395
  }
392
396
  } else if (isFilter) {
393
397
  try {
394
- if (action === 'readObject')
398
+ if (method === 'read')
395
399
  projection = { ...projections, ...projection }
396
400
 
397
- delete query._id
398
-
399
401
  dataTransferedOut += getBytes({ query, projection, sort, index, limit })
400
- let document = ''
401
402
 
403
+ let document = ''
402
404
  const cursor = arrayObj.find(query, projection).sort(sort).skip(index).limit(limit);
403
- if (!(await cursor.hasNext()) && action === 'updateObject' && data.upsert)
405
+ if (!(await cursor.hasNext()) && method === 'update' && data.upsert)
404
406
  document = { _id: ObjectId(data[type][i]._id) }
405
407
 
406
408
  while (await cursor.hasNext() || document) {
@@ -415,7 +417,7 @@ function object(action, data) {
415
417
  continue;
416
418
  }
417
419
 
418
- if (action === 'readObject') {
420
+ if (method === 'read') {
419
421
  document._id = document._id.toString()
420
422
  let object = data[type].find(obj => obj._id && obj._id.toString() === document._id.toString())
421
423
  if (object) {
@@ -436,7 +438,7 @@ function object(action, data) {
436
438
  dataTransferedOut += getBytes({ _id: document._id, update, options })
437
439
 
438
440
  let result
439
- if (action === 'updateObject') {
441
+ if (method === 'update') {
440
442
  if (options.returnNewDocument) {
441
443
  let object = await arrayObj.findOneAndUpdate({ _id: document._id }, update, options);
442
444
  for (let key of Object.keys(object)) {
@@ -451,7 +453,7 @@ function object(action, data) {
451
453
  } else
452
454
  result = await arrayObj.updateOne({ _id: document._id }, update, options);
453
455
  // TODO: if update.$push or update.$each read document with projection to get index and update the keys [] to include index
454
- } else if (action === 'deleteObject') {
456
+ } else if (method === 'delete') {
455
457
  result = await arrayObj.deleteOne({ _id: document._id });
456
458
  }
457
459
 
@@ -469,7 +471,7 @@ function object(action, data) {
469
471
  }
470
472
  }
471
473
 
472
- // if (action === 'createObject') {
474
+ // if (method === 'create') {
473
475
  // try {
474
476
  // dataTransferedOut += getBytes(data[type])
475
477
  // const result = await arrayObj.insertMany(data[type]);
@@ -490,7 +492,7 @@ function object(action, data) {
490
492
 
491
493
  } catch (error) {
492
494
  errorHandler(data, error)
493
- console.log(action, 'error', error);
495
+ console.log(method, 'error', error);
494
496
  resolve(data);
495
497
  }
496
498
  }, (error) => {
@@ -508,7 +510,9 @@ function createUpdate(update, options, data, isGlobal) {
508
510
  Object.keys(data).forEach(key => {
509
511
  if (isGlobal && !key.startsWith('$') || key === '_id')
510
512
  return
511
- data[key] = isValidDate(data[key])
513
+
514
+ if (isValidDate(data[key]))
515
+ data[key] = new Date(data[key])
512
516
 
513
517
  let operator
514
518
  if (key.endsWith(']')) {
@@ -626,8 +630,6 @@ async function createFilter(data, arrayObj) {
626
630
  index = data.$filter.index
627
631
  if (data.$filter.limit)
628
632
  limit = data.$filter.limit
629
- if (limit)
630
- limit = index + limit;
631
633
  }
632
634
 
633
635
  if (data['organization_id'])
@@ -657,7 +659,8 @@ function createQuery(queries) {
657
659
  continue
658
660
  }
659
661
 
660
- item.value = isValidDate(item.value)
662
+ if (isValidDate(item.value))
663
+ item.value = new Date(item.value)
661
664
 
662
665
  let key = item.key;
663
666
  if (!query[key]) {
@@ -772,21 +775,27 @@ function createData(data, array, type, dataTransferedIn, dataTransferedOut) {
772
775
  data.isFilter = true
773
776
  }
774
777
 
775
- data[type].push(...array)
776
-
777
- // if (data.$filter && data.$filter.sort)
778
- // data[type] = sortData(data[type], data.$filter.sort)
779
-
780
778
  // if (!data.request)
781
779
  // data.request = data[type] || {}
782
780
 
783
- // data[type] = array
784
-
785
- // if (data.returnLog) {
786
- // if (!data.log)
787
- // data.log = []
788
- // data.log.push(...data[type])
789
- // }
781
+ let key = '_id'
782
+ if (type !== 'object')
783
+ key = 'name'
784
+ for (let i = 0; i < array.length; i++) {
785
+ const matchIndex = data[type].findIndex((item) => item[key] === array[i][key]);
786
+ if (matchIndex !== -1) {
787
+ data[type][matchIndex].$storage.push(array[i].$storage)
788
+ delete array[i].$storage
789
+ data[type][matchIndex].$database.push(array[i].$database)
790
+ delete array[i].$database
791
+ data[type][matchIndex].$array.push(array[i].$array)
792
+ delete array[i].$array
793
+
794
+ // TODO: compare dates and merge and and updates to keep all synced and up to date
795
+ data[type][matchIndex] = { ...data[type][matchIndex], ...array[i] };
796
+ } else
797
+ data[type].push(array[i])
798
+ }
790
799
 
791
800
  return data
792
801
  }