diff --git a/.gitignore b/.gitignore index 71ac584da7..feed91778f 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ # See http://help.github.com/ignore-files/ for more about ignoring files. +/.idea # compiled output /dist @@ -29,3 +30,5 @@ server/config.js newrelic_agent.log newrelic.js + +/async-disk-cache \ No newline at end of file diff --git a/app/services/config.js b/app/services/config.js index 6c2e51622a..b083433b90 100644 --- a/app/services/config.js +++ b/app/services/config.js @@ -15,7 +15,13 @@ export default Ember.Service.extend({ this.set('configDB', db); this.setCurrentUser(); return db; - }).then(replicateConfigDB).then(loadConfig); + }) + .then(replicateConfigDB) + .catch((err) => { + console.log('replicate db error', err); + }) + .finally(loadConfig) + .catch((err)=>console.log(err)); }, createDB() { @@ -51,13 +57,13 @@ export default Ember.Service.extend({ console.log('Could not get configDB configs:', err); reject(err); } - const config = {}; + const configObj = {}; for (var i = 0; i < response.rows.length; i++) { if (!response.rows[i].error && response.rows[i].doc) { - config[response.rows[i].id] = response.rows[i].doc.value; + configObj[response.rows[i].id] = response.rows[i].doc.value; } } - resolve(config); + resolve(configObj); }); }, 'getting configuration from the database'); }, diff --git a/app/services/database.js b/app/services/database.js index 273eb3a645..244b238fb5 100644 --- a/app/services/database.js +++ b/app/services/database.js @@ -21,7 +21,7 @@ export default Ember.Service.extend(PouchAdapterUtils, { createDB(configs) { return new Ember.RSVP.Promise((resolve, reject) => { let pouchOptions = {}; - if (configs.config_use_google_auth) { + if (configs && configs.config_use_google_auth) { pouchOptions.ajax = { timeout: 30000 }; @@ -43,15 +43,13 @@ export default Ember.Service.extend(PouchAdapterUtils, { } } const url = `${document.location.protocol}//${document.location.host}/db/main`; - new PouchDB(url, pouchOptions, (err, db) => { - if (err) { - reject(err); - return; - } - createPouchViews(db); - resolve(db); - }); - }); + + this._createRemoteDB(url, pouchOptions) + .catch(() => this._createLocalDB('localMainDB', pouchOptions)) + .then((db) => resolve(db)) + .catch((err) => reject(err)); + + }, 'initialize application db'); }, queryMainDB(queryParams, mapReduce) { @@ -154,5 +152,36 @@ export default Ember.Service.extend(PouchAdapterUtils, { }); } return mappedRows; + }, + + _createRemoteDB(remoteUrl, pouchOptions) { + return new Ember.RSVP.Promise(function(resolve, reject) { + new PouchDB(remoteUrl, pouchOptions, (errRemote, remoteDB) => { + if (errRemote) { + reject(errRemote); + return; + } + + // remote db lazy created, check if db created correctly + remoteDB.info().then(() => { + createPouchViews(remoteDB); + resolve(remoteDB); + }).catch((err) => reject(err)); + }); + }); + }, + + _createLocalDB(localDBName, pouchOptions) { + return new Ember.RSVP.Promise(function(resolve, reject) { + new PouchDB(localDBName, pouchOptions, (errLocal, localDB) => { + if (errLocal) { + reject(errLocal); + return; + } + + createPouchViews(localDB); + resolve(localDB); + }); + }); } }); diff --git a/app/services/filesystem.js b/app/services/filesystem.js index 33062ae61e..e7e2e627cf 100644 --- a/app/services/filesystem.js +++ b/app/services/filesystem.js @@ -191,7 +191,7 @@ export default Ember.Service.extend({ }, /** - * Property to to determine if file system API is available. + * Property to determine if file system API is available. */ isFileSystemEnabled: function() { var filer = this.get('filer'); diff --git a/config/environment.js b/config/environment.js index c848905b61..5cd6f70c0d 100644 --- a/config/environment.js +++ b/config/environment.js @@ -62,7 +62,7 @@ module.exports = function(environment) { if (environment === 'production') { ENV.serviceWorker.debug = false; } - + return ENV; };