Created
February 8, 2017 18:34
-
-
Save chl03ks/97871ff4b3fc41f4979a12ca7849a226 to your computer and use it in GitHub Desktop.
Auto-migrate and Auto-update Promise Module loopback
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | |
}); | |
} | |
} | |
} | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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