diff --git a/packages/pg/lib/utils.js b/packages/pg/lib/utils.js index ba51c82c8..fcf3c6686 100644 --- a/packages/pg/lib/utils.js +++ b/packages/pg/lib/utils.js @@ -37,7 +37,8 @@ function arrayString(val) { } result += '\\\\x' + item.toString('hex') } else { - result += escapeElement(prepareValue(item)) + const prepared = prepareValue(item) + result += prepared == null ? 'NULL' : escapeElement(prepared) } } result += '}' diff --git a/packages/pg/test/unit/utils-tests.js b/packages/pg/test/unit/utils-tests.js index ff0d92944..debfaed89 100644 --- a/packages/pg/test/unit/utils-tests.js +++ b/packages/pg/test/unit/utils-tests.js @@ -181,6 +181,31 @@ test('prepareValue: complex array prepared properly', function () { assert.strictEqual(out, '{"{\\"x\\":42}","{\\"y\\":84}"}') }) +test('prepareValue: nullish custom array elements prepared as SQL NULL', function () { + for (const value of [null, undefined]) { + let calls = 0 + const customType = { + toPostgres: function () { + calls++ + return value + }, + } + assert.strictEqual(utils.prepareValue([customType, 'NULL', '', 0, false]), '{NULL,"NULL","","0","false"}') + assert.strictEqual(calls, 1) + assert.strictEqual(utils.prepareValue([[customType], [customType]]), '{{NULL},{NULL}}') + assert.strictEqual(calls, 3) + } +}) + +test('prepareValue: chained nullish custom array elements prepared as SQL NULL', function () { + const customType = { + toPostgres: function () { + return { toPostgres: () => null } + }, + } + assert.strictEqual(utils.prepareValue([customType]), '{NULL}') +}) + test('prepareValue: date array prepared properly', function () { helper.setTimezoneOffset(-330)