A tool that makes using seed files easier with knex
npm install --save-dev knex-seed-file
In this we'll use csv file generated with excel:
We want to ignore ascii value for color and use only columns 'id', 'name' and 'hexColor'
seedFile(knex, path, tableName, mapTo, options)
const path = require('path');
const seedFile = require('knex-seed-file');
exports.seed = function(knex, Promise) {
knex('fruits').del())
.then(() => seedFile(knex, path.resolve('./fruits.csv'), 'fruits', [
'id',
'name',
null,
'hexColor'
], {
columnSeparator: ';',
ignoreFirstLine: true
}));
);
};
Knex instance
Absolute file location
Table, where rows will be added
Will define where files columns(from left to right) will be mapped in the table
defaults to \t
defaults to \n
defaults to utf8
defaults to false
defaults to false
If you use useFirstLineForColumns, you do not need to specify a mapTo array