@cocreate/indexeddb 1.15.0 → 1.16.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 +14 -0
- package/package.json +1 -1
- package/src/index.js +182 -166
package/CHANGELOG.md
CHANGED
|
@@ -1,3 +1,17 @@
|
|
|
1
|
+
# [1.16.0](https://github.com/CoCreate-app/CoCreate-indexeddb/compare/v1.15.0...v1.16.0) (2023-10-24)
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
### Bug Fixes
|
|
5
|
+
|
|
6
|
+
* add() handle objectStores that opened index ([0bfebfc](https://github.com/CoCreate-app/CoCreate-indexeddb/commit/0bfebfc55a0ea80c365f21e36e16f6c637aaf133))
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
### Features
|
|
10
|
+
|
|
11
|
+
* create index if filter.quey.inxex is true ([6d6b134](https://github.com/CoCreate-app/CoCreate-indexeddb/commit/6d6b1348e5176a12322cf39b38d1cc1f0fdc76c6))
|
|
12
|
+
* processDatabase auto upgrade ([0b73644](https://github.com/CoCreate-app/CoCreate-indexeddb/commit/0b73644128f57cc629cba6d93befe71fa617d1be))
|
|
13
|
+
* processDatabase() upgrade true returns event ([26ee1d1](https://github.com/CoCreate-app/CoCreate-indexeddb/commit/26ee1d10400b6a94129d63f1077d318b80940bab))
|
|
14
|
+
|
|
1
15
|
# [1.15.0](https://github.com/CoCreate-app/CoCreate-indexeddb/compare/v1.14.6...v1.15.0) (2023-10-22)
|
|
2
16
|
|
|
3
17
|
|
package/package.json
CHANGED
package/src/index.js
CHANGED
|
@@ -93,7 +93,7 @@ async function send(data) {
|
|
|
93
93
|
for (let i = 0; i < databases.length; i++) {
|
|
94
94
|
let database = databases[i]
|
|
95
95
|
if (type === 'array') {
|
|
96
|
-
await processArray(data, newData, database,
|
|
96
|
+
await processArray(data, newData, database, type)
|
|
97
97
|
} else {
|
|
98
98
|
for (let j = 0; j < arrays.length; j++) {
|
|
99
99
|
if (type === 'index')
|
|
@@ -104,6 +104,8 @@ async function send(data) {
|
|
|
104
104
|
}
|
|
105
105
|
}
|
|
106
106
|
}
|
|
107
|
+
|
|
108
|
+
|
|
107
109
|
return createData(data, newData, type)
|
|
108
110
|
|
|
109
111
|
} catch (error) {
|
|
@@ -163,10 +165,8 @@ const processDatabase = (data, newData, type) => {
|
|
|
163
165
|
|
|
164
166
|
openRequest.onsuccess = function () {
|
|
165
167
|
let db = openRequest.result;
|
|
166
|
-
let dbs = [] // TODO: return an array of dbinstance??
|
|
167
|
-
if (data.method == 'update.database') {
|
|
168
|
-
// TODO: open a cursor at database array and save each array and object at value
|
|
169
168
|
|
|
169
|
+
if (data.method == 'update.database') {
|
|
170
170
|
let objectStoreNames = Array.from(db.objectStoreNames)
|
|
171
171
|
for (let array of objectStoreNames) {
|
|
172
172
|
let request = array.openCursor();
|
|
@@ -188,100 +188,75 @@ const processDatabase = (data, newData, type) => {
|
|
|
188
188
|
|
|
189
189
|
}
|
|
190
190
|
|
|
191
|
-
} else if (data.
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
}
|
|
216
|
-
}
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
request.onsuccess = function () {
|
|
220
|
-
if (databases.length - 1 === i && arrays.length - 1 === j) {
|
|
221
|
-
if (data.method == 'get.database') {
|
|
222
|
-
let transaction = request.transaction
|
|
223
|
-
|
|
224
|
-
if (data.upgrade == true && !transaction) {
|
|
225
|
-
request.result.close()
|
|
226
|
-
|
|
227
|
-
let version = dbVersion.get(database) || openRequest.result.version
|
|
228
|
-
let upgrade = indexedDB.open(database, Number(version += 1));
|
|
229
|
-
dbVersion.set(database, version)
|
|
230
|
-
|
|
231
|
-
upgrade.onupgradeneeded = function () {
|
|
232
|
-
resolve(upgrade)
|
|
233
|
-
};
|
|
234
|
-
|
|
235
|
-
} else {
|
|
236
|
-
resolve(request.result)
|
|
237
|
-
}
|
|
238
|
-
}
|
|
239
|
-
else
|
|
240
|
-
resolve(data)
|
|
191
|
+
} else if (data.upgrade === true) {
|
|
192
|
+
db.close()
|
|
193
|
+
|
|
194
|
+
let version = dbVersion.get(database) || openRequest.result.version
|
|
195
|
+
resolve(indexedDB.open(database, Number(version += 1)))
|
|
196
|
+
dbVersion.set(database, version)
|
|
197
|
+
|
|
198
|
+
} else if (data.array) {
|
|
199
|
+
db.close()
|
|
200
|
+
|
|
201
|
+
let version = dbVersion.get(database) || openRequest.result.version
|
|
202
|
+
let request = indexedDB.open(database, Number(version += 1));
|
|
203
|
+
dbVersion.set(database, version)
|
|
204
|
+
|
|
205
|
+
request.onupgradeneeded = function (event) {
|
|
206
|
+
const db = event.target.result;
|
|
207
|
+
|
|
208
|
+
let arrays = data.array;
|
|
209
|
+
if (!Array.isArray(arrays))
|
|
210
|
+
arrays = [arrays]
|
|
211
|
+
|
|
212
|
+
for (let j = 0; j < arrays.length; j++) {
|
|
213
|
+
if (!db.objectStoreNames.contains(arrays[j])) {
|
|
214
|
+
try {
|
|
215
|
+
db.createObjectStore(arrays[j], { keyPath: '_id', autoIncrement: true });
|
|
216
|
+
} catch (error) {
|
|
217
|
+
errorHandler(data, error, database, arrays[j])
|
|
241
218
|
}
|
|
242
|
-
}
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
if (
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
if (data.indexName) {
|
|
222
|
+
let index = data.indexName;
|
|
223
|
+
if (!Array.isArray(index))
|
|
224
|
+
index = [index]
|
|
225
|
+
for (let k = 0; k < index.length; k++) {
|
|
226
|
+
const transaction = event.target.transaction;
|
|
227
|
+
const objectStore = transaction.objectStore(arrays[j]);
|
|
228
|
+
objectStore.createIndex(index[k], index[k], { unique: data.unique || false });
|
|
251
229
|
}
|
|
252
|
-
};
|
|
253
|
-
} else {
|
|
254
|
-
if (databases.length - 1 === i && arrays.length - 1 === j) {
|
|
255
|
-
if (data.method == 'get.database')
|
|
256
|
-
resolve(db)
|
|
257
|
-
else
|
|
258
|
-
resolve(data)
|
|
259
230
|
}
|
|
260
231
|
}
|
|
261
|
-
}
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
if (
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
resolve(
|
|
279
|
-
}
|
|
280
|
-
|
|
281
|
-
}
|
|
282
|
-
}
|
|
232
|
+
};
|
|
233
|
+
|
|
234
|
+
request.onsuccess = function (event) {
|
|
235
|
+
if (databases.length - 1 === i) {
|
|
236
|
+
if (data.method == 'get.database')
|
|
237
|
+
resolve(event.target.result)
|
|
238
|
+
else
|
|
239
|
+
resolve(data)
|
|
240
|
+
}
|
|
241
|
+
};
|
|
242
|
+
|
|
243
|
+
request.onerror = function (event) {
|
|
244
|
+
errorHandler(data, event.target.error, database, array)
|
|
245
|
+
if (databases.length - 1 === i) {
|
|
246
|
+
if (data.method == 'get.database')
|
|
247
|
+
resolve(event.target.result)
|
|
248
|
+
else
|
|
249
|
+
resolve(data)
|
|
250
|
+
}
|
|
251
|
+
};
|
|
283
252
|
|
|
284
|
-
|
|
253
|
+
} else if (databases.length - 1 === i) {
|
|
254
|
+
if (data.method == 'get.database')
|
|
255
|
+
resolve(db)
|
|
256
|
+
else
|
|
257
|
+
resolve(data)
|
|
258
|
+
}
|
|
259
|
+
};
|
|
285
260
|
|
|
286
261
|
openRequest.onerror = function () {
|
|
287
262
|
errorHandler(data, openRequest.error, database)
|
|
@@ -298,7 +273,6 @@ const processDatabase = (data, newData, type) => {
|
|
|
298
273
|
|
|
299
274
|
async function processArray(data, newData, database, type) {
|
|
300
275
|
let db = await processDatabase({ method: 'get.database', database })
|
|
301
|
-
db.close()
|
|
302
276
|
|
|
303
277
|
let objectStoreNames = Array.from(db.objectStoreNames)
|
|
304
278
|
if (data.method == 'read.array') {
|
|
@@ -314,48 +288,70 @@ async function processArray(data, newData, database, type) {
|
|
|
314
288
|
newData.push({ name: objectStoreNames[i], $storage: 'indexeddb', $database: database })
|
|
315
289
|
}
|
|
316
290
|
} else {
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
if (data.method == 'delete.array')
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
291
|
+
db.close()
|
|
292
|
+
|
|
293
|
+
let request = await processDatabase({ method: 'get.database', database, upgrade: true })
|
|
294
|
+
await new Promise((resolve) => {
|
|
295
|
+
request.onupgradeneeded = function (event) {
|
|
296
|
+
let db = event.target.result
|
|
297
|
+
let arrays
|
|
298
|
+
if (data.method == 'update.array')
|
|
299
|
+
arrays = Object.keys(data[type])
|
|
300
|
+
else
|
|
301
|
+
arrays = data[type];
|
|
302
|
+
if (!Array.isArray(arrays))
|
|
303
|
+
arrays = [arrays]
|
|
304
|
+
|
|
305
|
+
for (let i = 0; i < arrays.length; i++) {
|
|
306
|
+
let error, array = arrays[i]
|
|
307
|
+
|
|
308
|
+
let arrayExist = db.objectStoreNames.contains(array)
|
|
309
|
+
if (arrayExist && data.method == 'create.array') {
|
|
310
|
+
error = 'array already exists'
|
|
311
|
+
} else if (data.method == 'delete.array') {
|
|
312
|
+
if (objectStoreNames.includes(array)) {
|
|
313
|
+
db.deleteObjectStore(array);
|
|
314
|
+
} else {
|
|
315
|
+
error = 'the array does not exist'
|
|
316
|
+
}
|
|
317
|
+
} else {
|
|
318
|
+
if (!arrayExist && data.method == 'update.array')
|
|
319
|
+
array = data[type][arrays[i]]
|
|
320
|
+
|
|
321
|
+
if (arrayExist) {
|
|
322
|
+
if (data.method == 'update.array') {
|
|
323
|
+
let transaction = event.target.transaction;
|
|
324
|
+
let objectStore = transaction.objectStore(array);
|
|
325
|
+
|
|
326
|
+
if (!objectStoreNames.includes(data[type][arrays[i]]))
|
|
327
|
+
array = objectStore.name = data[type][arrays[i]];
|
|
328
|
+
else
|
|
329
|
+
error = 'An array with the new name already exist'
|
|
330
|
+
}
|
|
331
|
+
} else if (data.method == 'update.array')
|
|
332
|
+
error = 'array does not exists'
|
|
347
333
|
}
|
|
348
|
-
} else if (data.method == 'update.array')
|
|
349
|
-
error = 'array does not exists'
|
|
350
|
-
}
|
|
351
334
|
|
|
352
|
-
|
|
335
|
+
if (!error)
|
|
336
|
+
newData.push({ name: array, $storage: 'indexeddb', $database: database, array })
|
|
337
|
+
else
|
|
338
|
+
errorHandler(data, error, database)
|
|
353
339
|
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
340
|
+
}
|
|
341
|
+
};
|
|
342
|
+
|
|
343
|
+
request.onsuccess = function (event) {
|
|
344
|
+
event.target.result.close()
|
|
345
|
+
resolve()
|
|
346
|
+
};
|
|
347
|
+
|
|
348
|
+
request.onerror = function (event) {
|
|
349
|
+
errorHandler(data, event.target.error, database)
|
|
350
|
+
resolve()
|
|
351
|
+
};
|
|
352
|
+
|
|
353
|
+
|
|
354
|
+
});
|
|
359
355
|
}
|
|
360
356
|
}
|
|
361
357
|
|
|
@@ -514,33 +510,45 @@ async function processObject(data, newData, database, array, type) {
|
|
|
514
510
|
|
|
515
511
|
let isIndex = false
|
|
516
512
|
let indexName, direction;
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
513
|
+
|
|
514
|
+
if (data.$filter) {
|
|
515
|
+
if (data.$filter.sort && data.$filter.sort[0] && data.$filter.sort[0].key) {
|
|
516
|
+
indexName = data.$filter.sort[0].key
|
|
517
|
+
if (indexName.includes('-'))
|
|
518
|
+
isIndex = false
|
|
519
|
+
else
|
|
520
|
+
isIndex = true
|
|
521
|
+
|
|
522
|
+
direction = data.$filter.sort[0].direction
|
|
523
|
+
if (direction == 'desc')
|
|
524
|
+
direction = 'prev'
|
|
525
|
+
else
|
|
526
|
+
direction = 'next'
|
|
527
|
+
|
|
528
|
+
} else if (data.$filter.query && data.$filter.query[0] && data.$filter.query[0].key && data.$filter.query[0].index) {
|
|
529
|
+
if (data.$filter.query[0].operator === '$eq')
|
|
530
|
+
range = IDBKeyRange.only(data.$filter.query[0].value);
|
|
531
|
+
else if (data.$filter.query[0].operator === '$gt')
|
|
532
|
+
range = IDBKeyRange.lowerBound(data.$filter.query[0].value, true);
|
|
533
|
+
else if (data.$filter.query[0].operator === '$lt')
|
|
534
|
+
range = IDBKeyRange.upperBound(data.$filter.query[0].value, true);
|
|
535
|
+
|
|
536
|
+
indexName = data.$filter.query[0].key
|
|
537
|
+
if (indexName.includes('-'))
|
|
538
|
+
isIndex = false
|
|
539
|
+
else
|
|
540
|
+
isIndex = true
|
|
541
|
+
}
|
|
523
542
|
}
|
|
524
543
|
|
|
525
544
|
if (isIndex) {
|
|
526
|
-
direction = data.$filter.sort[0].direction
|
|
527
|
-
if (direction == 'desc')
|
|
528
|
-
direction = 'prev'
|
|
529
|
-
else
|
|
530
|
-
direction = 'next'
|
|
531
|
-
|
|
532
545
|
let indexNames = Array.from(objectStore.indexNames)
|
|
533
546
|
let indexExist = indexNames.includes(indexName)
|
|
534
547
|
if (!indexExist) {
|
|
535
548
|
db.close()
|
|
536
|
-
db = await processDatabase({ method: 'get.database', database, upgrade: true })
|
|
537
|
-
|
|
549
|
+
db = await processDatabase({ method: 'get.database', database, array, indexName, upgrade: true })
|
|
550
|
+
objectStore = db.transaction(array, 'readwrite');
|
|
538
551
|
objectStore = transaction.objectStore(array);
|
|
539
|
-
try {
|
|
540
|
-
objectStore.createIndex(indexName, indexName, { unique: false })
|
|
541
|
-
} catch (error) {
|
|
542
|
-
// console.log(error)
|
|
543
|
-
}
|
|
544
552
|
}
|
|
545
553
|
objectStore = objectStore.index(indexName);
|
|
546
554
|
}
|
|
@@ -561,7 +569,6 @@ async function processObject(data, newData, database, array, type) {
|
|
|
561
569
|
|
|
562
570
|
if (index)
|
|
563
571
|
newData = newData.slice(index)
|
|
564
|
-
|
|
565
572
|
}
|
|
566
573
|
|
|
567
574
|
}
|
|
@@ -587,13 +594,13 @@ function openCursor(objectStore, range, direction, data, newData, isFilter, limi
|
|
|
587
594
|
if (isFilter) {
|
|
588
595
|
if (cursor)
|
|
589
596
|
isMatch = filter(objectStore, data[type][i], data[type][i], cursor.value)
|
|
590
|
-
else
|
|
597
|
+
else if (data.method === 'update.object' && !upsert)
|
|
591
598
|
isMatch = false
|
|
592
599
|
}
|
|
593
600
|
|
|
594
601
|
if (isMatch !== false) {
|
|
595
602
|
try {
|
|
596
|
-
data[type][i]._id = ObjectId(data[type][i]._id);
|
|
603
|
+
data[type][i]._id = ObjectId(data[type][i]._id).toString();
|
|
597
604
|
} catch (error) {
|
|
598
605
|
data[type][i]._id = ObjectId().toString()
|
|
599
606
|
}
|
|
@@ -602,17 +609,21 @@ function openCursor(objectStore, range, direction, data, newData, isFilter, limi
|
|
|
602
609
|
if (data.method == 'create.object') {
|
|
603
610
|
result = await add(objectStore, data[type][i])
|
|
604
611
|
} else if (data.method == 'update.object') {
|
|
605
|
-
|
|
612
|
+
let update
|
|
613
|
+
if (cursor)
|
|
614
|
+
update = createUpdate(cursor.value, data[type][i], globalOperators)
|
|
615
|
+
else
|
|
616
|
+
update = createUpdate({}, data[type][i], globalOperators)
|
|
617
|
+
if (update)
|
|
618
|
+
result = await put(objectStore, update)
|
|
606
619
|
}
|
|
607
620
|
|
|
608
621
|
if (result)
|
|
609
622
|
newData.push({ $storage: 'indexeddb', $database: database, $array: array, ...result })
|
|
610
|
-
|
|
611
|
-
resolve();
|
|
612
623
|
}
|
|
624
|
+
resolve();
|
|
613
625
|
} else if (cursor) {
|
|
614
626
|
let isMatch = true
|
|
615
|
-
|
|
616
627
|
if (isFilter)
|
|
617
628
|
isMatch = filter(objectStore, data, data[type][i], cursor.value)
|
|
618
629
|
|
|
@@ -638,8 +649,11 @@ function openCursor(objectStore, range, direction, data, newData, isFilter, limi
|
|
|
638
649
|
|
|
639
650
|
if (!limit || limit && limit < newData.length) {
|
|
640
651
|
cursor.continue();
|
|
641
|
-
} else {
|
|
652
|
+
} else if (cursor.close) {
|
|
642
653
|
cursor.close()
|
|
654
|
+
resolve();
|
|
655
|
+
} else {
|
|
656
|
+
resolve();
|
|
643
657
|
}
|
|
644
658
|
} else {
|
|
645
659
|
resolve();
|
|
@@ -655,7 +669,8 @@ function openCursor(objectStore, range, direction, data, newData, isFilter, limi
|
|
|
655
669
|
|
|
656
670
|
function add(objectStore, object) {
|
|
657
671
|
return new Promise((resolve, reject) => {
|
|
658
|
-
const
|
|
672
|
+
const store = objectStore.objectStore || objectStore
|
|
673
|
+
let request = store.add(object);
|
|
659
674
|
request.onsuccess = () => resolve(object);
|
|
660
675
|
request.onerror = () => reject(request.error);
|
|
661
676
|
});
|
|
@@ -671,7 +686,9 @@ function get(objectStore, object) {
|
|
|
671
686
|
|
|
672
687
|
function put(objectStore, object) {
|
|
673
688
|
return new Promise((resolve, reject) => {
|
|
674
|
-
const
|
|
689
|
+
const store = objectStore.objectStore || objectStore
|
|
690
|
+
let request = store.put(object);
|
|
691
|
+
|
|
675
692
|
request.onsuccess = () => resolve(object);
|
|
676
693
|
request.onerror = () => reject(request.error);
|
|
677
694
|
});
|
|
@@ -725,9 +742,6 @@ function createUpdate(update, data, globalOpertors) {
|
|
|
725
742
|
operatorKeys = { ...globalOpertors }
|
|
726
743
|
|
|
727
744
|
Object.keys(data).forEach(key => {
|
|
728
|
-
if (key === '_id')
|
|
729
|
-
return
|
|
730
|
-
|
|
731
745
|
if (key.startsWith('$')) {
|
|
732
746
|
if (!key.includes('.'))
|
|
733
747
|
for (let oldkey of Object.keys(data[key]))
|
|
@@ -800,8 +814,10 @@ function dotNotationToObjectUpdate(data, object = {}) {
|
|
|
800
814
|
} else if (operator === '$push' || operator === '$splice') {
|
|
801
815
|
if (typeof keys[i] === 'number' && newObject.length >= keys[i])
|
|
802
816
|
newObject.splice(keys[i], 0, value);
|
|
803
|
-
else
|
|
817
|
+
else if (newObject[keys[i]])
|
|
804
818
|
newObject[keys[i]].push(value);
|
|
819
|
+
else
|
|
820
|
+
newObject[keys[i]] = [value];
|
|
805
821
|
} else if (operator === '$each') {
|
|
806
822
|
if (!Array.isArray(value))
|
|
807
823
|
value = [value]
|