@cocreate/indexeddb 1.16.2 → 1.17.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,30 @@
1
+ ## [1.17.1](https://github.com/CoCreate-app/CoCreate-indexeddb/compare/v1.17.0...v1.17.1) (2023-11-09)
2
+
3
+
4
+ ### Bug Fixes
5
+
6
+ * $storage, $database, $array are arrays ([b6374f8](https://github.com/CoCreate-app/CoCreate-indexeddb/commit/b6374f84e4ff13751cc357bbba8d4c883380083f))
7
+ * meta name typo ([da33128](https://github.com/CoCreate-app/CoCreate-indexeddb/commit/da331289589ea151f78ce25942e18e133f6b87f1))
8
+ * update crud methods ([c74ed56](https://github.com/CoCreate-app/CoCreate-indexeddb/commit/c74ed5688bd9a25f4eb524064b799ad678bdcc57))
9
+ * updated date to ISO format with UTC timezone ([8bcc59a](https://github.com/CoCreate-app/CoCreate-indexeddb/commit/8bcc59a7d715937c6de139dcb12951a686aa1cff))
10
+
11
+ # [1.17.0](https://github.com/CoCreate-app/CoCreate-indexeddb/compare/v1.16.2...v1.17.0) (2023-11-03)
12
+
13
+
14
+ ### Bug Fixes
15
+
16
+ * favicon.ico path ([3ea941e](https://github.com/CoCreate-app/CoCreate-indexeddb/commit/3ea941e1e70879aa171ec3b98a81dc3769ec80f2))
17
+ * get.database updated to database.get ([b8058c6](https://github.com/CoCreate-app/CoCreate-indexeddb/commit/b8058c6bbe5682d9bc7f29775b3f306babcd5108))
18
+ * relocated $filter conditions to after index is open ([349f7ff](https://github.com/CoCreate-app/CoCreate-indexeddb/commit/349f7ff33b08ba0da77a313135523b6ccf3a9b41))
19
+ * transaction bug ([6016c07](https://github.com/CoCreate-app/CoCreate-indexeddb/commit/6016c07c56b8dda63281783e810f4747d6e59d80))
20
+ * update dependencies to the lates versions ([4bcf0b2](https://github.com/CoCreate-app/CoCreate-indexeddb/commit/4bcf0b223a73d8e73aea2083abf37c066f8c1d3d))
21
+ * update method to use object.update etc ([6b47872](https://github.com/CoCreate-app/CoCreate-indexeddb/commit/6b47872704e19f7ba9cab7b544dfbad77aeed377))
22
+
23
+
24
+ ### Features
25
+
26
+ * morph data[type] so the object can be synced across multiple storage, database, array ([8d3afb4](https://github.com/CoCreate-app/CoCreate-indexeddb/commit/8d3afb487ab4d4709c8bcc2ded6bf8d2ffaa0085))
27
+
1
28
  ## [1.16.2](https://github.com/CoCreate-app/CoCreate-indexeddb/compare/v1.16.1...v1.16.2) (2023-10-26)
2
29
 
3
30
 
package/demo/index.html CHANGED
@@ -6,9 +6,9 @@
6
6
  <meta name="viewport" content="width=device-width, initial-scale=1" />
7
7
  <link
8
8
  rel="icon"
9
- href="https://cdn.cocreate.app/favicon.ico"
10
- type="image/ico"
11
- sizes="16x16" />
9
+ type="image/png"
10
+ sizes="32x32"
11
+ href="../assets/favicon.ico" />
12
12
  <title>indexeddb | CoCreateJS</title>
13
13
  <link rel="manifest" href="/manifest.webmanifest" />
14
14
  </head>
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/indexeddb",
3
- "version": "1.16.2",
3
+ "version": "1.17.1",
4
4
  "description": "A simple indexeddb component in vanilla javascript. Easily configured using HTML5 data-attributes and/or JavaScript API.",
5
5
  "keywords": [
6
6
  "indexeddb",
@@ -58,6 +58,6 @@
58
58
  "webpack-log": "^3.0.1"
59
59
  },
60
60
  "dependencies": {
61
- "@cocreate/utils": "^1.26.1"
61
+ "@cocreate/utils": "^1.26.2"
62
62
  }
63
63
  }
package/src/index.js CHANGED
@@ -22,9 +22,8 @@
22
22
  * For details, visit <https://cocreate.app/licenses/> or contact us at sales@cocreate.app
23
23
  */
24
24
 
25
- import { ObjectId, dotNotationToObject, searchData, sortData, queryData, isValidDate } from '@cocreate/utils'
25
+ import { ObjectId, dotNotationToObject, searchData, sortData, queryData } from '@cocreate/utils'
26
26
 
27
- // let status = true;
28
27
  let indexedDbFunction
29
28
 
30
29
  function init() {
@@ -63,22 +62,15 @@ async function send(data) {
63
62
  try {
64
63
  let newData = [];
65
64
 
66
- let type = data.method.split('.');
67
- type = type[type.length - 1];
68
-
65
+ let type = data.method.split('.')[0];
69
66
  if (type === 'database') {
70
- if (data.method === 'get.database') {
67
+ if (data.method === 'database.get') {
71
68
  return await processDatabase(data)
72
69
  } else
73
70
  await processDatabase(data, newData, type)
74
71
  } else {
75
- if (data.request)
76
- data[type] = data.request
77
-
78
72
  if (!data['timeStamp'])
79
- data['timeStamp'] = new Date()
80
- else
81
- data['timeStamp'] = new Date(data['timeStamp'])
73
+ data['timeStamp'] = new Date().toISOString();
82
74
 
83
75
  let databases = data.database;
84
76
  if (!databases && data.organization_id)
@@ -117,28 +109,25 @@ const processDatabase = (data, newData, type) => {
117
109
  return new Promise((resolve, reject) => {
118
110
  if (!data)
119
111
  data = {}
120
- if (data.request)
121
- data[type] = data.request
122
-
123
- // if (!data['timeStamp'])
124
- data['timeStamp'] = new Date(data['timeStamp'])
112
+ if (!data[type])
113
+ data[type] = []
125
114
 
126
- if (data.method == 'read.database') {
115
+ if (data.method == 'database.read') {
127
116
  indexedDB.databases().then((databases) => {
128
117
  for (let database of databases) {
129
118
  if (data.$filter && data.$filter.query) {
130
119
  let isFilter = queryData(database, data.$filter.query)
131
120
  if (isFilter)
132
- newData.push({ $storage: 'indexeddb', ...database })
121
+ newData.push({ $storage: ['indexeddb'], ...database })
133
122
  } else
134
- newData.push({ $storage: 'indexeddb', ...database })
123
+ newData.push({ $storage: ['indexeddb'], ...database })
135
124
  }
136
125
 
137
126
  resolve()
138
127
  })
139
128
  } else {
140
129
  let databases
141
- if (data.method == 'update.database')
130
+ if (data.method == 'database.update')
142
131
  databases = Object.keys(data.database)
143
132
  else
144
133
  databases = data.database;
@@ -149,12 +138,12 @@ const processDatabase = (data, newData, type) => {
149
138
  for (let i = 0; i < databases.length; i++) {
150
139
  let openRequest, database = databases[i];
151
140
  switch (data.method) {
152
- case 'get.database':
153
- case 'create.database':
154
- case 'update.database':
141
+ case 'database.get':
142
+ case 'database.create':
143
+ case 'database.update':
155
144
  openRequest = indexedDB.open(database);
156
145
  break;
157
- case 'delete.database':
146
+ case 'database.delete':
158
147
  openRequest = indexedDB.deleteprocessDatabase(database);
159
148
  break;
160
149
  default:
@@ -165,7 +154,7 @@ const processDatabase = (data, newData, type) => {
165
154
  openRequest.onsuccess = function () {
166
155
  let db = openRequest.result;
167
156
 
168
- if (data.method == 'update.database') {
157
+ if (data.method == 'database.update') {
169
158
  let objectStoreNames = Array.from(db.objectStoreNames)
170
159
  for (let array of objectStoreNames) {
171
160
  let request = array.openCursor();
@@ -232,7 +221,7 @@ const processDatabase = (data, newData, type) => {
232
221
 
233
222
  request.onsuccess = function (event) {
234
223
  if (databases.length - 1 === i) {
235
- if (data.method == 'get.database')
224
+ if (data.method == 'database.get')
236
225
  resolve(event.target.result)
237
226
  else
238
227
  resolve(data)
@@ -242,7 +231,7 @@ const processDatabase = (data, newData, type) => {
242
231
  request.onerror = function (event) {
243
232
  errorHandler(data, event.target.error, database, array)
244
233
  if (databases.length - 1 === i) {
245
- if (data.method == 'get.database')
234
+ if (data.method == 'database.get')
246
235
  resolve(event.target.result)
247
236
  else
248
237
  resolve(data)
@@ -250,7 +239,7 @@ const processDatabase = (data, newData, type) => {
250
239
  };
251
240
 
252
241
  } else if (databases.length - 1 === i) {
253
- if (data.method == 'get.database')
242
+ if (data.method == 'database.get')
254
243
  resolve(db)
255
244
  else
256
245
  resolve(data)
@@ -271,10 +260,10 @@ const processDatabase = (data, newData, type) => {
271
260
  }
272
261
 
273
262
  async function processArray(data, newData, database, type) {
274
- let db = await processDatabase({ method: 'get.database', database })
263
+ let db = await processDatabase({ method: 'database.get', database })
275
264
 
276
265
  let objectStoreNames = Array.from(db.objectStoreNames)
277
- if (data.method == 'read.array') {
266
+ if (data.method == 'array.read') {
278
267
  if (!data[type])
279
268
  data[type] = []
280
269
 
@@ -282,19 +271,19 @@ async function processArray(data, newData, database, type) {
282
271
  if (data.$filter && data.$filter.query) {
283
272
  let isFilter = queryData({ name: objectStoreNames[i] }, data.$filter.query)
284
273
  if (isFilter)
285
- newData.push({ name: objectStoreNames, $storage: 'indexeddb', $database: database })
274
+ newData.push({ name: objectStoreNames, $storage: ['indexeddb'], $database: [database] })
286
275
  } else
287
- newData.push({ name: objectStoreNames[i], $storage: 'indexeddb', $database: database })
276
+ newData.push({ name: objectStoreNames[i], $storage: ['indexeddb'], $database: [database] })
288
277
  }
289
278
  } else {
290
279
  db.close()
291
280
 
292
- let request = await processDatabase({ method: 'get.database', database, upgrade: true })
281
+ let request = await processDatabase({ method: 'database.get', database, upgrade: true })
293
282
  await new Promise((resolve) => {
294
283
  request.onupgradeneeded = function (event) {
295
284
  let db = event.target.result
296
285
  let arrays
297
- if (data.method == 'update.array')
286
+ if (data.method == 'array.update')
298
287
  arrays = Object.keys(data[type])
299
288
  else
300
289
  arrays = data[type];
@@ -305,20 +294,20 @@ async function processArray(data, newData, database, type) {
305
294
  let error, array = arrays[i]
306
295
 
307
296
  let arrayExist = db.objectStoreNames.contains(array)
308
- if (arrayExist && data.method == 'create.array') {
297
+ if (arrayExist && data.method == 'array.create') {
309
298
  error = 'array already exists'
310
- } else if (data.method == 'delete.array') {
299
+ } else if (data.method == 'array.delete') {
311
300
  if (objectStoreNames.includes(array)) {
312
301
  db.deleteObjectStore(array);
313
302
  } else {
314
303
  error = 'the array does not exist'
315
304
  }
316
305
  } else {
317
- if (!arrayExist && data.method == 'update.array')
306
+ if (!arrayExist && data.method == 'array.update')
318
307
  array = data[type][arrays[i]]
319
308
 
320
309
  if (arrayExist) {
321
- if (data.method == 'update.array') {
310
+ if (data.method == 'array.update') {
322
311
  let transaction = event.target.transaction;
323
312
  let objectStore = transaction.objectStore(array);
324
313
 
@@ -327,12 +316,12 @@ async function processArray(data, newData, database, type) {
327
316
  else
328
317
  error = 'An array with the new name already exist'
329
318
  }
330
- } else if (data.method == 'update.array')
319
+ } else if (data.method == 'array.update')
331
320
  error = 'array does not exists'
332
321
  }
333
322
 
334
323
  if (!error)
335
- newData.push({ name: array, $storage: 'indexeddb', $database: database, array })
324
+ newData.push({ name: array, $storage: ['indexeddb'], $database: [database] })
336
325
  else
337
326
  errorHandler(data, error, database)
338
327
 
@@ -355,7 +344,7 @@ async function processArray(data, newData, database, type) {
355
344
  }
356
345
 
357
346
  async function processIndex(data, newData, database, array, type) {
358
- let db = await processDatabase({ method: 'get.database', database })
347
+ let db = await processDatabase({ method: 'database.get', database })
359
348
  let arrayExist = db.objectStoreNames.contains(array)
360
349
  if (arrayExist) {
361
350
  // ToDO: switch here to types
@@ -363,19 +352,19 @@ async function processIndex(data, newData, database, array, type) {
363
352
  let objectStore = transaction.objectStore(array);
364
353
  let indexNames = Array.from(objectStore.indexNames)
365
354
 
366
- if (data.method == 'read.index') {
355
+ if (data.method == 'index.read') {
367
356
  for (let i = 0; i < indexNames.length; i++) {
368
357
  let name = indexNames[i]
369
358
  if (data.$filter && data.$filter.query)
370
359
  if (!(queryData({ name }, data.$filter.query))) continue
371
360
 
372
- newData.push({ name, $storage: 'indexeddb', $database: database, $array: array })
361
+ newData.push({ name, $storage: ['indexeddb'], $database: [database], $array: [array] })
373
362
  }
374
363
  db.close()
375
364
  } else {
376
365
  db.close()
377
366
  let indexes
378
- if (data.method == 'update.index')
367
+ if (data.method == 'index.update')
379
368
  indexes = Object.keys(data[type])
380
369
  else
381
370
  indexes = data[type];
@@ -385,20 +374,20 @@ async function processIndex(data, newData, database, array, type) {
385
374
 
386
375
  for (let i = 0; i < indexes.length; i++) {
387
376
  let error, index = indexes[i]
388
- db = await processDatabase({ method: 'get.database', database, upgrade: true })
377
+ db = await processDatabase({ method: 'database.get', database, upgrade: true })
389
378
  let transaction = db.transaction;
390
379
  let objectStore = transaction.objectStore(array);
391
380
  let indexExist = indexNames.includes(index)
392
381
 
393
- if (data.method == 'create.index') {
382
+ if (data.method == 'index.create') {
394
383
  if (!indexExist)
395
384
  objectStore.createIndex(index, index, { unique: false })
396
385
  else
397
386
  error = 'index already exist'
398
387
  } else if (indexExist) {
399
- if (data.method == 'delete.index')
388
+ if (data.method == 'index.delete')
400
389
  objectStore.deleteIndex(index)
401
- else if (data.method == 'update.index' && !indexNames.includes(data[type][indexes[i]])) {
390
+ else if (data.method == 'index.update' && !indexNames.includes(data[type][indexes[i]])) {
402
391
  if (!indexNames.includes(data[type][indexes[i]])) {
403
392
  let indexObj = objectStore.index(index);
404
393
  index = indexObj.name = data[type][indexes[i]];
@@ -406,10 +395,10 @@ async function processIndex(data, newData, database, array, type) {
406
395
  error = 'new index name already exist'
407
396
  }
408
397
  } else
409
- error = 'index does not exist'
398
+ error = 'index does not exist';
410
399
 
411
400
  if (!error)
412
- newData.push({ name: index, $storage: 'indexeddb', $database: database, $array: array })
401
+ newData.push({ name: index, $storage: ['indexeddb'], $database: [database], $array: [array] })
413
402
  else
414
403
  errorHandler(data, error, database, array)
415
404
 
@@ -420,18 +409,23 @@ async function processIndex(data, newData, database, array, type) {
420
409
  }
421
410
 
422
411
  async function processObject(data, newData, database, array, type) {
423
- let db = await processDatabase({ method: 'get.database', database })
412
+ let db = await processDatabase({ method: 'database.get', database })
424
413
 
425
414
  try {
415
+ if (!data[type])
416
+ data[type] = []
417
+ if (data[type] && !Array.isArray(data[type]))
418
+ data[type] = [data[type]]
419
+
426
420
  let isFilter
427
421
  if (data.$filter && data.$filter.query)
428
422
  isFilter = true
429
423
 
430
424
  let arrayExist = db.objectStoreNames.contains(array)
431
425
  if (!arrayExist) {
432
- if (data.method == 'create.object' || data.method == 'update.object') {
433
- db.close()
434
- db = await processDatabase({ method: 'get.database', database, array })
426
+ db.close()
427
+ if (data.method == 'object.create' || data.method == 'object.update') {
428
+ db = await processDatabase({ method: 'database.get', database, array })
435
429
  } else {
436
430
  return errorHandler(data, "array does not exist", database, array)
437
431
  }
@@ -440,16 +434,13 @@ async function processObject(data, newData, database, array, type) {
440
434
  if (!array || !db)
441
435
  throw new Error({ error: "This is an error message.", db, array });
442
436
 
443
- if (!data[type])
444
- data[type] = []
445
- if (data[type] && !Array.isArray(data[type]))
446
- data[type] = [data[type]]
447
437
 
448
- if (isFilter && !data[type].length)
449
- data[type] = [{}]
438
+ if ((isFilter && !data[type].length) || data.isFilter)
439
+ data[type] = [{ isFilter: 'isEmptyObjectFilter' }];
440
+ // data[type].splice(0, 0, { isFilter: 'isEmptyObjectFilter' });
450
441
 
451
- let transactionType = "readwrite" //test
452
- if (data.method == 'read.object')
442
+ let transactionType = "readwrite"
443
+ if (data.method == 'object.read')
453
444
  transactionType = "readonly"
454
445
 
455
446
  let transaction = db.transaction([array], transactionType);
@@ -458,55 +449,45 @@ async function processObject(data, newData, database, array, type) {
458
449
  let globalOperators = getGlobalOperators(data)
459
450
  let upsert = data.upsert
460
451
 
461
- if (data.$filter) {
462
- let count = objectStore.count();
463
- count.onsuccess = function () {
464
- data.$filter.count = count.result
465
- }
466
- }
467
-
468
452
  for (let i = 0; i < data[type].length; i++) {
453
+ const reference = { $storage: ['indexeddb'], $database: [database], $array: [array] }
454
+
469
455
  delete data[type][i].$storage
470
456
  delete data[type][i].$database
471
457
  delete data[type][i].$array
472
458
 
473
- if (data.method == 'create.object') {
459
+ if (data.method == 'object.create') {
474
460
  if (data.organization_id)
475
461
  data[type][i]['organization_id'] = data.organization_id
476
462
 
477
463
  if (!data[type][i]._id)
478
464
  data[type][i]['_id'] = ObjectId().toString()
479
465
  data[type][i] = dotNotationToObject(data[type][i])
466
+
467
+ // TODO: user_id || clientId shohould be retrieved from CoCreate
480
468
  data[type][i]['created'] = { on: data.timeStamp, by: data.user_id || data.clientId }
469
+ data[type][i] = await add(objectStore, data[type][i])
470
+ data[type][i] = { ...reference, ...data[type][i] }
481
471
 
482
- let result = await add(objectStore, data[type][i])
483
- newData.push({ $storage: 'indexeddb', $database: database, $array: array, ...result })
484
472
  } else {
485
473
  if (data[type][i].$filter)
486
474
  isFilter = true
487
- // if (data.organization_id)
488
- // data[type][i]['organization_id'] = data.organization_id
489
475
 
490
- if (data.method == 'update.object') {
476
+ if (data.method == 'object.update') {
491
477
  if (data.organization_id)
492
478
  data[type][i]['organization_id'] = data.organization_id
493
479
 
494
480
  if (data[type][i].$upsert)
495
481
  upsert = data[type][i].$upsert
496
- data[type][i]['modified'] = { on: data.timeStamp, by: data.user_id || data.clientId }
482
+ // TODO: user_id || clientId shohould be retrieved from CoCreate
483
+ if (!data[type][i].modified)
484
+ data[type][i].modified = { on: data.timeStamp, by: data.user_id || data.clientId }
497
485
  }
498
486
 
499
487
  let index = 0, limit, range, direction
500
488
  if (data[type][i]._id)
501
489
  range = IDBKeyRange.only(data[type][i]._id);
502
490
  else if (isFilter) {
503
- if (data.object.$filter) {
504
- let count = objectStore.count();
505
- count.onsuccess = function () {
506
- data.object.$filter.count = count.result
507
- }
508
- }
509
-
510
491
  let isIndex = false
511
492
  let indexName, direction;
512
493
 
@@ -545,26 +526,43 @@ async function processObject(data, newData, database, array, type) {
545
526
  let indexExist = indexNames.includes(indexName)
546
527
  if (!indexExist) {
547
528
  db.close()
548
- db = await processDatabase({ method: 'get.database', database, array, indexName, upgrade: true })
549
- objectStore = db.transaction(array, 'readwrite');
529
+ db = await processDatabase({ method: 'database.get', database, array, indexName })
530
+ transaction = db.transaction(array, 'readwrite');
550
531
  objectStore = transaction.objectStore(array);
551
532
  }
552
533
  objectStore = objectStore.index(indexName);
553
534
  }
554
535
 
536
+ let count = objectStore.count();
537
+ count.onsuccess = function () {
538
+ if (data.$filter)
539
+ data.$filter.count = count.result
540
+ else if (data.object.$filter)
541
+ data.object.$filter.count = count.result
542
+ }
543
+
555
544
  if (data.$filter) {
556
545
  if (data.$filter.index)
557
546
  index = data.$filter.index
558
547
  if (data.$filter.limit)
559
548
  limit = data.$filter.limit
560
- if (limit)
561
- limit = (index || 0) + limit;
562
549
  }
563
- } else if (data.method == 'delete.object' || data.method == 'update.object' && !range && !upsert && !isFilter) {
550
+
551
+ if (data[type][i].$filter) {
552
+ if (data[type][i].$filter.index)
553
+ index = data[type][i].$filter.index
554
+ if (data[type][i].$filter.limit)
555
+ limit = data[type][i].$filter.limit
556
+ }
557
+
558
+ if (limit)
559
+ limit = (index || 0) + limit;
560
+
561
+ } else if (data.method == 'object.delete' || data.method == 'object.update' && !range && !upsert && !isFilter) {
564
562
  continue
565
563
  }
566
564
 
567
- await openCursor(objectStore, range, direction, data, newData, isFilter, limit, database, array, upsert, type, i, globalOperators);
565
+ await openCursor(objectStore, range, direction, data, newData, isFilter, limit, database, array, upsert, type, i, globalOperators, reference);
568
566
 
569
567
  if (index)
570
568
  newData = newData.slice(index)
@@ -583,22 +581,22 @@ async function processObject(data, newData, database, array, type) {
583
581
  }
584
582
  }
585
583
 
586
- function openCursor(objectStore, range, direction, data, newData, isFilter, limit, database, array, upsert, type, i, globalOperators) {
584
+ function openCursor(objectStore, range, direction, data, newData, isFilter, limit, database, array, upsert, type, i, globalOperators, reference) {
587
585
  return new Promise(async (resolve, reject) => {
588
586
  const request = objectStore.openCursor(range || null, direction);// next, prev
589
587
 
590
- let hasUpdated
588
+ let hasUpdated, matchedLength = 0
591
589
  request.onsuccess = async () => {
592
590
  let cursor = request.result
593
591
 
594
- if (data.method === 'create.object'
595
- || data.method === 'update.object'
592
+ if (data.method === 'object.create'
593
+ || data.method === 'object.update'
596
594
  && (!range && !isFilter && upsert || !cursor && upsert && !hasUpdated)) {
597
595
  let isMatch = true
598
596
  if (isFilter) {
599
597
  if (cursor)
600
598
  isMatch = filter(objectStore, data[type][i], data[type][i], cursor.value)
601
- else if (data.method === 'update.object' && !upsert)
599
+ else if (data.method === 'object.update' && !upsert)
602
600
  isMatch = false
603
601
  }
604
602
 
@@ -610,20 +608,17 @@ function openCursor(objectStore, range, direction, data, newData, isFilter, limi
610
608
  }
611
609
 
612
610
  let result
613
- if (data.method == 'create.object') {
614
- result = await add(objectStore, data[type][i])
615
- } else if (data.method == 'update.object') {
616
- let update
617
- if (cursor)
618
- update = createUpdate(cursor.value, data[type][i], globalOperators)
619
- else
620
- update = createUpdate({}, data[type][i], globalOperators)
611
+ if (data.method == 'object.create') {
612
+ data[type][i] = await add(objectStore, data[type][i])
613
+ } else if (data.method == 'object.update') {
614
+ let update = createUpdate((cursor && cursor.value) ? cursor.value : {}, data[type][i], globalOperators)
621
615
  if (update)
622
- result = await put(objectStore, update)
616
+ data[type][i] = await put(objectStore, update)
623
617
  }
618
+ data[type][i] = { ...reference, ...data[type][i] }
619
+
620
+ matchedLength++
624
621
 
625
- if (result)
626
- newData.push({ $storage: 'indexeddb', $database: database, $array: array, ...result })
627
622
  }
628
623
  resolve();
629
624
  } else if (cursor) {
@@ -634,7 +629,7 @@ function openCursor(objectStore, range, direction, data, newData, isFilter, limi
634
629
  if (isMatch !== false) {
635
630
  let result = cursor.value
636
631
 
637
- if (data.method == 'update.object') {
632
+ if (data.method == 'object.update') {
638
633
  let update = createUpdate(cursor.value, data[type][i], globalOperators)
639
634
  if (update)
640
635
  result = await put(objectStore, update)
@@ -643,15 +638,25 @@ function openCursor(objectStore, range, direction, data, newData, isFilter, limi
643
638
  // TODO: if update.$<operator>.someKey[] requires the index of inerted item added to the field name. update.$<operator>.someKey[<index>]
644
639
  // TODO: if $addToSet get field name and item if it does not exist.
645
640
  // TODO: if $pull get field name and find if item exist and delete.
646
- } else if (data.method == 'delete.object') {
641
+ } else if (data.method == 'object.delete') {
647
642
  result = cursor.value
648
643
  cursor.delete()
649
644
  }
650
645
 
651
- newData.push({ $storage: 'indexeddb', $database: database, $array: array, ...result })
646
+ if (data[type][i]._id)
647
+ data[type][i] = { ...reference, ...result }
648
+ else {
649
+ let object = data[type].find(obj => obj._id && obj._id === result._id)
650
+ if (object)
651
+ object = { ...reference, ...object, ...result }
652
+ else
653
+ newData.push({ ...reference, ...result })
654
+ }
655
+
656
+ matchedLength++
652
657
  }
653
658
 
654
- if (!limit || limit && limit < newData.length) {
659
+ if (!limit || limit && (limit < newData.length || limit < matchedLength)) {
655
660
  cursor.continue();
656
661
  } else if (cursor.close) {
657
662
  cursor.close()
@@ -659,6 +664,7 @@ function openCursor(objectStore, range, direction, data, newData, isFilter, limi
659
664
  } else {
660
665
  resolve();
661
666
  }
667
+
662
668
  } else {
663
669
  resolve();
664
670
  }
@@ -711,15 +717,6 @@ function filter(objectStore, data, object, result) {
711
717
  let isMatch = true;
712
718
  let filter = data.$filter || object.$filter
713
719
  if (filter) {
714
- try {
715
- let count = objectStore.count();
716
- count.onsuccess = function () {
717
- filter.count = count.result
718
- }
719
- } catch (error) {
720
- errorHandler(data, error, database, array)
721
- }
722
-
723
720
  if (filter.query)
724
721
  isMatch = queryData(result, filter.query);
725
722
  if (isMatch && filter.search)
@@ -781,7 +778,6 @@ function createUpdate(update, data, globalOpertors) {
781
778
  function dotNotationToObjectUpdate(data, object = {}) {
782
779
  try {
783
780
  for (const key of Object.keys(data)) {
784
- let value = isValidDate(data[key])
785
781
  let newObject = object
786
782
  let oldObject = new Object(newObject)
787
783
  let keys = key.replace(/\[(\d+)\]/g, '.$1').split('.');
@@ -854,18 +850,17 @@ function dotNotationToObjectUpdate(data, object = {}) {
854
850
  }
855
851
 
856
852
  function createData(data, newData, type) {
857
- data.request = data[type] || {}
853
+ // data.request = data[type] || {}
854
+ if (data[type] && data[type][0] && data[type][0].isFilter === 'isEmptyObjectFilter') {
855
+ data[type].shift()
856
+ data.isFilter = true
857
+ }
858
+
859
+ data[type].push(...newData)
858
860
 
859
861
  if (data.$filter && data.$filter.sort)
860
- data[type] = sortData(newData, data.$filter.sort)
861
- else
862
- data[type] = newData
862
+ data[type] = sortData(data[type], data.$filter.sort)
863
863
 
864
- if (data.returnLog) {
865
- if (!data.log)
866
- data.log = []
867
- data.log.push(...data[type])
868
- }
869
864
 
870
865
  return data
871
866
  }
@@ -1,37 +1,37 @@
1
- async function testCollections() {
1
+ async function testArrays() {
2
2
  if (!('indexedDB' in window)) {
3
3
  console.log("This browser doesn't support IndexedDB.");
4
4
  return;
5
5
  } else {
6
6
 
7
- let createCollection = await CoCreate.indexeddb.process({
8
- method: 'create.array',
9
- database: ['testDB', 'testDB1', 'testDB2'],
10
- array: ['testCollection', 'testCollection1', 'testCollection2']
7
+ let createObject = await Coindex.createeddb.process({
8
+ method: 'array.create',
9
+ database: ['testDatabase', 'testDatabase1', 'testDatabase2'],
10
+ array: ['testArray', 'testArray1', 'testArray2']
11
11
 
12
12
  })
13
- console.log('createCollection', createCollection)
13
+ console.log('createObject', createObject)
14
14
 
15
- let readCollection = await CoCreate.indexeddb.process({
16
- method: 'read.array',
17
- database: ['testDB', 'testDB1', 'testDB2'],
15
+ let readObject = await Coindex.createeddb.process({
16
+ method: 'array.read',
17
+ database: ['testDatabase', 'testDatabase1', 'testDatabase2'],
18
18
  })
19
- console.log('readCollection', readCollection)
19
+ console.log('readObject', readObject)
20
20
 
21
- let updateCollection = await CoCreate.indexeddb.process({
22
- method: 'update.array',
23
- database: ['testDB', 'testDB1', 'testDB2'],
24
- array: { testCollection: 'testCollectionA' }
21
+ let updateObject = await Coindex.createeddb.process({
22
+ method: 'array.update',
23
+ database: ['testDatabase', 'testDatabase1', 'testDatabase2'],
24
+ array: { testArray: 'testArrayA' }
25
25
  })
26
- console.log('updateCollection', updateCollection)
26
+ console.log('updateObject', updateObject)
27
27
 
28
28
 
29
- let deleteCollection = await CoCreate.indexeddb.process({
30
- method: 'delete.array',
31
- database: ['testDB', 'testDB1', 'testDB2'],
32
- array: ['testCollectionA', 'testCollection1', 'testCollection2']
29
+ let deleteObject = await Coindex.createeddb.process({
30
+ method: 'array.delete',
31
+ database: ['testDatabase', 'testDatabase1', 'testDatabase2'],
32
+ array: ['testArrayA', 'testArray1', 'testArray2']
33
33
  })
34
- console.log('deleteCollection', deleteCollection)
34
+ console.log('deleteObject', deleteObject)
35
35
 
36
36
  }
37
37
  }
package/test/databases.js CHANGED
@@ -3,27 +3,27 @@ async function testDatabases() {
3
3
  console.log("This browser doesn't support IndexedDB.");
4
4
  return;
5
5
  } else {
6
- let createDatabase = await CoCreate.indexeddb.process({
7
- method: 'create.database',
6
+ let createDatabase = await Coindex.createeddb.process({
7
+ method: 'database.create',
8
8
  database: 'testDB',
9
9
  })
10
10
  console.log('createDatabase', createDatabase)
11
11
 
12
- let readDatabase = await CoCreate.indexeddb.process({
13
- method: 'read.database',
12
+ let readDatabase = await Coindex.createeddb.process({
13
+ method: 'database.read',
14
14
  database: 'testDB',
15
15
  })
16
16
  console.log('readDatabase', readDatabase)
17
17
 
18
- let updateDatabase = await CoCreate.indexeddb.process({
19
- method: 'update.database',
18
+ let updateDatabase = await Coindex.createeddb.process({
19
+ method: 'database.update',
20
20
  database: { testDB: 'testDatabase1' }
21
21
  })
22
22
  console.log('updateDatabase', updateDatabase)
23
23
 
24
24
 
25
- let deleteDatabase = await CoCreate.indexeddb.process({
26
- method: 'delete.database',
25
+ let deleteDatabase = await Coindex.createeddb.process({
26
+ method: 'database.delete',
27
27
  database: ['testDB', 'testDB1', 'testDB2'],
28
28
  })
29
29
  console.log('deleteDatabase', deleteDatabase)
package/test/documents.js CHANGED
@@ -1,13 +1,13 @@
1
1
  async function testDocuments() {
2
- if (!('indexedDB' in window)) {
3
- console.log("This browser doesn't support IndexedDB.");
2
+ if (!('indexedDatabase' in window)) {
3
+ console.log("This browser doesn't support IndexedDatabase.");
4
4
  return;
5
5
  } else {
6
6
 
7
- let createDocument = await CoCreate.indexeddb.process({
8
- method: 'create.object',
9
- database: ['testDB', 'testDB1', 'testDB2'],
10
- array: ['testCollection', 'testCollection1', 'testCollection2'],
7
+ let createObject = await Coindex.createeddb.process({
8
+ method: 'object.create',
9
+ database: ['testDatabase', 'testDatabase1', 'testDatabase2'],
10
+ array: ['testArray', 'testArray1', 'testArray2'],
11
11
  object: {
12
12
  organization_id: '5ff747727005da1c272740ab',
13
13
  'books.action.title': 'matr',
@@ -15,24 +15,24 @@ async function testDocuments() {
15
15
  }
16
16
 
17
17
  })
18
- console.log('createDocument', createDocument)
19
- let _id = createDocument.object[0]._id
18
+ console.log('createObject', createObject)
19
+ let _id = createObject.object[0]._id
20
20
 
21
- let readDocument = await CoCreate.indexeddb.process({
22
- method: 'read.object',
23
- database: ['testDB', 'testDB1', 'testDB2'],
24
- array: ['testCollection', 'testCollection1', 'testCollection2'],
21
+ let readObject = await Coindex.createeddb.process({
22
+ method: 'object.read',
23
+ database: ['testDatabase', 'testDatabase1', 'testDatabase2'],
24
+ array: ['testArray', 'testArray1', 'testArray2'],
25
25
  object: {
26
26
  _id,
27
27
  }
28
28
 
29
29
  })
30
- console.log('readDocument', readDocument)
30
+ console.log('readObject', readObject)
31
31
 
32
- let updateDocument = await CoCreate.indexeddb.process({
33
- method: 'update.object',
34
- database: ['testDB', 'testDB1', 'testDB2'],
35
- array: ['testCollection', 'testCollection1', 'testCollection2'],
32
+ let updateObject = await Coindex.createeddb.process({
33
+ method: 'object.update',
34
+ database: ['testDatabase', 'testDatabase1', 'testDatabase2'],
35
+ array: ['testArray', 'testArray1', 'testArray2'],
36
36
  object: {
37
37
  _id,
38
38
  'books.action.title': 'matr',
@@ -40,13 +40,13 @@ async function testDocuments() {
40
40
  }
41
41
 
42
42
  })
43
- console.log('updateDocument', updateDocument)
43
+ console.log('updateObject', updateObject)
44
44
 
45
45
 
46
- let deleteDocument = await CoCreate.indexeddb.process({
47
- method: 'delete.object',
48
- database: ['testDB', 'testDB1', 'testDB2'],
49
- array: ['testCollection', 'testCollection1', 'testCollection2'],
46
+ let deleteObject = await Coindex.createeddb.process({
47
+ method: 'object.delete',
48
+ database: ['testDatabase', 'testDatabase1', 'testDatabase2'],
49
+ array: ['testArray', 'testArray1', 'testArray2'],
50
50
  // object: {
51
51
  // _id
52
52
  // }
@@ -58,10 +58,10 @@ async function testDocuments() {
58
58
 
59
59
 
60
60
  })
61
- console.log('deleteDocument', deleteDocument)
61
+ console.log('deleteObject', deleteObject)
62
62
 
63
- // let deleteDatabase = await CoCreate.indexeddb.deleteDatabase({
64
- // database: ['testDB', 'testDB1', 'testDB2'],
63
+ // let deleteDatabase = await Coindex.createeddb.deleteDatabase({
64
+ // database: ['testDatabase', 'testDatabase1', 'testDatabase2'],
65
65
  // })
66
66
  // console.log('deleteDatabase', deleteDatabase)
67
67
  }
package/test/index.html CHANGED
@@ -6,9 +6,9 @@
6
6
  <meta name="viewport" content="width=device-width, initial-scale=1" />
7
7
  <link
8
8
  rel="icon"
9
- href="https://cdn.cocreate.app/favicon.ico"
10
- type="image/ico"
11
- sizes="16x16" />
9
+ type="image/png"
10
+ sizes="32x32"
11
+ href="../assets/favicon.ico" />
12
12
  <title>indexeddb | CoCreateJS</title>
13
13
  <link rel="manifest" href="/manifest.webmanifest" />
14
14
  </head>
package/test/indexes.js CHANGED
@@ -3,7 +3,7 @@ async function testIndexes() {
3
3
  console.log("This browser doesn't support IndexedDB.");
4
4
  return;
5
5
  } else {
6
- let createIndexes = await CoCreate.indexeddb.createIndex({
6
+ let createIndexes = await Coindex.createeddb.createIndex({
7
7
  database: ['testDB', 'testDB1', 'testDB2'],
8
8
  array: ['testCollection', 'testCollection1', 'testCollection2'],
9
9
  index: 'testIndexes'
@@ -11,20 +11,20 @@ async function testIndexes() {
11
11
  })
12
12
  console.log('createIndexes', createIndexes)
13
13
 
14
- let readIndexes = await CoCreate.indexeddb.readIndex({
14
+ let readIndexes = await Coindex.createeddb.readIndex({
15
15
  database: ['testDB', 'testDB1', 'testDB2'],
16
16
  array: ['testCollection', 'testCollection1', 'testCollection2']
17
17
  })
18
18
  console.log('readIndexes', readIndexes)
19
19
 
20
- let updateIndexes = await CoCreate.indexeddb.updateIndex({
20
+ let updateIndexes = await Coindex.createeddb.updateIndex({
21
21
  database: ['testDB', 'testDB1', 'testDB2'],
22
22
  array: ['testCollection', 'testCollection1', 'testCollection2'],
23
23
  index: { testIndexes: 'testIndexes1' }
24
24
  })
25
25
  console.log('updateIndexes', updateIndexes)
26
26
 
27
- let deleteIndexes = await CoCreate.indexeddb.deleteIndex({
27
+ let deleteIndexes = await Coindex.createeddb.deleteIndex({
28
28
  database: ['testDB', 'testDB1', 'testDB2'],
29
29
  array: ['testCollection', 'testCollection1', 'testCollection2'],
30
30
  index: 'testIndexes1'