/knex-seed-file

Primary LanguageJavaScriptMIT LicenseMIT

knex-seed-file

A tool that makes using seed files easier with knex

Installation

npm install --save-dev knex-seed-file

Usage

In this we'll use csv file generated with excel:

fruits

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
    }));
  );
};

Attributes

knex

Knex instance

path

Absolute file location

tableName

Table, where rows will be added

mapTo

Will define where files columns(from left to right) will be mapped in the table

options

columnSeparator

defaults to \t

rowSeparator

defaults to \n

encoding

defaults to utf8

ignoreFirstLine

defaults to false

useFirstLineForColumns

defaults to false

If you use useFirstLineForColumns, you do not need to specify a mapTo array