Navigation Menu

Skip to content
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

Support Postgres per-query type conversion #3527

Open
oranoran opened this issue Nov 8, 2019 · 2 comments
Open

Support Postgres per-query type conversion #3527

oranoran opened this issue Nov 8, 2019 · 2 comments

Comments

@oranoran
Copy link
Contributor

oranoran commented Nov 8, 2019

Applies to: Postgres with pg driver >= 7.10

Until recently, pg driver supported only a global definition for converting SQL types returned from queries into JS types. Therefore users had to try and find a global configuration matching their needs.

For example, for supporting a high-precision float arithmetic in a specific query, a developer might need to use a custom converter into a JS high-percision number type. Since the conversion configuration in pg used to be global (per returned SQL type), the configured conversion would necessarily apply to any other query returning the same SQL data type, regardless of whether such a conversion is needed or convenient for developers.

Since version 7.10, released in April 2019, it's possible to customize the type conversion function per query, like so:

const query = {
  text: 'SELECT * from some_table',
  types: {
    getTypeParser: () => val => val,
  },
}

It would be useful for knex to support passing this per-query configuration when executing an SQL query on top of pg.

A possible API for this in Knex could be:

knex.select('x').pgtypes({
  getTypeParser: () => val => val,
});
@elhigu
Copy link
Member

elhigu commented Nov 8, 2019

There is an API to pass extra arguments to driver for query... https://knexjs.org/#Builder-options

If that doesn't work, lets convert this issue to bug report.

@oranoran
Copy link
Contributor Author

oranoran commented Nov 9, 2019

There is an API to pass extra arguments to driver for query... https://knexjs.org/#Builder-options

If that doesn't work, lets convert this issue to bug report.

Got it, I missed this existing API.
Hopefully it works for this use case, let's see.

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