diff options
author | Alex Yatskov <alex@foosoft.net> | 2016-11-07 22:12:18 -0800 |
---|---|---|
committer | Alex Yatskov <alex@foosoft.net> | 2016-11-07 22:12:18 -0800 |
commit | 638fe54c2a947347c304b39421c8ea8e0503f274 (patch) | |
tree | e0816c43e309a0d08c0b239eb32872ea8e4d474a /ext | |
parent | 407da51918d9621932bc6f8de4cf9b87efc78ef5 (diff) |
Prevent duplicate import
Diffstat (limited to 'ext')
-rw-r--r-- | ext/bg/js/database.js | 24 |
1 files changed, 13 insertions, 11 deletions
diff --git a/ext/bg/js/database.js b/ext/bg/js/database.js index 79a0a7a1..50a6ecd7 100644 --- a/ext/bg/js/database.js +++ b/ext/bg/js/database.js @@ -161,19 +161,21 @@ class Database { } const indexLoaded = (title, version, entities, hasTerms, hasKanji) => { - return this.db.dictionaries.add({title, version, hasTerms, hasKanji}).then(() => { - this.entities = entities || {}; - - const rows = []; - for (const name in entities || {}) { - rows.push({ - name, - value: entities[name], - dictionary: title - }); + return this.db.dictionaries.where('title').equals(title).count().then(count => { + if (count > 0) { + return Promise.reject(`dictionary "${title}" is already imported`); } - return this.db.entities.bulkAdd(rows); + return this.db.dictionaries.add({title, version, hasTerms, hasKanji}).then(() => { + this.entities = entities || {}; + + const rows = []; + for (const name in entities || {}) { + rows.push({name, value: entities[name], dictionary: title}); + } + + return this.db.entities.bulkAdd(rows); + }); }); }; |