Created
January 14, 2017 15:36
-
-
Save jimthedev/1ae0c32e24e6b673c9b8f5ee24a4945c to your computer and use it in GitHub Desktop.
Sequelize database seed function
This file contains hidden or 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
if(process.env.NODE_ENV !== 'production') { | |
sequelize.sync({force: true}).then(() => { | |
// Some sample projects | |
var projects = { | |
data: [ | |
{ | |
name: 'Kitchen', | |
}, { | |
name: 'Bathroom', | |
}, | |
], | |
model: Project | |
}; | |
var tasks = { | |
data: [ | |
{ | |
title: 'Stain floors', | |
goalDate: '2017-01-28', | |
completed: false, | |
projectId: 1 | |
},{ | |
title: 'Install baseboards', | |
goalDate: '2017-01-30', | |
completed: false, | |
projectId: 1 | |
}, | |
], | |
model: Task | |
}; | |
function seed(seeds) { | |
return sequelize.Promise.each(seeds, (seed)=> { | |
return sequelize.Promise.each(seed.data, (item) => { | |
return seed.model.create(item); | |
}); | |
}); | |
} | |
seed([ | |
projects, | |
tasks | |
]).then(() =>{ | |
console.log('all created'); | |
// Bring up express or any other apps you might want to do after seeding a database | |
}); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is now much more concise and available in http://npm.im/seedquelize