Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions packages/pg/lib/utils.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
'use strict'

const defaults = require('./defaults')
const nodeUtils = require('util')

const { isDate } = require('util/types')

const invalidDateDeprecationNotice = nodeUtils.deprecate(
() => {},
'Sending an invalid date to Postgres is deprecated and will throw an error in the next major version of pg. Ensure any Date object passed as a query parameter is valid.',
'PG_INVALID_DATE'
)

function escapeElement(elementRepresentation) {
const escaped = elementRepresentation.replace(/\\/g, '\\\\').replace(/"/g, '\\"')

Expand Down Expand Up @@ -54,6 +61,9 @@ const prepareValue = function (val, seen) {
return Buffer.from(val.buffer, val.byteOffset, val.byteLength)
}
if (isDate(val)) {
if (isNaN(val.getTime())) {
invalidDateDeprecationNotice()
}
if (defaults.parseInputDatesAsUTC) {
return dateToStringUTC(val)
} else {
Expand Down
17 changes: 17 additions & 0 deletions packages/pg/test/unit/utils-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,23 @@ test('prepareValues: 1 BC date prepared properly', function () {
helper.resetTimezoneOffset()
})

test('prepareValue: invalid date emits deprecation warning', function () {
const warningSeen = new Promise((resolve) => {
const onWarning = (warning) => {
if (warning.code === 'PG_INVALID_DATE') {
process.removeListener('warning', onWarning)
resolve()
}
}
process.on('warning', onWarning)
})

const out = utils.prepareValue(new Date(NaN))
assert.strictEqual(out, '0NaN-NaN-NaNTNaN:NaN:NaN.NaN+NaN:NaN')

return warningSeen
})

test('prepareValues: undefined prepared properly', function () {
const out = utils.prepareValue(void 0)
assert.strictEqual(out, null)
Expand Down
Loading