使用brianc / node-postgres批量插入Postgres

时间:2014-06-03 06:44:50

标签: node.js postgresql node-postgres

我在使用pg(https://github.com/brianc/node-postgres)的nodejs中有以下代码 我为员工创建订阅的代码就是这样。

    client.query(
      'INSERT INTO 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 with created');
        set_response(201);

      });

正如您已经注意到datasetArr是一个数组。我想一次为多个员工创建大量订阅。但是我不想循环遍历数组。有没有办法用pg开箱即用?

5 个答案:

答案 0 :(得分:5)

我搜索了同样的问题,但尚未找到解决方案。 使用异步库,可以多次使用查询,并进行必要的错误处理。

可能是这个代码变体有帮助。 (用于将10.000个小json对象插入空数据库,需要6秒)。

Christoph

function insertData(item,callback) {
  client.query('INSERT INTO subscriptions (subscription_guid, employer_guid, employee_guid)
       values ($1,$2,$3)', [
        item.subscription_guid,
        item.employer_guid,
        item.employee_guid
       ], 
  function(err,result) {
    // return any err to async.each iterator
    callback(err);
  })
}
async.each(datasetArr,insertData,function(err) {
  // Release the client to the 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 with created');
  set_response(201);
})

答案 1 :(得分:1)

将您的数据结构创建为:

[ [val1,val2],[val1,val2] ...]

然后将其转换为字符串:

 JSON.stringify([['a','b'],['c']]).replace(/\[/g,"(").replace(/\]/g,")").replace(/"/g,'\'').slice(1,-1)

将其附加到查询中,您就完成了!

同意它有字符串解析成本,但它比单插入更便宜。

答案 2 :(得分:1)

我认为最好的方法是使用PostgreSQL json函数:

client.query('INSERT INTO table (columns) ' +
        'SELECT m.* FROM json_populate_recordset(null::your_custom_type, $1) AS m',
        [JSON.stringify(your_json_object_array)], function(err, result) {
      if(err) {
            console.log(err);
      } else {
            console.log(result);
      }
});

答案 3 :(得分:1)

要从NodeJS批量插入Postgresql,更好的选择是使用' COPY' Postgres和pg-copy-streams提供的命令。

来自https://gist.github.com/sairamkrish/477d20980611202f46a2d44648f7b14b

的代码段
/*
  Pseudo code - to serve as a help guide. 
*/
const copyFrom = require('pg-copy-streams').from;
const Readable = require('stream').Readable;
const { Pool,Client } = require('pg');
const fs = require('fs');
const path = require('path');
const datasourcesConfigFilePath = path.join(__dirname,'..','..','server','datasources.json');
const datasources = JSON.parse(fs.readFileSync(datasourcesConfigFilePath, 'utf8'));

const pool = new Pool({
    user: datasources.PG.user,
    host: datasources.PG.host,
    database: datasources.PG.database,
    password: datasources.PG.password,
    port: datasources.PG.port,
});

export const bulkInsert = (employees) => {
  pool.connect().then(client=>{
    let done = () => {
      client.release();
    }
    var stream = client.query(copyFrom('COPY employee (name,age,salary) FROM STDIN'));
    var rs = new Readable;
    let currentIndex = 0;
    rs._read = function () {
      if (currentIndex === employees.length) {
        rs.push(null);
      } else {
        let employee = employees[currentIndex];
        rs.push(employee.name + '\t' + employee.age + '\t' + employee.salary + '\n');
        currentIndex = currentIndex+1;
      }
    };
    let onError = strErr => {
      console.error('Something went wrong:', strErr);
      done();
    };
    rs.on('error', onError);
    stream.on('error', onError);
    stream.on('end',done);
    rs.pipe(stream);
  });
}

更详细的细节explained in this link

答案 4 :(得分:-3)

使用ORM;例如:Objection

此外,根据数据库服务器和所需的活动连接数增加连接池大小。

someMovie
  .$relatedQuery('actors')
  .insert([
    {firstName: 'Jennifer', lastName: 'Lawrence'},
    {firstName: 'Bradley', lastName: 'Cooper'}
  ])
  .then(function (actors) {
    console.log(actors[0].firstName);
    console.log(actors[1].firstName);
  });
相关问题