Last active
November 13, 2021 15:24
-
-
Save fdevibe/dd50b091ea3a7c7eb8fa to your computer and use it in GitHub Desktop.
Dumb, straightforward seeding Sequelize data. Feel free to improve. :-)
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
#! /usr/bin/env node | |
'use strict'; | |
var db = require('./models'), | |
optimist = require('optimist'), | |
_ = require('lodash'), | |
path = require('path'); | |
_.each(optimist.argv._, function (file) { | |
var module = require(path.join(process.cwd(), file)); | |
module.seed(db.sequelize); | |
}); |
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
var _ = require('lodash'), | |
fs = require('fs'), | |
path = require('path'), | |
csv = require('csv'), | |
Sequelize = require('sequelize'); | |
function addUser(db, filename, rolesByName, addresses) { | |
var parser = csv.parse({}, function (err, data) { | |
db.models.User.bulkCreate( | |
_.map(data, function (record, index) { | |
// Create user attributes object and return it | |
}) | |
); | |
fs.createReadStream(filename).pipe(parser); | |
} | |
var seed = function (db) { | |
Sequelize.Promise.all([ | |
db.models.Role.findAll(), | |
db.models.Address.findAll() | |
]).spread(function (roles, addresses) { | |
var rolesByName = _.each(roles, function (role) { | |
rolesByName[role.name.toLowerCase()] = role; | |
}); | |
addUsers(db, path.join(__dirname, 'data', 'users.csv'), rolesByName, addresses); | |
}); | |
}; | |
module.exports = { | |
seed: seed | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks. I made this based on your code: