Skip to content

Commit b04c853

Browse files
committed
Fix support for null in arrays - fixes porsager#371
1 parent 31ccd7b commit b04c853

File tree

3 files changed

+47
-7
lines changed

3 files changed

+47
-7
lines changed

src/connection.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -731,7 +731,7 @@ function Connection(options, queues = {}, { onopen = noop, onend = noop, onclose
731731
options.shared.typeArrayMap[oid] = typarray
732732
options.parsers[typarray] = (xs) => arrayParser(xs, parser)
733733
options.parsers[typarray].array = true
734-
options.serializers[typarray] = (xs) => arraySerializer(xs, options.serializers[oid])
734+
options.serializers[typarray] = (xs) => arraySerializer(xs, options.serializers[oid], options)
735735
}
736736

737737
function tryNext(x, xs) {

src/types.js

+14-4
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ function arrayEscape(x) {
231231
.replace(escapeQuote, '\\"')
232232
}
233233

234-
export const arraySerializer = function arraySerializer(xs, serializer) {
234+
export const arraySerializer = function arraySerializer(xs, serializer, options) {
235235
if (Array.isArray(xs) === false)
236236
return xs
237237

@@ -243,9 +243,19 @@ export const arraySerializer = function arraySerializer(xs, serializer) {
243243
if (Array.isArray(first) && !first.type)
244244
return '{' + xs.map(x => arraySerializer(x, serializer)).join(',') + '}'
245245

246-
return '{' + xs.map(x =>
247-
'"' + arrayEscape(serializer ? serializer(x.type ? x.value : x) : '' + x) + '"'
248-
).join(',') + '}'
246+
return '{' + xs.map(x => {
247+
if (x === undefined) {
248+
x = options.transform.undefined
249+
if (x === undefined)
250+
throw Errors.generic('UNDEFINED_VALUE', 'Undefined values are not allowed')
251+
}
252+
253+
return x === null
254+
? 'null'
255+
: x === undefined
256+
? '' + options.transform.undefined
257+
: '"' + arrayEscape(serializer ? serializer(x.type ? x.value : x) : '' + x) + '"'
258+
}).join(',') + '}'
249259
}
250260

251261
const arrayParserState = {

tests/index.js

+32-2
Original file line numberDiff line numberDiff line change
@@ -2142,7 +2142,7 @@ t('Does not try rollback when commit errors', async() => {
21422142
})
21432143

21442144
t('Last keyword used even with duplicate keywords', async() => {
2145-
await sql`create table test (x int);`
2145+
await sql`create table test (x int)`
21462146
await sql`insert into test values(1)`
21472147
const [{ x }] = await sql`
21482148
select
@@ -2151,5 +2151,35 @@ t('Last keyword used even with duplicate keywords', async() => {
21512151
where x in ${ sql([1, 2]) }
21522152
`
21532153

2154-
return [x, true]
2154+
return [x, true, await sql`drop table test`]
2155+
})
2156+
2157+
t('Insert array with null', async() => {
2158+
await sql`create table test (x int[])`
2159+
await sql`insert into test ${ sql({ x: [1, null, 3] }) }`
2160+
return [
2161+
1,
2162+
(await sql`select x from test`)[0].x[0],
2163+
await sql`drop table test`
2164+
]
2165+
})
2166+
2167+
t('Insert array with undefined throws', async() => {
2168+
await sql`create table test (x int[])`
2169+
return [
2170+
'UNDEFINED_VALUE',
2171+
await sql`insert into test ${ sql({ x: [1, undefined, 3] }) }`.catch(e => e.code),
2172+
await sql`drop table test`
2173+
]
2174+
})
2175+
2176+
t('Insert array with undefined transform', async() => {
2177+
const sql = postgres({ ...options, transform: { undefined: null } })
2178+
await sql`create table test (x int[])`
2179+
await sql`insert into test ${ sql({ x: [1, undefined, 3] }) }`
2180+
return [
2181+
1,
2182+
(await sql`select x from test`)[0].x[0],
2183+
await sql`drop table test`
2184+
]
21552185
})

0 commit comments

Comments
 (0)