node-postgres icon indicating copy to clipboard operation
node-postgres copied to clipboard

Support for binary results

Open msand opened this issue 1 year ago • 0 comments

Hello, thanks for a wonderful pg library!

I'm trying to get binary support working, and have a minimal proof of concept here:

const pg = require("pg");

pg.types.setTypeParser(1184, "binary", (value) =>
  value === null ? null : value.readBigInt64BE(0),
);

async function getTimestamptzAsBigInt() {
  const client = new pg.Client({
    host: "localhost",
    user: "postgres",
    password: "postgres",
    database: "postgres",
    port: 5432,
    binary: true,
  });

  await client.connect();

  const result = await client.query({
    text: "SELECT current_timestamp;",
    queryMode: 'extended',
  });

  // This should be a BigInt representing number of microseconds
  // since midnight, January 1st, 2000
  console.log(result.rows[0].current_timestamp); 
  
  await client.end();
}

getTimestamptzAsBigInt().catch(console.error);

But it requires some minor patches: https://github.com/brianc/node-postgres/pull/3316

Wdyt?

msand avatar Sep 19 '24 14:09 msand