Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
gajus opened this issue Feb 18, 2019 · 6 comments
Closed

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

gajus opened this issue Feb 18, 2019 · 6 comments

Comments

@gajus
Copy link
Contributor

gajus commented Feb 18, 2019

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?

@gajus
Copy link
Contributor Author

gajus commented Feb 18, 2019

This could be as simple as allowing to override types.

this.types = require('pg-types')

@charmander
Copy link
Collaborator

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

@gajus gajus closed this as completed Feb 18, 2019
@gajus
Copy link
Contributor Author

gajus commented Feb 19, 2019

Is there a way to override TypeOverrides after pool has been instantiated?

const client = new pg.Client({});

client._types = types;

The latter does not seem to do the trick.

Will debug this further on my own (seems like I could definitely override the query getTypeParser,

query._result._getTypeParser = this._types.getTypeParser.bind(this._types)
), but if there is a better way, please let me know.

@charmander
Copy link
Collaborator

Is there a way to override TypeOverrides after pool has been instantiated? … new pg.Client

On a pool or a client? What you wrote should work fine for a client for queries after the assignment. Client.prototype.setTypeParser/Client.prototype.getTypeParser exist too. On a pool, you can assign arbitrary client options after instantiation, so set pool.options.types.

@gajus
Copy link
Contributor Author

gajus commented Feb 19, 2019

Thank you

@gajus
Copy link
Contributor Author

gajus commented 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 gajus/slonik@009a212.

vitaly-t added a commit to vitaly-t/node-postgres that referenced this issue Oct 8, 2020
`TypeOverrides` is too important not to expose it. It should be available from the root.

Related: brianc#1838, brianc#2363
@B4nan B4nan mentioned this issue Apr 23, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants