JSONB arguments not treated consistently
See original GitHub issueAccording to docs, there is no special treatment required for JSONB bind arguments. In practice there is:
- When the argument is a single object, it is bound correctly
- When the argument is an Array, it is necessary to call JSON.stringify on the args.
Here’s a test case that reproduces the issue:
const { Client } = require('pg');
const { inspect } = require('util');
async function doIt() {
const client = new Client();
console.log('Connecting...');
await client.connect();
console.log('Connected');
const a = [
{
the: 'quick',
brown: 'fox',
jumps: 'over the',
lazy: 'dog',
some: { other: 'object', dilroy: ['hello', 'world'], numpty: new Date() }
}
];
console.log('Inserting...');
const result = await client.query(`insert into sb_json(c1) values ($1) returning *`, [a]);
console.log(`result = ${inspect(result, { depth: null })}`);
console.log('Disconnecting...');
await client.end();
}
doIt()
.then(() => console.log('All done'))
.catch(e => console.log(inspect(e)));
Current documented contract is that no special treatment is required.
Possible solution:
- Document current behavior. This is the less ideal solution, as arguably it is better to be consistent. Never call JSON.stringify on args, or always do so. Not one case for arrays, and on case for single items.
- Inspect the contents of the array. If it contains all primitive types insert for that type. Otherwise call JSON.stringify internally. This operation would run in O(n) time - for a very large array it would be slow.
Issue Analytics
- State:
- Created 4 years ago
- Reactions:8
- Comments:17 (4 by maintainers)
Top Results From Across the Web
15: 9.16. JSON Functions and Operators - PostgreSQL
Returns the first JSON item returned by the JSON path for the specified JSON value. Returns NULL if there are no results. The...
Read more >Numeric argument passed with jq --arg not matching data with
jq is data-type-aware: .ID , as defined in the JSON input, is a number,. but any command-line argument passed with --arg (such as...
Read more >Working with JSON Data | Pivotal Greenplum Docs
0"}'::jsonb; -- The array on the right side is not considered contained within the -- array on the left, even though a similar...
Read more >Solve common issues with JSON in SQL Server
Since JSON_QUERY always returns valid JSON, FOR JSON knows that this result does not have to be escaped. JSON generated with the ...
Read more >jsonb type | Materialize Documentation
The returned value is always of type jsonb . If the requested array element or object key does not exist, or if either...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Because we don’t use pg arrays, we’ve implemented following workaround:
Maybe it helps someone.
@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.