Suor / pg-bricks

Higher level PostgreSQL client for Node.js

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

where statement does not detect node.js Buffer

vsenko opened this issue · comments

In the following situation:

db.select().from(table).where(column, value)...

if value is node.js Buffer, search statement will become corrupted (it will contain lots of internal Buffer information).

It is OK for sql-bricks and sql-bricks-postgres because they are designed not solely for node.js, but pg-bricks uses pg which is designed only for node.js and it would be great if pg-bricks will handle node.js Buffer.

pg returns Buffer for bytea data type. And it accepts Buffers as parameters in Parameterized Queries and Prepared Statements.

If it is impossible (or nearly impossible) to pass Objects as parameters in sql-bricks, a simple workaround could look like this (convert Buffer to PG bytea hex representation):

if (Buffer.isBuffer(parameter)) {
  parameter = `\\x${parameter.toString('hex')}`;
}

pg-bricks should never stringify parameters, it should pass them to pg as is. So if Buffer is stringified then this is an issue in both pg and sql-bricks, if not, but stringification is still wrong, then this is the issue in sql-bricks only and, last, if no stringification is made, but you get an error while making the query then this is the issue in pg.

Please be more specific on what you do and what error you get.

Here is the code:

const db = require('pg-bricks').configure('connstring');

const value = new Buffer('some buffer');
const query = db.select().from('table').where('column', value);

console.log(query.toString());

and the output is:

SELECT * FROM "table" WHERE 0 = 'c' AND 1 = 'o' AND 2 = 'l' AND 3 = 'u' AND 4 = 'm' AND 5 = 'n' AND 0 = 115 AND 1 = 111 AND 2 = 109 AND 3 = 101 AND 4 = 32 AND 5 = 98 AND 6 = 117 AND 7 = 102 AND 8 = 102 AND 9 = 101 AND 10 = 114 AND "asciiSlice" = null AND "base64Slice" = null AND "binarySlice" = null AND "hexSlice" = null AND "ucs2Slice" = null AND "utf8Slice" = null AND "asciiWrite" = null AND "base64Write" = null AND "binaryWrite" = null AND "hexWrite" = null AND "ucs2Write" = null AND "utf8Write" = null AND copy = null AND parent = '{}' AND "offset" = 4664 AND "toString" = null AND equals = null AND inspect = null AND compare = null AND "indexOf" = null AND fill = null AND get = null AND "set" = null AND write = null AND "toJSON" = null AND slice = null AND "readUIntLE" = null AND "readUIntBE" = null AND "readUInt8" = null AND "readUInt16LE" = null AND "readUInt16BE" = null AND "readUInt32LE" = null AND "readUInt32BE" = null AND "readIntLE" = null AND "readIntBE" = null AND "readInt8" = null AND "readInt16LE" = null AND "readInt16BE" = null AND "readInt32LE" = null AND "readInt32BE" = null AND "readFloatLE" = null AND "readFloatBE" = null AND "readDoubleLE" = null AND "readDoubleBE" = null AND "writeUIntLE" = null AND "writeUIntBE" = null AND "writeUInt8" = null AND "writeUInt16LE" = null AND "writeUInt16BE" = null AND "writeUInt32LE" = null AND "writeUInt32BE" = null AND "writeIntLE" = null AND "writeIntBE" = null AND "writeInt8" = null AND "writeInt16LE" = null AND "writeInt16BE" = null AND "writeInt32LE" = null AND "writeInt32BE" = null AND "writeFloatLE" = null AND "writeFloatBE" = null AND "writeDoubleLE" = null AND "writeDoubleBE" = null

Using object:

const db = require('pg-bricks').configure('connstring');

const value = new Buffer('some buffer');
const query = db.select().from('table').where({'column': value});

console.log(query.toString());
SELECT * FROM "table" WHERE "column" = '{"type":"Buffer","data":[115,111,109,101,32,98,117,102,102,101,114]}'

At first sight this issue is with sql-bricks but sql-bricks targets not only node.js, they aim to run in any JS environment (as far as I understand their docs) while Buffer objects only exist in node.js environment. Furthermore Buffers are very common in node.js environment and pg supports them.
So the problem is:

  • sql-bricks know nothing about Buffer and processes them as objects;
  • pg understand Buffer objects and works with them correctly;
  • pg-bricks uses sql-bricks to build queries and fails to process queries with Buffers as parameters but on the other side results from pg can contain Buffers, so users have to manually process results (are they Buffers or not) if they intend to use results in queries.
  1. sql-bricks targets Node.js so it should support buffers. It supports both Node.js and browser not least common denominator. It could happiliy support buffers in Node.js and still run in browser.
  2. pg-bricks have nothing to do with query stringification. If query.toString() works incorrectly this is not an issue here. pg-bricks should pass buffers (and any other values!) to pg without trying to process them with sql-bricks or otherwise.

So the question is does query.run() works? If yes then the only issue is with query stringification, which belongs to sql-bricks. If it doesn't work then please reopen with actual error with stacktrace.

query.run() fails with something like:
[error: column "asciiSlice" does not exist]

This is error is coming from database. You are trying to use column wich doesn't exist. Don't see how this is related to pg-bricks.