question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to configure types only for a particular instance of node-postgres?

See original GitHub issue

It appears that the only way to configure type parser is by doing so globally, i.e.

var types = require('pg').types
types.setTypeParser(20, function(val) {
  return parseInt(val)
});

– https://github.com/brianc/node-pg-types

I would like to restrict type parsing to a particular connection.

Is there an API at the moment to do so?

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
charmandercommented, Feb 18, 2019

pg.types is a convenience reexport; pg itself doesn’t use that property. You can override type parsing by configuration (i.e. per client or pool) by passing something that satisfies the necessary interface in the types property, the most convenient something I’m aware of being a TypeOverrides from pg/lib/type-overrides.js:

const TypeOverrides = require('pg/lib/type-overrides');

const OID_INT8 = 20;

const types = new TypeOverrides();
types.setTypeParser(OID_INT8, BigInt);

const pool = new pg.Pool({
    ⋮
    types,
});

const client = new pg.Client({
    ⋮
    types,
});
0reactions
gajuscommented, Oct 23, 2019

Just in case anyone comes across this, if you are using pg-native, you also need to override client.native._types. See https://github.com/gajus/slonik/commit/009a2124264d267681ed9a680db10d94e3990dae.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Queries – node-postgres
The api for executing queries supports both callbacks and promises. I'll provide an example for both styles here. For the sake of brevity...
Read more >
Documentation: 15: CREATE TYPE - PostgreSQL
Composite Types. The first form of CREATE TYPE creates a composite type. The composite type is specified by a list of attribute names...
Read more >
node-postgres: how to execute "WHERE col IN (<dynamic ...
node -postgres appears to work exclusively with bound parameters: client.query('SELECT * FROM table WHERE id = $1', [ id ]) ; this will...
Read more >
Building and running a Node.JS, TypeScript, PostgreSQL app ...
JS enhanced with TypeScript, PostgreSQL as our database, and how to ... For example, after you install the types package, you'll be able...
Read more >
CRUD REST API with Node.js, Express, and PostgreSQL
api=> CREATE TABLE users ( ID SERIAL PRIMARY KEY, name VARCHAR(30), email VARCHAR(30) );. Make sure not to use the backtick ` character...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found