Use native indexedDB pointer

This commit is contained in:
toasted-nutbread 2019-11-03 16:13:40 -05:00
parent 183ec6b381
commit e48bfb8f1a

View File

@ -23,11 +23,11 @@ class Database {
} }
async prepare() { async prepare() {
if (this.db) { if (this.db !== null) {
throw new Error('Database already initialized'); throw new Error('Database already initialized');
} }
const idb = await Database.open('dict', 4, (db, transaction, oldVersion) => { this.db = await Database.open('dict', 4, (db, transaction, oldVersion) => {
Database.upgrade(db, transaction, oldVersion, [ Database.upgrade(db, transaction, oldVersion, [
{ {
version: 2, version: 2,
@ -78,13 +78,6 @@ class Database {
} }
]); ]);
}); });
this.db = {
backendDB: () => idb,
close: () => idb.close(),
get name() { return idb.name; },
set name(v) {}
};
} }
async purge() { async purge() {
@ -120,10 +113,8 @@ class Database {
progressRate = 1000; progressRate = 1000;
} }
const db = this.db.backendDB();
for (const [objectStoreName, index] of targets) { for (const [objectStoreName, index] of targets) {
const dbTransaction = db.transaction([objectStoreName], 'readwrite'); const dbTransaction = this.db.transaction([objectStoreName], 'readwrite');
const dbObjectStore = dbTransaction.objectStore(objectStoreName); const dbObjectStore = dbTransaction.objectStore(objectStoreName);
const dbIndex = dbObjectStore.index(index); const dbIndex = dbObjectStore.index(index);
const only = IDBKeyRange.only(dictionaryName); const only = IDBKeyRange.only(dictionaryName);
@ -146,8 +137,7 @@ class Database {
} }
}; };
const db = this.db.backendDB(); const dbTransaction = this.db.transaction(['terms'], 'readonly');
const dbTransaction = db.transaction(['terms'], 'readonly');
const dbTerms = dbTransaction.objectStore('terms'); const dbTerms = dbTransaction.objectStore('terms');
const dbIndex1 = dbTerms.index('expression'); const dbIndex1 = dbTerms.index('expression');
const dbIndex2 = dbTerms.index('reading'); const dbIndex2 = dbTerms.index('reading');
@ -176,8 +166,7 @@ class Database {
} }
}; };
const db = this.db.backendDB(); const dbTransaction = this.db.transaction(['terms'], 'readonly');
const dbTransaction = db.transaction(['terms'], 'readonly');
const dbTerms = dbTransaction.objectStore('terms'); const dbTerms = dbTransaction.objectStore('terms');
const dbIndex = dbTerms.index('expression'); const dbIndex = dbTerms.index('expression');
@ -202,8 +191,7 @@ class Database {
} }
}; };
const db = this.db.backendDB(); const dbTransaction = this.db.transaction(['terms'], 'readonly');
const dbTransaction = db.transaction(['terms'], 'readonly');
const dbTerms = dbTransaction.objectStore('terms'); const dbTerms = dbTransaction.objectStore('terms');
const dbIndex = dbTerms.index('sequence'); const dbIndex = dbTerms.index('sequence');
@ -240,8 +228,7 @@ class Database {
} }
}; };
const db = this.db.backendDB(); const dbTransaction = this.db.transaction([tableName], 'readonly');
const dbTransaction = db.transaction([tableName], 'readonly');
const dbTerms = dbTransaction.objectStore(tableName); const dbTerms = dbTransaction.objectStore(tableName);
const dbIndex = dbTerms.index(indexName); const dbIndex = dbTerms.index(indexName);
@ -259,8 +246,7 @@ class Database {
this.validate(); this.validate();
let result = null; let result = null;
const db = this.db.backendDB(); const dbTransaction = this.db.transaction(['tagMeta'], 'readonly');
const dbTransaction = db.transaction(['tagMeta'], 'readonly');
const dbTerms = dbTransaction.objectStore('tagMeta'); const dbTerms = dbTransaction.objectStore('tagMeta');
const dbIndex = dbTerms.index('name'); const dbIndex = dbTerms.index('name');
const only = IDBKeyRange.only(name); const only = IDBKeyRange.only(name);
@ -277,8 +263,7 @@ class Database {
this.validate(); this.validate();
const results = []; const results = [];
const db = this.db.backendDB(); const dbTransaction = this.db.transaction(['dictionaries'], 'readonly');
const dbTransaction = db.transaction(['dictionaries'], 'readonly');
const dbDictionaries = dbTransaction.objectStore('dictionaries'); const dbDictionaries = dbTransaction.objectStore('dictionaries');
await Database.getAll(dbDictionaries, null, null, info => results.push(info)); await Database.getAll(dbDictionaries, null, null, info => results.push(info));
@ -296,8 +281,7 @@ class Database {
'termMeta', 'termMeta',
'tagMeta' 'tagMeta'
]; ];
const db = this.db.backendDB(); const dbCountTransaction = this.db.transaction(objectStoreNames, 'readonly');
const dbCountTransaction = db.transaction(objectStoreNames, 'readonly');
const targets = []; const targets = [];
for (const objectStoreName of objectStoreNames) { for (const objectStoreName of objectStoreNames) {
@ -334,7 +318,7 @@ class Database {
const maxTransactionLength = 1000; const maxTransactionLength = 1000;
const bulkAdd = async (objectStoreName, items, total, current) => { const bulkAdd = async (objectStoreName, items, total, current) => {
const db = this.db.backendDB(); const db = this.db;
for (let i = 0; i < items.length; i += maxTransactionLength) { for (let i = 0; i < items.length; i += maxTransactionLength) {
if (progressCallback) { if (progressCallback) {
progressCallback(total, current + i / items.length); progressCallback(total, current + i / items.length);
@ -360,7 +344,7 @@ class Database {
throw new Error('Unsupported dictionary version'); throw new Error('Unsupported dictionary version');
} }
const db = this.db.backendDB(); const db = this.db;
const dbCountTransaction = db.transaction(['dictionaries'], 'readonly'); const dbCountTransaction = db.transaction(['dictionaries'], 'readonly');
const dbIndex = dbCountTransaction.objectStore('dictionaries').index('title'); const dbIndex = dbCountTransaction.objectStore('dictionaries').index('title');
const only = IDBKeyRange.only(summary.title); const only = IDBKeyRange.only(summary.title);