-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
JSONB arguments not treated consistently #2012
Comments
With the existence of See also #442 |
^-- This comment from the linked ticket (#442) resonates with me. Better to be consistent in behavior.
|
https://node-postgres.com/features/types |
@jasperblues @charmander we are not intended to use pg arrays. Is there any way to say "treat javascript arrays always as pg json"? We use knex if it's matter. |
I see the comment, probably from @brianc , but I don't see the way how to replace the array handling: https://github.com/brianc/node-postgres/blob/master/packages/pg/lib/utils.js#L47 |
Looked for this answer for so long. |
As a workaround you could 'wrap' your arrays in an object and turn that into the JSONB text you want:
or something similar to that. does that work? |
Because we don't use pg arrays, we've implemented following workaround: // @ts-ignore
import pgUtils from 'pg/lib/utils.js'
// a workaround to force storage of array as json until
// https://github.com/brianc/node-postgres/issues/2012
// is fixed
const originalPrepareValue = pgUtils.prepareValue
pgUtils.prepareValue = <T>(val: T): T | string => {
if (Array.isArray(val)) {
return JSON.stringify(val)
}
return originalPrepareValue(val)
} Maybe it helps someone. |
Just hit this too. Ugly. Can we provide some kind of bind directive to tell PG how to interpret the array, or what the required behavior for a given insert statement is. |
If the library would expose the The API could look something like this: pool.query({
text: 'SELECT $1, $2, $3',
values: ['1', 2, [1, 2, 3]],
paramTypes: [null, null, OID.JSONB]
); I'm not sure if it's any better then wrapping the values in custom serialized type as suggested in #2012 (comment). |
This and #1462 seem to be duplicates of each other |
Is it possible to use |
@rightaway No, type parsers are for data coming from the PostgreSQL server. Serialization happens in |
Thanks for the I tried
|
The workaround provided in #2012 (comment) only works for them, because they do not use any SQL array inputs. The |
Thank you! |
Related to brianc#2012. Since we cannot disambiguate between Arrays that should be converted to PostgreSQL array types and Arrays that should be treated as JSON, make it at least possible to define custom Array-derived types. This also makes it possible to properly serialize Array-derived multirange collections where elements must not be independently escaped.
If #3360 makes it, it would be possible to implement this behavior like this: Array.prototype.toPostgres = function() { return JSON.stringify(this) } It would also be possible to define custom Arrays that would Just Work. For example: class DateMultiRange extends Array implements DateRange[] {
toPostgres(prepareValue: (x: any) => any) {
return `{${this.map((range) => prepareValue(range)).join(',')}}`
}
} |
According to docs, there is no special treatment required for JSONB bind arguments. In practice there is:
Here's a test case that reproduces the issue:
Current documented contract is that no special treatment is required.
Possible solution:
The text was updated successfully, but these errors were encountered: