Run SequelizeJs migration and seeders with NodeJs script.

Amar
2 min readDec 29, 2020

--

Many of us using SequelizeJs ORM for RDBMS Database such as PostgreSQL, MySQL and MSSQL. We can SequelizeJs for SQLite Database for Desktop applications as well. For instance, development of desktop applications by using ElectronJs.

The most common problem with SequelizeJs is running migration and seeders for before going to deploy or install our application. Especially, if you are using this for desktop applications where you need to run all these things before starting the application for the first time. May be you have run this when you get some updates also. Obviously, this is a problem for our API testing also where we need to run latest migration and seeders before going to run test cases.

I also encountered similar kind of problems. So, I have implemented a one time solutions in the form npm module which resolves all above mentioned problems.

https://www.npmjs.com/package/sequelize-auto-migration

The above sequelize-auto-migration package gives a solution to auto migration problem. For example, take a look at below same code to know how to auto run migrations and seeders for ElectronJs application.

const {app, BrowserWindow} = require('electron'); 
const models = require('./models'); // Please load models folder here.
const SequelizeAuto = require('sequelize-auto-migration')(models); // We need to send Database configuration to the module to run our migrations and seeders
const path = require('path')
const async = require('async')
async function createWindow () {// Here we are configuring our migration and seeder folders paths

// You can make same configuration in your expressJs application if you want to auto run your migrations before starting the application or before starting automation testing scripts (Ex: MochaJs)
await SequelizeAuto.config({
migrationPath:path.resolve('./migrations'),
seedPath: path.resolve('./seeders')
});

async.waterfall([
function(callback){
// This method runs all your pending migrations
SequelizeAuto.migrate(function(err, result){
if(err){
callback(err, null);
}else{
callback(null, true);
}
});
},
function(migrationSuccess, callback){
if(migrationSuccess){
// This method runs all your pending seeders after migration completed.

SequelizeAuto.migrationSeed((err, result) => {
if(err){
callback(err, null);
}else{
callback(null, true);
}
})
} // End of if statement }. // end of callback method.
],function(err, result){
if(err){
console.log('There is some error in migration or seedes::::');
console.log(err);
}else{
console.log('All migraitons and seeders success');
}
});
// I feel ready event is the best place where we can run our pending migrations
app.on('ready', createWindow)
....... You will have the rest of the code here

Click the below link to the complete example.

I hope this module makes our job for auto migrations in the SequelizeJs. This module is completely open source. So, I welcome folks who are interested in making enhancements to module.

Please provide the feedback in the form comments, git hub issues in case if you face any. The Github repository link is

https://github.com/medaamarnadh/sequelize-migration

--

--