Skip to content

Commit b153202

Browse files
Eprince-hubporsager
authored andcommitted
Fix null json array transform error
1 parent 9d4da62 commit b153202

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 {
@@ -329,7 +333,7 @@ export const fromKebab = x => x.replace(/-/g, '_')
329333

330334
function createJsonTransform(fn) {
331335
return function jsonTransform(x, column) {
332-
return column.type === 114 || column.type === 3802
336+
return (x && typeof x in allowList) && (column.type === 114 || column.type === 3802)
333337
? Array.isArray(x)
334338
? x.map(x => jsonTransform(x, column))
335339
: 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
@@ -611,6 +611,14 @@ t('Transform nested json in arrays', async() => {
611611
return ['aBcD', (await sql`select '[{"a_b":1},{"c_d":2}]'::jsonb as x`)[0].x.map(Object.keys).join('')]
612612
})
613613

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

0 commit comments

Comments
 (0)