setTypeParser not applied to json_build_object values?
See original GitHub issueNot sure if this is by design, but I have noticed that values returned by json_build_object
are not being processed by setTypeParser
function. Even if I specify in the query of what type the value will be, it is still ignored.
Example: having this set pg.types.setTypeParser(1114, (val) => val === null ? null : moment(val));
and running same query with only difference of how values are selected will return different results.
SELECT time_column FROM test_table
time_column
is instance of moment
SELECT json_build_object('time_column', time_column) FROM test_table
time_column
is a string with date coming straight from database
Is this a bug or it is expected behavior?
Running 7.5.0 version.
Issue Analytics
- State:
- Created 5 years ago
- Reactions:3
- Comments:5 (3 by maintainers)
Top Results From Across the Web
Sequelize giving TypeError - node.js
Deploying the app in heroku seems to be giving me some kind of error when conecting with postgres by using sequelize (I'm not...
Read more >JSON_BUILD_OBJECT
Converts a comma-separated argument list to a JSON object. The argument list consists of alternating keys and values.
Read more >How to use the pg.types function in pg
config'); // remember: all values returned from the server are either NULL or a string pg.types.setTypeParser(20, val => (val === null ? null...
Read more >Data Types – node-postgres
There is no data type in JavaScript for a uuid/guid so node-postgres ... on your outbound value, automatically converting it to json for...
Read more >Working with PostgreSQL's timestamp without timezone in Node
Normally, attributes of this type hold date-time values in UTC and the application logic converts these values to the user timezone. If you...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
The result of
json_build_object(...)
is of typejson
. There’s no additional typing for the contents of the fields within the JSON as PostgreSQL itself doesn’t provide or even track that information. From the server’s perspective it’s just “json” without any detail about what’s inside.If you want to do something specific to transform a value to a Date or moment object you’ll have to either override the parser for the json type itself (either globally or on that one query) or transform the result (probably easier).
I see your point, but when it says that you can override type parsing and doesn’t mention that it does not apply to json functions, it can be misleading and lead to avoidable bugs. Anyway, I should have posted this in node-pg-types.