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

it should be possible to turn off type casting

Open bkilinc opened this issue 4 years ago • 7 comments

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 avatar Aug 12 '21 13:08 bkilinc

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

bkilinc avatar Aug 12 '21 13:08 bkilinc

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.

charmander avatar Aug 12 '21 19:08 charmander

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.

bkilinc avatar Aug 12 '21 20:08 bkilinc

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

AlexGalays avatar Aug 16 '21 15:08 AlexGalays

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 avatar Oct 01 '21 19:10 bkilinc

there are (were) other people that suffers from the same problem.

https://stackoverflow.com/questions/51082774/knex-silently-converts-postgres-timestamps-with-timezone-and-returns-incorrect-t

bkilinc avatar Oct 01 '21 20:10 bkilinc

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

  1. https://github.com/brianc/node-postgres/issues/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)

ilibar-zpt avatar Oct 11 '23 11:10 ilibar-zpt