node.js - Bulk insert into Postgres with brianc/node-postgres -
i have following code in nodejs uses pg (https://github.com/brianc/node-postgres) code create subscriptions employee such.
client.query( 'insert subscriptions (subscription_guid, employer_guid, employee_guid) values ($1,$2,$3)', [ datasetarr[0].subscription_guid, datasetarr[0].employer_guid, datasetarr[0].employee_guid ], function(err, result) { done(); if (err) { set_response(500, err, res); logger.error('error running query', err); return console.error('error running query', err); } logger.info('subscription created'); set_response(201); });
as have noticed datasetarr array. create mass subscriptions more 1 employee @ time. not loop through array. there way out of box pg?
i did search same question, found no solution yet. async library simple use query several times, , necessary error handling.
may code variant helps. (for inserting 10.000 small json objects empty database took 6 sec).
christoph
function insertdata(item,callback) { client.query('insert subscriptions (subscription_guid, employer_guid, employee_guid) values ($1,$2,$3)', [ item.subscription_guid, item.employer_guid, item.employee_guid ], function(err,result) { // return err async.each iterator callback(err); }) } async.each(datasetarr,insertdata,function(err) { // release client pg module done(); if (err) { set_response(500, err, res); logger.error('error running query', err); return console.error('error running query', err); } logger.info('subscription created'); set_response(201); })
Comments
Post a Comment