Last active
December 5, 2020 18:20
-
-
Save vafrcor/db503cc0cf568ff13159abfdee5cb5b8 to your computer and use it in GitHub Desktop.
Node Sequelize Migrator Programmatically
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 execSync = require('child_process').execSync; | |
const _ = require('lodash'); | |
const asyncjs = require('async'); | |
const sequelizeCliExec = (command) => { | |
let commands=[ | |
'db:drop', | |
'db:create', | |
'db:migrate', | |
'db:migrate:undo:all', | |
'db:seed:undo:all', | |
'db:seed:all' | |
]; | |
if(!_.includes(commands, command)){ | |
throw new Error('Invalid Sequelize Command: '+command); | |
} | |
return new Promise((resolve, reject) => { | |
try { | |
execSync('node_modules/.bin/sequelize '+command, { | |
stdio: 'inherit' | |
}); | |
resolve('OK'); | |
} catch (e) { | |
reject(e); | |
} | |
}); | |
}; | |
module.exports = function(options) { | |
var db_migrator = { | |
options: {}, | |
init: function(options) { | |
this.options = Object.assign({ | |
drop_existing_db: false, | |
drop_tables: true, | |
migration: true, | |
seed_rollback: true, | |
seed: true | |
}, options); | |
}, | |
exec: function() { | |
var self = this; | |
var tasks= {}; | |
if(self.options.drop_existing_db){ | |
tasks.drop_existing_db= function(cb){ | |
if(self.options.debug){ | |
console.log('Migrator run command: `sequelize db:drop`'); | |
} | |
sequelizeCliExec('db:drop').then(function(){ | |
cb(null, true); | |
}).catch(function(err){ | |
throw err; | |
}); | |
}; | |
} | |
if(self.options.drop_tables){ | |
tasks.drop_tables= function(cb){ | |
if(self.options.debug){ | |
console.log('Migrator run command: `sequelize db:migrate:undo:all`'); | |
} | |
sequelizeCliExec('db:migrate:undo:all').then(function(){ | |
cb(null, true); | |
}).catch(function(err){ | |
throw err; | |
}); | |
}; | |
} | |
if(self.options.migration){ | |
tasks.migration= function(cb){ | |
if(self.options.debug){ | |
console.log('Migrator run command: `sequelize db:migrate`'); | |
} | |
sequelizeCliExec('db:migrate').then(function(){ | |
cb(null, true); | |
}).catch(function(err){ | |
throw err; | |
}); | |
}; | |
} | |
if(self.options.seed_rollback){ | |
tasks.seed_rollback= function(cb){ | |
if(self.options.debug){ | |
console.log('Migrator run command: `sequelize db:seed:undo:all`'); | |
} | |
sequelizeCliExec('db:seed:undo:all').then(function(){ | |
cb(null, true); | |
}).catch(function(err){ | |
throw err; | |
}); | |
}; | |
} | |
if(self.options.seed){ | |
tasks.seeder= function(cb){ | |
if(self.options.debug){ | |
console.log('Migrator run command: `sequelize db:seed:all`'); | |
} | |
sequelizeCliExec('db:seed:all').then(function(){ | |
cb(null, true); | |
}).catch(function(err){ | |
throw err; | |
}); | |
}; | |
} | |
// execute serial flow | |
asyncjs.series(tasks, function(err, result){ | |
if(err){ | |
if(self.options.debug){ | |
console.log('Migrator error: ', err); | |
} | |
throw err; | |
} | |
if(self.options.debug){ | |
console.log('Migrator result: ', result); | |
} | |
}); | |
} | |
}; | |
db_migrator.init(options); | |
return db_migrator; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Good stuff! Just a minor, I don't think you need the whole lodash just to test if an array includes a string. You can use the native
array.includes(string)
.thanks!