Skip to content

Commit 5dea953

Browse files
committed
Fix nested json array transform - fixes porsager#506
1 parent a12108a commit 5dea953

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
@@ -331,7 +331,7 @@ function createJsonTransform(fn) {
331331
return function jsonTransform(x, column) {
332332
return column.type === 114 || column.type === 3802
333333
? Array.isArray(x)
334-
? x.map(jsonTransform)
334+
? x.map(x => jsonTransform(x, column))
335335
: Object.entries(x).reduce((acc, [k, v]) => Object.assign(acc, { [fn(k)]: v }), {})
336336
: x
337337
}

tests/index.js

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

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

0 commit comments

Comments
 (0)