-
Notifications
You must be signed in to change notification settings - Fork 5
/
indexed-db.js
749 lines (635 loc) · 17.8 KB
/
indexed-db.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
import Service, { inject as service } from '@ember/service';
import { set } from '@ember/object';
import { Promise } from 'rsvp';
import { later } from '@ember/runloop';
import { typeOf as getTypeOf } from '@ember/utils';
import { A as array } from '@ember/array';
import { task, timeout } from 'ember-concurrency';
import Dexie from 'dexie';
import { buildWaiter, waitForPromise } from '@ember/test-waiters';
import { macroCondition, isTesting } from '@embroider/macros';
const testWaiter = buildWaiter('ember-indexeddb');
/**
* This service allows interacting with an IndexedDB database.
*
* @module Services
* @class IndexedDb
* @extends Ember.Service
* @public
*/
export default class IndexedDbService extends Service {
@service store;
@service indexedDbConfiguration;
/**
* The actual Dexie database.
*
* @property db
* @type {Dexie}
* @public
*/
db;
/**
* The database name to use.
* Overwrite this if you want to use something different.
*
* @property databaseName
* @type {String}
* @default 'ember-indexeddb'
* @public
*/
databaseName = 'ember-indexeddb';
/**
* If set to true, it will output which indecies are used for queries.
* This can be used to debug your indecies.
*
* @property _shouldLogQuery
* @type {Boolean}
* @default false
* @private
*/
_shouldLogQuery = false;
/**
* This is an object with an array per model type.
* It holds all the objects per model type that should be bulk saved.
* After actually saving, this will be cleared.
*
* @property _saveQueue
* @type {Object}
* @private
*/
_saveQueue = {};
/**
* This is the test waiter used to ensure all promises are resolved in tests.
* This is set by the this._registerTestWaiter() method.
*
* @property _testWaiter
* @type {Function}
* @private
*/
_testWaiter;
/**
* This is a promise that is used for bulk saving.
* All bulkSave() operations use and return the same promise, which is cached here.
*
* @property _savePromise
* @type {Promise}
* @private
*/
_savePromise;
/**
* All currently running promises are temporarily saved here.
* This is used to check if there are running transactions.
*
* @property _promiseQueue
* @type {Promise[]}
* @private
*/
_promiseQueue = array();
/**
* e.g. MS Edge doesn't support compound indices.
* For these cases, querying shouldn't try to use them.
*
* @property _supportsCompoundIndicies
* @type {Boolean}
* @readOnly
* @private
*/
_supportsCompoundIndices = true;
/**
* Call this and wait until it resolves before doing anything with IndexedDB!
* This should be done in beforeModel() on the application route.
* It will reject if IndexedDB is not available.
*
* Also available as a task: `indexedDb.setupTask.perform()`
*
* @method setup
* @return {Promise}
* @public
*/
setup() {
this.setupTask.perform();
}
@task(function* () {
if (this.db) {
return this.db;
}
let testWaiterToken = testWaiter.beginAsync();
let db = new Dexie(this.databaseName);
let { indexedDbConfiguration } = this;
indexedDbConfiguration.setupDatabase(db);
set(this, 'db', db);
yield openDb(db);
testWaiter.endAsync(testWaiterToken);
return db;
})
setupTask;
/**
* Query indexedDB.
* This uses _buildQuery under the hood.
* This resolved to an array.
*
* @method query
* @param {String} type The model type to query
* @param {Object} query The query data
* @return {Promise[]}
* @public
*/
query(type, query) {
let queryPromise = this._buildQuery(type, query);
let promise = new Promise(
(resolve, reject) => queryPromise.toArray().then(resolve, reject),
'indexedDb/query'
);
this._addToPromiseQueue(promise);
return promise;
}
/**
* Query indexedDB.
* This uses _buildQuery under the hood.
* This resolved to an object.
*
* @method queryRecord
* @param {String} type The model type to query
* @param {Object} query The query data
* @return {Promise}
* @public
*/
queryRecord(type, query) {
let queryPromise = this._buildQuery(type, query);
let promise = new Promise(
(resolve, reject) => queryPromise.first().then(resolve, reject),
'indexedDb/queryRecord'
);
this._addToPromiseQueue(promise);
return promise;
}
/**
* Find one or multiple items by id.
* If id is an array, this will try to fetch all of these objects and resolve with an array.
* Otherwise, it will resolve with an object.
*
* @method find
* @param {String} type The model type to find
* @param {String|String[]} id One or multiple ids to fetch
* @return {Promise}
* @public
*/
find(type, id) {
let { db } = this;
if (getTypeOf(id) === 'array') {
return db[type].where('id').anyOf(id.map(this._toString)).toArray();
}
let promise = new Promise(
(resolve, reject) =>
db[type].get(this._toString(id)).then(resolve, reject),
'indexedDb/find'
);
this._addToPromiseQueue(promise);
return promise;
}
/**
* Find all of a given type.
*
* @method findAll
* @param {String} type The model type to find.
* @return {Promise}
* @public
*/
findAll(type) {
let { db } = this;
let promise = new Promise(
(resolve, reject) => db[type].toArray().then(resolve, reject),
'indexedDb/findAll'
);
this._addToPromiseQueue(promise);
return promise;
}
/**
* Add one or multiple items to the database.
*
* @method add
* @param {String} type The model type to add
* @param {Object|Object[]} items One or multiple objects to add to the database.
* @return {Promise}
* @public
*/
add(type, items) {
let { db } = this;
// Single Item?
if (getTypeOf(items) !== 'array') {
items = array([items]);
}
let data = items.map((item) => {
return this._mapItem(type, item);
});
let promise = new Promise(
(resolve, reject) => db[type].bulkPut(data).then(resolve, reject),
'indexedDb/add'
);
this._addToPromiseQueue(promise);
return promise;
}
/**
* Save/update an item.
*
* @method save
* @param {String} type The model type of the object
* @param {String} id The id of the object
* @param {Object} item The serialized object to save.
* @return {Promise}
* @public
*/
save(type, id, item) {
let { db } = this;
let data = this._mapItem(type, item);
let promise = new Promise(
(resolve, reject) => db[type].put(data).then(resolve, reject),
'indexedDb/save'
);
this._addToPromiseQueue(promise);
return promise;
}
/**
* This will wait for 10ms and try to build a queue, and save everything at once if possible.
*
* @method saveBulk
* @param {String} type The model type to save
* @param {Object} item The data to save
* @return {Promise}
* @public
*/
saveBulk(type, item) {
let { _savePromise: savePromise, _saveQueue: saveQueue } = this;
// If no save promise exists, create a new one
if (!savePromise) {
savePromise = new Promise((resolve, reject) => {
later(
this,
() => {
this._bulkSave().then((val) => {
set(this, '_savePromise', null);
resolve(val);
}, reject);
},
100
);
}, 'indexedDb/saveBulk');
set(this, '_savePromise', savePromise);
this._addToPromiseQueue(savePromise);
}
let queue = saveQueue[type];
if (!queue) {
queue = array();
saveQueue[type] = queue;
}
queue.pushObject(item);
return savePromise;
}
/**
* Clear a database table.
*
* @method clear
* @param {String} type The model type to clear.
* @return {Promise}
* @public
*/
clear(type) {
let { db } = this;
let promise = new Promise(
(resolve, reject) => db[type].clear().then(resolve, reject),
'indexedDb/clear'
);
this._addToPromiseQueue(promise);
return promise;
}
/**
* Delete one item.
*
* @method delete
* @param {String} type The model type to delete
* @param {String} id The id of the entry to delete
* @return {Promise}
* @public
*/
delete(type, id) {
let { db } = this;
let promise = new Promise(
(resolve, reject) => db[type].delete(id).then(resolve, reject),
'indexedDb/delete'
);
this._addToPromiseQueue(promise);
return promise;
}
/**
* Drop the entire database.
*
* Also available as a task: `indexedDb.dropDatabaseTask.perform()`
*
* @method dropDatabase
* @return {Promise}
* @public
*/
dropDatabase() {
return this.dropDatabaseTask.perform();
}
@task(function* () {
let { db } = this;
if (!db) {
return;
}
let testWaiterToken = testWaiter.beginAsync();
// Ensure the db is open
yield openDb(db);
yield this.waitForQueueTask.perform();
yield timeout(100);
yield db.delete();
yield closeDb(db);
set(this, 'db', null);
testWaiter.endAsync(testWaiterToken);
})
dropDatabaseTask;
/**
* Export a complete dump of the current database.
* The output of this can be used to recreate the exact database state via this.importDatabase(config);
*
* Also available as a task: `indexedDb.exportDatabaseTask.perform()`
*
* @method exportDatabase
* @return {Promise}
* @public
*/
exportDatabase() {
let promise = this.exportDatabaseTask.perform();
this._addToPromiseQueue(promise);
return promise;
}
@task(function* () {
let { db, databaseName, currentVersion: version } = this;
let config = {
databaseName,
version,
stores: {},
data: {},
};
// Now, open database without specifying any version. This will make the database open any existing database and read its schema automatically.
yield openDb(db);
// Save the last version number
set(config, 'version', db.verno);
let promises = array();
db.tables.forEach(function (table) {
let primKeyAndIndexes = [table.schema.primKey].concat(
table.schema.indexes
);
let schemaSyntax = primKeyAndIndexes
.map(function (index) {
return index.src;
})
.join(',');
set(config.stores, table.name, schemaSyntax);
let promise = table.each((object) => {
let arr = config.data[table.name];
if (!arr) {
arr = array();
set(config.data, table.name, arr);
}
arr.push(object);
});
promises.push(promise);
});
yield Promise.all(promises);
return config;
})
exportDatabaseTask;
/**
* Import a complete database dump as created by this.exportDatabase()
*
* Also available as a task: `indexedDb.importDatabaseTask.perform()`
*
* @method importDatabase
* @param {Object} config A configuration object as created by this.exportDatabase()
* @return {Promise}
* @public
*/
importDatabase(config) {
let promise = this.importDatabaseTask.perform(config);
this._addToPromiseQueue(promise);
return promise;
}
@task(function* (config) {
let { databaseName, version, stores, data } = config;
log('====================================');
log('Importing database dump!');
log('Dropping existing database...');
yield this.dropDatabaseTask.perform();
log(`Setting up database ${databaseName} in version ${version}...`);
let db = new Dexie(databaseName);
db.version(version).stores(stores);
log('Opening database...');
yield openDb(db);
let tables = Object.keys(data);
while (tables.length) {
let table = tables.shift();
log(`Importing ${data[table].length} rows for ${table}...`);
yield db[table].bulkPut(data[table]);
}
// This is closed here, don't forget to call 'setup' again, to do eventually necessary migrations
yield closeDb(db);
log('Database import done!');
})
importDatabaseTask;
/**
* Wait for all queued objects ot be resolved.
* This will resolve when there are no open processes anymore.
*
* Also available as a task: `indexedDb.waitForQueueTask.perform()`
*
* @method waitForQueue
* @return {Promise}
* @public
*/
waitForQueue() {
return this.waitForQueueTask.perform();
}
@task(function* () {
while (this._promiseQueue.length > 0 || this._savePromise) {
yield timeout(100);
}
})
waitForQueueTask;
/**
* Get the queue and save everything in it in bulk.
*
* @method _bulkSave
* @private
*/
_bulkSave() {
let { _saveQueue: saveQueue } = this;
let promises = array();
for (let i in saveQueue) {
promises.push(this.add(i, saveQueue[i]));
saveQueue[i] = array();
}
return Promise.all(promises, 'indexedDb/_bulkSave');
}
_logQuery(str, query) {
if (this._shouldLogQuery) {
// eslint-disable-next-line
console.log(`[QUERY]: ${str}`, query);
}
}
/**
* Build a query for Dexie.
*
* This will try to find a matching index, and use it if possible.
* It can also handle multi-indecies, if they have been specified and are supported.
* If no matching index is found, it will fetch everything and try to filter it via JS.
* Note that this is _much_ slower than querying by actual indecies, so you should definitely use that if possible!
* If you are using multiple query arguments, and only one of them is found as index, it will query the database by this index
* and then do the other queries via JS filter.
*
* Note that this will also auto-convert boolean query arguments to 1/0.
*
* @method _buildQuery
* @param {String} type The model type to query
* @param {Object} query The actual query
* @return {Dexie.Collection}
* @private
*/
_buildQuery(type, query) {
let { db, _supportsCompoundIndices: supportsCompoundIndices } = this;
let keys = Object.keys(query);
// Order of query params is important!
let { schema } = db[type];
let { indexes } = schema;
// Only one, try to find a simple index
if (keys.length === 1) {
let key = keys[0];
let index = indexes.find((index) => {
let { keyPath } = index;
return keyPath === key;
});
if (index) {
this._logQuery(`Using index "${key}"`, query);
let value = normalizeValue(query[key]);
return db[type].where(key).equals(value);
}
}
// try to find a fitting multi index
// only if the client supports compound indices!
if (keys.length > 1 && supportsCompoundIndices) {
let multiIndex = indexes.find((index) => {
let { keyPath } = index;
// If keyPath is not set, not an array or not the same length as the keys, it's not the correct one
if (
!keyPath ||
getTypeOf(keyPath) !== 'array' ||
keyPath.length !== keys.length
) {
return false;
}
// If one of the keys is not in the keyPath, return false
return !keys.find((key) => !keyPath.includes(key));
});
// If a multi index is found, use it
if (multiIndex) {
let { keyPath, name: keyName } = multiIndex;
let compareValues = array();
keyPath.forEach((key) => {
let value = normalizeValue(query[key]);
compareValues.push(value);
});
this._logQuery(`Using compound index "${keyPath}"`, query);
return db[type].where(keyName).equals(compareValues);
}
}
// Else, filter manually
// Try to find at least a single actual index, if possible...
let whereKey = keys.find((key) => {
return indexes.some((index) => {
let { keyPath } = index;
return keyPath === key;
});
});
let whereKeyValue = whereKey ? normalizeValue(query[whereKey]) : null;
let vanillaFilterKeys = keys.filter((key) => !whereKey || key !== whereKey);
let collection = whereKey
? db[type].where(whereKey).equals(whereKeyValue)
: db[type];
if (whereKey) {
this._logQuery(
`Using index "${whereKey}" and vanilla filtering for ${vanillaFilterKeys.join(
', '
)}`,
query
);
} else {
this._logQuery(`Using vanilla filtering`, query);
}
return collection.filter((item) => {
return vanillaFilterKeys.every((key) => {
return (
normalizeValue(item.json.attributes[key]) ===
normalizeValue(query[key])
);
});
});
}
_mapItem(type, item) {
let { indexedDbConfiguration } = this;
return indexedDbConfiguration.mapItem(type, item);
}
_toString(val) {
return `${val}`;
}
/**
* Add a promise to the promise queue.
* When the promise resolves or rejects, it will be removed from the promise queue.
*
* @method _addToPromiseQueue
* @param {Promise} promise
* @return {Promise}
* @private
*/
_addToPromiseQueue(promise) {
let { _promiseQueue: promiseQueue } = this;
let wrappedPromise = waitForPromise(promise);
promiseQueue.pushObject(wrappedPromise);
let removeObject = () => {
promiseQueue.removeObject(wrappedPromise);
};
wrappedPromise.finally(removeObject);
return wrappedPromise;
}
constructor() {
super(...arguments);
try {
window.IDBKeyRange.only([1]);
} catch (e) {
this._supportsCompoundIndices = false;
}
}
}
async function openDb(db) {
while (!db.isOpen()) {
await db.open();
}
return db;
}
async function closeDb(db) {
while (db.isOpen()) {
await db.close();
}
return db;
}
function normalizeValue(value) {
if (typeof value === 'boolean') {
return value ? 1 : 0;
}
return value;
}
function log(str) {
if (macroCondition(!isTesting())) {
// eslint-disable-next-line no-console
console.log(str);
}
}