summaryrefslogtreecommitdiff
path: root/ext/bg/js/database.js
diff options
context:
space:
mode:
authortoasted-nutbread <toasted-nutbread@users.noreply.github.com>2019-11-09 20:48:30 -0500
committertoasted-nutbread <toasted-nutbread@users.noreply.github.com>2019-11-10 13:01:39 -0500
commitaa92855b37ac7b87f6c0f989d10b7208b543d154 (patch)
tree9f45e74bca20503b9045bb8ccf30824356bf8ccc /ext/bg/js/database.js
parent75883ed885961e2252f972e4ce7e9a26ede5cabe (diff)
Fixes for Edge
Diffstat (limited to 'ext/bg/js/database.js')
-rw-r--r--ext/bg/js/database.js112
1 files changed, 63 insertions, 49 deletions
diff --git a/ext/bg/js/database.js b/ext/bg/js/database.js
index c37fd9f7..b6cf9063 100644
--- a/ext/bg/js/database.js
+++ b/ext/bg/js/database.js
@@ -27,57 +27,63 @@ class Database {
throw new Error('Database already initialized');
}
- this.db = await Database.open('dict', 4, (db, transaction, oldVersion) => {
- Database.upgrade(db, transaction, oldVersion, [
- {
- version: 2,
- stores: {
- terms: {
- primaryKey: {keyPath: 'id', autoIncrement: true},
- indices: ['dictionary', 'expression', 'reading']
- },
- kanji: {
- primaryKey: {autoIncrement: true},
- indices: ['dictionary', 'character']
- },
- tagMeta: {
- primaryKey: {autoIncrement: true},
- indices: ['dictionary']
- },
- dictionaries: {
- primaryKey: {autoIncrement: true},
- indices: ['title', 'version']
+ try {
+ this.db = await Database.open('dict', 4, (db, transaction, oldVersion) => {
+ Database.upgrade(db, transaction, oldVersion, [
+ {
+ version: 2,
+ stores: {
+ terms: {
+ primaryKey: {keyPath: 'id', autoIncrement: true},
+ indices: ['dictionary', 'expression', 'reading']
+ },
+ kanji: {
+ primaryKey: {autoIncrement: true},
+ indices: ['dictionary', 'character']
+ },
+ tagMeta: {
+ primaryKey: {autoIncrement: true},
+ indices: ['dictionary']
+ },
+ dictionaries: {
+ primaryKey: {autoIncrement: true},
+ indices: ['title', 'version']
+ }
}
- }
- },
- {
- version: 3,
- stores: {
- termMeta: {
- primaryKey: {autoIncrement: true},
- indices: ['dictionary', 'expression']
- },
- kanjiMeta: {
- primaryKey: {autoIncrement: true},
- indices: ['dictionary', 'character']
- },
- tagMeta: {
- primaryKey: {autoIncrement: true},
- indices: ['dictionary', 'name']
+ },
+ {
+ version: 3,
+ stores: {
+ termMeta: {
+ primaryKey: {autoIncrement: true},
+ indices: ['dictionary', 'expression']
+ },
+ kanjiMeta: {
+ primaryKey: {autoIncrement: true},
+ indices: ['dictionary', 'character']
+ },
+ tagMeta: {
+ primaryKey: {autoIncrement: true},
+ indices: ['dictionary', 'name']
+ }
}
- }
- },
- {
- version: 4,
- stores: {
- terms: {
- primaryKey: {keyPath: 'id', autoIncrement: true},
- indices: ['dictionary', 'expression', 'reading', 'sequence']
+ },
+ {
+ version: 4,
+ stores: {
+ terms: {
+ primaryKey: {keyPath: 'id', autoIncrement: true},
+ indices: ['dictionary', 'expression', 'reading', 'sequence']
+ }
}
}
- }
- ]);
- });
+ ]);
+ });
+ return true;
+ } catch (e) {
+ console.error(e);
+ return false;
+ }
}
async purge() {
@@ -786,14 +792,15 @@ class Database {
for (const objectStoreName of objectStoreNames) {
const {primaryKey, indices} = stores[objectStoreName];
+ const objectStoreNames = transaction.objectStoreNames || db.objectStoreNames;
const objectStore = (
- transaction.objectStoreNames.contains(objectStoreName) ?
+ Database.listContains(objectStoreNames, objectStoreName) ?
transaction.objectStore(objectStoreName) :
db.createObjectStore(objectStoreName, primaryKey)
);
for (const indexName of indices) {
- if (objectStore.indexNames.contains(indexName)) { continue; }
+ if (Database.listContains(objectStore.indexNames, indexName)) { continue; }
objectStore.createIndex(indexName, indexName, {});
}
@@ -808,4 +815,11 @@ class Database {
request.onsuccess = () => resolve();
});
}
+
+ static listContains(list, value) {
+ for (let i = 0, ii = list.length; i < ii; ++i) {
+ if (list[i] === value) { return true; }
+ }
+ return false;
+ }
}