Skip to content

Commit eab71e5

Browse files
committed
Support transform.undefined - fixes porsager#314
1 parent 86445ca commit eab71e5

File tree

3 files changed

+22
-5
lines changed

3 files changed

+22
-5
lines changed

src/index.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -397,7 +397,7 @@ function parseOptions(a, b) {
397397
onnotify : o.onnotify,
398398
onclose : o.onclose,
399399
onparameter : o.onparameter,
400-
transform : parseTransform(o.transform || {}),
400+
transform : parseTransform(o.transform || { undefined: undefined }),
401401
connection : Object.assign({ application_name: 'postgres.js' }, o.connection),
402402
target_session_attrs: tsa(o, url, env),
403403
debug : o.debug,
@@ -429,6 +429,7 @@ function max_lifetime() {
429429

430430
function parseTransform(x) {
431431
return {
432+
undefined: x.undefined,
432433
column: {
433434
from: typeof x.column === 'function' ? x.column : x.column && x.column.from,
434435
to: x.column && x.column.to

src/types.js

+10-4
Original file line numberDiff line numberDiff line change
@@ -73,10 +73,16 @@ export class Builder extends NotTagged {
7373
}
7474
}
7575

76-
export function handleValue(x, parameters, types) {
77-
const value = x instanceof Parameter ? x.value : x
78-
if (value === undefined)
79-
throw Errors.generic('UNDEFINED_VALUE', 'Undefined values are not allowed')
76+
export function handleValue(x, parameters, types, options) {
77+
let value = x instanceof Parameter ? x.value : x
78+
if (value === undefined) {
79+
x instanceof Parameter
80+
? x.value = options.transform.undefined
81+
: value = x = options.transform.undefined
82+
83+
if (value === undefined)
84+
throw Errors.generic('UNDEFINED_VALUE', 'Undefined values are not allowed')
85+
}
8086

8187
return '$' + (types.push(
8288
x instanceof Parameter

tests/index.js

+10
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,16 @@ t('Undefined values throws', async() => {
319319
return ['UNDEFINED_VALUE', error]
320320
})
321321

322+
t('Transform undefined', async() => {
323+
const sql = postgres({ transform: { undefined: null } })
324+
return [null, (await sql`select ${ undefined } as x`)[0].x]
325+
})
326+
327+
t('Transform undefined in array', async() => {
328+
const sql = postgres({ transform: { undefined: null } })
329+
return [null, (await sql`select * from (values ${ sql([undefined, undefined]) }) as x(x, y)`)[0].y]
330+
})
331+
322332
t('Null sets to null', async() =>
323333
[null, (await sql`select ${ null } as x`)[0].x]
324334
)

0 commit comments

Comments
 (0)