Fixes for Edge

This commit is contained in:
toasted-nutbread 2019-11-09 20:48:30 -05:00
parent 75883ed885
commit aa92855b37

View File

@ -27,57 +27,63 @@ class Database {
throw new Error('Database already initialized'); throw new Error('Database already initialized');
} }
this.db = await Database.open('dict', 4, (db, transaction, oldVersion) => { try {
Database.upgrade(db, transaction, oldVersion, [ this.db = await Database.open('dict', 4, (db, transaction, oldVersion) => {
{ Database.upgrade(db, transaction, oldVersion, [
version: 2, {
stores: { version: 2,
terms: { stores: {
primaryKey: {keyPath: 'id', autoIncrement: true}, terms: {
indices: ['dictionary', 'expression', 'reading'] primaryKey: {keyPath: 'id', autoIncrement: true},
}, indices: ['dictionary', 'expression', 'reading']
kanji: { },
primaryKey: {autoIncrement: true}, kanji: {
indices: ['dictionary', 'character'] primaryKey: {autoIncrement: true},
}, indices: ['dictionary', 'character']
tagMeta: { },
primaryKey: {autoIncrement: true}, tagMeta: {
indices: ['dictionary'] primaryKey: {autoIncrement: true},
}, indices: ['dictionary']
dictionaries: { },
primaryKey: {autoIncrement: true}, dictionaries: {
indices: ['title', 'version'] 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: 4,
stores: {
terms: {
primaryKey: {keyPath: 'id', autoIncrement: true},
indices: ['dictionary', 'expression', 'reading', 'sequence']
}
} }
} }
}, ]);
{ });
version: 3, return true;
stores: { } catch (e) {
termMeta: { console.error(e);
primaryKey: {autoIncrement: true}, return false;
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']
}
}
}
]);
});
} }
async purge() { async purge() {
@ -786,14 +792,15 @@ class Database {
for (const objectStoreName of objectStoreNames) { for (const objectStoreName of objectStoreNames) {
const {primaryKey, indices} = stores[objectStoreName]; const {primaryKey, indices} = stores[objectStoreName];
const objectStoreNames = transaction.objectStoreNames || db.objectStoreNames;
const objectStore = ( const objectStore = (
transaction.objectStoreNames.contains(objectStoreName) ? Database.listContains(objectStoreNames, objectStoreName) ?
transaction.objectStore(objectStoreName) : transaction.objectStore(objectStoreName) :
db.createObjectStore(objectStoreName, primaryKey) db.createObjectStore(objectStoreName, primaryKey)
); );
for (const indexName of indices) { for (const indexName of indices) {
if (objectStore.indexNames.contains(indexName)) { continue; } if (Database.listContains(objectStore.indexNames, indexName)) { continue; }
objectStore.createIndex(indexName, indexName, {}); objectStore.createIndex(indexName, indexName, {});
} }
@ -808,4 +815,11 @@ class Database {
request.onsuccess = () => resolve(); 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;
}
} }