Skip to content

it should be possible to turn off type casting #2596

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

Open
bkilinc opened this issue Aug 12, 2021 · 7 comments
Open

it should be possible to turn off type casting #2596

bkilinc opened this issue Aug 12, 2021 · 7 comments

Comments

@bkilinc
Copy link

bkilinc commented Aug 12, 2021

In my opinion type-casting should not be done in this level. It should be done in higher level ORM like libraries.
Type casting in here is one-way. Only for result of SELECTs. It cannot be done for in INSERTs and UPDATES or WHEREs in queries.
This may make testing tricky. You insert a date value, but it may be different when you read it according to timezone. Floats may be slightly different also, because of rounding etc.
in mysql driver it is possible to turn of type casting by typeCast: false and also dateStrings: true.

@bkilinc
Copy link
Author

bkilinc commented Aug 12, 2021

I turn of typecasting like this. but it should be easier.

import pg from 'pg'; 
var types = pg.types;
function noParse(val) {
    return String(val);
}
for (let dataType of Object.keys(types.builtins)) {
        types.setTypeParser(types.builtins[dataType], noParse);
}

@charmander
Copy link
Collaborator

Config option:

types: {
    getTypeParser: () => String,
},

Easy enough for how niche it is, and it’s nice to have defaults that allow switching to binary mode. The issue with dates is being tracked elsewhere.

It cannot be done for in INSERTs and UPDATES or WHEREs in queries.

Not sure what you mean by this. pg tries its best to serialize values symmetrically.

@bkilinc
Copy link
Author

bkilinc commented Aug 12, 2021

I need low level postgres driver for node that returns actual values from database as string. Type casting will be handled on higher level.
This is not symetrical. I update a date value as '2021-08-14' in an UPDATE statement and when I read the value with SELECT, it is converted to Date Object. It is not the same. I would like to assert same value. I would like to get exactly the same '2021-08-14' string value.

@AlexGalays
Copy link

it is for a test? it's easy enough to get back at the string from a Date.

@bkilinc
Copy link
Author

bkilinc commented Oct 1, 2021

it is for a test? it's easy enough to get back at the string from a Date.

Sorry for very late answer. this is something that annoys me everytime. It is not for testing only. I just want to simply set typeCasting off. Just like MySql driver. I use Knex and on top of that there is a simple ORM module which handles type casting (in two way). So I expect to get actual SQL string data from database tables.

@bkilinc
Copy link
Author

bkilinc commented Oct 1, 2021

@ilibar-zpt
Copy link

ilibar-zpt commented Oct 11, 2023

+1
I've just spent almost whole day debugging a problem with comparing dates, which is a compound of:

  1. parseInputDatesAsUTC is not documented #2141
  2. PG timestamp of 2023-03-01 15:15:21.345832+00 in JS Date loses precision and becomes 2023-03-01T15:15:21.345Z (notice subsecond precision), which makes it impossible to use = operator (very cool)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants