I've got the migration strategy via sequelize-cli set up and running so the table can be constructed properly. It works using sequelize db:migrate
and it creates the table just fine. And sequelize db:migrate:undo
will delete the table.
I've also included code (per the SE comment here) to insert a couple of records. That works too. Here's the code for all that:
'use strict';
module.exports = {
up: function(queryInterface, DataTypes, done) {
return queryInterface.createTable('survey', {
id: {
type: DataTypes.INTEGER,
primaryKey: true,
autoIncrement: true
},
state: {
type: DataTypes.TEXT
},
age: {
type: DataTypes.INTEGER
},
race: {
type: DataTypes.TEXT
},
gender: {
type: DataTypes.TEXT
},
education: {
type: DataTypes.TEXT
},
q1: {
type: DataTypes.INTEGER
},
q2: {
type: DataTypes.INTEGER
},
.
.
.
q24: {
type: DataTypes.INTEGER
},
q25: {
type: DataTypes.INTEGER
}
}).then(function() {
queryInterface.sequelize.query("INSERT INTO survey (state, age, race, gender, q1, q7, q24) VALUES ('Texas', 42, 'white', 'female', 5, 4, 3), ('Louisiana', 19, 'hispanic', 'male', 1, 2,5)");
done();
});
},
down: function(queryInterface, Sequelize) {
return queryInterface.dropTable('survey');
}
};
But I have a CSV file with over 3000 records that needs to be used as the seed data. It's in the db
folder and called survey.csv
. I also have a seeders
folder that I think was created automatically by a sequelize init
or sequelize migration:create
. It's currently empty.
I could use the concatenate function in Excel to create the ordered n-tuples of data to insert and just paste those 3000 lines into promise part of up
, but that seems ridiculous.
I found this reply for a similar question, but in Rails:
You need the CSV library. From the docs:
arr_of_arrs = CSV.read("path/to/file.csv")
This will give you a 2D array which you can process as you like.
CSV
similar toIO.read
, but with a few extras such as header parsing.
It looks like there's a handful (or more) csv node packages, but golly, they're so complicated (like the simply-named csv).
Is there a simpler csv parser (like the one for Rails) that I can use to facilitate the dumping of the csv into the database? Or another method that doesn't require a bazillion lines of superfluous code copied from a spreadsheet?