Skip to content

Instantly share code, notes, and snippets.

@bq1990
Created January 2, 2015 06:36
Show Gist options
  • Save bq1990/4f24eca04fe006ccf1e5 to your computer and use it in GitHub Desktop.
Save bq1990/4f24eca04fe006ccf1e5 to your computer and use it in GitHub Desktop.
Knex seed example
'use strict';
var users = require('../sample_users');
exports.seed = function(knex, Promise) {
var userPromises = [];
users.forEach(function(user){
userPromises.push(createUser(knex, user));
});
return Promise.all(userPromises);
};
function createUser(knex, user) {
return knex.table('users')
.returning('id')
.insert(
{
email: user.email,
password: user.password, // hash for 123456
role: user.role,
type: user.type,
active: user.active
}
)
.then(function(userIds){
return knex('profiles')
.insert(
{
firstname: user.Profile.firstname,
lastname: user.Profile.lastname,
address1: user.Profile.address1,
city: user.Profile.city,
user_id: userIds[0],
country_code: user.Profile.country_code,
phone: user.Profile.phone
}
);
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment