mirror of
https://github.com/brianc/node-postgres.git
synced 2025-12-08 20:16:25 +00:00
Change instanceof(Date) to util.types.isDate(Date) (#2862)
* change instanceof to isDate * use both methods to check for valid Date * add test for PR 2862 * use only isDate(date) in place of instanceof Date * Extend compatibility of `isDate` use back to Node 8 * Clean up test --------- Co-authored-by: Charmander <~@charmander.me> Reviewed-by: Charmander <~@charmander.me>
This commit is contained in:
parent
9cf2184d09
commit
3e7bd2f681
@ -2,6 +2,9 @@
|
||||
|
||||
const defaults = require('./defaults')
|
||||
|
||||
const util = require('util')
|
||||
const { isDate } = util.types || util // Node 8 doesn't have `util.types`
|
||||
|
||||
function escapeElement(elementRepresentation) {
|
||||
const escaped = elementRepresentation.replace(/\\/g, '\\\\').replace(/"/g, '\\"')
|
||||
|
||||
@ -60,7 +63,7 @@ const prepareValue = function (val, seen) {
|
||||
}
|
||||
return buf.slice(val.byteOffset, val.byteOffset + val.byteLength) // Node.js v4 does not support those Buffer.from params
|
||||
}
|
||||
if (val instanceof Date) {
|
||||
if (isDate(val)) {
|
||||
if (defaults.parseInputDatesAsUTC) {
|
||||
return dateToStringUTC(val)
|
||||
} else {
|
||||
|
||||
23
packages/pg/test/integration/gh-issues/2862-tests.js
Normal file
23
packages/pg/test/integration/gh-issues/2862-tests.js
Normal file
@ -0,0 +1,23 @@
|
||||
'use strict'
|
||||
|
||||
const helper = require('../test-helper')
|
||||
const assert = require('assert')
|
||||
const vm = require('vm')
|
||||
|
||||
const suite = new helper.Suite()
|
||||
|
||||
suite.testAsync('Handle date objects as Date', async () => {
|
||||
const crossRealmDate = await vm.runInNewContext('new Date()')
|
||||
assert(!(crossRealmDate instanceof Date))
|
||||
const date = new Date(crossRealmDate.getTime())
|
||||
const client = new helper.pg.Client()
|
||||
await client.connect()
|
||||
|
||||
await client.query('CREATE TEMP TABLE foo(bar timestamptz, bar2 timestamptz)')
|
||||
await client.query('INSERT INTO foo(bar, bar2) VALUES($1, $2)', [date, crossRealmDate])
|
||||
const results = await client.query('SELECT * FROM foo')
|
||||
const row = results.rows[0]
|
||||
assert.deepStrictEqual(row.bar, date)
|
||||
assert.deepStrictEqual(row.bar2, date)
|
||||
await client.end()
|
||||
})
|
||||
Loading…
x
Reference in New Issue
Block a user