Pg-promise: Multi-query rejects with the wrong error.

Created on 2 Nov 2017  路  3Comments  路  Source: vitaly-t/pg-promise

This succeeds:

var pgp = require('pg-promise')();

pgp({ ... })
.one('SELECT 1; SELECT 2;')
.then((result) => {
  console.log('result', result);
}, (err) => {
  console.log('err', err);
});

This fails with an unclear error:

var pgp = require('pg-promise')();

pgp({ ... })
.one('SELECT 1; SELECT * from "foo";')
.then((result) => {
  console.log('result', result);
}, (err) => {
  console.log('err', err);
});

The error is:

TypeError: Cannot read property 'length' of undefined
    at new QueryResultError (/Users/matthew/dev/pg-promise-test/node_modules/pg-promise/lib/errors/queryResult.js:130:32)
    at Query.ctx.db.client.query (/Users/matthew/dev/pg-promise-test/node_modules/pg-promise/lib/query.js:192:41)
    at Query.handleReadyForQuery (/Users/matthew/dev/pg-promise-test/node_modules/pg/lib/query.js:126:10)
    at Connection.<anonymous> (/Users/matthew/dev/pg-promise-test/node_modules/pg/lib/client.js:163:19)
    at emitOne (events.js:101:20)
    at Connection.emit (events.js:188:7)
    at Socket.<anonymous> (/Users/matthew/dev/pg-promise-test/node_modules/pg/lib/connection.js:118:12)
    at emitOne (events.js:96:13)
    at Socket.emit (events.js:188:7)
    at readableAddChunk (_stream_readable.js:176:18)
    at Socket.Readable.push (_stream_readable.js:134:10)
    at TCP.onread (net.js:548:20)

I believe this is an appropriate fix. I'm happy to submit a PR and write tests if you'd like.

While I have your attention, I'm also curious what direction you're planning to take query result enforcement for multi-statement queries. Are you only maintaining backwards compatibility, or are you planning to make it more robust? My team writes a lot of transactions in the form BEGIN; IRRELEVANT RESULT ...; IRRELEVANT RESULT ...; SELECT ...; END; and are using a modified version of this library to apply result checking to arbitrary results instead of only checking the last result.

bug

All 3 comments

About the issue.

I will be able to test it in about 10 hours from now.


About the protocol.

It was originally designed to control results of a single query, up until v7 of the driver that suddenly started supporting multiple results. The library was updated in v6 to process only the last query result, which is consistent with the driver, pgAdmin, and the previous versions of pg-promise.

And in v7.0.0 of the library I added new methods [multi] and [multiResult] to execute multi-query statements and get all the results at once. Their logic does not include any result control, they just return what is there. And I'm not planning to change that, to avoid ambiguities in such restrictions.

If you want a method that deals with mult-query results while controlling it somehow, just use event [extend] to extend the protocol with your own method.

Here's an example of adding custom method multiCheck:

const initOptions = {
  extend: obj => {
    obj.multiCheck = (query, values) => {
      return obj.multi(query, values).then(data => {
        if (/*I don't like something in the data*/) {
          throw new Error('I don\'t like the data!');
        }
        return data;
      });
    }
  }
};

and are using a modified version of this library to apply result checking to arbitrary results instead of only checking the last result.

The library's protocol is fully extensible by design, no point in modifying the library itself.

I can confirm that the issue is definitely there, and needs to be fixed.

Fixed in v7.2.1.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

vitaly-t picture vitaly-t  路  3Comments

normanfeltz picture normanfeltz  路  4Comments

hawkeye64 picture hawkeye64  路  4Comments

realcarbonneau picture realcarbonneau  路  4Comments

Juanflugel picture Juanflugel  路  3Comments