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 is it possible to share the same code?
'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();
});
Follow the template scaffolded by seed:create
from sequelize-cli:
$ node_modules/.bin/sequelize seed:create --name my-seed-name
Then run your seed:
$ node_modules/.bin/sequelize db:seed
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With