question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

setTypeParser not applied to json_build_object values?

See original GitHub issue

Not 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:closed
  • Created 5 years ago
  • Reactions:3
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
sehropecommented, Oct 11, 2018

The result of json_build_object(...) is of type json. 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).

1reaction
druliacommented, Oct 11, 2018

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found