Skip to content

Commit 2b83a56

Browse files
committed
Build cjs + deno
1 parent bee62f3 commit 2b83a56

File tree

6 files changed

+92
-16
lines changed

6 files changed

+92
-16
lines changed

cjs/src/connection.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -354,7 +354,7 @@ function Connection(options, queues = {}, { onopen = noop, onend = noop, onclose
354354
statementCount = 1
355355
lifeTimer.start()
356356
socket.on('data', data)
357-
keep_alive != null && socket.setKeepAlive(true, 1000 * keep_alive)
357+
keep_alive && socket.setKeepAlive(true, 1000 * keep_alive)
358358
const s = StartupMessage()
359359
write(s)
360360
} catch (err) {
@@ -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) {

cjs/src/types.js

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ function arrayEscape(x) {
231231
.replace(escapeQuote, '\\"')
232232
}
233233

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

@@ -243,9 +243,17 @@ const arraySerializer = module.exports.arraySerializer = function arraySerialize
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+
: '"' + arrayEscape(serializer ? serializer(x.type ? x.value : x) : '' + x) + '"'
256+
}).join(',') + '}'
249257
}
250258

251259
const arrayParserState = {

cjs/tests/index.js

Lines changed: 32 additions & 2 deletions
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
})

deno/src/connection.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -358,7 +358,7 @@ function Connection(options, queues = {}, { onopen = noop, onend = noop, onclose
358358
statementCount = 1
359359
lifeTimer.start()
360360
socket.on('data', data)
361-
keep_alive != null && socket.setKeepAlive(true)
361+
keep_alive && socket.setKeepAlive(true)
362362
const s = StartupMessage()
363363
write(s)
364364
} catch (err) {
@@ -735,7 +735,7 @@ function Connection(options, queues = {}, { onopen = noop, onend = noop, onclose
735735
options.shared.typeArrayMap[oid] = typarray
736736
options.parsers[typarray] = (xs) => arrayParser(xs, parser)
737737
options.parsers[typarray].array = true
738-
options.serializers[typarray] = (xs) => arraySerializer(xs, options.serializers[oid])
738+
options.serializers[typarray] = (xs) => arraySerializer(xs, options.serializers[oid], options)
739739
}
740740

741741
function tryNext(x, xs) {

deno/src/types.js

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ function arrayEscape(x) {
232232
.replace(escapeQuote, '\\"')
233233
}
234234

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

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

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

252260
const arrayParserState = {

deno/tests/index.js

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2144,7 +2144,7 @@ t('Does not try rollback when commit errors', async() => {
21442144
})
21452145

21462146
t('Last keyword used even with duplicate keywords', async() => {
2147-
await sql`create table test (x int);`
2147+
await sql`create table test (x int)`
21482148
await sql`insert into test values(1)`
21492149
const [{ x }] = await sql`
21502150
select
@@ -2153,7 +2153,37 @@ t('Last keyword used even with duplicate keywords', async() => {
21532153
where x in ${ sql([1, 2]) }
21542154
`
21552155

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

21592189
;window.addEventListener("unload", () => Deno.exit(process.exitCode))

0 commit comments

Comments
 (0)