tusken icon indicating copy to clipboard operation
tusken copied to clipboard

Add `pg.json_to_record` support

Open aleclarson opened this issue 3 years ago • 3 comments

Allow providing a schema object like so…

pg.json_to_record({ a: 1, b: '' }, { a: t.int, b: t.text }).as('row')

…that creates the following SQL…

json_to_record('{"a":1,"b":""}'::json) as row(a int, b text)

aleclarson avatar Sep 05 '22 14:09 aleclarson

Would it make more sense to have .as be required and make the schema object as its 2nd argument?

pg.json_to_record({ a: 1, b: '' }).as('row', { a: t.int, b: t.text })

aleclarson avatar Sep 05 '22 14:09 aleclarson

And of course, json_to_record should accept a string too. If passed an object, JSON.stringify is used implicitly.

pg.json_to_record('{ "a": 1, "b": "" }').as('row', { a: t.int, b: t.text })

aleclarson avatar Sep 05 '22 14:09 aleclarson

Similarly, do the same for json_to_recordset.

pg.json_to_recordset([{ a: 1, b: '' }, ...]).as('row', { a: t.int, b: t.text })

…and the jsonb variants

aleclarson avatar Sep 05 '22 14:09 aleclarson