Skip to content

Commit 8890904

Browse files
committed
Fix handling of null and undefined
1 parent de0070a commit 8890904

File tree

2 files changed

+27
-11
lines changed

2 files changed

+27
-11
lines changed

lib/index.js

+12-10
Original file line numberDiff line numberDiff line change
@@ -379,11 +379,13 @@ export default function Postgres(url, options) {
379379

380380
for (let i = 1; i < xs.length; i++) {
381381
arg = args[i - 1]
382-
str += (arg.rows
383-
? parseRows(arg.rows, xargs, types)
384-
: arg.row
385-
? parseRow(arg.row, xargs, types)
386-
: parseValue(arg, xargs, types)
382+
str += (!arg
383+
? parseValue(arg, xargs, types)
384+
: arg.rows
385+
? parseRows(arg.rows, xargs, types)
386+
: arg.row
387+
? parseRow(arg.row, xargs, types)
388+
: parseValue(arg, xargs, types)
387389
) + xs[i]
388390
}
389391

@@ -410,8 +412,11 @@ export default function Postgres(url, options) {
410412
}
411413

412414
function getType(x) {
415+
if (x == null)
416+
return { type: types.string.to, value: x }
417+
413418
const value = x.type ? x.value : x
414-
, type = x && x.array ? typeArrayMap[x.type || inferType(value)] : (x.type || inferType(value))
419+
, type = x.array ? typeArrayMap[x.type || inferType(value)] : (x.type || inferType(value))
415420

416421
return {
417422
type,
@@ -445,10 +450,7 @@ function parseOptions(uri = {}, options) {
445450
onnotice : o.onnotice,
446451
onparameter : o.onparameter,
447452
none : crypto.randomBytes(18).toString('base64'),
448-
connection : {
449-
application_name: 'postgres.js',
450-
...o.connection
451-
},
453+
connection : { application_name: 'postgres.js', ...o.connection },
452454
...mergeUserTypes(o.types)
453455
}
454456
}

tests/index.js

+15-1
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,14 @@ t('Drop table', async() =>
5757
['DROP TABLE', (await sql`drop table test`).command]
5858
)
5959

60+
t('null', async() =>
61+
[null, (await sql`select ${ null } as x`)[0].x]
62+
)
63+
64+
t('undefined to null', async() =>
65+
[null, (await sql`select ${ undefined } as x`)[0].x]
66+
)
67+
6068
t('Integer', async() =>
6169
[1, (await sql`select ${ 1 } as x`)[0].x]
6270
)
@@ -237,7 +245,13 @@ t('Point type array', async() => {
237245
t('sql file', async() =>
238246
[1, (await sql.file('./select.sql'))[0].x]
239247
)
240-
248+
/*
249+
t('select column vars', async() => {
250+
await sql`create table test (x int)`
251+
await sql`insert into test values (1)`
252+
return [1, (await sql`select ${ 'x' } from test`)[0].x]
253+
})
254+
*/
241255
t('sql file can stream', async() => {
242256
let result
243257
await sql.file('./select.sql')

0 commit comments

Comments
 (0)