@cocreate/indexeddb 1.9.2 → 1.9.3

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 +7 -0
  2. package/package.json +1 -1
  3. package/src/index.js +247 -244
package/CHANGELOG.md CHANGED
@@ -1,3 +1,10 @@
1
+ ## [1.9.3](https://github.com/CoCreate-app/CoCreate-indexeddb/compare/v1.9.2...v1.9.3) (2023-05-19)
2
+
3
+
4
+ ### Bug Fixes
5
+
6
+ * modules renamed to actions ([ec7dfd3](https://github.com/CoCreate-app/CoCreate-indexeddb/commit/ec7dfd3401bd97c09f7a0d6779e3293d90f49998))
7
+
1
8
  ## [1.9.2](https://github.com/CoCreate-app/CoCreate-indexeddb/compare/v1.9.1...v1.9.2) (2023-05-10)
2
9
 
3
10
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cocreate/indexeddb",
3
- "version": "1.9.2",
3
+ "version": "1.9.3",
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",
package/src/index.js CHANGED
@@ -4,12 +4,12 @@
4
4
  *
5
5
  * SPDX-License-Identifier: MIT
6
6
  ********************************************************************************/
7
- import {ObjectId, dotNotationToObject, searchData, sortData, queryData} from '@cocreate/utils'
7
+ import { ObjectId, dotNotationToObject, searchData, sortData, queryData } from '@cocreate/utils'
8
8
  import uuid from '@cocreate/uuid'
9
9
 
10
10
  let status = true;
11
11
 
12
- function createDatabase(data){
12
+ function createDatabase(data) {
13
13
  return Database('createDatabase', data)
14
14
  }
15
15
 
@@ -59,18 +59,18 @@ const Database = (action, data) => {
59
59
  if (action == 'updateDatabase')
60
60
  databases = Object.keys(data.database).map((key) => [key, data.database[key]])
61
61
  else
62
- databases = data.database;
63
-
62
+ databases = data.database;
63
+
64
64
  if (!Array.isArray(databases))
65
65
  databases = [databases]
66
-
66
+
67
67
  let databasesLength = databases.length
68
68
  for (let database of databases) {
69
69
  if (action == 'updateDatabase')
70
70
  [database, value] = database
71
71
 
72
72
  let openRequest;
73
- switch(action) {
73
+ switch (action) {
74
74
  case 'getDatabase':
75
75
  case 'createDatabase':
76
76
  case 'updateDatabase':
@@ -81,65 +81,66 @@ const Database = (action, data) => {
81
81
  break;
82
82
  default:
83
83
  errorHandler(data, 'unknown action', database)
84
- resolve(data) }
85
-
86
- openRequest.onsuccess = function() {
84
+ resolve(data)
85
+ }
86
+
87
+ openRequest.onsuccess = function () {
87
88
  let db = openRequest.result;
88
- let dbs = [] // ToDo: return an array of dbinstance??
89
+ let dbs = [] // TODO: return an array of dbinstance??
89
90
  if (action == 'updateDatabase') {
90
- // ToDo: open a cursor at database collection and save each collection and document at value
91
+ // TODO: open a cursor at database collection and save each collection and document at value
91
92
 
92
93
  let objectStoreNames = Array.from(db.objectStoreNames)
93
94
  for (let collection of objectStoreNames) {
94
95
  let request = collection.openCursor();
95
-
96
- request.onsuccess = function() {
96
+
97
+ request.onsuccess = function () {
97
98
  let cursor = request.result;
98
99
  if (cursor) {
99
100
  let value = cursor.value;
100
-
101
+
101
102
  cursor.continue();
102
103
  } else {
103
104
  // -1
104
105
  }
105
106
  };
106
-
107
- request.onerror = function() {
107
+
108
+ request.onerror = function () {
108
109
  errorHandler(data, request.error, database, collection)
109
110
  };
110
-
111
+
111
112
  }
112
-
113
+
113
114
  } else if (data.collection && data.collection.length) {
114
115
  let collections = data.collection;
115
116
  if (!Array.isArray(collections))
116
117
  collections = [collections]
117
-
118
+
118
119
  let collectionsLength = collections.length
119
120
  for (let collection of collections) {
120
-
121
+
121
122
  let collectionExist = db.objectStoreNames.contains(collection)
122
123
  if (!collectionExist || collectionsLength == 1 && data.upgrade == true) {
123
124
  db.close()
124
-
125
+
125
126
  let version = dbVersion.get(database) || openRequest.result.version
126
127
  let request = indexedDB.open(database, Number(version += 1));
127
128
  dbVersion.set(database, version)
128
-
129
- request.onupgradeneeded = function() {
129
+
130
+ request.onupgradeneeded = function () {
130
131
  db = request.result;
131
132
 
132
133
  if (!collectionExist) {
133
134
  db = request.result;
134
135
  try {
135
- db.createObjectStore(collection, {keyPath: '_id', autoIncrement: true});
136
+ db.createObjectStore(collection, { keyPath: '_id', autoIncrement: true });
136
137
  } catch (error) {
137
138
  errorHandler(data, error, database, collection)
138
139
  }
139
140
  }
140
141
  };
141
-
142
- request.onsuccess = function() {
142
+
143
+ request.onsuccess = function () {
143
144
  collectionsLength -= 1
144
145
 
145
146
  if (!collectionsLength)
@@ -148,15 +149,15 @@ const Database = (action, data) => {
148
149
  if (!databasesLength && !collectionsLength) {
149
150
  if (action == 'getDatabase') {
150
151
  let transaction = request.transaction
151
-
152
+
152
153
  if (data.upgrade == true && !transaction) {
153
154
  request.result.close()
154
155
 
155
156
  let version = dbVersion.get(database) || openRequest.result.version
156
157
  let upgrade = indexedDB.open(database, Number(version += 1));
157
158
  dbVersion.set(database, version)
158
-
159
- upgrade.onupgradeneeded = function() {
159
+
160
+ upgrade.onupgradeneeded = function () {
160
161
  resolve(upgrade)
161
162
  };
162
163
 
@@ -164,12 +165,12 @@ const Database = (action, data) => {
164
165
  resolve(request.result)
165
166
  }
166
167
  }
167
- else
168
+ else
168
169
  resolve(data)
169
170
  }
170
171
  };
171
-
172
- request.onerror = function() {
172
+
173
+ request.onerror = function () {
173
174
  errorHandler(data, request.error, database, collection)
174
175
  collectionsLength -= 1
175
176
 
@@ -200,18 +201,18 @@ const Database = (action, data) => {
200
201
  if (!databasesLength) {
201
202
  if (action == 'getDatabase') {
202
203
  let transaction = openRequest.transaction
203
-
204
- if (data.upgrade == true && !transaction) {
204
+
205
+ if (data.upgrade == true && !transaction) {
205
206
  db.close()
206
207
 
207
208
  let version = dbVersion.get(database) || openRequest.result.version
208
209
  let upgrade = indexedDB.open(database, Number(version += 1));
209
210
  dbVersion.set(database, version)
210
-
211
- upgrade.onupgradeneeded = function() {
211
+
212
+ upgrade.onupgradeneeded = function () {
212
213
  resolve(upgrade)
213
214
  };
214
-
215
+
215
216
  } else {
216
217
  resolve(db)
217
218
  }
@@ -220,10 +221,10 @@ const Database = (action, data) => {
220
221
  resolve(data)
221
222
  }
222
223
  }
223
-
224
+
224
225
  }
225
226
 
226
- openRequest.onerror = function() {
227
+ openRequest.onerror = function () {
227
228
  errorHandler(data, openRequest.error, database)
228
229
  resolve(data)
229
230
  };
@@ -237,7 +238,7 @@ const Database = (action, data) => {
237
238
  }
238
239
 
239
240
 
240
- function createCollection(data){
241
+ function createCollection(data) {
241
242
  return collection('createCollection', data)
242
243
  }
243
244
 
@@ -264,7 +265,7 @@ const collection = (action, data) => {
264
265
  if (!data['timeStamp'])
265
266
  data['timeStamp'] = new Date().toISOString()
266
267
 
267
- let databases = data.database;
268
+ let databases = data.database;
268
269
 
269
270
  if (!Array.isArray(databases))
270
271
  databases = [databases]
@@ -272,21 +273,21 @@ const collection = (action, data) => {
272
273
  for (let database of databases) {
273
274
  let openRequest = indexedDB.open(database)
274
275
 
275
- openRequest.onsuccess = function() {
276
+ openRequest.onsuccess = function () {
276
277
  let db = openRequest.result;
277
278
  db.close()
278
279
  let objectStoreNames = Array.from(db.objectStoreNames)
279
280
  if (action == 'readCollection') {
280
281
  if (!data[type])
281
282
  data[type] = []
282
-
283
- for (let collection of objectStoreNames){
283
+
284
+ for (let collection of objectStoreNames) {
284
285
  if (data.filter && data.filter.query) {
285
- let isFilter = queryData({name: collection}, data.filter.query)
286
+ let isFilter = queryData({ name: collection }, data.filter.query)
286
287
  if (isFilter)
287
- collectionArray.push({name: collection, db: 'indexeddb', database})
288
+ collectionArray.push({ name: collection, db: 'indexeddb', database })
288
289
  } else
289
- collectionArray.push({name: collection, db: 'indexeddb', database})
290
+ collectionArray.push({ name: collection, db: 'indexeddb', database })
290
291
  }
291
292
 
292
293
  databasesLength -= 1
@@ -312,7 +313,7 @@ const collection = (action, data) => {
312
313
  collectionExist && ['updateCollection', 'deleteCollection'].includes(action) ||
313
314
  !collectionExist && ['createCollection', 'updateCollection'].includes(action)
314
315
  ) {
315
- getDatabase({database, collection, upgrade: true}).then((db) => {
316
+ getDatabase({ database, collection, upgrade: true }).then((db) => {
316
317
  if (collectionExist && action == 'updateCollection') {
317
318
  let transaction = db.transaction;
318
319
  let objectStore = transaction.objectStore(collection);
@@ -323,19 +324,19 @@ const collection = (action, data) => {
323
324
  }
324
325
  else
325
326
  errorHandler(data, 'collection does not exist', database)
326
-
327
- db.result.close()
328
327
 
329
- collectionArray.push({name: value, db: 'indexeddb', database})
328
+ db.result.close()
329
+
330
+ collectionArray.push({ name: value, db: 'indexeddb', database })
330
331
  collectionsLength -= 1
331
332
 
332
333
  if (!collectionsLength)
333
334
  databasesLength -= 1
334
335
 
335
- if (!databasesLength && !collectionsLength){
336
+ if (!databasesLength && !collectionsLength) {
336
337
  resolve(createData(data, collectionArray, type))
337
- }
338
-
338
+ }
339
+
339
340
  } else {
340
341
  db = db.result
341
342
 
@@ -345,23 +346,23 @@ const collection = (action, data) => {
345
346
 
346
347
  db.close()
347
348
 
348
- collectionArray.push({name: collection, db: 'indexeddb', database})
349
+ collectionArray.push({ name: collection, db: 'indexeddb', database })
349
350
  collectionsLength -= 1
350
-
351
+
351
352
  if (!collectionsLength)
352
353
  databasesLength -= 1
353
-
354
+
354
355
  if (!databasesLength && !collectionsLength) {
355
356
  resolve(createData(data, collectionArray, type))
356
357
  }
357
-
358
+
358
359
  }
359
360
  }, (error) => {
360
361
  errorHandler(data, error, database)
361
- })
362
-
362
+ })
363
+
363
364
  } else {
364
- collectionArray.push({name: collection, db: 'indexeddb', database})
365
+ collectionArray.push({ name: collection, db: 'indexeddb', database })
365
366
  collectionsLength -= 1
366
367
 
367
368
  if (!collectionsLength)
@@ -373,12 +374,12 @@ const collection = (action, data) => {
373
374
  }
374
375
 
375
376
  }
376
-
377
+
377
378
  }
378
379
 
379
380
  };
380
-
381
- openRequest.onerror = function() {
381
+
382
+ openRequest.onerror = function () {
382
383
  errorHandler(data, openRequest.error, database)
383
384
  databasesLength -= 1
384
385
 
@@ -393,7 +394,7 @@ const collection = (action, data) => {
393
394
  }
394
395
 
395
396
 
396
- function createIndex(data){
397
+ function createIndex(data) {
397
398
  return index('createIndex', data)
398
399
  }
399
400
 
@@ -419,11 +420,11 @@ const index = (action, data) => {
419
420
 
420
421
  if (!data['timeStamp'])
421
422
  data['timeStamp'] = new Date().toISOString()
422
-
423
- let databases = data.database;
423
+
424
+ let databases = data.database;
424
425
  if (!Array.isArray(databases))
425
426
  databases = [databases]
426
-
427
+
427
428
  let collections = data.collection;
428
429
  if (!Array.isArray(collections))
429
430
  collections = [collections]
@@ -432,27 +433,28 @@ const index = (action, data) => {
432
433
  for (let database of databases) {
433
434
  let collectionsLength = collections.length
434
435
  for (let collection of collections) {
435
- getDatabase({database}).then((db) => {
436
+ getDatabase({ database }).then((db) => {
436
437
  let objectStoreNames = Array.from(db.objectStoreNames)
437
438
  if (objectStoreNames.includes(collection)) {
438
439
  let transaction = db.transaction(collection, "readonly");
439
440
  let objectStore = transaction.objectStore(collection);
440
441
  let indexNames = Array.from(objectStore.indexNames)
441
-
442
+
442
443
  if (action == 'readIndex') {
443
444
  for (let index of indexNames) {
444
445
  if (data.filter && data.filter.query) {
445
- let isFilter = queryData({name: index}, data.filter.query)
446
+ let isFilter = queryData({ name: index }, data.filter.query)
446
447
  if (isFilter)
447
- indexArray.push({name: index, db: 'indexeddb', database, collection})
448
+ indexArray.push({ name: index, db: 'indexeddb', database, collection })
448
449
  } else
449
- indexArray.push({name: index, db: 'indexeddb', database, collection}) }
450
+ indexArray.push({ name: index, db: 'indexeddb', database, collection })
451
+ }
450
452
  collectionsLength -= 1
451
453
  db.close()
452
454
 
453
455
  if (!collectionsLength)
454
456
  databasesLength -= 1
455
-
457
+
456
458
  if (!databasesLength && !collectionsLength)
457
459
  resolve(createData(data, indexArray, type))
458
460
 
@@ -465,11 +467,11 @@ const index = (action, data) => {
465
467
  indexes = data[type];
466
468
  if (!Array.isArray(indexes))
467
469
  indexes = [indexes]
468
-
470
+
469
471
  let indexesLength = indexes.length
470
472
  for (let index of indexes) {
471
-
472
- getDatabase({database, upgrade: true}).then((db2) => {
473
+
474
+ getDatabase({ database, upgrade: true }).then((db2) => {
473
475
  let transaction = db2.transaction;
474
476
  let objectStore = transaction.objectStore(collection);
475
477
 
@@ -481,8 +483,8 @@ const index = (action, data) => {
481
483
 
482
484
  if (action == 'createIndex') {
483
485
  if (!indexExist) {
484
- objectStore.createIndex(index, index, {unique: false})
485
- indexArray.push({name: index, db: 'indexeddb', database, collection})
486
+ objectStore.createIndex(index, index, { unique: false })
487
+ indexArray.push({ name: index, db: 'indexeddb', database, collection })
486
488
  }
487
489
  else
488
490
  errorHandler(data, 'index exist', database, collection)
@@ -492,7 +494,7 @@ const index = (action, data) => {
492
494
  let indexObj = objectStore.index(index);
493
495
  if (indexExist && !valueExist) {
494
496
  indexObj.name = value;
495
- indexArray.push({name: value, db: 'indexeddb', database, collection})
497
+ indexArray.push({ name: value, db: 'indexeddb', database, collection })
496
498
  }
497
499
  else
498
500
  errorHandler(data, 'index does not exist', database, collection)
@@ -501,7 +503,7 @@ const index = (action, data) => {
501
503
  if (action == 'deleteIndex') {
502
504
  if (indexExist) {
503
505
  objectStore.deleteIndex(index)
504
- indexArray.push({name: index, db: 'indexeddb', database, collection})
506
+ indexArray.push({ name: index, db: 'indexeddb', database, collection })
505
507
  }
506
508
  else
507
509
  errorHandler(data, 'index does not exist', database, collection)
@@ -512,26 +514,26 @@ const index = (action, data) => {
512
514
 
513
515
  if (!indexesLength)
514
516
  collectionsLength -= 1
515
-
517
+
516
518
  if (!collectionsLength)
517
- databasesLength -= 1
518
-
519
+ databasesLength -= 1
520
+
519
521
  if (!databasesLength && !collectionsLength && !indexesLength) {
520
522
  resolve(createData(data, indexArray, type))
521
- }
523
+ }
522
524
  });
523
525
  }
524
-
525
- }
526
-
526
+
527
+ }
528
+
527
529
  } else {
528
530
  db.close()
529
531
  errorHandler(data, `collection: ${collection} does not exist`, database, collection)
530
532
  resolve(data)
531
533
  }
532
- });
534
+ });
533
535
 
534
- }
536
+ }
535
537
  }
536
538
 
537
539
  }, (error) => {
@@ -539,7 +541,7 @@ const index = (action, data) => {
539
541
  });
540
542
  }
541
543
 
542
- function createDocument(data){
544
+ function createDocument(data) {
543
545
  return document('createDocument', data)
544
546
  }
545
547
 
@@ -560,17 +562,17 @@ const document = (action, data) => {
560
562
  return new Promise((resolve, reject) => {
561
563
  let type = 'document'
562
564
  let documents = []
563
-
565
+
564
566
  if (data.request)
565
567
  data[type] = data.request
566
-
568
+
567
569
  if (data.document && !Array.isArray(data.document))
568
570
  data.document = [data.document]
569
571
 
570
572
  if (!data['timeStamp'])
571
573
  data['timeStamp'] = new Date().toISOString()
572
574
 
573
- let databases = data.database;
575
+ let databases = data.database;
574
576
  if (!Array.isArray(databases))
575
577
  databases = [databases]
576
578
 
@@ -580,42 +582,42 @@ const document = (action, data) => {
580
582
 
581
583
  let databasesLength = databases.length
582
584
  for (let database of databases) {
583
- getDatabase({database}).then((db) => {
585
+ getDatabase({ database }).then((db) => {
584
586
  let collectionsLength = collections.length
585
587
  for (let collection of collections) {
586
588
  let collectionExist = db.objectStoreNames.contains(collection)
587
- if (!collectionExist) {
589
+ if (!collectionExist) {
588
590
  db.close()
589
- getDatabase({database, collection}).then((db) => {
591
+ getDatabase({ database, collection }).then((db) => {
590
592
  if (data.filter || action == 'readDocument' && (!data.document || !data.document.length)) {
591
593
  db.close()
592
594
  readDocuments(data, database, collection).then((filterDocs) => {
593
- getDatabase({database}).then((db) => {
595
+ getDatabase({ database }).then((db) => {
594
596
  let transaction = db.transaction([collection], "readwrite");
595
597
  let objectStore = transaction.objectStore(collection);
596
-
597
- runDocs({action, data, objectStore, documents, filterDocs, database, collection, type}).then(() => {
598
+
599
+ runDocs({ action, data, objectStore, documents, filterDocs, database, collection, type }).then(() => {
598
600
  db.close()
599
601
  collectionsLength -= 1
600
-
602
+
601
603
  if (!collectionsLength)
602
604
  databasesLength -= 1
603
-
605
+
604
606
  if (!databasesLength && !collectionsLength) {
605
607
  resolve(createData(data, documents, type))
606
608
  }
607
609
  })
608
610
  })
609
-
611
+
610
612
  })
611
613
  } else {
612
614
  let transaction = db.transaction([collection], "readwrite");
613
615
  let objectStore = transaction.objectStore(collection);
614
-
615
- runDocs({action, data, objectStore, documents, database, collection, type}).then(() => {
616
+
617
+ runDocs({ action, data, objectStore, documents, database, collection, type }).then(() => {
616
618
  db.close()
617
619
  collectionsLength -= 1
618
-
620
+
619
621
  if (!collectionsLength)
620
622
  databasesLength -= 1
621
623
 
@@ -626,47 +628,47 @@ const document = (action, data) => {
626
628
  }
627
629
 
628
630
  }).catch((error) => {
629
- errorHandler(data, {message: error, data}, database)
631
+ errorHandler(data, { message: error, data }, database)
630
632
  collectionsLength -= 1
631
-
633
+
632
634
  if (!collectionsLength)
633
635
  databasesLength -= 1
634
636
 
635
- if (!databasesLength && !collectionsLength)
637
+ if (!databasesLength && !collectionsLength)
636
638
  resolve(createData(data, documents, type))
637
-
639
+
638
640
  })
639
641
 
640
- } else {
642
+ } else {
641
643
  if (data.filter || action == 'readDocument' && (!data.document || !data.document.length)) {
642
-
644
+
643
645
  db.close()
644
646
  readDocuments(data, database, collection).then((filterDocs) => {
645
- getDatabase({database}).then((db) => {
647
+ getDatabase({ database }).then((db) => {
646
648
  let transaction = db.transaction([collection], "readwrite");
647
649
  let objectStore = transaction.objectStore(collection);
648
-
649
- runDocs({action, data, objectStore, documents, filterDocs, database, collection, type}).then(() => {
650
+
651
+ runDocs({ action, data, objectStore, documents, filterDocs, database, collection, type }).then(() => {
650
652
  db.close()
651
653
  collectionsLength -= 1
652
-
654
+
653
655
  if (!collectionsLength)
654
656
  databasesLength -= 1
655
-
657
+
656
658
  if (!databasesLength && !collectionsLength) {
657
659
  resolve(createData(data, documents, type))
658
660
  }
659
- })
661
+ })
660
662
  })
661
663
  })
662
664
  } else {
663
665
  let transaction = db.transaction([collection], "readwrite");
664
666
  let objectStore = transaction.objectStore(collection);
665
-
666
- runDocs({action, data, objectStore, documents, database, collection, type}).then(() => {
667
+
668
+ runDocs({ action, data, objectStore, documents, database, collection, type }).then(() => {
667
669
  db.close()
668
670
  collectionsLength -= 1
669
-
671
+
670
672
  if (!collectionsLength)
671
673
  databasesLength -= 1
672
674
  if (!databasesLength && !collectionsLength) {
@@ -678,11 +680,11 @@ const document = (action, data) => {
678
680
  }
679
681
 
680
682
  }).catch((error) => {
681
- errorHandler(data, {message: error, data}, database)
683
+ errorHandler(data, { message: error, data }, database)
682
684
 
683
685
  databasesLength -= 1
684
686
 
685
- if (!databasesLength)
687
+ if (!databasesLength)
686
688
  resolve(createData(data, documents, type))
687
689
  })
688
690
 
@@ -690,22 +692,22 @@ const document = (action, data) => {
690
692
  })
691
693
  }
692
694
 
693
- function runDocs({action, data, objectStore, documents, filterDocs, database, collection, type}) {
695
+ function runDocs({ action, data, objectStore, documents, filterDocs, database, collection, type }) {
694
696
  return new Promise((resolve, reject) => {
695
697
  let docs = []
696
-
698
+
697
699
  if (Array.isArray(data[type]))
698
700
  docs.push(...data[type]);
699
701
  // else if (data[type] != undefined && action == 'createDocument')
700
702
  // docs.push(data[type])
701
703
  // else if (data[type] != undefined)
702
704
  // docs.push({...data[type]})
703
-
705
+
704
706
 
705
707
  if (filterDocs && filterDocs.length) {
706
708
  if (action == 'readDocument') {
707
709
  for (let i = 0; i < filterDocs.length; i++)
708
- documents.push({db: 'indexeddb', database, collection, ...filterDocs[i]})
710
+ documents.push({ db: 'indexeddb', database, collection, ...filterDocs[i] })
709
711
 
710
712
  }
711
713
 
@@ -724,19 +726,19 @@ function runDocs({action, data, objectStore, documents, filterDocs, database, co
724
726
  }
725
727
 
726
728
  let docsLength = docs.length
727
-
729
+
728
730
  if (docsLength > 0) {
729
731
  for (let doc of docs) {
730
- // ToDo deDuplcate document_id per collection
732
+ // TODO deDuplcate document_id per collection
731
733
  delete doc.db
732
734
  delete doc.database
733
735
  delete doc.collection
734
-
736
+
735
737
  if (action == 'updateDocument' || action == 'deleteDocument') {
736
738
  if (doc._id) {
737
739
  if (data.organization_id)
738
740
  doc['organization_id'] = data.organization_id
739
- doc['modified'] = {on: data.timeStamp, by: data.user_id || data.clientId}
741
+ doc['modified'] = { on: data.timeStamp, by: data.user_id || data.clientId }
740
742
 
741
743
  updateDoc(action, data, doc, objectStore, database, collection).then((doc) => {
742
744
  doc.db = 'indexeddb'
@@ -747,7 +749,7 @@ function runDocs({action, data, objectStore, documents, filterDocs, database, co
747
749
  if (!docsLength) {
748
750
  resolve()
749
751
  }
750
- })
752
+ })
751
753
  } else {
752
754
  docsLength -= 1
753
755
  if (!docsLength)
@@ -762,7 +764,7 @@ function runDocs({action, data, objectStore, documents, filterDocs, database, co
762
764
 
763
765
  if (data.organization_id)
764
766
  doc['organization_id'] = data.organization_id
765
- doc['created'] = {on: data.timeStamp, by: data.user_id || data.clientId}
767
+ doc['created'] = { on: data.timeStamp, by: data.user_id || data.clientId }
766
768
  request = objectStore.add(doc);
767
769
  }
768
770
 
@@ -771,31 +773,31 @@ function runDocs({action, data, objectStore, documents, filterDocs, database, co
771
773
  request = objectStore.get(doc._id);
772
774
  else {
773
775
  docsLength -= 1
774
- errorHandler(data, {message: 'requires _id', document: doc}, database, objectStore.name)
775
- if (!docsLength) {
776
+ errorHandler(data, { message: 'requires _id', document: doc }, database, objectStore.name)
777
+ if (!docsLength) {
776
778
  resolve()
777
779
  }
778
780
  }
779
781
  }
780
-
781
- request.onsuccess = function() {
782
+
783
+ request.onsuccess = function () {
782
784
  if (request.result) {
783
785
  if (action == 'readDocument')
784
786
  doc = request.result
785
787
  doc.db = 'indexeddb'
786
788
  doc.database = database
787
789
  doc.collection = collection
788
-
790
+
789
791
  documents.push(doc)
790
792
  }
791
793
  docsLength -= 1
792
- if (!docsLength) {
794
+ if (!docsLength) {
793
795
  resolve()
794
796
  }
795
797
  };
796
-
797
- request.onerror = function() {
798
- errorHandler(data, {message: request.error, document: doc}, database, objectStore.name)
798
+
799
+ request.onerror = function () {
800
+ errorHandler(data, { message: request.error, document: doc }, database, objectStore.name)
799
801
  resolve()
800
802
  };
801
803
  }
@@ -804,100 +806,101 @@ function runDocs({action, data, objectStore, documents, filterDocs, database, co
804
806
  resolve()
805
807
  }
806
808
  }, (error) => {
807
- errorHandler(data, {message: error, document: doc}, database, objectStore.name)
809
+ errorHandler(data, { message: error, document: doc }, database, objectStore.name)
808
810
  })
809
811
  }
810
812
 
811
813
  function updateDoc(action, data, doc, objectStore, database, collection) {
812
814
  return new Promise((resolve, reject) => {
813
- let get = objectStore.get(doc._id)
814
- get.onsuccess = function() {;
815
-
816
- let result = get.result
817
- if (result || data.upsert == true) {
818
- let modify = true
819
- if (result && doc.modified && result.modified)
820
- if (doc.modified.on < result.modified.on)
821
- modify = false
822
-
823
- if (modify) {
824
- if (action == 'updateDocument') {
825
- doc = dotNotationToObject(doc, get.result)
826
-
827
- if (data.updateName){
828
- for (let key of Object.keys(data.updateName)){
829
- let val = doc[key]
830
- delete doc[key]
831
- doc[data.updateName[key]] = val
832
- }
833
- }
834
-
835
- if (data.deleteName){
836
- for (let key of Object.keys(data.deleteName)){
837
- delete doc[key]
838
- }
839
-
815
+ let get = objectStore.get(doc._id)
816
+ get.onsuccess = function () {
817
+ ;
818
+
819
+ let result = get.result
820
+ if (result || data.upsert == true) {
821
+ let modify = true
822
+ if (result && doc.modified && result.modified)
823
+ if (doc.modified.on < result.modified.on)
824
+ modify = false
825
+
826
+ if (modify) {
827
+ if (action == 'updateDocument') {
828
+ doc = dotNotationToObject(doc, get.result)
829
+
830
+ if (data.updateName) {
831
+ for (let key of Object.keys(data.updateName)) {
832
+ let val = doc[key]
833
+ delete doc[key]
834
+ doc[data.updateName[key]] = val
840
835
  }
841
-
842
- let put = objectStore.put(doc);
843
-
844
- put.onsuccess = function() {
845
- resolve(doc)
846
- };
847
-
848
- put.onerror = function() {
849
- errorHandler(data, put.error, database, collection, doc)
850
- resolve(doc)
851
- };
852
-
853
836
  }
854
837
 
855
- if (action == 'deleteDocument') {
856
- let deleteDoc = objectStore.delete(doc._id);
857
-
858
- deleteDoc.onsuccess = function() {
859
- resolve(doc)
860
- };
861
-
862
- deleteDoc.onerror = function() {
863
- errorHandler(data, deleteDoc.error, database, collection, doc)
864
- resolve(doc)
865
- };
838
+ if (data.deleteName) {
839
+ for (let key of Object.keys(data.deleteName)) {
840
+ delete doc[key]
841
+ }
842
+
866
843
  }
867
-
868
- } else {
869
- // ToDo: handle if _id not found upsert
870
- errorHandler(data, 'doc in db is newer', database, collection, doc)
871
- resolve(doc)
844
+
845
+ let put = objectStore.put(doc);
846
+
847
+ put.onsuccess = function () {
848
+ resolve(doc)
849
+ };
850
+
851
+ put.onerror = function () {
852
+ errorHandler(data, put.error, database, collection, doc)
853
+ resolve(doc)
854
+ };
855
+
856
+ }
857
+
858
+ if (action == 'deleteDocument') {
859
+ let deleteDoc = objectStore.delete(doc._id);
860
+
861
+ deleteDoc.onsuccess = function () {
862
+ resolve(doc)
863
+ };
864
+
865
+ deleteDoc.onerror = function () {
866
+ errorHandler(data, deleteDoc.error, database, collection, doc)
867
+ resolve(doc)
868
+ };
872
869
  }
870
+
873
871
  } else {
874
- errorHandler(data, 'document doest not exist and upsert is not true', database, collection, doc)
872
+ // TODO: handle if _id not found upsert
873
+ errorHandler(data, 'doc in db is newer', database, collection, doc)
875
874
  resolve(doc)
876
875
  }
877
-
878
- };
879
-
880
- get.onerror = function() {
881
- errorHandler(data, get.error, database, collection, doc)
876
+ } else {
877
+ errorHandler(data, 'document doest not exist and upsert is not true', database, collection, doc)
882
878
  resolve(doc)
883
- };
884
-
879
+ }
880
+
881
+ };
882
+
883
+ get.onerror = function () {
884
+ errorHandler(data, get.error, database, collection, doc)
885
+ resolve(doc)
886
+ };
887
+
885
888
  })
886
889
  }
887
890
 
888
891
  const readDocuments = (data, database, collection) => {
889
892
  return new Promise((resolve, reject) => {
890
893
  let openRequest = indexedDB.open(database);
891
-
892
- openRequest.onsuccess = function() {
894
+
895
+ openRequest.onsuccess = function () {
893
896
  let db = openRequest.result;
894
897
  try {
895
898
  let transaction = db.transaction([collection], "readonly");
896
899
  let objectStore = transaction.objectStore(collection);
897
-
900
+
898
901
  if (data.filter) {
899
902
  let count = objectStore.count();
900
- count.onsuccess = function() {
903
+ count.onsuccess = function () {
901
904
  data.filter.count = count.result
902
905
  }
903
906
  }
@@ -909,7 +912,7 @@ const readDocuments = (data, database, collection) => {
909
912
  // if (indexName.includes('-'))
910
913
  // isIndex = false
911
914
  // else
912
- // isIndex = true
915
+ // isIndex = true
913
916
  }
914
917
 
915
918
  if (isIndex) {
@@ -923,12 +926,12 @@ const readDocuments = (data, database, collection) => {
923
926
  let indexExist = indexNames.includes(indexName)
924
927
  if (!indexExist) {
925
928
  db.close()
926
- getDatabase({database, upgrade: true}).then((db2) => {
929
+ getDatabase({ database, upgrade: true }).then((db2) => {
927
930
  let transaction = db2.transaction;
928
931
  let objectStore = transaction.objectStore(collection);
929
932
  try {
930
- objectStore.createIndex(indexName, indexName, {unique: false})
931
- } catch(error) {
933
+ objectStore.createIndex(indexName, indexName, { unique: false })
934
+ } catch (error) {
932
935
  // console.log(error)
933
936
  }
934
937
  const indexStore = objectStore.index(indexName);
@@ -950,18 +953,18 @@ const readDocuments = (data, database, collection) => {
950
953
  readDocs(data, database, collection, objectStore).then((results) => {
951
954
  db.close()
952
955
  resolve(results)
953
- })
956
+ })
954
957
  }
955
958
  } catch (error) {
956
959
  errorHandler(data, error, database)
957
960
  db.close()
958
961
  resolve([])
959
- return
962
+ return
960
963
  }
961
964
 
962
965
  };
963
966
 
964
- openRequest.onerror = function() {
967
+ openRequest.onerror = function () {
965
968
  errorHandler(data, openRequest.error)
966
969
  resolve(data)
967
970
  };
@@ -970,7 +973,7 @@ const readDocuments = (data, database, collection) => {
970
973
 
971
974
  async function readDocs(data, database, collection, objectStore, direction) {
972
975
  return new Promise((resolve, reject) => {
973
-
976
+
974
977
  let results = [], index = 0, limit
975
978
  if (data.filter) {
976
979
  if (data.filter.startIndex)
@@ -982,7 +985,7 @@ async function readDocs(data, database, collection, objectStore, direction) {
982
985
  }
983
986
 
984
987
  const request = objectStore.openCursor(null, direction);
985
- request.onsuccess = function() {
988
+ request.onsuccess = function () {
986
989
  let cursor = request.result;
987
990
  if (cursor) {
988
991
  let isFilter = true;
@@ -1006,8 +1009,8 @@ async function readDocs(data, database, collection, objectStore, direction) {
1006
1009
  resolve(results)
1007
1010
  }
1008
1011
  }
1009
-
1010
- request.onerror = function() {
1012
+
1013
+ request.onerror = function () {
1011
1014
  errorHandler(data, request.error, database, collection)
1012
1015
  db.close()
1013
1016
  resolve([])
@@ -1016,12 +1019,12 @@ async function readDocs(data, database, collection, objectStore, direction) {
1016
1019
  });
1017
1020
  }
1018
1021
 
1019
- async function generateDB(organization = {document: {}}, user = {document: {}}) {
1022
+ async function generateDB(organization = { document: {} }, user = { document: {} }) {
1020
1023
  const organization_id = organization.document._id || ObjectId();
1021
1024
  const primaryKey = organization.document.key || uuid.generate();
1022
1025
  const user_id = user.document._id || ObjectId();
1023
1026
 
1024
- let apiKey = await readDocument({database: organization_id, collection: 'keys', organization_id})
1027
+ let apiKey = await readDocument({ database: organization_id, collection: 'keys', organization_id })
1025
1028
  if (apiKey && apiKey.document && apiKey.document[0])
1026
1029
  return
1027
1030
 
@@ -1032,7 +1035,7 @@ async function generateDB(organization = {document: {}}, user = {document: {}})
1032
1035
  organization.document._id = organization_id
1033
1036
  organization.document.key = primaryKey
1034
1037
  organization.document.name = organization.document.name || 'untitiled',
1035
- organization.organization_id = organization_id
1038
+ organization.organization_id = organization_id
1036
1039
  createDocument(organization);
1037
1040
 
1038
1041
  // Create primary key
@@ -1077,10 +1080,10 @@ async function generateDB(organization = {document: {}}, user = {document: {}})
1077
1080
  user.collection = 'users'
1078
1081
  user.document._id = user_id
1079
1082
  user.document.name = user.document.name || 'untitiled',
1080
- user.document.password = user.document.password || btoa('0000'),
1081
- user.document.connected_orgs = [organization_id],
1082
- user.document.current_org = organization_id,
1083
- user.organization_id = organization_id
1083
+ user.document.password = user.document.password || btoa('0000'),
1084
+ user.document.connected_orgs = [organization_id],
1085
+ user.document.current_org = organization_id,
1086
+ user.organization_id = organization_id
1084
1087
  createDocument(user);
1085
1088
 
1086
1089
  // Create role permission
@@ -1095,7 +1098,7 @@ async function generateDB(organization = {document: {}}, user = {document: {}})
1095
1098
  "collections": {
1096
1099
  "*": ["*"]
1097
1100
  },
1098
- "modules": {
1101
+ "actions": {
1099
1102
  "*": ""
1100
1103
  },
1101
1104
  "admin": "true",
@@ -1103,7 +1106,7 @@ async function generateDB(organization = {document: {}}, user = {document: {}})
1103
1106
  },
1104
1107
  organization_id
1105
1108
  };
1106
- createDocument(role);
1109
+ createDocument(role);
1107
1110
 
1108
1111
  // Create user permission
1109
1112
  let userKey = {
@@ -1119,18 +1122,18 @@ async function generateDB(organization = {document: {}}, user = {document: {}})
1119
1122
  };
1120
1123
  createDocument(userKey);
1121
1124
 
1122
- return [organization, key, user, role, userKey]
1123
-
1125
+ return [organization, key, user, role, userKey]
1126
+
1124
1127
  } catch (error) {
1125
1128
  return false
1126
1129
  }
1127
1130
  }
1128
1131
 
1129
- function errorHandler(data, error, database, collection){
1132
+ function errorHandler(data, error, database, collection) {
1130
1133
  if (typeof error == 'object')
1131
1134
  error['db'] = 'indexeddb'
1132
1135
  else
1133
- error = {db: 'indexeddb', message: error}
1136
+ error = { db: 'indexeddb', message: error }
1134
1137
 
1135
1138
  if (database)
1136
1139
  error['database'] = database
@@ -1150,8 +1153,8 @@ function createData(data, array, type) {
1150
1153
  data[type] = sortData(array, data.filter.sort)
1151
1154
  else
1152
1155
  data[type] = array
1153
-
1154
- if (data.returnLog){
1156
+
1157
+ if (data.returnLog) {
1155
1158
  if (!data.log)
1156
1159
  data.log = []
1157
1160
  data.log.push(...data[type])
@@ -1167,12 +1170,12 @@ function init() {
1167
1170
  console.log("This browser doesn't support IndexedDB.");
1168
1171
  setStatus(false);
1169
1172
  return;
1170
- } else {
1173
+ } else {
1171
1174
  try {
1172
1175
  indexedDB.databases()
1173
1176
  if (window.CoCreateConfig && window.CoCreateConfig.indexeddb !== false)
1174
1177
  setStatus(true)
1175
- } catch(e) {
1178
+ } catch (e) {
1176
1179
  setStatus(false);
1177
1180
  }
1178
1181
  }
@@ -1182,7 +1185,7 @@ function setStatus(value) {
1182
1185
  if (window.CoCreateConfig)
1183
1186
  window.CoCreateConfig.indexeddb = value;
1184
1187
  else
1185
- window.CoCreateConfig = {indexeddb: value};
1188
+ window.CoCreateConfig = { indexeddb: value };
1186
1189
 
1187
1190
  status = value;
1188
1191
  }
@@ -1198,21 +1201,21 @@ export default {
1198
1201
  readDatabase,
1199
1202
  updateDatabase,
1200
1203
  deleteDatabase,
1201
-
1204
+
1202
1205
  createCollection,
1203
1206
  readCollection,
1204
1207
  updateCollection,
1205
- deleteCollection,
1208
+ deleteCollection,
1206
1209
 
1207
1210
  createIndex,
1208
1211
  readIndex,
1209
1212
  updateIndex,
1210
1213
  deleteIndex,
1211
1214
 
1212
- createDocument,
1215
+ createDocument,
1213
1216
  readDocument,
1214
- updateDocument,
1215
- deleteDocument,
1217
+ updateDocument,
1218
+ deleteDocument,
1216
1219
 
1217
1220
  generateDB
1218
1221
  };