Skip to content

Latest commit

 

History

History
36 lines (26 loc) · 1.02 KB

README.md

File metadata and controls

36 lines (26 loc) · 1.02 KB

node-bulk-insert

Simple SQL generator for bulk insert statements

Works nicely with node-postgres.

Install

npm install @ataube/bulk-insert --save

Usage

Simple

const bulkInsert = require('node-bulk-insert');
const values = [{ name: 'Ben', age: 20 }, { name: 'Michael', age: 30 }]];

const { sql } = bulkInsert('myTable', values);

// INSERT INTO myTable (name,age) VALUES ($1,$2),($3,$4) RETURNING *;

Ignore Fields

const bulkInsert = require('node-bulk-insert');
const options = { ignore: ['age'] };
const values = [{ name: 'Ben', age: 20 }, { name: 'Michael', age: 30 }]];

const { sql, values } = bulkInsert('myTable', values, options);

// sql: INSERT INTO myTable (name) VALUES ($1),($2) RETURNING *;
// values: ['Ben', 'Michael']

More samples can also be found in the tests.

Options

  • return {string} - defines the returning fields of sql statements, ie. { return: 'id, name' }
  • ignore {Array} - defines fields to be ignored