Skip to content

Commit f9beb8b

Browse files
Eprince-hubporsager
authored andcommitted
Fix null json array transform error
1 parent 8ff1d09 commit f9beb8b

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

src/types.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@ export const types = {
3939
}
4040
}
4141

42+
const allowList = {
43+
object: typeof Object
44+
}
45+
4246
class NotTagged { then() { notTagged() } catch() { notTagged() } finally() { notTagged() }}
4347

4448
export class Identifier extends NotTagged {
@@ -325,7 +329,7 @@ export const fromKebab = x => x.replace(/-/g, '_')
325329

326330
function createJsonTransform(fn) {
327331
return function jsonTransform(x, column) {
328-
return column.type === 114 || column.type === 3802
332+
return (x && typeof x in allowList) && (column.type === 114 || column.type === 3802)
329333
? Array.isArray(x)
330334
? x.map(x => jsonTransform(x, column))
331335
: Object.entries(x).reduce((acc, [k, v]) => Object.assign(acc, { [fn(k)]: v }), {})

tests/index.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -612,6 +612,14 @@ t('Transform nested json in arrays', async() => {
612612
return ['aBcD', (await sql`select '[{"a_b":1},{"c_d":2}]'::jsonb as x`)[0].x.map(Object.keys).join('')]
613613
})
614614

615+
t('Transform null json in arrays', async() => {
616+
const sql = postgres({
617+
...options,
618+
transform: postgres.camel
619+
})
620+
return [null, (await sql`select '[{"a_b":null},{"c_d":null}]'::jsonb as x`)[0].x.map(Object.keys).join('')]
621+
})
622+
615623
t('unsafe', async() => {
616624
await sql`create table test (x int)`
617625
return [1, (await sql.unsafe('insert into test values ($1) returning *', [1]))[0].x, await sql`drop table test`]

0 commit comments

Comments
 (0)