@cocreate/indexeddb 1.13.2 → 1.13.4
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 +17 -0
- package/CoCreate.config.js +25 -25
- package/package.json +1 -1
- package/src/index.js +229 -290
- package/test/documents.js +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -1,3 +1,20 @@
|
|
|
1
|
+
## [1.13.4](https://github.com/CoCreate-app/CoCreate-indexeddb/compare/v1.13.3...v1.13.4) (2023-09-17)
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
### Bug Fixes
|
|
5
|
+
|
|
6
|
+
* renamed filter to $filter ([00c58a5](https://github.com/CoCreate-app/CoCreate-indexeddb/commit/00c58a585b27e88c735a35e9a85cf234b060347c))
|
|
7
|
+
* update indexeddb() to indexeddb.send() ([6568ef6](https://github.com/CoCreate-app/CoCreate-indexeddb/commit/6568ef68d4f38490ffc57086661d4a5b8ae3b721))
|
|
8
|
+
|
|
9
|
+
## [1.13.3](https://github.com/CoCreate-app/CoCreate-indexeddb/compare/v1.13.2...v1.13.3) (2023-09-07)
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
### Bug Fixes
|
|
13
|
+
|
|
14
|
+
* get.database response ([a2311df](https://github.com/CoCreate-app/CoCreate-indexeddb/commit/a2311df7ef6c29cab8e21144ff10fa94a9c71540))
|
|
15
|
+
* handle method get.database ([dbc998e](https://github.com/CoCreate-app/CoCreate-indexeddb/commit/dbc998ef9e7cd141c14c41c55e625bec9c996e55))
|
|
16
|
+
* Refactor condition for filtering objects in `processObject` function ([d5fc6a3](https://github.com/CoCreate-app/CoCreate-indexeddb/commit/d5fc6a32ff3f09d2d6542c7733d4963df7900513))
|
|
17
|
+
|
|
1
18
|
## [1.13.2](https://github.com/CoCreate-app/CoCreate-indexeddb/compare/v1.13.1...v1.13.2) (2023-08-21)
|
|
2
19
|
|
|
3
20
|
|
package/CoCreate.config.js
CHANGED
|
@@ -1,25 +1,25 @@
|
|
|
1
|
-
module.exports = {
|
|
2
|
-
"organization_id": "",
|
|
3
|
-
"key": "",
|
|
4
|
-
"host": "",
|
|
5
|
-
"sources": [
|
|
6
|
-
{
|
|
7
|
-
"array": "files",
|
|
8
|
-
"object": {
|
|
9
|
-
"_id": "637ca2aa50234ef1671ce308",
|
|
10
|
-
"name": "index.html",
|
|
11
|
-
"path": "/docs/indexeddb/index.html",
|
|
12
|
-
"src": "{{./docs/index.html}}",
|
|
13
|
-
"host": [
|
|
14
|
-
"cocreate.app",
|
|
15
|
-
"general.cocreate.app"
|
|
16
|
-
],
|
|
17
|
-
"directory": "/docs/indexeddb",
|
|
18
|
-
"parentDirectory": "{{parentDirectory}}",
|
|
19
|
-
"content-type": "{{content-type}}",
|
|
20
|
-
"public": "true",
|
|
21
|
-
"website_id": "644d4bff8036fb9d1d1fd69c"
|
|
22
|
-
}
|
|
23
|
-
}
|
|
24
|
-
]
|
|
25
|
-
}
|
|
1
|
+
module.exports = {
|
|
2
|
+
"organization_id": "",
|
|
3
|
+
"key": "",
|
|
4
|
+
"host": "",
|
|
5
|
+
"sources": [
|
|
6
|
+
{
|
|
7
|
+
"array": "files",
|
|
8
|
+
"object": {
|
|
9
|
+
"_id": "637ca2aa50234ef1671ce308",
|
|
10
|
+
"name": "index.html",
|
|
11
|
+
"path": "/docs/indexeddb/index.html",
|
|
12
|
+
"src": "{{./docs/index.html}}",
|
|
13
|
+
"host": [
|
|
14
|
+
"cocreate.app",
|
|
15
|
+
"general.cocreate.app"
|
|
16
|
+
],
|
|
17
|
+
"directory": "/docs/indexeddb",
|
|
18
|
+
"parentDirectory": "{{parentDirectory}}",
|
|
19
|
+
"content-type": "{{content-type}}",
|
|
20
|
+
"public": "true",
|
|
21
|
+
"website_id": "644d4bff8036fb9d1d1fd69c"
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
]
|
|
25
|
+
};
|
package/package.json
CHANGED
package/src/index.js
CHANGED
|
@@ -49,23 +49,26 @@ function setStatus(value) {
|
|
|
49
49
|
|
|
50
50
|
if (!value) {
|
|
51
51
|
indexedDbFunction = indexedDb
|
|
52
|
-
|
|
53
|
-
} else if (!
|
|
54
|
-
|
|
52
|
+
send = value;
|
|
53
|
+
} else if (!send) {
|
|
54
|
+
send = indexedDbFunction
|
|
55
55
|
indexedDbFunction = true
|
|
56
56
|
}
|
|
57
57
|
}
|
|
58
58
|
|
|
59
|
-
async function
|
|
59
|
+
async function send(data) {
|
|
60
60
|
try {
|
|
61
61
|
let newData = [];
|
|
62
62
|
|
|
63
63
|
let type = data.method.split('.');
|
|
64
64
|
type = type[type.length - 1];
|
|
65
65
|
|
|
66
|
-
if (type === 'database')
|
|
67
|
-
|
|
68
|
-
|
|
66
|
+
if (type === 'database') {
|
|
67
|
+
if (data.method === 'get.database') {
|
|
68
|
+
return await processDatabase(data)
|
|
69
|
+
} else
|
|
70
|
+
await processDatabase(data, newData, type)
|
|
71
|
+
} else {
|
|
69
72
|
if (data.request)
|
|
70
73
|
data[type] = data.request
|
|
71
74
|
|
|
@@ -115,12 +118,12 @@ const processDatabase = (data, newData, type) => {
|
|
|
115
118
|
if (data.method == 'read.database') {
|
|
116
119
|
indexedDB.databases().then((databases) => {
|
|
117
120
|
for (let database of databases) {
|
|
118
|
-
if (data
|
|
119
|
-
let isFilter = queryData(database, data
|
|
121
|
+
if (data.$filter && data.$filter.query) {
|
|
122
|
+
let isFilter = queryData(database, data.$filter.query)
|
|
120
123
|
if (isFilter)
|
|
121
|
-
newData.push({ storage: 'indexeddb', database })
|
|
124
|
+
newData.push({ $storage: 'indexeddb', $database: database })
|
|
122
125
|
} else
|
|
123
|
-
newData.push({ storage: 'indexeddb', database })
|
|
126
|
+
newData.push({ $storage: 'indexeddb', $database: database })
|
|
124
127
|
}
|
|
125
128
|
|
|
126
129
|
resolve()
|
|
@@ -296,12 +299,12 @@ async function processArray(data, newData, database, type) {
|
|
|
296
299
|
data[type] = []
|
|
297
300
|
|
|
298
301
|
for (let i = 0; i < objectStoreNames.length; i++) {
|
|
299
|
-
if (data
|
|
300
|
-
let isFilter = queryData({ name: objectStoreNames[i] }, data
|
|
302
|
+
if (data.$filter && data.$filter.query) {
|
|
303
|
+
let isFilter = queryData({ name: objectStoreNames[i] }, data.$filter.query)
|
|
301
304
|
if (isFilter)
|
|
302
|
-
newData.push({ name: objectStoreNames, storage: 'indexeddb', database })
|
|
305
|
+
newData.push({ name: objectStoreNames, $storage: 'indexeddb', $database: database })
|
|
303
306
|
} else
|
|
304
|
-
newData.push({ name: objectStoreNames[i], storage: 'indexeddb', database })
|
|
307
|
+
newData.push({ name: objectStoreNames[i], $storage: 'indexeddb', $database: database })
|
|
305
308
|
}
|
|
306
309
|
} else {
|
|
307
310
|
let arrays
|
|
@@ -342,7 +345,7 @@ async function processArray(data, newData, database, type) {
|
|
|
342
345
|
db.result.close()
|
|
343
346
|
|
|
344
347
|
if (!error)
|
|
345
|
-
newData.push({ name: array, storage: 'indexeddb', database })
|
|
348
|
+
newData.push({ name: array, $storage: 'indexeddb', $database: database })
|
|
346
349
|
else
|
|
347
350
|
errorHandler(data, error, database)
|
|
348
351
|
}
|
|
@@ -361,10 +364,10 @@ async function processIndex(data, newData, database, array, type) {
|
|
|
361
364
|
if (data.method == 'read.index') {
|
|
362
365
|
for (let i = 0; i < indexNames.length; i++) {
|
|
363
366
|
let name = indexNames[i]
|
|
364
|
-
if (data
|
|
365
|
-
if (!(queryData({ name }, data
|
|
367
|
+
if (data.$filter && data.$filter.query)
|
|
368
|
+
if (!(queryData({ name }, data.$filter.query))) continue
|
|
366
369
|
|
|
367
|
-
newData.push({ name, storage: 'indexeddb', database, array })
|
|
370
|
+
newData.push({ name, $storage: 'indexeddb', $database: database, $array: array })
|
|
368
371
|
}
|
|
369
372
|
db.close()
|
|
370
373
|
} else {
|
|
@@ -404,7 +407,7 @@ async function processIndex(data, newData, database, array, type) {
|
|
|
404
407
|
error = 'index does not exist'
|
|
405
408
|
|
|
406
409
|
if (!error)
|
|
407
|
-
newData.push({ name: index, storage: 'indexeddb', database, array })
|
|
410
|
+
newData.push({ name: index, $storage: 'indexeddb', $database: database, $array: array })
|
|
408
411
|
else
|
|
409
412
|
errorHandler(data, error, database, array)
|
|
410
413
|
|
|
@@ -416,303 +419,256 @@ async function processIndex(data, newData, database, array, type) {
|
|
|
416
419
|
|
|
417
420
|
async function processObject(data, newData, database, array, type) {
|
|
418
421
|
let db = await processDatabase({ method: 'get.database', database })
|
|
419
|
-
|
|
422
|
+
|
|
423
|
+
let isFilter
|
|
424
|
+
if (data.$filter && data.$filter.query)
|
|
425
|
+
isFilter = true
|
|
426
|
+
|
|
420
427
|
let arrayExist = db.objectStoreNames.contains(array)
|
|
421
|
-
if (arrayExist) {
|
|
422
|
-
if (data
|
|
423
|
-
db.close()
|
|
424
|
-
filteredObjects = await readObject(data, database, array)
|
|
425
|
-
db = await processDatabase({ method: 'get.database', database })
|
|
426
|
-
}
|
|
427
|
-
} else {
|
|
428
|
-
db.close()
|
|
429
|
-
db = processDatabase({ method: 'get.database', database, array })
|
|
430
|
-
if (data.filter || data.method == 'read.object' && (!data.object || !data.object.length)) {
|
|
428
|
+
if (!arrayExist) {
|
|
429
|
+
if (data.$filter || data.method == 'read.object' && (!data.object || data.object && !data.object.length)) {
|
|
431
430
|
db.close()
|
|
432
|
-
|
|
433
|
-
db = processDatabase({ method: 'get.database', database })
|
|
431
|
+
db = await processDatabase({ method: 'get.database', database, array })
|
|
434
432
|
}
|
|
435
433
|
}
|
|
436
434
|
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
transaction = db.transaction([array], "readwrite");
|
|
440
|
-
objectStore = transaction.objectStore(array);
|
|
441
|
-
}
|
|
442
|
-
|
|
443
|
-
let objects = []
|
|
435
|
+
if (!array || !db)
|
|
436
|
+
throw new Error({ error: "This is an error message.", db, array });
|
|
444
437
|
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
objects.push(data[type])
|
|
449
|
-
// else if (data[type] != undefined && data.method == 'create.object')
|
|
450
|
-
// objects.push(data[type])
|
|
451
|
-
// else if (data[type] != undefined)
|
|
452
|
-
// objects.push({...data[type]})
|
|
438
|
+
let transactionType = "readwrite"
|
|
439
|
+
if (data.method == 'read.object')
|
|
440
|
+
transactionType = "readonly"
|
|
453
441
|
|
|
442
|
+
let transaction = db.transaction([array], transactionType);
|
|
443
|
+
let objectStore = transaction.objectStore(array);
|
|
454
444
|
|
|
455
|
-
if (
|
|
456
|
-
|
|
457
|
-
for (let i = 0; i < filteredObjects.length; i++)
|
|
458
|
-
newData.push({ storage: 'indexeddb', database, array, ...filteredObjects[i] })
|
|
445
|
+
if (data[type] && !Array.isArray(data[type]))
|
|
446
|
+
data[type] = [data[type]]
|
|
459
447
|
|
|
460
|
-
|
|
448
|
+
if (isFilter && (!data[type] || !data[type].length))
|
|
449
|
+
data[type] = [{}]
|
|
461
450
|
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
if (updateData)
|
|
465
|
-
objects.shift();
|
|
466
|
-
for (let filterDoc of filteredObjects) {
|
|
467
|
-
filterDoc = dotNotationToObject(updateData, filterDoc)
|
|
468
|
-
objects.push(filterDoc)
|
|
469
|
-
}
|
|
470
|
-
}
|
|
451
|
+
let upsert = data.upsert
|
|
452
|
+
try {
|
|
471
453
|
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
454
|
+
for (let i = 0; i < data[type].length; i++) {
|
|
455
|
+
delete data[type][i].$storage
|
|
456
|
+
delete data[type][i].$database
|
|
457
|
+
delete data[type][i].$array
|
|
475
458
|
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
459
|
+
if (data.method == 'create.object') {
|
|
460
|
+
if (!data[type][i]._id)
|
|
461
|
+
data[type][i]['_id'] = ObjectId()
|
|
462
|
+
data[type][i] = dotNotationToObject(data[type][i])
|
|
463
|
+
data[type][i]['created'] = { on: data.timeStamp, by: data.user_id || data.clientId }
|
|
481
464
|
|
|
482
|
-
|
|
483
|
-
|
|
465
|
+
let result = await add(objectStore, data[type][i])
|
|
466
|
+
newData.push({ $storage: 'indexeddb', $database: database, $array: array, ...result })
|
|
467
|
+
} else {
|
|
468
|
+
if (data[type][i].$filter)
|
|
469
|
+
isFilter = true
|
|
484
470
|
if (data.organization_id)
|
|
485
|
-
|
|
486
|
-
objects[i]['modified'] = { on: data.timeStamp, by: data.user_id || data.clientId }
|
|
487
|
-
|
|
488
|
-
await updateObject(data, objects[i], objectStore, database, array)
|
|
489
|
-
objects[i].storage = 'indexeddb'
|
|
490
|
-
objects[i].database = database
|
|
491
|
-
objects[i].array = array
|
|
492
|
-
newData.push(objects[i])
|
|
493
|
-
}
|
|
494
|
-
} else if (data.method == 'create.object') {
|
|
495
|
-
if (!objects[i]._id)
|
|
496
|
-
objects[i]['_id'] = ObjectId()
|
|
497
|
-
objects[i] = dotNotationToObject(objects[i])
|
|
498
|
-
|
|
499
|
-
if (data.organization_id)
|
|
500
|
-
objects[i]['organization_id'] = data.organization_id
|
|
501
|
-
objects[i]['created'] = { on: data.timeStamp, by: data.user_id || data.clientId }
|
|
502
|
-
await addGet(data, newData, database, array, objects[i], objectStore, 'add')
|
|
503
|
-
} else if (data.method == 'read.object') {
|
|
504
|
-
if (objects[i]._id)
|
|
505
|
-
await addGet(data, newData, database, array, objects[i]._id, objectStore, 'get')
|
|
506
|
-
else
|
|
507
|
-
errorHandler(data, { message: 'requires _id', object: objects[i] }, database, objectStore.name)
|
|
508
|
-
}
|
|
509
|
-
|
|
510
|
-
}
|
|
471
|
+
data[type][i]['organization_id'] = data.organization_id
|
|
511
472
|
|
|
512
|
-
|
|
513
|
-
|
|
473
|
+
if (data.method == 'update.object') {
|
|
474
|
+
if (data[type][i].$upsert)
|
|
475
|
+
upsert = data[type][i].$upsert
|
|
514
476
|
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
request.onsuccess = function () {
|
|
520
|
-
if (request.result) {
|
|
521
|
-
if (data.method == 'read.object')
|
|
522
|
-
object = request.result
|
|
523
|
-
object.storage = 'indexeddb'
|
|
524
|
-
object.database = database
|
|
525
|
-
object.array = array
|
|
526
|
-
|
|
527
|
-
newData.push(object)
|
|
528
|
-
}
|
|
529
|
-
resolve()
|
|
530
|
-
};
|
|
477
|
+
if (!data[type][i]._id && upsert)
|
|
478
|
+
data[type][i]._id = ObjectId()
|
|
479
|
+
data[type][i]['modified'] = { on: data.timeStamp, by: data.user_id || data.clientId }
|
|
480
|
+
}
|
|
531
481
|
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
482
|
+
let index = 0, limit, range, direction
|
|
483
|
+
if (data[type][i]._id)
|
|
484
|
+
range = IDBKeyRange.only(data[type][i]._id);
|
|
485
|
+
else if (isFilter) {
|
|
486
|
+
if (data.$filter || object.$filter) {
|
|
487
|
+
let count = objectStore.count();
|
|
488
|
+
count.onsuccess = function () {
|
|
489
|
+
data.$filter.count = count.result
|
|
490
|
+
}
|
|
491
|
+
}
|
|
536
492
|
|
|
537
|
-
|
|
538
|
-
|
|
493
|
+
let isIndex = false
|
|
494
|
+
let indexName, direction;
|
|
495
|
+
if (data.$filter && data.$filter.sort && data.$filter.sort[0] && data.$filter.sort[0].name) {
|
|
496
|
+
indexName = data.$filter.sort[0].name
|
|
497
|
+
// if (indexName.includes('-'))
|
|
498
|
+
// isIndex = false
|
|
499
|
+
// else
|
|
500
|
+
// isIndex = true
|
|
501
|
+
}
|
|
539
502
|
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
delete doc[key]
|
|
559
|
-
doc[data.updateName[key]] = val
|
|
503
|
+
if (isIndex) {
|
|
504
|
+
direction = data.$filter.sort[0].direction
|
|
505
|
+
if (direction == 'desc')
|
|
506
|
+
direction = 'prev'
|
|
507
|
+
else
|
|
508
|
+
direction = 'next'
|
|
509
|
+
|
|
510
|
+
let indexNames = Array.from(objectStore.indexNames)
|
|
511
|
+
let indexExist = indexNames.includes(indexName)
|
|
512
|
+
if (!indexExist) {
|
|
513
|
+
db.close()
|
|
514
|
+
db = await processDatabase({ method: 'get.database', database, upgrade: true })
|
|
515
|
+
transaction = db.transaction([array], 'readwrite');
|
|
516
|
+
objectStore = transaction.objectStore(array);
|
|
517
|
+
try {
|
|
518
|
+
objectStore.createIndex(indexName, indexName, { unique: false })
|
|
519
|
+
} catch (error) {
|
|
520
|
+
// console.log(error)
|
|
560
521
|
}
|
|
561
522
|
}
|
|
523
|
+
objectStore = objectStore.index(indexName);
|
|
524
|
+
}
|
|
562
525
|
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
526
|
+
if (data.$filter) {
|
|
527
|
+
if (data.$filter.index)
|
|
528
|
+
index = data.$filter.index
|
|
529
|
+
if (data.$filter.limit)
|
|
530
|
+
limit = data.$filter.limit
|
|
531
|
+
if (limit)
|
|
532
|
+
limit = index + limit;
|
|
533
|
+
}
|
|
534
|
+
}
|
|
567
535
|
|
|
536
|
+
let cursor = await openCursor(objectStore, range, direction)
|
|
537
|
+
if (cursor) {
|
|
538
|
+
let isMatch = true
|
|
539
|
+
|
|
540
|
+
if (isFilter)
|
|
541
|
+
isMatch = filter(objectStore, data[type][i], cursor.value)
|
|
542
|
+
|
|
543
|
+
if (isMatch !== false) {
|
|
544
|
+
let result
|
|
545
|
+
if (data.method == 'create.object') {
|
|
546
|
+
result = await add(objectStore, data[type][i])
|
|
547
|
+
} else if (data.method == 'read.object') {
|
|
548
|
+
result = await get(objectStore, data[type][i])
|
|
549
|
+
} else if (data.method == 'update.object') {
|
|
550
|
+
data[type][i] = dotNotationToObject(data[type][i], cursor.value)
|
|
551
|
+
data[type][i] = createUpdate(data, data[type][i])
|
|
552
|
+
result = await put(objectStore, data[type][i])
|
|
553
|
+
} else if (data.method == 'delete.object') {
|
|
554
|
+
result = await deleteObject(objectStore, data[type][i])
|
|
568
555
|
}
|
|
569
556
|
|
|
570
|
-
|
|
557
|
+
newData.push({ $storage: 'indexeddb', $database: database, $array: array, ...result })
|
|
558
|
+
}
|
|
571
559
|
|
|
572
|
-
|
|
573
|
-
resolve(doc)
|
|
574
|
-
};
|
|
560
|
+
limit && limit == newData.length ? '' : cursor.continue();
|
|
575
561
|
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
562
|
+
} else if (index)
|
|
563
|
+
newData = newData.slice(index)
|
|
564
|
+
}
|
|
565
|
+
}
|
|
566
|
+
} catch (error) {
|
|
567
|
+
errorHandler(data, error, database, array)
|
|
568
|
+
}
|
|
580
569
|
|
|
581
|
-
|
|
570
|
+
db.close()
|
|
571
|
+
}
|
|
582
572
|
|
|
583
|
-
|
|
584
|
-
|
|
573
|
+
function openCursor(objectStore, range, direction) {
|
|
574
|
+
return new Promise(async (resolve, reject) => {
|
|
575
|
+
const request = objectStore.openCursor(range, direction);// next, prev
|
|
576
|
+
request.onsuccess = () => resolve(request.result);
|
|
577
|
+
request.onerror = () => reject(request.error)
|
|
578
|
+
});
|
|
579
|
+
}
|
|
585
580
|
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
581
|
+
function add(objectStore, object) {
|
|
582
|
+
return new Promise((resolve, reject) => {
|
|
583
|
+
const request = objectStore.add(object);
|
|
584
|
+
request.onsuccess = () => resolve(object);
|
|
585
|
+
request.onerror = () => reject(request.error);
|
|
586
|
+
});
|
|
587
|
+
}
|
|
589
588
|
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
589
|
+
function get(objectStore, object) {
|
|
590
|
+
return new Promise((resolve, reject) => {
|
|
591
|
+
const request = objectStore.get(object._id);
|
|
592
|
+
request.onsuccess = () => resolve(request.result);
|
|
593
|
+
request.onerror = () => reject(request.error);
|
|
594
|
+
});
|
|
595
|
+
}
|
|
595
596
|
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
resolve(doc)
|
|
604
|
-
}
|
|
597
|
+
function put(objectStore, object) {
|
|
598
|
+
return new Promise((resolve, reject) => {
|
|
599
|
+
const request = objectStore.put(object);
|
|
600
|
+
request.onsuccess = () => resolve(object);
|
|
601
|
+
request.onerror = () => reject(request.error);
|
|
602
|
+
});
|
|
603
|
+
}
|
|
605
604
|
|
|
606
|
-
|
|
605
|
+
function deleteObject(objectStore, object) {
|
|
606
|
+
return new Promise((resolve, reject) => {
|
|
607
|
+
const request = objectStore.delete(object._id);
|
|
608
|
+
request.onsuccess = () => resolve(object);
|
|
609
|
+
request.onerror = () => reject(request.error);
|
|
610
|
+
});
|
|
611
|
+
}
|
|
607
612
|
|
|
608
|
-
get.onerror = function () {
|
|
609
|
-
errorHandler(data, get.error, database, array, doc)
|
|
610
|
-
resolve(doc)
|
|
611
|
-
};
|
|
612
613
|
|
|
613
|
-
|
|
614
|
-
|
|
614
|
+
function createUpdate(data, object) {
|
|
615
|
+
if (data.updateName) {
|
|
616
|
+
for (let key of Object.keys(data.updateName)) {
|
|
617
|
+
let val = object[key]
|
|
618
|
+
delete object[key]
|
|
619
|
+
object[data.updateName[key]] = val
|
|
620
|
+
}
|
|
621
|
+
}
|
|
615
622
|
|
|
616
|
-
|
|
617
|
-
|
|
623
|
+
if (data.deleteName) {
|
|
624
|
+
for (let key of Object.keys(data.deleteName)) {
|
|
625
|
+
delete object[key]
|
|
626
|
+
}
|
|
618
627
|
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
let objectStore = transaction.objectStore(array);
|
|
623
|
-
|
|
624
|
-
if (data.filter) {
|
|
625
|
-
let count = objectStore.count();
|
|
626
|
-
count.onsuccess = function () {
|
|
627
|
-
data.filter.count = count.result
|
|
628
|
-
}
|
|
629
|
-
}
|
|
628
|
+
}
|
|
629
|
+
return object
|
|
630
|
+
}
|
|
630
631
|
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
if (data.filter && data.filter.sort && data.filter.sort[0] && data.filter.sort[0].name) {
|
|
634
|
-
indexName = data.filter.sort[0].name
|
|
635
|
-
// if (indexName.includes('-'))
|
|
636
|
-
// isIndex = false
|
|
637
|
-
// else
|
|
638
|
-
// isIndex = true
|
|
639
|
-
}
|
|
632
|
+
function filter(data, object, result) {
|
|
633
|
+
let isMatch = true;
|
|
640
634
|
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
let indexNames = Array.from(objectStore.indexNames)
|
|
649
|
-
let indexExist = indexNames.includes(indexName)
|
|
650
|
-
if (!indexExist) {
|
|
651
|
-
db.close()
|
|
652
|
-
db = await processDatabase({ method: 'get.database', database, upgrade: true })
|
|
653
|
-
transaction = db.transaction;
|
|
654
|
-
objectStore = transaction.objectStore(array);
|
|
655
|
-
try {
|
|
656
|
-
objectStore.createIndex(indexName, indexName, { unique: false })
|
|
657
|
-
} catch (error) {
|
|
658
|
-
// console.log(error)
|
|
659
|
-
}
|
|
660
|
-
}
|
|
661
|
-
objectStore = objectStore.index(indexName);
|
|
662
|
-
}
|
|
635
|
+
if (data.$filter || object.$filter) {
|
|
636
|
+
let count = objectStore.count();
|
|
637
|
+
count.onsuccess = function () {
|
|
638
|
+
data.$filter.count = count.result
|
|
639
|
+
}
|
|
640
|
+
}
|
|
663
641
|
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
if (limit)
|
|
671
|
-
limit = index + limit;
|
|
672
|
-
}
|
|
642
|
+
if (data.$filter) {
|
|
643
|
+
if (data.$filter.query)
|
|
644
|
+
isMatch = queryData(result, data.$filter.query);
|
|
645
|
+
if (isMatch && data.$filter.search)
|
|
646
|
+
isMatch = searchData(result, data.$filter.search);
|
|
647
|
+
}
|
|
673
648
|
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
let isFilter = true;
|
|
679
|
-
let value = cursor.value;
|
|
680
|
-
if (data.filter) {
|
|
681
|
-
if (data.filter.query)
|
|
682
|
-
isFilter = queryData(value, data.filter.query);
|
|
683
|
-
if (isFilter && data.filter.search)
|
|
684
|
-
isFilter = searchData(value, data.filter.search);
|
|
685
|
-
}
|
|
686
|
-
if (isFilter !== false)
|
|
687
|
-
results.push(value)
|
|
688
|
-
if (limit && limit == results.length) {
|
|
689
|
-
results = results.slice(index, limit)
|
|
690
|
-
resolve(results)
|
|
691
|
-
} else
|
|
692
|
-
cursor.continue();
|
|
693
|
-
} else {
|
|
694
|
-
if (index)
|
|
695
|
-
results = results.slice(index)
|
|
696
|
-
resolve(results)
|
|
697
|
-
}
|
|
698
|
-
}
|
|
649
|
+
let modify = true
|
|
650
|
+
if (result && object.modified && result.modified)
|
|
651
|
+
if (object.modified.on < result.modified.on)
|
|
652
|
+
modify = false
|
|
699
653
|
|
|
700
|
-
|
|
701
|
-
|
|
702
|
-
db.close()
|
|
703
|
-
resolve([])
|
|
704
|
-
};
|
|
654
|
+
return isMatch
|
|
655
|
+
}
|
|
705
656
|
|
|
657
|
+
function createData(data, newData, type) {
|
|
658
|
+
data.request = data[type] || {}
|
|
706
659
|
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
660
|
+
if (data.$filter && data.$filter.sort)
|
|
661
|
+
data[type] = sortData(newData, data.$filter.sort)
|
|
662
|
+
else
|
|
663
|
+
data[type] = newData
|
|
710
664
|
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
}
|
|
665
|
+
if (data.returnLog) {
|
|
666
|
+
if (!data.log)
|
|
667
|
+
data.log = []
|
|
668
|
+
data.log.push(...data[type])
|
|
669
|
+
}
|
|
670
|
+
|
|
671
|
+
return data
|
|
716
672
|
}
|
|
717
673
|
|
|
718
674
|
function errorHandler(data, error, database, array) {
|
|
@@ -731,23 +687,6 @@ function errorHandler(data, error, database, array) {
|
|
|
731
687
|
data.error = [error]
|
|
732
688
|
}
|
|
733
689
|
|
|
734
|
-
function createData(data, newData, type) {
|
|
735
|
-
data.request = data[type] || {}
|
|
736
|
-
|
|
737
|
-
if (data.filter && data.filter.sort)
|
|
738
|
-
data[type] = sortData(newData, data.filter.sort)
|
|
739
|
-
else
|
|
740
|
-
data[type] = newData
|
|
741
|
-
|
|
742
|
-
if (data.returnLog) {
|
|
743
|
-
if (!data.log)
|
|
744
|
-
data.log = []
|
|
745
|
-
data.log.push(...data[type])
|
|
746
|
-
}
|
|
747
|
-
|
|
748
|
-
return data
|
|
749
|
-
}
|
|
750
|
-
|
|
751
690
|
init();
|
|
752
691
|
|
|
753
|
-
export default
|
|
692
|
+
export default { send }
|