diff options
author | frankiebee <frankie.diamond@gmail.com> | 2017-03-24 10:14:08 +0800 |
---|---|---|
committer | frankiebee <frankie.diamond@gmail.com> | 2017-03-24 10:14:08 +0800 |
commit | 282775c52f558bbd90d4d0a1706a835dde6c3202 (patch) | |
tree | 5539676e8940496e6b10c906157f302d1a6c4a62 /library/controllers/index-db-controller.js | |
parent | 8e30acb41a2cd97efd5428c60c2a5c910758b412 (diff) | |
download | tangerine-wallet-browser-282775c52f558bbd90d4d0a1706a835dde6c3202.tar tangerine-wallet-browser-282775c52f558bbd90d4d0a1706a835dde6c3202.tar.gz tangerine-wallet-browser-282775c52f558bbd90d4d0a1706a835dde6c3202.tar.bz2 tangerine-wallet-browser-282775c52f558bbd90d4d0a1706a835dde6c3202.tar.lz tangerine-wallet-browser-282775c52f558bbd90d4d0a1706a835dde6c3202.tar.xz tangerine-wallet-browser-282775c52f558bbd90d4d0a1706a835dde6c3202.tar.zst tangerine-wallet-browser-282775c52f558bbd90d4d0a1706a835dde6c3202.zip |
add versioning and migrateing data
Diffstat (limited to 'library/controllers/index-db-controller.js')
-rw-r--r-- | library/controllers/index-db-controller.js | 25 |
1 files changed, 13 insertions, 12 deletions
diff --git a/library/controllers/index-db-controller.js b/library/controllers/index-db-controller.js index 6bcae9845..041ddae2e 100644 --- a/library/controllers/index-db-controller.js +++ b/library/controllers/index-db-controller.js @@ -23,20 +23,25 @@ module.exports = class IndexDbController extends EventEmitter { } dbOpenRequest.onsuccess = (event) => { this.db = dbOpenRequest.result - if (!this.db.objectStoreNames.length) { - Object.keys(this.initialState).forEach((key) => { - this._add(key, this.initialState[key]) - }) - } this.emit('success') resolve(this.db) } dbOpenRequest.onupgradeneeded = (event) => { - // if (this.migrators) this.db = event.target.result - this.migrate() + this.db.createObjectStore('dataStore') } }) + .then((openRequest) => { + return this.get('dataStore') + }) + .then((data) => { + if (!data) { + return this._add('dataStore', this.initialState) + .then(() => this.get('dataStore')) + .then((versionedData) => Promise.resolve(versionedData.data)) + } + return Promise.resolve(data) + }) } requestObjectStore (key, type = 'readonly') { @@ -47,7 +52,7 @@ module.exports = class IndexDbController extends EventEmitter { }) } - get (key) { + get (key = 'dataStore') { return this.requestObjectStore(key) .then((dataObject)=> { return new Promise((resolve, reject) => { @@ -67,10 +72,6 @@ module.exports = class IndexDbController extends EventEmitter { }) } - migrate () { - this.db.createObjectStore('dataStore') - } - _add (key, objStore, cb = logger) { return this.requestObjectStore(key, 'readwrite') .then((dataObject)=> { |