Dictionary simplification

This commit is contained in:
Alex Yatskov 2016-11-13 12:49:28 -08:00
parent dab46eda3e
commit 2587b9ae6b

View File

@ -20,11 +20,10 @@
class Database { class Database {
constructor() { constructor() {
this.db = null; this.db = null;
this.dbVer = 6;
this.entities = {}; this.entities = {};
} }
init() { prepare() {
if (this.db !== null) { if (this.db !== null) {
return Promise.reject('database already initialized'); return Promise.reject('database already initialized');
} }
@ -35,41 +34,9 @@ class Database {
kanji: '++, dictionary, character', kanji: '++, dictionary, character',
entities: '++, dictionary', entities: '++, dictionary',
dictionaries: '++, title, version', dictionaries: '++, title, version',
meta: 'name, value',
}); });
return Promise.resolve(); return this.db.open();
}
prepare() {
this.init();
return this.db.meta.get('version').then(row => {
return row ? row.value : 0;
}).catch(() => {
return 0;
}).then(version => {
if (this.dbVer === version) {
return true;
}
const db = this.db;
this.db.close();
this.db = null;
return db.delete().then(() => {
this.init();
return false;
});
});
}
seal() {
if (this.db === null) {
return Promise.reject('database not initialized');
}
return this.db.meta.put({name: 'version', value: this.dbVer});
} }
findTerm(term, dictionaries) { findTerm(term, dictionaries) {