diff options
author | Alex Yatskov <alex@foosoft.net> | 2016-09-13 15:59:18 -0700 |
---|---|---|
committer | Alex Yatskov <alex@foosoft.net> | 2016-09-13 15:59:18 -0700 |
commit | 0e89d0e7e68d58407480e0e40e80c2f00f3c2f66 (patch) | |
tree | ce43416ec948bb83328eba0dde738f86f0df780f /ext/bg/js/dictionary.js | |
parent | cd4f16c096746d13502dc7b258dfe16c97344ba1 (diff) |
Database stuff
Diffstat (limited to 'ext/bg/js/dictionary.js')
-rw-r--r-- | ext/bg/js/dictionary.js | 41 |
1 files changed, 34 insertions, 7 deletions
diff --git a/ext/bg/js/dictionary.js b/ext/bg/js/dictionary.js index 20a94f8f..936dc3c1 100644 --- a/ext/bg/js/dictionary.js +++ b/ext/bg/js/dictionary.js @@ -20,26 +20,53 @@ class Dictionary { constructor() { this.db = null; + this.dbVer = 1; this.entities = null; } initDb() { + if (this.db !== null) { + return Promise.reject('database already initialized'); + } + this.db = new Dexie('dict'); this.db.version(1).stores({ terms: '++id,expression,reading', entities: '++,name', - kanji: '++,character' + kanji: '++,character', + meta: 'name,value', }); - - this.entities = null; } - deleteDb() { - return this.db === null ? Promise.resolve() : this.db.delete(); + prepareDb() { + 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() { - return Dexie.exists('dict'); + sealDb() { + if (this.db === null) { + return Promise.reject('database not initialized'); + } + + return this.db.meta.put({name: 'version', value: this.dbVer}); } findTerm(term) { |