Sequelize js orm migration with seeder
Is there a way to populate the db (migration + seeds) using sequelizejs ORM?
my migration
'use strict';
/**
* Module dependencies.
*/
var Schema = require('../src/schemas/users');
module.exports = {
up: function (queryInterface, Sequelize) {
return queryInterface.createTable(
Schema.name,
Schema.definition(Sequelize)
);
},
down: function (queryInterface, Sequelize) {
return queryInterface.dropTable(Schema.name);
}
};
take a look at Node Sequelize migrations / models can you share the same code?
First try
'use strict';
/**
* Module dependencies.
*/
var Async = require('async');
var Models = require('../src/models');
var User = Models.users;
/**
* Users to insert.
*/
var users = [
{
firstname: 'abcdefghijklmnopqrstuvwxyz',
lastname: 'abcdefghijklmnopqrstuvwxyz',
email : 'abcdefghijklmnopqrstuvwxyz@abcdefghijklmnopqrstuvwxyz.abcdefghijklmnopqrstuvwxyz',
username:'abcdefghijklmnopqrstuvwxyz',
password:'abcdefghijklmnopqrstuvwxyz'
}
];
var calls = [];
function createUserPromise(current, index, users) {
calls.push(function(callback) {
User
.create(current)
.then(function(user) {
callback(null, user);
})
.catch(function(err){
return callback(err);
});
});
}
users.forEach(createUserPromise);
Async.parallel(calls , function(err, results){
if(err){
console.log(err);
}
else{
var info = results.map(function(result){
return result.dataValues;
});
console.log(info);
}
process.exit();
});
+3
source to share
1 answer
Follow the pattern seed:create
with sequelize-cli :
$ node_modules/.bin/sequelize seed:create --name my-seed-name
Then start your seed:
$ node_modules/.bin/sequelize db:seed
+1
source to share