Created
January 2, 2015 06:36
-
-
Save bq1990/4f24eca04fe006ccf1e5 to your computer and use it in GitHub Desktop.
Knex seed example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'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