Skip to content

Commit 98d2ffb

Browse files
committed
Fix nested json array transform - fixes #506
1 parent 74978fa commit 98d2ffb

File tree

2 files changed

+9
-1
lines changed

2 files changed

+9
-1
lines changed

src/types.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -327,7 +327,7 @@ function createJsonTransform(fn) {
327327
return function jsonTransform(x, column) {
328328
return column.type === 114 || column.type === 3802
329329
? Array.isArray(x)
330-
? x.map(jsonTransform)
330+
? x.map(x => jsonTransform(x, column))
331331
: Object.entries(x).reduce((acc, [k, v]) => Object.assign(acc, { [fn(k)]: v }), {})
332332
: x
333333
}

tests/index.js

+8
Original file line numberDiff line numberDiff line change
@@ -604,6 +604,14 @@ t('column toKebab', async() => {
604604
return ['hello-world', Object.keys((await sql`select * from test`)[0])[0], await sql`drop table test`]
605605
})
606606

607+
t('Transform nested json in arrays', async() => {
608+
const sql = postgres({
609+
...options,
610+
transform: postgres.camel
611+
})
612+
return ['aBcD', (await sql`select '[{"a_b":1},{"c_d":2}]'::jsonb as x`)[0].x.map(Object.keys).join('')]
613+
})
614+
607615
t('unsafe', async() => {
608616
await sql`create table test (x int)`
609617
return [1, (await sql.unsafe('insert into test values ($1) returning *', [1]))[0].x, await sql`drop table test`]

0 commit comments

Comments
 (0)