Coder Social home page Coder Social logo

Comments (4)

charmander avatar charmander commented on August 27, 2024 1

@Mickael-van-der-Beek That’s expected; you can set its type parser.

=> SELECT typname, oid, typarray FROM pg_type WHERE typname = 'text';
 typname | oid | typarray 
---------+-----+----------
 text    |  25 |     1009
(1 row)

=> SELECT typname, oid, typarray FROM pg_type WHERE typname = 'my_enum';
 typname |  oid   | typarray 
---------+--------+----------
 my_enum | 381744 |   381743
(1 row)
const OID_ARRAY_TEXT = 1009;
const OID_ARRAY_MY_ENUM = 381743;

pg.types.setTypeParser(OID_ARRAY_MY_ENUM, pg.types.getTypeParser(OID_ARRAY_TEXT));

from node-pg-types.

net avatar net commented on August 27, 2024 1

For any poor souls that find their way here, this will set all existing enum types to be parsed as text:

// node-postgres doesn't properly handle enum arrays out of the box,
// so we have to manually set them up here to parse as text arrays.
//
// https://github.com/brianc/node-pg-types/issues/56

const textArrayOID = (
  await pool.query("SELECT typarray FROM pg_type WHERE typname = 'text'")
).rows[0].typarray;

const enumOIDs = (
  await pool.query("SELECT typname, typarray FROM pg_type WHERE typtype = 'e'")
).rows;

const arrayTextParser = pg.types.getTypeParser(textArrayOID);

enumOIDs.forEach(({ typname, typarray }) => {
  const parser =
    {
      // Parsers for specific types can be set up here, for example:
      // topic_tags: value => arrayTextParser(value).map(s => s.toLowerCase()),
    }[typname] || arrayTextParser;

  pg.types.setTypeParser(typarray, parser);
});

Obviously, based on the number of issues pointing here, this should just be the default behavior instead of each downstream library having to re-implement enum array parsing itself. @bendrucker could this be re-opened?

from node-pg-types.

Mickael-van-der-Beek avatar Mickael-van-der-Beek commented on August 27, 2024

@brianc Would it be possible to know if this is considered a bug or expected behaviour?

from node-pg-types.

bendrucker avatar bendrucker commented on August 27, 2024

Querying OID info and registering parsers as you have is clever

this should just be the default behavior

Definitely not. Changing parser registration from sync into async is a big (breaking) change. Coming in hot on a 7 year old issue is not the way to make that kind of change.

from node-pg-types.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.