it should be possible to turn off type casting
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.
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);
}
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.
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.
it is for a test? it's easy enough to get back at the string from a Date.
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.
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
+1 I've just spent almost whole day debugging a problem with comparing dates, which is a compound of:
- https://github.com/brianc/node-postgres/issues/2141
- PG timestamp of
2023-03-01 15:15:21.345832+00in JS Date loses precision and becomes2023-03-01T15:15:21.345Z(notice subsecond precision), which makes it impossible to use=operator (very cool)