Skip to content

Commit

Permalink
Fix null json array transform error
Browse files Browse the repository at this point in the history
  • Loading branch information
Eprince-hub authored and porsager committed Nov 3, 2022
1 parent 9d4da62 commit b153202
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ export const types = {
}
}

const allowList = {
object: typeof Object
}

class NotTagged { then() { notTagged() } catch() { notTagged() } finally() { notTagged() }}

export class Identifier extends NotTagged {
Expand Down Expand Up @@ -329,7 +333,7 @@ export const fromKebab = x => x.replace(/-/g, '_')

function createJsonTransform(fn) {
return function jsonTransform(x, column) {
return column.type === 114 || column.type === 3802
return (x && typeof x in allowList) && (column.type === 114 || column.type === 3802)
? Array.isArray(x)
? x.map(x => jsonTransform(x, column))
: Object.entries(x).reduce((acc, [k, v]) => Object.assign(acc, { [fn(k)]: v }), {})
Expand Down
8 changes: 8 additions & 0 deletions tests/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -611,6 +611,14 @@ t('Transform nested json in arrays', async() => {
return ['aBcD', (await sql`select '[{"a_b":1},{"c_d":2}]'::jsonb as x`)[0].x.map(Object.keys).join('')]
})

t('Transform null json in arrays', async() => {
const sql = postgres({
...options,
transform: postgres.camel
})
return [null, (await sql`select '[{"a_b":null},{"c_d":null}]'::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`]
Expand Down

0 comments on commit b153202

Please sign in to comment.