1.0.2 • Published 5 years ago

knex-seed-file v1.0.2

Weekly downloads
100
License
MIT
Repository
github
Last release
5 years ago

knex-seed-file

A tool that makes using seed files easier with knex

Installation

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

Usage

Here we'll use csv file created using excel:

fruits

We want to ignore ascii value for color and use only columns 'id', 'name' and 'hexColor'

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',
    {
      columnSeparator: ';',
      ignoreFirstLine: true,
      mapTo: ['id', 'name', null, 'hexColor']
    }));
};

Properties

seedFile(knex, path, tableName, mapTo, options)
PropertyDescriptionDefault valueRequired
knexKnex instanceundefinedyes
pathAbsolute path to file containing values to be insertedundefinedyes
tableNameCorresponding table name in the databaseundefinedyes

Options

PropertyDescriptionDefault value
mapToDefines where columns in the file will be mapped in the tableundefined
columnSeparator\t
rowSeparator\n
encodingutf8
ignoreFirstLinefalse
useFirstLineForColumnsDefines, whether first line in the file will be used when mapping data to the table.true

If you use mapTo array, useFirstLineForColumns will be set false. However, if the first row still contains labels for the data, you need to set ignoreFirstLine to true.

1.0.2

5 years ago

1.0.1

5 years ago

1.0.0

5 years ago

0.5.1

7 years ago

0.5.0

7 years ago

0.4.1

7 years ago

0.4.0

7 years ago

0.3.1

7 years ago

0.3.0

7 years ago

0.2.0

8 years ago

0.1.4

8 years ago

0.1.3

8 years ago

0.1.2

8 years ago

0.1.1

8 years ago