-
-
Notifications
You must be signed in to change notification settings - Fork 101
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Passing array to Json input causes error in PostgreSQL #263
Comments
master...bradleyayers:json-array-input
|
I'm currently thinking the best option here is to simply change the input type in the mapper for json types to be What do you think @adelsz? |
I have a similar issue: CREATE TABLE example (f JSONB);
/* @name MyQuery */
INSERT INTO example VALUES(:f); myQuery.run({f: 42}, db) // works
myQuery.run({f: "asdf"}, db) // broken with 22P02 | INVALID TEXT REPRESENTATION postgresql error even though both strings and number are valid json I had to call |
When passing an array as a query parameter for a
json
, PostgreSQL emits an error. Here's a test case 7164361.pgtyped/packages/example/src/index.ts
Lines 110 to 119 in 7164361
The issue is brianc/node-postgres#2012, specifically that
pg
encodes array and object parameters differently. Arrays are encoded as native arrays, but objects receiveJSON.stringify
treatment. Apparently there's a good performance reason for using native array, but there's plenty of people who think it shouldn't make this blanket assumption, and I'm guessing the behavior also pre-dates JSON support in PostgreSQL so it wasn't originally broken.This issue is spread across both
pg
andpgtyped
, however PgTyped is in a unique position to solve it (as it has parameter type information). Although PgTyped isn't opinionated about what PostgreSQL library to use, withpg
being so popular I think it would be great if PgTyped made the experience using the two libraries together seamless.There's a few options I can see:
Json[]
for parameters (presumably splitJson
intoInputJson
andOutputJson
, or something like that. This isn't great because it's leaking a quirk ofpg
and degrading the ergonomics.JSON.stringify
to json parameters during preprocessing. This will require type information at runtime which is straight forward to add to the IR object for queries in SQL, but not for queries in TS. It would be a shame for this behavior to diverge between the SQL and TS flavours of PgTyped. The only way I can see it working in TS would be an IR object, but that will make the ergonomics of queries in TS even worse (having to import another thing, or not using a generic in thesql
tag).JSON.stringify
to all array and object parameters, and incur the performance penalty in worse query plans.Json[]
type so that it's not broken, and for SQL-in-SQL add the preprocessing support so it 'just works'.@adelsz interested in your thoughts on this, I'm not sure what direction you want to take the library.
The text was updated successfully, but these errors were encountered: