brianc/node-postgres-docs

PostgreSQL Enum Arrays don't convert to Javascript Array

Closed this issue · 3 comments

When I retrieve a profile (shown below) from the profiles table you will notice that the 2 enumerated arrays lookingForCountries and lookingForReligions haven't been converted to javascript arrays, but the character varying array photos has.
{
nickname: 'Cool Man Test Account',
description: 'I am the coolest man alive',
age: 40,
location: 'Oxford',
gender: 'Male',
sexuality: 'Straight',
religion: 'Christianity',
alcohol: 'Yes',
smoking: 'No',
lookingForGender: 'Female',
lookingForSexuality: 'Straight',
country: 'Any',
interests: '0',
lookingForCountries: '{"United Kingdom",Albania}',
lookingForReligions: '{Agnostism,Atheism}',
photos: [ '', '', '', '', '' ],
lookingForFromAge: '34',
lookingForToAge: 40
}
I can provide all of the table definitions, enum definitions and javascript code if you like, but this should be easy to replicate as you just create an enum type, create a column of enum array and try to retrieve the table row from Javascript.

See: brianc/node-pg-types#56

This driver does not attempt to query the database for information about custom types.
You either have to setup a custom type parser for your enum type's current OID, or it might be easier to just cast the value to text[].

Thanks for the information.
I am surprised the driver needs to query the database to convert an array into another array, but still.
The driver actually returns the enum array as a string so I've decided to do the following:
profile.lookingForCountries = profile.lookingForCountries.replace('{', '').replace('}', '').replaceAll('"', '').split(',')
It is a bit long-winded, but it works.

I am surprised the driver needs to query the database to convert an array into another array

The issue is more that it can’t know the text represents any kind of array at all without looking up the type code.

Anyway, instead of doing those replacements – which is a bit of a hack – you should either cast ::text[] in your query or at least use postgres-array’s parse. (It’s already used by pg, not an extra dependency.)