@cocreate/indexeddb 1.16.1 → 1.17.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +24 -0
- package/demo/index.html +3 -3
- package/package.json +2 -2
- package/src/index.js +133 -130
- package/test/collections.js +20 -20
- package/test/databases.js +4 -4
- package/test/documents.js +24 -24
- package/test/index.html +3 -3
package/CHANGELOG.md
CHANGED
|
@@ -1,3 +1,27 @@
|
|
|
1
|
+
# [1.17.0](https://github.com/CoCreate-app/CoCreate-indexeddb/compare/v1.16.2...v1.17.0) (2023-11-03)
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
### Bug Fixes
|
|
5
|
+
|
|
6
|
+
* favicon.ico path ([3ea941e](https://github.com/CoCreate-app/CoCreate-indexeddb/commit/3ea941e1e70879aa171ec3b98a81dc3769ec80f2))
|
|
7
|
+
* get.database updated to database.get ([b8058c6](https://github.com/CoCreate-app/CoCreate-indexeddb/commit/b8058c6bbe5682d9bc7f29775b3f306babcd5108))
|
|
8
|
+
* relocated $filter conditions to after index is open ([349f7ff](https://github.com/CoCreate-app/CoCreate-indexeddb/commit/349f7ff33b08ba0da77a313135523b6ccf3a9b41))
|
|
9
|
+
* transaction bug ([6016c07](https://github.com/CoCreate-app/CoCreate-indexeddb/commit/6016c07c56b8dda63281783e810f4747d6e59d80))
|
|
10
|
+
* update dependencies to the lates versions ([4bcf0b2](https://github.com/CoCreate-app/CoCreate-indexeddb/commit/4bcf0b223a73d8e73aea2083abf37c066f8c1d3d))
|
|
11
|
+
* update method to use object.update etc ([6b47872](https://github.com/CoCreate-app/CoCreate-indexeddb/commit/6b47872704e19f7ba9cab7b544dfbad77aeed377))
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
### Features
|
|
15
|
+
|
|
16
|
+
* morph data[type] so the object can be synced across multiple storage, database, array ([8d3afb4](https://github.com/CoCreate-app/CoCreate-indexeddb/commit/8d3afb487ab4d4709c8bcc2ded6bf8d2ffaa0085))
|
|
17
|
+
|
|
18
|
+
## [1.16.2](https://github.com/CoCreate-app/CoCreate-indexeddb/compare/v1.16.1...v1.16.2) (2023-10-26)
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
### Bug Fixes
|
|
22
|
+
|
|
23
|
+
* expanded the try catch ([ab77e43](https://github.com/CoCreate-app/CoCreate-indexeddb/commit/ab77e434f4811832089d63256afb8e678209b4f7))
|
|
24
|
+
|
|
1
25
|
## [1.16.1](https://github.com/CoCreate-app/CoCreate-indexeddb/compare/v1.16.0...v1.16.1) (2023-10-25)
|
|
2
26
|
|
|
3
27
|
|
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
|
-
|
|
10
|
-
|
|
11
|
-
|
|
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/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cocreate/indexeddb",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.17.0",
|
|
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.
|
|
61
|
+
"@cocreate/utils": "^1.26.2"
|
|
62
62
|
}
|
|
63
63
|
}
|
package/src/index.js
CHANGED
|
@@ -63,18 +63,13 @@ async function send(data) {
|
|
|
63
63
|
try {
|
|
64
64
|
let newData = [];
|
|
65
65
|
|
|
66
|
-
let type = data.method.split('.');
|
|
67
|
-
type = type[type.length - 1];
|
|
68
|
-
|
|
66
|
+
let type = data.method.split('.')[0];
|
|
69
67
|
if (type === 'database') {
|
|
70
|
-
if (data.method === 'get
|
|
68
|
+
if (data.method === 'database.get') {
|
|
71
69
|
return await processDatabase(data)
|
|
72
70
|
} else
|
|
73
71
|
await processDatabase(data, newData, type)
|
|
74
72
|
} else {
|
|
75
|
-
if (data.request)
|
|
76
|
-
data[type] = data.request
|
|
77
|
-
|
|
78
73
|
if (!data['timeStamp'])
|
|
79
74
|
data['timeStamp'] = new Date()
|
|
80
75
|
else
|
|
@@ -105,7 +100,6 @@ async function send(data) {
|
|
|
105
100
|
}
|
|
106
101
|
}
|
|
107
102
|
|
|
108
|
-
|
|
109
103
|
return createData(data, newData, type)
|
|
110
104
|
|
|
111
105
|
} catch (error) {
|
|
@@ -118,13 +112,10 @@ const processDatabase = (data, newData, type) => {
|
|
|
118
112
|
return new Promise((resolve, reject) => {
|
|
119
113
|
if (!data)
|
|
120
114
|
data = {}
|
|
121
|
-
if (data
|
|
122
|
-
data[type] =
|
|
123
|
-
|
|
124
|
-
// if (!data['timeStamp'])
|
|
125
|
-
data['timeStamp'] = new Date(data['timeStamp'])
|
|
115
|
+
if (!data[type])
|
|
116
|
+
data[type] = []
|
|
126
117
|
|
|
127
|
-
if (data.method == 'read
|
|
118
|
+
if (data.method == 'database.read') {
|
|
128
119
|
indexedDB.databases().then((databases) => {
|
|
129
120
|
for (let database of databases) {
|
|
130
121
|
if (data.$filter && data.$filter.query) {
|
|
@@ -139,7 +130,7 @@ const processDatabase = (data, newData, type) => {
|
|
|
139
130
|
})
|
|
140
131
|
} else {
|
|
141
132
|
let databases
|
|
142
|
-
if (data.method == 'update
|
|
133
|
+
if (data.method == 'database.update')
|
|
143
134
|
databases = Object.keys(data.database)
|
|
144
135
|
else
|
|
145
136
|
databases = data.database;
|
|
@@ -150,12 +141,12 @@ const processDatabase = (data, newData, type) => {
|
|
|
150
141
|
for (let i = 0; i < databases.length; i++) {
|
|
151
142
|
let openRequest, database = databases[i];
|
|
152
143
|
switch (data.method) {
|
|
153
|
-
case 'get
|
|
154
|
-
case 'create
|
|
155
|
-
case 'update
|
|
144
|
+
case 'database.get':
|
|
145
|
+
case 'database.create':
|
|
146
|
+
case 'database.update':
|
|
156
147
|
openRequest = indexedDB.open(database);
|
|
157
148
|
break;
|
|
158
|
-
case 'delete
|
|
149
|
+
case 'database.delete':
|
|
159
150
|
openRequest = indexedDB.deleteprocessDatabase(database);
|
|
160
151
|
break;
|
|
161
152
|
default:
|
|
@@ -166,7 +157,7 @@ const processDatabase = (data, newData, type) => {
|
|
|
166
157
|
openRequest.onsuccess = function () {
|
|
167
158
|
let db = openRequest.result;
|
|
168
159
|
|
|
169
|
-
if (data.method == 'update
|
|
160
|
+
if (data.method == 'database.update') {
|
|
170
161
|
let objectStoreNames = Array.from(db.objectStoreNames)
|
|
171
162
|
for (let array of objectStoreNames) {
|
|
172
163
|
let request = array.openCursor();
|
|
@@ -233,7 +224,7 @@ const processDatabase = (data, newData, type) => {
|
|
|
233
224
|
|
|
234
225
|
request.onsuccess = function (event) {
|
|
235
226
|
if (databases.length - 1 === i) {
|
|
236
|
-
if (data.method == 'get
|
|
227
|
+
if (data.method == 'database.get')
|
|
237
228
|
resolve(event.target.result)
|
|
238
229
|
else
|
|
239
230
|
resolve(data)
|
|
@@ -243,7 +234,7 @@ const processDatabase = (data, newData, type) => {
|
|
|
243
234
|
request.onerror = function (event) {
|
|
244
235
|
errorHandler(data, event.target.error, database, array)
|
|
245
236
|
if (databases.length - 1 === i) {
|
|
246
|
-
if (data.method == 'get
|
|
237
|
+
if (data.method == 'database.get')
|
|
247
238
|
resolve(event.target.result)
|
|
248
239
|
else
|
|
249
240
|
resolve(data)
|
|
@@ -251,7 +242,7 @@ const processDatabase = (data, newData, type) => {
|
|
|
251
242
|
};
|
|
252
243
|
|
|
253
244
|
} else if (databases.length - 1 === i) {
|
|
254
|
-
if (data.method == 'get
|
|
245
|
+
if (data.method == 'database.get')
|
|
255
246
|
resolve(db)
|
|
256
247
|
else
|
|
257
248
|
resolve(data)
|
|
@@ -272,10 +263,10 @@ const processDatabase = (data, newData, type) => {
|
|
|
272
263
|
}
|
|
273
264
|
|
|
274
265
|
async function processArray(data, newData, database, type) {
|
|
275
|
-
let db = await processDatabase({ method: 'get
|
|
266
|
+
let db = await processDatabase({ method: 'database.get', database })
|
|
276
267
|
|
|
277
268
|
let objectStoreNames = Array.from(db.objectStoreNames)
|
|
278
|
-
if (data.method == 'read
|
|
269
|
+
if (data.method == 'array.read') {
|
|
279
270
|
if (!data[type])
|
|
280
271
|
data[type] = []
|
|
281
272
|
|
|
@@ -290,12 +281,12 @@ async function processArray(data, newData, database, type) {
|
|
|
290
281
|
} else {
|
|
291
282
|
db.close()
|
|
292
283
|
|
|
293
|
-
let request = await processDatabase({ method: 'get
|
|
284
|
+
let request = await processDatabase({ method: 'database.get', database, upgrade: true })
|
|
294
285
|
await new Promise((resolve) => {
|
|
295
286
|
request.onupgradeneeded = function (event) {
|
|
296
287
|
let db = event.target.result
|
|
297
288
|
let arrays
|
|
298
|
-
if (data.method == 'update
|
|
289
|
+
if (data.method == 'array.update')
|
|
299
290
|
arrays = Object.keys(data[type])
|
|
300
291
|
else
|
|
301
292
|
arrays = data[type];
|
|
@@ -306,20 +297,20 @@ async function processArray(data, newData, database, type) {
|
|
|
306
297
|
let error, array = arrays[i]
|
|
307
298
|
|
|
308
299
|
let arrayExist = db.objectStoreNames.contains(array)
|
|
309
|
-
if (arrayExist && data.method == 'create
|
|
300
|
+
if (arrayExist && data.method == 'array.create') {
|
|
310
301
|
error = 'array already exists'
|
|
311
|
-
} else if (data.method == 'delete
|
|
302
|
+
} else if (data.method == 'array.delete') {
|
|
312
303
|
if (objectStoreNames.includes(array)) {
|
|
313
304
|
db.deleteObjectStore(array);
|
|
314
305
|
} else {
|
|
315
306
|
error = 'the array does not exist'
|
|
316
307
|
}
|
|
317
308
|
} else {
|
|
318
|
-
if (!arrayExist && data.method == 'update
|
|
309
|
+
if (!arrayExist && data.method == 'array.update')
|
|
319
310
|
array = data[type][arrays[i]]
|
|
320
311
|
|
|
321
312
|
if (arrayExist) {
|
|
322
|
-
if (data.method == 'update
|
|
313
|
+
if (data.method == 'array.update') {
|
|
323
314
|
let transaction = event.target.transaction;
|
|
324
315
|
let objectStore = transaction.objectStore(array);
|
|
325
316
|
|
|
@@ -328,7 +319,7 @@ async function processArray(data, newData, database, type) {
|
|
|
328
319
|
else
|
|
329
320
|
error = 'An array with the new name already exist'
|
|
330
321
|
}
|
|
331
|
-
} else if (data.method == 'update
|
|
322
|
+
} else if (data.method == 'array.update')
|
|
332
323
|
error = 'array does not exists'
|
|
333
324
|
}
|
|
334
325
|
|
|
@@ -356,7 +347,7 @@ async function processArray(data, newData, database, type) {
|
|
|
356
347
|
}
|
|
357
348
|
|
|
358
349
|
async function processIndex(data, newData, database, array, type) {
|
|
359
|
-
let db = await processDatabase({ method: 'get
|
|
350
|
+
let db = await processDatabase({ method: 'database.get', database })
|
|
360
351
|
let arrayExist = db.objectStoreNames.contains(array)
|
|
361
352
|
if (arrayExist) {
|
|
362
353
|
// ToDO: switch here to types
|
|
@@ -364,7 +355,7 @@ async function processIndex(data, newData, database, array, type) {
|
|
|
364
355
|
let objectStore = transaction.objectStore(array);
|
|
365
356
|
let indexNames = Array.from(objectStore.indexNames)
|
|
366
357
|
|
|
367
|
-
if (data.method == 'read
|
|
358
|
+
if (data.method == 'index.read') {
|
|
368
359
|
for (let i = 0; i < indexNames.length; i++) {
|
|
369
360
|
let name = indexNames[i]
|
|
370
361
|
if (data.$filter && data.$filter.query)
|
|
@@ -376,7 +367,7 @@ async function processIndex(data, newData, database, array, type) {
|
|
|
376
367
|
} else {
|
|
377
368
|
db.close()
|
|
378
369
|
let indexes
|
|
379
|
-
if (data.method == 'update
|
|
370
|
+
if (data.method == 'index.update')
|
|
380
371
|
indexes = Object.keys(data[type])
|
|
381
372
|
else
|
|
382
373
|
indexes = data[type];
|
|
@@ -386,20 +377,20 @@ async function processIndex(data, newData, database, array, type) {
|
|
|
386
377
|
|
|
387
378
|
for (let i = 0; i < indexes.length; i++) {
|
|
388
379
|
let error, index = indexes[i]
|
|
389
|
-
db = await processDatabase({ method: 'get
|
|
380
|
+
db = await processDatabase({ method: 'database.get', database, upgrade: true })
|
|
390
381
|
let transaction = db.transaction;
|
|
391
382
|
let objectStore = transaction.objectStore(array);
|
|
392
383
|
let indexExist = indexNames.includes(index)
|
|
393
384
|
|
|
394
|
-
if (data.method == 'create
|
|
385
|
+
if (data.method == 'index.create') {
|
|
395
386
|
if (!indexExist)
|
|
396
387
|
objectStore.createIndex(index, index, { unique: false })
|
|
397
388
|
else
|
|
398
389
|
error = 'index already exist'
|
|
399
390
|
} else if (indexExist) {
|
|
400
|
-
if (data.method == 'delete
|
|
391
|
+
if (data.method == 'index.delete')
|
|
401
392
|
objectStore.deleteIndex(index)
|
|
402
|
-
else if (data.method == 'update
|
|
393
|
+
else if (data.method == 'index.update' && !indexNames.includes(data[type][indexes[i]])) {
|
|
403
394
|
if (!indexNames.includes(data[type][indexes[i]])) {
|
|
404
395
|
let indexObj = objectStore.index(index);
|
|
405
396
|
index = indexObj.name = data[type][indexes[i]];
|
|
@@ -421,79 +412,76 @@ async function processIndex(data, newData, database, array, type) {
|
|
|
421
412
|
}
|
|
422
413
|
|
|
423
414
|
async function processObject(data, newData, database, array, type) {
|
|
424
|
-
let db = await processDatabase({ method: 'get
|
|
415
|
+
let db = await processDatabase({ method: 'database.get', database })
|
|
425
416
|
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
417
|
+
try {
|
|
418
|
+
let isFilter
|
|
419
|
+
if (data.$filter && data.$filter.query)
|
|
420
|
+
isFilter = true
|
|
429
421
|
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
if (data.method == 'create.object' || data.method == 'update.object') {
|
|
422
|
+
let arrayExist = db.objectStoreNames.contains(array)
|
|
423
|
+
if (!arrayExist) {
|
|
433
424
|
db.close()
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
425
|
+
if (data.method == 'object.create' || data.method == 'object.update') {
|
|
426
|
+
db = await processDatabase({ method: 'database.get', database, array })
|
|
427
|
+
} else {
|
|
428
|
+
return errorHandler(data, "array does not exist", database, array)
|
|
429
|
+
}
|
|
437
430
|
}
|
|
438
|
-
}
|
|
439
|
-
|
|
440
|
-
if (!array || !db)
|
|
441
|
-
throw new Error({ error: "This is an error message.", db, array });
|
|
442
431
|
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
transactionType = "readonly"
|
|
432
|
+
if (!array || !db)
|
|
433
|
+
throw new Error({ error: "This is an error message.", db, array });
|
|
446
434
|
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
if (data[type] && !Array.isArray(data[type]))
|
|
452
|
-
data[type] = [data[type]]
|
|
435
|
+
if (!data[type])
|
|
436
|
+
data[type] = []
|
|
437
|
+
if (data[type] && !Array.isArray(data[type]))
|
|
438
|
+
data[type] = [data[type]]
|
|
453
439
|
|
|
454
|
-
|
|
455
|
-
|
|
440
|
+
if ((isFilter && !data[type].length) || data.isFilter)
|
|
441
|
+
data[type] = [{ isFilter: 'isEmptyObjectFilter' }];
|
|
442
|
+
// data[type].splice(0, 0, { isFilter: 'isEmptyObjectFilter' });
|
|
456
443
|
|
|
457
|
-
|
|
458
|
-
|
|
444
|
+
let transactionType = "readwrite"
|
|
445
|
+
if (data.method == 'object.read')
|
|
446
|
+
transactionType = "readonly"
|
|
459
447
|
|
|
460
|
-
|
|
461
|
-
let
|
|
462
|
-
count.onsuccess = function () {
|
|
463
|
-
data.$filter.count = count.result
|
|
464
|
-
}
|
|
465
|
-
}
|
|
448
|
+
let transaction = db.transaction([array], transactionType);
|
|
449
|
+
let objectStore = transaction.objectStore(array);
|
|
466
450
|
|
|
467
|
-
|
|
451
|
+
let globalOperators = getGlobalOperators(data)
|
|
452
|
+
let upsert = data.upsert
|
|
468
453
|
|
|
469
454
|
for (let i = 0; i < data[type].length; i++) {
|
|
470
455
|
delete data[type][i].$storage
|
|
471
456
|
delete data[type][i].$database
|
|
472
457
|
delete data[type][i].$array
|
|
473
458
|
|
|
474
|
-
if (data.method == 'create
|
|
459
|
+
if (data.method == 'object.create') {
|
|
475
460
|
if (data.organization_id)
|
|
476
461
|
data[type][i]['organization_id'] = data.organization_id
|
|
477
462
|
|
|
478
463
|
if (!data[type][i]._id)
|
|
479
464
|
data[type][i]['_id'] = ObjectId().toString()
|
|
480
465
|
data[type][i] = dotNotationToObject(data[type][i])
|
|
466
|
+
|
|
467
|
+
// TODO: user_id || clientId shohould be retrieved from CoCreate
|
|
481
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].$storage = 'indexeddb'
|
|
471
|
+
data[type][i].$database = database
|
|
472
|
+
data[type][i].$array = array
|
|
482
473
|
|
|
483
|
-
let result = await add(objectStore, data[type][i])
|
|
484
|
-
newData.push({ $storage: 'indexeddb', $database: database, $array: array, ...result })
|
|
485
474
|
} else {
|
|
486
475
|
if (data[type][i].$filter)
|
|
487
476
|
isFilter = true
|
|
488
|
-
// if (data.organization_id)
|
|
489
|
-
// data[type][i]['organization_id'] = data.organization_id
|
|
490
477
|
|
|
491
|
-
if (data.method == 'update
|
|
478
|
+
if (data.method == 'object.update') {
|
|
492
479
|
if (data.organization_id)
|
|
493
480
|
data[type][i]['organization_id'] = data.organization_id
|
|
494
481
|
|
|
495
482
|
if (data[type][i].$upsert)
|
|
496
483
|
upsert = data[type][i].$upsert
|
|
484
|
+
// TODO: user_id || clientId shohould be retrieved from CoCreate
|
|
497
485
|
data[type][i]['modified'] = { on: data.timeStamp, by: data.user_id || data.clientId }
|
|
498
486
|
}
|
|
499
487
|
|
|
@@ -501,13 +489,6 @@ async function processObject(data, newData, database, array, type) {
|
|
|
501
489
|
if (data[type][i]._id)
|
|
502
490
|
range = IDBKeyRange.only(data[type][i]._id);
|
|
503
491
|
else if (isFilter) {
|
|
504
|
-
if (data.object.$filter) {
|
|
505
|
-
let count = objectStore.count();
|
|
506
|
-
count.onsuccess = function () {
|
|
507
|
-
data.object.$filter.count = count.result
|
|
508
|
-
}
|
|
509
|
-
}
|
|
510
|
-
|
|
511
492
|
let isIndex = false
|
|
512
493
|
let indexName, direction;
|
|
513
494
|
|
|
@@ -546,22 +527,39 @@ async function processObject(data, newData, database, array, type) {
|
|
|
546
527
|
let indexExist = indexNames.includes(indexName)
|
|
547
528
|
if (!indexExist) {
|
|
548
529
|
db.close()
|
|
549
|
-
db = await processDatabase({ method: 'get
|
|
550
|
-
|
|
530
|
+
db = await processDatabase({ method: 'database.get', database, array, indexName })
|
|
531
|
+
transaction = db.transaction(array, 'readwrite');
|
|
551
532
|
objectStore = transaction.objectStore(array);
|
|
552
533
|
}
|
|
553
534
|
objectStore = objectStore.index(indexName);
|
|
554
535
|
}
|
|
555
536
|
|
|
537
|
+
let count = objectStore.count();
|
|
538
|
+
count.onsuccess = function () {
|
|
539
|
+
if (data.$filter)
|
|
540
|
+
data.$filter.count = count.result
|
|
541
|
+
else if (data.object.$filter)
|
|
542
|
+
data.object.$filter.count = count.result
|
|
543
|
+
}
|
|
544
|
+
|
|
556
545
|
if (data.$filter) {
|
|
557
546
|
if (data.$filter.index)
|
|
558
547
|
index = data.$filter.index
|
|
559
548
|
if (data.$filter.limit)
|
|
560
549
|
limit = data.$filter.limit
|
|
561
|
-
if (limit)
|
|
562
|
-
limit = (index || 0) + limit;
|
|
563
550
|
}
|
|
564
|
-
|
|
551
|
+
|
|
552
|
+
if (data[type][i].$filter) {
|
|
553
|
+
if (data[type][i].$filter.index)
|
|
554
|
+
index = data[type][i].$filter.index
|
|
555
|
+
if (data[type][i].$filter.limit)
|
|
556
|
+
limit = data[type][i].$filter.limit
|
|
557
|
+
}
|
|
558
|
+
|
|
559
|
+
if (limit)
|
|
560
|
+
limit = (index || 0) + limit;
|
|
561
|
+
|
|
562
|
+
} else if (data.method == 'object.delete' || data.method == 'object.update' && !range && !upsert && !isFilter) {
|
|
565
563
|
continue
|
|
566
564
|
}
|
|
567
565
|
|
|
@@ -572,29 +570,34 @@ async function processObject(data, newData, database, array, type) {
|
|
|
572
570
|
}
|
|
573
571
|
|
|
574
572
|
}
|
|
573
|
+
|
|
574
|
+
if (db.close)
|
|
575
|
+
db.close()
|
|
576
|
+
|
|
575
577
|
} catch (error) {
|
|
576
578
|
errorHandler(data, error, database, array)
|
|
577
|
-
|
|
579
|
+
if (db.close)
|
|
580
|
+
db.close()
|
|
578
581
|
|
|
579
|
-
|
|
582
|
+
}
|
|
580
583
|
}
|
|
581
584
|
|
|
582
585
|
function openCursor(objectStore, range, direction, data, newData, isFilter, limit, database, array, upsert, type, i, globalOperators) {
|
|
583
586
|
return new Promise(async (resolve, reject) => {
|
|
584
587
|
const request = objectStore.openCursor(range || null, direction);// next, prev
|
|
585
588
|
|
|
586
|
-
let hasUpdated
|
|
589
|
+
let hasUpdated, matchedLength = 0
|
|
587
590
|
request.onsuccess = async () => {
|
|
588
591
|
let cursor = request.result
|
|
589
592
|
|
|
590
|
-
if (data.method === 'create
|
|
591
|
-
|| data.method === 'update
|
|
593
|
+
if (data.method === 'object.create'
|
|
594
|
+
|| data.method === 'object.update'
|
|
592
595
|
&& (!range && !isFilter && upsert || !cursor && upsert && !hasUpdated)) {
|
|
593
596
|
let isMatch = true
|
|
594
597
|
if (isFilter) {
|
|
595
598
|
if (cursor)
|
|
596
599
|
isMatch = filter(objectStore, data[type][i], data[type][i], cursor.value)
|
|
597
|
-
else if (data.method === 'update
|
|
600
|
+
else if (data.method === 'object.update' && !upsert)
|
|
598
601
|
isMatch = false
|
|
599
602
|
}
|
|
600
603
|
|
|
@@ -606,20 +609,19 @@ function openCursor(objectStore, range, direction, data, newData, isFilter, limi
|
|
|
606
609
|
}
|
|
607
610
|
|
|
608
611
|
let result
|
|
609
|
-
if (data.method == 'create
|
|
610
|
-
|
|
611
|
-
} else if (data.method == 'update
|
|
612
|
-
let update
|
|
613
|
-
if (cursor)
|
|
614
|
-
update = createUpdate(cursor.value, data[type][i], globalOperators)
|
|
615
|
-
else
|
|
616
|
-
update = createUpdate({}, data[type][i], globalOperators)
|
|
612
|
+
if (data.method == 'object.create') {
|
|
613
|
+
data[type][i] = await add(objectStore, data[type][i])
|
|
614
|
+
} else if (data.method == 'object.update') {
|
|
615
|
+
let update = createUpdate((cursor && cursor.value) ? cursor.value : {}, data[type][i], globalOperators)
|
|
617
616
|
if (update)
|
|
618
|
-
|
|
617
|
+
data[type][i] = await put(objectStore, update)
|
|
619
618
|
}
|
|
619
|
+
data[type][i].$storage = 'indexeddb'
|
|
620
|
+
data[type][i].$database = database
|
|
621
|
+
data[type][i].$array = array
|
|
622
|
+
|
|
623
|
+
matchedLength++
|
|
620
624
|
|
|
621
|
-
if (result)
|
|
622
|
-
newData.push({ $storage: 'indexeddb', $database: database, $array: array, ...result })
|
|
623
625
|
}
|
|
624
626
|
resolve();
|
|
625
627
|
} else if (cursor) {
|
|
@@ -630,7 +632,7 @@ function openCursor(objectStore, range, direction, data, newData, isFilter, limi
|
|
|
630
632
|
if (isMatch !== false) {
|
|
631
633
|
let result = cursor.value
|
|
632
634
|
|
|
633
|
-
if (data.method == 'update
|
|
635
|
+
if (data.method == 'object.update') {
|
|
634
636
|
let update = createUpdate(cursor.value, data[type][i], globalOperators)
|
|
635
637
|
if (update)
|
|
636
638
|
result = await put(objectStore, update)
|
|
@@ -639,15 +641,25 @@ function openCursor(objectStore, range, direction, data, newData, isFilter, limi
|
|
|
639
641
|
// TODO: if update.$<operator>.someKey[] requires the index of inerted item added to the field name. update.$<operator>.someKey[<index>]
|
|
640
642
|
// TODO: if $addToSet get field name and item if it does not exist.
|
|
641
643
|
// TODO: if $pull get field name and find if item exist and delete.
|
|
642
|
-
} else if (data.method == 'delete
|
|
644
|
+
} else if (data.method == 'object.delete') {
|
|
643
645
|
result = cursor.value
|
|
644
646
|
cursor.delete()
|
|
645
647
|
}
|
|
646
648
|
|
|
647
|
-
|
|
649
|
+
if (data[type][i]._id)
|
|
650
|
+
data[type][i] = { $storage: 'indexeddb', $database: database, $array: array, ...result }
|
|
651
|
+
else {
|
|
652
|
+
let object = data[type].find(obj => obj._id && obj._id === result._id)
|
|
653
|
+
if (object)
|
|
654
|
+
object = { $storage: 'indexeddb', $database: database, $array: array, ...object, ...result }
|
|
655
|
+
else
|
|
656
|
+
newData.push({ $storage: 'indexeddb', $database: database, $array: array, ...result })
|
|
657
|
+
}
|
|
658
|
+
|
|
659
|
+
matchedLength++
|
|
648
660
|
}
|
|
649
661
|
|
|
650
|
-
if (!limit || limit && limit < newData.length) {
|
|
662
|
+
if (!limit || limit && (limit < newData.length || limit < matchedLength)) {
|
|
651
663
|
cursor.continue();
|
|
652
664
|
} else if (cursor.close) {
|
|
653
665
|
cursor.close()
|
|
@@ -655,6 +667,7 @@ function openCursor(objectStore, range, direction, data, newData, isFilter, limi
|
|
|
655
667
|
} else {
|
|
656
668
|
resolve();
|
|
657
669
|
}
|
|
670
|
+
|
|
658
671
|
} else {
|
|
659
672
|
resolve();
|
|
660
673
|
}
|
|
@@ -707,15 +720,6 @@ function filter(objectStore, data, object, result) {
|
|
|
707
720
|
let isMatch = true;
|
|
708
721
|
let filter = data.$filter || object.$filter
|
|
709
722
|
if (filter) {
|
|
710
|
-
try {
|
|
711
|
-
let count = objectStore.count();
|
|
712
|
-
count.onsuccess = function () {
|
|
713
|
-
filter.count = count.result
|
|
714
|
-
}
|
|
715
|
-
} catch (error) {
|
|
716
|
-
errorHandler(data, error, database, array)
|
|
717
|
-
}
|
|
718
|
-
|
|
719
723
|
if (filter.query)
|
|
720
724
|
isMatch = queryData(result, filter.query);
|
|
721
725
|
if (isMatch && filter.search)
|
|
@@ -850,18 +854,17 @@ function dotNotationToObjectUpdate(data, object = {}) {
|
|
|
850
854
|
}
|
|
851
855
|
|
|
852
856
|
function createData(data, newData, type) {
|
|
853
|
-
data.request = data[type] || {}
|
|
857
|
+
// data.request = data[type] || {}
|
|
858
|
+
if (data[type] && data[type][0] && data[type][0].isFilter === 'isEmptyObjectFilter') {
|
|
859
|
+
data[type].shift()
|
|
860
|
+
data.isFilter = true
|
|
861
|
+
}
|
|
862
|
+
|
|
863
|
+
data[type].push(...newData)
|
|
854
864
|
|
|
855
865
|
if (data.$filter && data.$filter.sort)
|
|
856
|
-
data[type] = sortData(
|
|
857
|
-
else
|
|
858
|
-
data[type] = newData
|
|
866
|
+
data[type] = sortData(data[type], data.$filter.sort)
|
|
859
867
|
|
|
860
|
-
if (data.returnLog) {
|
|
861
|
-
if (!data.log)
|
|
862
|
-
data.log = []
|
|
863
|
-
data.log.push(...data[type])
|
|
864
|
-
}
|
|
865
868
|
|
|
866
869
|
return data
|
|
867
870
|
}
|
package/test/collections.js
CHANGED
|
@@ -1,37 +1,37 @@
|
|
|
1
|
-
async function
|
|
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
|
|
8
|
-
method: 'create
|
|
9
|
-
database: ['
|
|
10
|
-
array: ['
|
|
7
|
+
let createObject = await CoCreate.indexeddb.process({
|
|
8
|
+
method: 'array.create',
|
|
9
|
+
database: ['testDatabase', 'testDatabase1', 'testDatabase2'],
|
|
10
|
+
array: ['testArray', 'testArray1', 'testArray2']
|
|
11
11
|
|
|
12
12
|
})
|
|
13
|
-
console.log('
|
|
13
|
+
console.log('createObject', createObject)
|
|
14
14
|
|
|
15
|
-
let
|
|
16
|
-
method: 'read
|
|
17
|
-
database: ['
|
|
15
|
+
let readObject = await CoCreate.indexeddb.process({
|
|
16
|
+
method: 'array.read',
|
|
17
|
+
database: ['testDatabase', 'testDatabase1', 'testDatabase2'],
|
|
18
18
|
})
|
|
19
|
-
console.log('
|
|
19
|
+
console.log('readObject', readObject)
|
|
20
20
|
|
|
21
|
-
let
|
|
22
|
-
method: 'update
|
|
23
|
-
database: ['
|
|
24
|
-
array: {
|
|
21
|
+
let updateObject = await CoCreate.indexeddb.process({
|
|
22
|
+
method: 'array.update',
|
|
23
|
+
database: ['testDatabase', 'testDatabase1', 'testDatabase2'],
|
|
24
|
+
array: { testArray: 'testArrayA' }
|
|
25
25
|
})
|
|
26
|
-
console.log('
|
|
26
|
+
console.log('updateObject', updateObject)
|
|
27
27
|
|
|
28
28
|
|
|
29
|
-
let
|
|
30
|
-
method: 'delete
|
|
31
|
-
database: ['
|
|
32
|
-
array: ['
|
|
29
|
+
let deleteObject = await CoCreate.indexeddb.process({
|
|
30
|
+
method: 'array.delete',
|
|
31
|
+
database: ['testDatabase', 'testDatabase1', 'testDatabase2'],
|
|
32
|
+
array: ['testArrayA', 'testArray1', 'testArray2']
|
|
33
33
|
})
|
|
34
|
-
console.log('
|
|
34
|
+
console.log('deleteObject', deleteObject)
|
|
35
35
|
|
|
36
36
|
}
|
|
37
37
|
}
|
package/test/databases.js
CHANGED
|
@@ -4,26 +4,26 @@ async function testDatabases() {
|
|
|
4
4
|
return;
|
|
5
5
|
} else {
|
|
6
6
|
let createDatabase = await CoCreate.indexeddb.process({
|
|
7
|
-
method: 'create
|
|
7
|
+
method: 'database.create',
|
|
8
8
|
database: 'testDB',
|
|
9
9
|
})
|
|
10
10
|
console.log('createDatabase', createDatabase)
|
|
11
11
|
|
|
12
12
|
let readDatabase = await CoCreate.indexeddb.process({
|
|
13
|
-
method: 'read
|
|
13
|
+
method: 'database.read',
|
|
14
14
|
database: 'testDB',
|
|
15
15
|
})
|
|
16
16
|
console.log('readDatabase', readDatabase)
|
|
17
17
|
|
|
18
18
|
let updateDatabase = await CoCreate.indexeddb.process({
|
|
19
|
-
method: 'update
|
|
19
|
+
method: 'database.update',
|
|
20
20
|
database: { testDB: 'testDatabase1' }
|
|
21
21
|
})
|
|
22
22
|
console.log('updateDatabase', updateDatabase)
|
|
23
23
|
|
|
24
24
|
|
|
25
25
|
let deleteDatabase = await CoCreate.indexeddb.process({
|
|
26
|
-
method: 'delete
|
|
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 (!('
|
|
3
|
-
console.log("This browser doesn't support
|
|
2
|
+
if (!('indexedDatabase' in window)) {
|
|
3
|
+
console.log("This browser doesn't support IndexedDatabase.");
|
|
4
4
|
return;
|
|
5
5
|
} else {
|
|
6
6
|
|
|
7
|
-
let
|
|
8
|
-
method: 'create
|
|
9
|
-
database: ['
|
|
10
|
-
array: ['
|
|
7
|
+
let createObject = await CoCreate.indexeddb.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('
|
|
19
|
-
let _id =
|
|
18
|
+
console.log('createObject', createObject)
|
|
19
|
+
let _id = createObject.object[0]._id
|
|
20
20
|
|
|
21
|
-
let
|
|
22
|
-
method: 'read
|
|
23
|
-
database: ['
|
|
24
|
-
array: ['
|
|
21
|
+
let readObject = await CoCreate.indexeddb.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('
|
|
30
|
+
console.log('readObject', readObject)
|
|
31
31
|
|
|
32
|
-
let
|
|
33
|
-
method: 'update
|
|
34
|
-
database: ['
|
|
35
|
-
array: ['
|
|
32
|
+
let updateObject = await CoCreate.indexeddb.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('
|
|
43
|
+
console.log('updateObject', updateObject)
|
|
44
44
|
|
|
45
45
|
|
|
46
|
-
let
|
|
47
|
-
method: 'delete
|
|
48
|
-
database: ['
|
|
49
|
-
array: ['
|
|
46
|
+
let deleteObject = await CoCreate.indexeddb.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('
|
|
61
|
+
console.log('deleteObject', deleteObject)
|
|
62
62
|
|
|
63
63
|
// let deleteDatabase = await CoCreate.indexeddb.deleteDatabase({
|
|
64
|
-
// database: ['
|
|
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
|
-
|
|
10
|
-
|
|
11
|
-
|
|
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>
|