Simple SQL generator for bulk insert statements
Works nicely with node-postgres.
npm install @ataube/bulk-insert --save
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 *;
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.
- return {string} - defines the returning fields of sql statements, ie.
{ return: 'id, name' }
- ignore {Array} - defines fields to be ignored