vitaly-t/pg-promise

helpers.sets() formats arrays with objects in them as text arrays instead of JSON / JSONB arrays

Lehks opened this issue · 2 comments

Lehks commented

Expected behavior

"data"=array['{"a":"a"}'::json,'{"b":"b"}'::json] or something alike.

Actual behavior

"data"=array['{"a":"a"}','{"b":"b"}']. Note the missing conversion to JSON which means that the resulting array will be a plain TEXT[].

Steps to reproduce

const pgp = require('pg-promise')();

console.log(pgp.helpers.sets({ data: [{ a: 'a' }, { b: 'b' }] }));

Environment

  • Version of pg-promise: 10.11.1
  • OS type (Linux/Windows/Mac): Linux (WSL)
  • Version of Node.js: 15.8.0 and 12.16.3

Just trying to help:

  • In pg-promise's README.md, it says

Please note, however, that nested parameters are not supported within the helpers namespace.

  • In the docs for the helpers.set method,data should be:

A simple, non-null and non-array source object.
If it is anything else, the method will throw TypeError = Invalid parameter 'data' specified.

Based on your explanation,

Note the missing conversion to JSON which means that the resulting array will be a plain TEXT[].

some combination of JSON.stringify() or JSON.parse() and implementing an algorithm for your data array using pgp.helpers.sets might help you arrive at a solution?

Closing the issue, as the OP hasn't provided any response.