node.jspostgresqlnode-postgres

Bulk insert into Postgres with brianc/node-postgres


I have the following code in nodejs that uses the pg (https://github.com/brianc/node-postgres) My code to create subscriptions for an employee is as such.

    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);

      });

As you have already noticed datasetArr is an array. I would like to create mass subscriptions for more than one employee at a time. However I would not like to loop through the array. Is there a way to do it out of the box with pg?


Solution

  • I did a search for the same question, but found no solution yet. With the async library it is very simple to use the query several times, and do the necessary error handling.

    May be this code variant helps. (for inserting 10.000 small json objects to an empty database it took 6 sec).

    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);
    })