Open mantariksh opened 1 week ago
Although this could just be a simplified example, I might as well mention that detecting braces is a bad idea, especially globally in every value processed by a pg client. In real code, if you want to use pg’s types
option for this, you should get the array OID for your enum and set a parser specifically for it that always parses it as an array (with or without postgres-array). But maybe you don’t want to use pg’s types
option for this – it might be better handled at the SQL layer by casting the enum-typed array to text[]
, or at a higher layer than pg that knows about the schema.
@charmander thanks for responding! This is just a simplified example for the sake of illustrating the bug. I actually encountered this bug as a user of sequelize
, which does exactly what you said (get the array OID, set a specific parser), but I traced the bug to this library so thought I'd propose a fix here.
Relevant sequelize
code here: https://github.com/sequelize/sequelize/blob/b9e71a7af57568dd85b8a2fdaac59b96ce0f0e95/src/dialects/postgres/connection-manager.js#L81
Minimal reproducible example
See https://github.com/mantariksh/pg-bug-repro/blob/main/pg.mjs, the instructions are in the README.
Behaviour observed
When I pass a custom type parser via the
types.getTypeParser
function passed to theClient
constructor, the type parser runs correctly for single-line statements but not for multi-line statements.For instance, consider the following table (where
enum_Users_roles
has two possible values,Admin
andView
)I want
Client
to check for any values wrapped in braces ({}
) and parse them as an array, as follows:When I run a single-line statement, I get the correct result:
However, when I run a multi-line statement, the array is no longer parsed (sorry that the SQL statement here is somewhat complex, I got it directly from
sequelize
):