Skip to content

Instantly share code, notes, and snippets.

@chl03ks
Created February 8, 2017 18:34
Show Gist options
  • Save chl03ks/97871ff4b3fc41f4979a12ca7849a226 to your computer and use it in GitHub Desktop.
Save chl03ks/97871ff4b3fc41f4979a12ca7849a226 to your computer and use it in GitHub Desktop.
Auto-migrate and Auto-update Promise Module loopback
const app = require('../server');
const log = require('debug')('boot:automigrate');
const path = require('path');
const env = process.env.NODE_ENV;
let datasources = require(path.resolve(__dirname, '../datasources.production.json'));
let models = path.resolve(__dirname, '../model-config.production.json')
module.exports = {
autoUpdateAll: function(key) {
if (typeof models[key].dataSource != 'undefined') {
if (typeof datasources[models[key].dataSource] != 'undefined') {
return app.dataSources[models[key].dataSource].autoupdate(key)
.then(() => {
log('Model ' + key + ' updated');
return Promise.resolve(key);
})
.catch(() => {
log('Error running autoupdate(' + key + ')', err);
return Promise.reject(new Error('Error autoupdate(' + key + ')', err));
});
}
}
},
autoMigrateAll: function(key) {
if (typeof models[key].dataSource != 'undefined') {
if (typeof datasources[models[key].dataSource] != 'undefined') {
return app.dataSources[models[key].dataSource].automigrate(key)
.then(() => {
log('Model ' + key + ' migrated');
return Promise.resolve(key);
})
.catch((err) => {
log('Error running automigrate(' + key + ')', err);
return Promise.reject(new Error('Error automigrate(' + key + ')', err));
});
}
}
}
};
let models = require(path.resolve(__dirname, '../model-config.json'));
let databaseAction = env === 'production' ? database.autoMigrateAll: database.autoUpdateAll;
Promise.all(Object.keys(models).map(databaseAction))
.then(() => Promise.all(...)
.catch((error) => {
log('Error: ', error);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment