question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

JSONB arguments not treated consistently

See original GitHub issue

According 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:open
  • Created 4 years ago
  • Reactions:8
  • Comments:17 (4 by maintainers)

github_iconTop GitHub Comments

6reactions
Bessonovcommented, Apr 1, 2020

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.

2reactions
Bessonovcommented, Mar 26, 2020

@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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found