Parsing PG Tuples
See original GitHub issueHas a way been explored to parse Postgres Tuples into JavaScript objects?
I have a query that must select a tuple (i.e. it can’t be flattened), say in the form of a composite type:
create type custom_type as (
a int,
b text
);
When you do something like:
select x::custom_type as x from y;
You get an object in pg
that looks like:
{
"x": "(2,'Hello, world!')"
}
Has a library been developed to parse this format? Is there a way to get this information in a better format from postgres? In my scenario I’ve queried pg_catalog
for all the database type information, so I know the type OIDs of the tuple attributes.
Another option to explore is calling to_json
or a similar function in the select statement, so:
select to_json(x::custom_type) as x from y;
…but if I do that I’m afraid that what I get back will be incompatible with what pg-types
parses. Is there any guarantee that I’ll only get back, say strings, so I can run the pg-types
parsers anyway on the returned JSON object?
Thanks!
In case I’m falling victim to the XY problem for a more complete picture I’m the author of PostGraphQL and I’m trying to add support for Postgres compound types in general.
Issue Analytics
- State:
- Created 7 years ago
- Reactions:1
- Comments:9 (5 by maintainers)
Top GitHub Comments
I had another look at it within
pg-types
. The library is definitely getting it astext
. However, the type Id i’m getting is 73804, which is a very high number:@brianc my guess is that high type Id-s represent custom types. It would be great if
pg-types
could support ranges of type id-s for parsers, so we could say something liketypeId > 65535
=> my custom type parser, or something like that.If this is the case then it may be possible to integrate automatic parsing for tuples, although it wouldn’t be trivial, because the format itself isn’t.
Either way, you would need a proper parser for this. And I don’t know of any module that can do it today.
If someone land here a simple parser that auto load types from Postgres https://www.npmjs.com/package/pg-tuple-types