forked from indexeddbshim/IndexedDBShim
/
IDBObjectStore.js
843 lines (778 loc) · 34.3 KB
/
IDBObjectStore.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
import {createDOMException} from './DOMException';
import {IDBCursor, IDBCursorWithValue} from './IDBCursor';
import {setSQLForKeyRange, convertValueToKeyRange} from './IDBKeyRange';
import DOMStringList from './DOMStringList';
import * as util from './util';
import * as Key from './Key';
import {executeFetchIndexData, buildFetchIndexDataSQL, IDBIndex} from './IDBIndex';
import IDBTransaction from './IDBTransaction';
import * as Sca from './Sca';
import CFG from './CFG';
import SyncPromise from 'sync-promise';
const readonlyProperties = ['keyPath', 'indexNames', 'transaction', 'autoIncrement'];
/**
* IndexedDB Object Store
* http://dvcs.w3.org/hg/IndexedDB/raw-file/tip/Overview.html#idl-def-IDBObjectStore
* @param {IDBObjectStoreProperties} storeProperties
* @param {IDBTransaction} transaction
* @constructor
*/
function IDBObjectStore () {
throw new TypeError('Illegal constructor');
}
const IDBObjectStoreAlias = IDBObjectStore;
IDBObjectStore.__createInstance = function (storeProperties, transaction) {
function IDBObjectStore () {
const me = this;
me[Symbol.toStringTag] = 'IDBObjectStore';
util.defineReadonlyProperties(this, readonlyProperties);
me.__name = me.__originalName = storeProperties.name;
me.__keyPath = Array.isArray(storeProperties.keyPath) ? storeProperties.keyPath.slice() : storeProperties.keyPath;
me.__transaction = transaction;
me.__idbdb = storeProperties.idbdb;
me.__cursors = storeProperties.cursors || [];
// autoInc is numeric (0/1) on WinPhone
me.__autoIncrement = !!storeProperties.autoInc;
me.__indexes = {};
me.__indexHandles = {};
me.__indexNames = DOMStringList.__createInstance();
const indexList = storeProperties.indexList;
for (const indexName in indexList) {
if (indexList.hasOwnProperty(indexName)) {
const index = IDBIndex.__createInstance(me, indexList[indexName]);
me.__indexes[index.name] = index;
if (!index.__deleted) {
me.indexNames.push(index.name);
}
}
}
me.__oldIndexNames = me.indexNames.clone();
Object.defineProperty(this, '__currentName', {
get: function () {
return '__pendingName' in this ? this.__pendingName : this.name;
}
});
Object.defineProperty(this, 'name', {
enumerable: false,
configurable: false,
get: function () {
return this.__name;
},
set: function (name) {
const me = this;
name = util.convertToDOMString(name);
const oldName = me.name;
IDBObjectStoreAlias.__invalidStateIfDeleted(me);
IDBTransaction.__assertVersionChange(me.transaction);
IDBTransaction.__assertActive(me.transaction);
if (oldName === name) {
return;
}
if (me.__idbdb.__objectStores[name] && !me.__idbdb.__objectStores[name].__pendingDelete) {
throw createDOMException('ConstraintError', 'Object store "' + name + '" already exists in ' + me.__idbdb.name);
}
me.__name = name;
const oldStore = me.__idbdb.__objectStores[oldName];
oldStore.__name = name; // Fix old references
me.__idbdb.__objectStores[name] = oldStore; // Ensure new reference accessible
delete me.__idbdb.__objectStores[oldName]; // Ensure won't be found
me.__idbdb.objectStoreNames.splice(me.__idbdb.objectStoreNames.indexOf(oldName), 1, name);
const oldHandle = me.transaction.__storeHandles[oldName];
oldHandle.__name = name; // Fix old references
me.transaction.__storeHandles[name] = oldHandle; // Ensure new reference accessible
me.__pendingName = oldName;
const sql = 'UPDATE __sys__ SET "name" = ? WHERE "name" = ?';
const sqlValues = [util.escapeSQLiteStatement(name), util.escapeSQLiteStatement(oldName)];
CFG.DEBUG && console.log(sql, sqlValues);
me.transaction.__addNonRequestToTransactionQueue(function objectStoreClear (tx, args, success, error) {
tx.executeSql(sql, sqlValues, function (tx, data) {
// This SQL preserves indexes per https://www.sqlite.org/lang_altertable.html
const sql = 'ALTER TABLE ' + util.escapeStoreNameForSQL(oldName) + ' RENAME TO ' + util.escapeStoreNameForSQL(name);
CFG.DEBUG && console.log(sql);
tx.executeSql(sql, [], function (tx, data) {
delete me.__pendingName;
success();
});
}, function (tx, err) {
error(err);
});
});
}
});
}
IDBObjectStore.prototype = IDBObjectStoreAlias.prototype;
return new IDBObjectStore();
};
/**
* Clones an IDBObjectStore instance for a different IDBTransaction instance.
* @param {IDBObjectStore} store
* @param {IDBTransaction} transaction
* @protected
*/
IDBObjectStore.__clone = function (store, transaction) {
const newStore = IDBObjectStore.__createInstance({
name: store.__currentName,
keyPath: Array.isArray(store.keyPath) ? store.keyPath.slice() : store.keyPath,
autoInc: store.autoIncrement,
indexList: {},
idbdb: store.__idbdb,
cursors: store.__cursors
}, transaction);
['__indexes', '__indexNames', '__oldIndexNames', '__deleted', '__pendingDelete', '__pendingCreate', '__originalName'].forEach((p) => {
newStore[p] = store[p];
});
return newStore;
};
IDBObjectStore.__invalidStateIfDeleted = function (store, msg) {
if (store.__deleted || store.__pendingDelete || (store.__pendingCreate && (store.transaction && store.transaction.__errored))) {
throw createDOMException('InvalidStateError', msg || 'This store has been deleted');
}
};
/**
* Creates a new object store in the database.
* @param {IDBDatabase} db
* @param {IDBObjectStore} store
* @protected
*/
IDBObjectStore.__createObjectStore = function (db, store) {
// Add the object store to the IDBDatabase
const storeName = store.__currentName;
store.__pendingCreate = true;
db.__objectStores[storeName] = store;
db.objectStoreNames.push(storeName);
// Add the object store to WebSQL
const transaction = db.__versionTransaction;
const storeHandles = transaction.__storeHandles;
if (!storeHandles[storeName] ||
// These latter conditions are to allow store
// recreation to create new clone object
storeHandles[storeName].__pendingDelete ||
storeHandles[storeName].__deleted) {
storeHandles[storeName] = IDBObjectStore.__clone(store, transaction);
}
transaction.__addNonRequestToTransactionQueue(function createObjectStore (tx, args, success, failure) {
function error (tx, err) {
CFG.DEBUG && console.log(err);
failure(createDOMException('UnknownError', 'Could not create object store "' + storeName + '"', err));
}
const escapedStoreNameSQL = util.escapeStoreNameForSQL(storeName);
// key INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL UNIQUE
const sql = ['CREATE TABLE', escapedStoreNameSQL, '(key BLOB', store.autoIncrement ? 'UNIQUE, inc INTEGER PRIMARY KEY AUTOINCREMENT' : 'PRIMARY KEY', ', value BLOB)'].join(' ');
CFG.DEBUG && console.log(sql);
tx.executeSql(sql, [], function (tx, data) {
function insertStoreInfo () {
const encodedKeyPath = JSON.stringify(store.keyPath);
tx.executeSql('INSERT INTO __sys__ VALUES (?,?,?,?,?)', [util.escapeSQLiteStatement(storeName), encodedKeyPath, store.autoIncrement, '{}', 1], function () {
delete store.__pendingCreate;
delete store.__deleted;
success(store);
}, error);
}
if (!CFG.useSQLiteIndexes) {
insertStoreInfo();
return;
}
tx.executeSql('CREATE INDEX IF NOT EXISTS ' + (
util.sqlQuote('sk_' + escapedStoreNameSQL.slice(1, -1))
) + ' ON ' + escapedStoreNameSQL + '("key")', [], insertStoreInfo, error);
}, error);
});
return storeHandles[storeName];
};
/**
* Deletes an object store from the database.
* @param {IDBDatabase} db
* @param {IDBObjectStore} store
* @protected
*/
IDBObjectStore.__deleteObjectStore = function (db, store) {
// Remove the object store from the IDBDatabase
store.__pendingDelete = true;
// We don't delete the other index holders in case need reversion
store.__indexNames = DOMStringList.__createInstance();
db.objectStoreNames.splice(db.objectStoreNames.indexOf(store.__currentName), 1);
const storeHandle = db.__versionTransaction.__storeHandles[store.__currentName];
if (storeHandle) {
storeHandle.__indexNames = DOMStringList.__createInstance();
storeHandle.__pendingDelete = true;
}
// Remove the object store from WebSQL
const transaction = db.__versionTransaction;
transaction.__addNonRequestToTransactionQueue(function deleteObjectStore (tx, args, success, failure) {
function error (tx, err) {
CFG.DEBUG && console.log(err);
failure(createDOMException('UnknownError', 'Could not delete ObjectStore', err));
}
tx.executeSql('SELECT "name" FROM __sys__ WHERE "name" = ?', [util.escapeSQLiteStatement(store.__currentName)], function (tx, data) {
if (data.rows.length > 0) {
tx.executeSql('DROP TABLE ' + util.escapeStoreNameForSQL(store.__currentName), [], function () {
tx.executeSql('DELETE FROM __sys__ WHERE "name" = ?', [util.escapeSQLiteStatement(store.__currentName)], function () {
delete store.__pendingDelete;
store.__deleted = true;
if (storeHandle) {
delete storeHandle.__pendingDelete;
storeHandle.__deleted = true;
}
success();
}, error);
}, error);
}
});
});
};
// Todo: Although we may end up needing to do cloning genuinely asynchronously (for Blobs and FileLists),
// and we'll want to ensure the queue starts up synchronously, we nevertheless do the cloning
// before entering the queue and its callback since the encoding we do is preceded by validation
// which we must do synchronously anyways. If we reimplement Blobs and FileLists asynchronously,
// we can detect these types (though validating synchronously as possible) and once entering the
// queue callback, ensure they load before triggering success or failure (perhaps by returning and
// a `SyncPromise` from the `Sca.clone` operation and later detecting and ensuring it is resolved
// before continuing).
/**
* Determines whether the given inline or out-of-line key is valid, according to the object store's schema.
* @param {*} value Used for inline keys
* @param {*} key Used for out-of-line keys
* @private
*/
IDBObjectStore.prototype.__validateKeyAndValueAndCloneValue = function (value, key, cursorUpdate) {
const me = this;
if (me.keyPath !== null) {
if (key !== undefined) {
throw createDOMException('DataError', 'The object store uses in-line keys and the key parameter was provided', me);
}
// Todo Binary: Avoid blobs loading async to ensure cloning (and errors therein)
// occurs sync; then can make cloning and this method without callbacks (except where ok
// to be async)
const clonedValue = Sca.clone(value);
key = Key.extractKeyValueDecodedFromValueUsingKeyPath(clonedValue, me.keyPath); // May throw so "rethrow"
if (key.invalid) {
throw createDOMException('DataError', 'KeyPath was specified, but key was invalid.');
}
if (key.failure) {
if (!cursorUpdate) {
if (!me.autoIncrement) {
throw createDOMException('DataError', 'Could not evaluate a key from keyPath and there is no key generator');
}
if (!Key.checkKeyCouldBeInjectedIntoValue(clonedValue, me.keyPath)) {
throw createDOMException('DataError', 'A key could not be injected into a value');
}
// A key will be generated
return [undefined, clonedValue];
}
throw createDOMException('DataError', 'Could not evaluate a key from keyPath');
}
// An `IDBCursor.update` call will also throw if not equal to the cursor’s effective key
return [key.value, clonedValue];
}
if (key === undefined) {
if (!me.autoIncrement) {
throw createDOMException('DataError', 'The object store uses out-of-line keys and has no key generator and the key parameter was not provided.', me);
}
// A key will be generated
key = undefined;
} else {
Key.convertValueToKeyRethrowingAndIfInvalid(key);
}
const clonedValue = Sca.clone(value);
return [key, clonedValue];
};
/**
* From the store properties and object, extracts the value for the key in the object store
* If the table has auto increment, get the current number (unless it has a keyPath leading to a
* valid but non-numeric or < 1 key)
* @param {Object} tx
* @param {Object} value
* @param {Object} key
* @param {function} success
* @param {function} failure
*/
IDBObjectStore.prototype.__deriveKey = function (tx, value, key, success, failCb) {
const me = this;
// Only run if cloning is needed
function keyCloneThenSuccess (oldCn) { // We want to return the original key, so we don't need to accept an argument here
Sca.encode(key, function (key) {
key = Sca.decode(key);
success(key, oldCn);
});
}
if (me.autoIncrement) {
// If auto-increment and no valid primaryKey found on the keyPath, get and set the new value, and use
if (key === undefined) {
Key.generateKeyForStore(tx, me, function (failure, key, oldCn) {
if (failure) {
failCb(createDOMException('ConstraintError', 'The key generator\'s current number has reached the maximum safe integer limit'));
return;
}
if (me.keyPath !== null) {
// Should not throw now as checked earlier
Key.injectKeyIntoValueUsingKeyPath(value, key, me.keyPath);
}
success(key, oldCn);
}, failCb);
} else {
Key.possiblyUpdateKeyGenerator(tx, me, key, keyCloneThenSuccess, failCb);
}
// Not auto-increment
} else {
keyCloneThenSuccess();
}
};
IDBObjectStore.prototype.__insertData = function (tx, encoded, value, clonedKeyOrCurrentNumber, oldCn, success, error) {
const me = this;
// The `ConstraintError` to occur for `add` upon a duplicate will occur naturally in attempting an insert
// We process the index information first as it will stored in the same table as the store
const paramMap = {};
const indexPromises = Object.keys(
// We do not iterate `indexNames` as those can be modified synchronously (e.g.,
// `deleteIndex` could, by its synchronous removal from `indexNames`, prevent
// iteration here of an index though per IndexedDB test
// `idbobjectstore_createIndex4-deleteIndex-event_order.js`, `createIndex`
// should be allowed to first fail even in such a case).
me.__indexes
).map((indexName) => {
// While this may sometimes resolve sync and sometimes async, the
// idea is to avoid, where possible, unnecessary delays (and
// consuming code ought to only see a difference in the browser
// where we can't control the transaction timeout anyways).
return new SyncPromise((resolve, reject) => {
const index = me.__indexes[indexName];
if (
// `createIndex` was called synchronously after the current insertion was added to
// the transaction queue so although it was added to `__indexes`, it is not yet
// ready to be checked here for the insertion as it will be when running the
// `createIndex` operation (e.g., if two items with the same key were added and
// *then* a unique index was created, it should not continue to err and abort
// yet, as we're still handling the insertions which must be processed (e.g., to
// add duplicates which then cause a unique index to fail))
index.__pendingCreate ||
// If already deleted (and not just slated for deletion (by `__pendingDelete`
// after this add), we avoid checks
index.__deleted
) {
resolve();
return;
}
let indexKey;
try {
indexKey = Key.extractKeyValueDecodedFromValueUsingKeyPath(value, index.keyPath, index.multiEntry); // Add as necessary to this and skip past this index if exceptions here)
if (indexKey.invalid || indexKey.failure) {
throw new Error('Go to catch');
}
} catch (err) {
resolve();
return;
}
indexKey = indexKey.value;
function setIndexInfo (index) {
if (indexKey === undefined) {
return;
}
paramMap[index.__currentName] = Key.encode(indexKey, index.multiEntry);
}
if (index.unique) {
const multiCheck = index.multiEntry && Array.isArray(indexKey);
const fetchArgs = buildFetchIndexDataSQL(true, index, indexKey, 'key', multiCheck);
executeFetchIndexData(null, ...fetchArgs, tx, null, function success (key) {
if (key === undefined) {
setIndexInfo(index);
resolve();
return;
}
reject(createDOMException(
'ConstraintError',
'Index already contains a record equal to ' +
(multiCheck ? 'one of the subkeys of' : '') +
'`indexKey`'
));
}, reject);
} else {
setIndexInfo(index);
resolve();
}
});
});
SyncPromise.all(indexPromises).then(() => {
const sqlStart = ['INSERT INTO', util.escapeStoreNameForSQL(me.__currentName), '('];
const sqlEnd = [' VALUES ('];
const insertSqlValues = [];
if (clonedKeyOrCurrentNumber !== undefined) {
// Key.convertValueToKey(primaryKey); // Already run
sqlStart.push(util.sqlQuote('key'), ',');
sqlEnd.push('?,');
insertSqlValues.push(util.escapeSQLiteStatement(Key.encode(clonedKeyOrCurrentNumber)));
}
for (const key in paramMap) {
sqlStart.push(util.escapeIndexNameForSQL(key) + ',');
sqlEnd.push('?,');
insertSqlValues.push(util.escapeSQLiteStatement(paramMap[key]));
}
// removing the trailing comma
sqlStart.push(util.sqlQuote('value') + ' )');
sqlEnd.push('?)');
insertSqlValues.push(util.escapeSQLiteStatement(encoded));
const insertSql = sqlStart.join(' ') + sqlEnd.join(' ');
CFG.DEBUG && console.log('SQL for adding', insertSql, insertSqlValues);
tx.executeSql(insertSql, insertSqlValues, function (tx, data) {
success(clonedKeyOrCurrentNumber);
}, function (tx, err) {
// Should occur for `add` operation
error(createDOMException('ConstraintError', err.message, err));
});
}).catch(function (err) {
function fail () {
// Todo: Add a different error object here if `assignCurrentNumber` fails in reverting?
error(err);
}
if (typeof oldCn === 'number') {
Key.assignCurrentNumber(tx, me, oldCn, fail, fail);
return;
}
fail();
});
};
IDBObjectStore.prototype.add = function (value /* , key */) {
const me = this;
const key = arguments[1];
if (!(me instanceof IDBObjectStore)) {
throw new TypeError('Illegal invocation');
}
if (arguments.length === 0) {
throw new TypeError('No value was specified');
}
IDBObjectStore.__invalidStateIfDeleted(me);
IDBTransaction.__assertActive(me.transaction);
me.transaction.__assertWritable();
const request = me.transaction.__createRequest(me);
const [ky, clonedValue] = me.__validateKeyAndValueAndCloneValue(value, key, false);
IDBObjectStore.__storingRecordObjectStore(request, me, true, clonedValue, true, ky);
return request;
};
IDBObjectStore.prototype.put = function (value /*, key */) {
const me = this;
const key = arguments[1];
if (!(me instanceof IDBObjectStore)) {
throw new TypeError('Illegal invocation');
}
if (arguments.length === 0) {
throw new TypeError('No value was specified');
}
IDBObjectStore.__invalidStateIfDeleted(me);
IDBTransaction.__assertActive(me.transaction);
me.transaction.__assertWritable();
const request = me.transaction.__createRequest(me);
const [ky, clonedValue] = me.__validateKeyAndValueAndCloneValue(value, key, false);
IDBObjectStore.__storingRecordObjectStore(request, me, true, clonedValue, false, ky);
return request;
};
IDBObjectStore.prototype.__overwrite = function (tx, key, cb, error) {
const me = this;
// First try to delete if the record exists
// Key.convertValueToKey(key); // Already run
const sql = 'DELETE FROM ' + util.escapeStoreNameForSQL(me.__currentName) + ' WHERE "key" = ?';
const encodedKey = Key.encode(key);
tx.executeSql(sql, [util.escapeSQLiteStatement(encodedKey)], function (tx, data) {
CFG.DEBUG && console.log('Did the row with the', key, 'exist? ', data.rowsAffected);
cb(tx);
}, function (tx, err) {
error(err);
});
};
IDBObjectStore.__storingRecordObjectStore = function (request, store, invalidateCache, value, noOverwrite /* , key */) {
const key = arguments[5];
store.transaction.__pushToQueue(request, function (tx, args, success, error) {
store.__deriveKey(tx, value, key, function (clonedKeyOrCurrentNumber, oldCn) {
Sca.encode(value, function (encoded) {
function insert (tx) {
store.__insertData(tx, encoded, value, clonedKeyOrCurrentNumber, oldCn, function (...args) {
if (invalidateCache) {
store.__cursors.forEach((cursor) => {
cursor.__invalidateCache();
});
}
success(...args);
}, error);
}
if (!noOverwrite) {
store.__overwrite(tx, clonedKeyOrCurrentNumber, insert, error);
return;
}
insert(tx);
});
}, error);
});
};
IDBObjectStore.prototype.__get = function (query, getKey, getAll, count) {
const me = this;
if (count !== undefined) {
count = util.enforceRange(count, 'unsigned long');
}
IDBObjectStore.__invalidStateIfDeleted(me);
IDBTransaction.__assertActive(me.transaction);
const range = convertValueToKeyRange(query, !getAll);
const col = getKey ? 'key' : 'value';
let sql = ['SELECT', util.sqlQuote(col), 'FROM', util.escapeStoreNameForSQL(me.__currentName)];
const sqlValues = [];
if (range !== undefined) {
sql.push('WHERE');
setSQLForKeyRange(range, util.sqlQuote('key'), sql, sqlValues);
}
if (!getAll) {
count = 1;
}
if (count) {
if (typeof count !== 'number' || isNaN(count) || !isFinite(count)) {
throw new TypeError('The count parameter must be a finite number');
}
sql.push('LIMIT', count);
}
sql = sql.join(' ');
return me.transaction.__addToTransactionQueue(function objectStoreGet (tx, args, success, error) {
CFG.DEBUG && console.log('Fetching', me.__currentName, sqlValues);
tx.executeSql(sql, sqlValues, function (tx, data) {
CFG.DEBUG && console.log('Fetched data', data);
let ret;
try {
// Opera can't deal with the try-catch here.
if (data.rows.length === 0) {
return getAll ? success([]) : success();
}
ret = [];
if (getKey) {
for (let i = 0; i < data.rows.length; i++) {
// Key.convertValueToKey(data.rows.item(i).key); // Already validated before storage
ret.push(
Key.decode(util.unescapeSQLiteResponse(data.rows.item(i).key), false)
);
}
} else {
for (let i = 0; i < data.rows.length; i++) {
ret.push(
Sca.decode(util.unescapeSQLiteResponse(data.rows.item(i).value))
);
}
}
if (!getAll) {
ret = ret[0];
}
} catch (e) {
// If no result is returned, or error occurs when parsing JSON
CFG.DEBUG && console.log(e);
}
success(ret);
}, function (tx, err) {
error(err);
});
}, undefined, me);
};
IDBObjectStore.prototype.get = function (query) {
if (!arguments.length) {
throw new TypeError('A parameter was missing for `IDBObjectStore.get`.');
}
return this.__get(query);
};
IDBObjectStore.prototype.getKey = function (query) {
if (!arguments.length) {
throw new TypeError('A parameter was missing for `IDBObjectStore.getKey`.');
}
return this.__get(query, true);
};
IDBObjectStore.prototype.getAll = function (/* query, count */) {
const [query, count] = arguments;
return this.__get(query, false, true, count);
};
IDBObjectStore.prototype.getAllKeys = function (/* query, count */) {
const [query, count] = arguments;
return this.__get(query, true, true, count);
};
IDBObjectStore.prototype['delete'] = function (query) {
const me = this;
if (!(this instanceof IDBObjectStore)) {
throw new TypeError('Illegal invocation');
}
if (!arguments.length) {
throw new TypeError('A parameter was missing for `IDBObjectStore.delete`.');
}
IDBObjectStore.__invalidStateIfDeleted(me);
IDBTransaction.__assertActive(me.transaction);
me.transaction.__assertWritable();
const range = convertValueToKeyRange(query, true);
const sqlArr = ['DELETE FROM', util.escapeStoreNameForSQL(me.__currentName), 'WHERE'];
const sqlValues = [];
setSQLForKeyRange(range, util.sqlQuote('key'), sqlArr, sqlValues);
const sql = sqlArr.join(' ');
return me.transaction.__addToTransactionQueue(function objectStoreDelete (tx, args, success, error) {
CFG.DEBUG && console.log('Deleting', me.__currentName, sqlValues);
tx.executeSql(sql, sqlValues, function (tx, data) {
CFG.DEBUG && console.log('Deleted from database', data.rowsAffected);
me.__cursors.forEach((cursor) => {
cursor.__invalidateCache(); // Delete
});
success();
}, function (tx, err) {
error(err);
});
}, undefined, me);
};
IDBObjectStore.prototype.clear = function () {
const me = this;
if (!(this instanceof IDBObjectStore)) {
throw new TypeError('Illegal invocation');
}
IDBObjectStore.__invalidStateIfDeleted(me);
IDBTransaction.__assertActive(me.transaction);
me.transaction.__assertWritable();
return me.transaction.__addToTransactionQueue(function objectStoreClear (tx, args, success, error) {
tx.executeSql('DELETE FROM ' + util.escapeStoreNameForSQL(me.__currentName), [], function (tx, data) {
CFG.DEBUG && console.log('Cleared all records from database', data.rowsAffected);
me.__cursors.forEach((cursor) => {
cursor.__invalidateCache(); // Clear
});
success();
}, function (tx, err) {
error(err);
});
}, undefined, me);
};
IDBObjectStore.prototype.count = function (/* query */) {
const me = this;
const query = arguments[0];
if (!(me instanceof IDBObjectStore)) {
throw new TypeError('Illegal invocation');
}
IDBObjectStore.__invalidStateIfDeleted(me);
IDBTransaction.__assertActive(me.transaction);
// We don't need to add to cursors array since has the count parameter which won't cache
return IDBCursorWithValue.__createInstance(query, 'next', me, me, 'key', 'value', true).__req;
};
IDBObjectStore.prototype.openCursor = function (/* query, direction */) {
const me = this;
const [query, direction] = arguments;
if (!(me instanceof IDBObjectStore)) {
throw new TypeError('Illegal invocation');
}
IDBObjectStore.__invalidStateIfDeleted(me);
const cursor = IDBCursorWithValue.__createInstance(query, direction, me, me, 'key', 'value');
me.__cursors.push(cursor);
return cursor.__req;
};
IDBObjectStore.prototype.openKeyCursor = function (/* query, direction */) {
const me = this;
if (!(me instanceof IDBObjectStore)) {
throw new TypeError('Illegal invocation');
}
IDBObjectStore.__invalidStateIfDeleted(me);
const [query, direction] = arguments;
const cursor = IDBCursor.__createInstance(query, direction, me, me, 'key', 'key');
me.__cursors.push(cursor);
return cursor.__req;
};
IDBObjectStore.prototype.index = function (indexName) {
const me = this;
if (!(me instanceof IDBObjectStore)) {
throw new TypeError('Illegal invocation');
}
if (arguments.length === 0) {
throw new TypeError('No index name was specified');
}
IDBObjectStore.__invalidStateIfDeleted(me);
IDBTransaction.__assertNotFinished(me.transaction);
const index = me.__indexes[indexName];
if (!index || index.__deleted) {
throw createDOMException('NotFoundError', 'Index "' + indexName + '" does not exist on ' + me.__currentName);
}
if (!me.__indexHandles[indexName] ||
me.__indexes[indexName].__pendingDelete ||
me.__indexes[indexName].__deleted
) {
me.__indexHandles[indexName] = IDBIndex.__clone(index, me);
}
return me.__indexHandles[indexName];
};
/**
* Creates a new index on the object store.
* @param {string} indexName
* @param {string} keyPath
* @param {object} optionalParameters
* @returns {IDBIndex}
*/
IDBObjectStore.prototype.createIndex = function (indexName, keyPath /* , optionalParameters */) {
const me = this;
let optionalParameters = arguments[2];
if (!(me instanceof IDBObjectStore)) {
throw new TypeError('Illegal invocation');
}
indexName = String(indexName); // W3C test within IDBObjectStore.js seems to accept string conversion
if (arguments.length === 0) {
throw new TypeError('No index name was specified');
}
if (arguments.length === 1) {
throw new TypeError('No key path was specified');
}
IDBTransaction.__assertVersionChange(me.transaction);
IDBObjectStore.__invalidStateIfDeleted(me);
IDBTransaction.__assertActive(me.transaction);
if (me.__indexes[indexName] && !me.__indexes[indexName].__deleted && !me.__indexes[indexName].__pendingDelete) {
throw createDOMException('ConstraintError', 'Index "' + indexName + '" already exists on ' + me.__currentName);
}
keyPath = util.convertToSequenceDOMString(keyPath);
if (!util.isValidKeyPath(keyPath)) {
throw createDOMException('SyntaxError', 'A valid keyPath must be supplied');
}
if (Array.isArray(keyPath) && optionalParameters && optionalParameters.multiEntry) {
throw createDOMException('InvalidAccessError', 'The keyPath argument was an array and the multiEntry option is true.');
}
optionalParameters = optionalParameters || {};
/** @name IDBIndexProperties **/
const indexProperties = {
columnName: indexName,
keyPath,
optionalParams: {
unique: !!optionalParameters.unique,
multiEntry: !!optionalParameters.multiEntry
}
};
const index = IDBIndex.__createInstance(me, indexProperties);
IDBIndex.__createIndex(me, index);
return index;
};
IDBObjectStore.prototype.deleteIndex = function (name) {
const me = this;
if (!(me instanceof IDBObjectStore)) {
throw new TypeError('Illegal invocation');
}
if (arguments.length === 0) {
throw new TypeError('No index name was specified');
}
IDBTransaction.__assertVersionChange(me.transaction);
IDBObjectStore.__invalidStateIfDeleted(me);
IDBTransaction.__assertActive(me.transaction);
const index = me.__indexes[name];
if (!index) {
throw createDOMException('NotFoundError', 'Index "' + name + '" does not exist on ' + me.__currentName);
}
IDBIndex.__deleteIndex(me, index);
};
readonlyProperties.forEach((prop) => {
Object.defineProperty(IDBObjectStore.prototype, prop, {
enumerable: true,
configurable: true,
get: function () {
throw new TypeError('Illegal invocation');
}
});
});
Object.defineProperty(IDBObjectStore.prototype, 'name', {
enumerable: true,
configurable: true,
get: function () {
throw new TypeError('Illegal invocation');
},
set: function (val) {
throw new TypeError('Illegal invocation');
}
});
IDBObjectStore.prototype[Symbol.toStringTag] = 'IDBObjectStorePrototype';
Object.defineProperty(IDBObjectStore, 'prototype', {
writable: false
});
export default IDBObjectStore;