From 5dea9530bda12e9abf1131fd7d3022f329fd7923 Mon Sep 17 00:00:00 2001 From: Rasmus Porsager Date: Sat, 15 Oct 2022 23:29:17 +0200 Subject: [PATCH] Fix nested json array transform - fixes #506 --- src/types.js | 2 +- tests/index.js | 8 ++++++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/src/types.js b/src/types.js index befc8b50..3d6b295f 100644 --- a/src/types.js +++ b/src/types.js @@ -331,7 +331,7 @@ function createJsonTransform(fn) { return function jsonTransform(x, column) { return column.type === 114 || column.type === 3802 ? Array.isArray(x) - ? x.map(jsonTransform) + ? x.map(x => jsonTransform(x, column)) : Object.entries(x).reduce((acc, [k, v]) => Object.assign(acc, { [fn(k)]: v }), {}) : x } diff --git a/tests/index.js b/tests/index.js index ee4bf11f..f0989d5f 100644 --- a/tests/index.js +++ b/tests/index.js @@ -603,6 +603,14 @@ t('column toKebab', async() => { return ['hello-world', Object.keys((await sql`select * from test`)[0])[0], await sql`drop table test`] }) +t('Transform nested json in arrays', async() => { + const sql = postgres({ + ...options, + transform: postgres.camel + }) + return ['aBcD', (await sql`select '[{"a_b":1},{"c_d":2}]'::jsonb as x`)[0].x.map(Object.keys).join('')] +}) + t('unsafe', async() => { await sql`create table test (x int)` return [1, (await sql.unsafe('insert into test values ($1) returning *', [1]))[0].x, await sql`drop table test`]