Database stuff
This commit is contained in:
parent
cd4f16c096
commit
0e89d0e7e6
@ -20,26 +20,53 @@
|
|||||||
class Dictionary {
|
class Dictionary {
|
||||||
constructor() {
|
constructor() {
|
||||||
this.db = null;
|
this.db = null;
|
||||||
|
this.dbVer = 1;
|
||||||
this.entities = null;
|
this.entities = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
initDb() {
|
initDb() {
|
||||||
|
if (this.db !== null) {
|
||||||
|
return Promise.reject('database already initialized');
|
||||||
|
}
|
||||||
|
|
||||||
this.db = new Dexie('dict');
|
this.db = new Dexie('dict');
|
||||||
this.db.version(1).stores({
|
this.db.version(1).stores({
|
||||||
terms: '++id,expression,reading',
|
terms: '++id,expression,reading',
|
||||||
entities: '++,name',
|
entities: '++,name',
|
||||||
kanji: '++,character'
|
kanji: '++,character',
|
||||||
|
meta: 'name,value',
|
||||||
});
|
});
|
||||||
|
|
||||||
this.entities = null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
deleteDb() {
|
prepareDb() {
|
||||||
return this.db === null ? Promise.resolve() : this.db.delete();
|
this.initDb();
|
||||||
|
|
||||||
|
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.initDb();
|
||||||
|
return false;
|
||||||
|
});
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
existsDb() {
|
sealDb() {
|
||||||
return Dexie.exists('dict');
|
if (this.db === null) {
|
||||||
|
return Promise.reject('database not initialized');
|
||||||
|
}
|
||||||
|
|
||||||
|
return this.db.meta.put({name: 'version', value: this.dbVer});
|
||||||
}
|
}
|
||||||
|
|
||||||
findTerm(term) {
|
findTerm(term) {
|
||||||
|
@ -35,15 +35,14 @@ class Translator {
|
|||||||
return loadJson('bg/data/tags.json');
|
return loadJson('bg/data/tags.json');
|
||||||
}).then(tagMeta => {
|
}).then(tagMeta => {
|
||||||
this.tagMeta = tagMeta;
|
this.tagMeta = tagMeta;
|
||||||
return this.dictionary.existsDb();
|
return this.dictionary.prepareDb();
|
||||||
}).then(exists => {
|
}).then(exists => {
|
||||||
this.dictionary.initDb();
|
|
||||||
if (!exists) {
|
if (!exists) {
|
||||||
return Promise.all([
|
return Promise.all([
|
||||||
this.dictionary.importKanjiDict('bg/data/kanjidic/index.json'),
|
this.dictionary.importKanjiDict('bg/data/kanjidic/index.json'),
|
||||||
this.dictionary.importTermDict('bg/data/edict/index.json'),
|
this.dictionary.importTermDict('bg/data/edict/index.json'),
|
||||||
this.dictionary.importTermDict('bg/data/enamdict/index.json')
|
this.dictionary.importTermDict('bg/data/enamdict/index.json')
|
||||||
]);
|
]).then(() => this.dictionary.sealDb());
|
||||||
}
|
}
|
||||||
}).then(() => {
|
}).then(() => {
|
||||||
this.loaded = true;
|
this.loaded = true;
|
||||||
|
Loading…
Reference in New Issue
Block a user