Skip to content

Instantly share code, notes, and snippets.

@vafrcor
Last active December 5, 2020 18:20
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vafrcor/db503cc0cf568ff13159abfdee5cb5b8 to your computer and use it in GitHub Desktop.
Save vafrcor/db503cc0cf568ff13159abfdee5cb5b8 to your computer and use it in GitHub Desktop.
Node Sequelize Migrator Programmatically
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;
};
@AlexRex
Copy link

AlexRex commented Sep 29, 2020

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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment