mirror of
https://github.com/brianc/node-postgres.git
synced 2026-01-18 15:55:05 +00:00
parent
b9a528cb3b
commit
16322c2d50
@ -30,7 +30,10 @@ export default {
|
||||
head: (
|
||||
<>
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
||||
<meta name="description" content="node-postgres is a collection of node.js modules for interfacing with your PostgreSQL database." />
|
||||
<meta
|
||||
name="description"
|
||||
content="node-postgres is a collection of node.js modules for interfacing with your PostgreSQL database."
|
||||
/>
|
||||
<meta name="og:title" content="node-postgres" />
|
||||
<script async src="https://www.googletagmanager.com/gtag/js?id=UA-100138145-1"></script>
|
||||
<script
|
||||
|
||||
@ -30,9 +30,24 @@ describe('error handling', function () {
|
||||
const queuedRead1 = cursor.read(1)
|
||||
const queuedRead2 = cursor.read(1)
|
||||
|
||||
assert(await immediateRead.then(() => null, (err) => err))
|
||||
assert(await queuedRead1.then(() => null, (err) => err))
|
||||
assert(await queuedRead2.then(() => null, (err) => err))
|
||||
assert(
|
||||
await immediateRead.then(
|
||||
() => null,
|
||||
(err) => err
|
||||
)
|
||||
)
|
||||
assert(
|
||||
await queuedRead1.then(
|
||||
() => null,
|
||||
(err) => err
|
||||
)
|
||||
)
|
||||
assert(
|
||||
await queuedRead2.then(
|
||||
() => null,
|
||||
(err) => err
|
||||
)
|
||||
)
|
||||
|
||||
client.end()
|
||||
})
|
||||
|
||||
@ -39,11 +39,11 @@ function promisify(Promise, callback) {
|
||||
const result = new Promise(function (resolve, reject) {
|
||||
res = resolve
|
||||
rej = reject
|
||||
}).catch(err => {
|
||||
}).catch((err) => {
|
||||
// replace the stack trace that leads to `TCP.onStreamRead` with one that leads back to the
|
||||
// application that created the query
|
||||
Error.captureStackTrace(err);
|
||||
throw err;
|
||||
Error.captureStackTrace(err)
|
||||
throw err
|
||||
})
|
||||
return { callback: cb, result: result }
|
||||
}
|
||||
|
||||
@ -3,7 +3,11 @@ if (module === require.main) {
|
||||
const allowExitOnIdle = process.env.ALLOW_EXIT_ON_IDLE === '1'
|
||||
const Pool = require('../index')
|
||||
|
||||
const pool = new Pool({ maxLifetimeSeconds: 2, idleTimeoutMillis: 200, ...(allowExitOnIdle ? { allowExitOnIdle: true } : {}) })
|
||||
const pool = new Pool({
|
||||
maxLifetimeSeconds: 2,
|
||||
idleTimeoutMillis: 200,
|
||||
...(allowExitOnIdle ? { allowExitOnIdle: true } : {}),
|
||||
})
|
||||
pool.query('SELECT NOW()', (err, res) => console.log('completed first'))
|
||||
pool.on('remove', () => {
|
||||
console.log('removed')
|
||||
|
||||
@ -1,8 +1,7 @@
|
||||
const pg = require('./lib')
|
||||
|
||||
const params = {
|
||||
text:
|
||||
'select typname, typnamespace, typowner, typlen, typbyval, typcategory, typispreferred, typisdefined, typdelim, typrelid, typelem, typarray from pg_type where typtypmod = $1 and typisdefined = $2',
|
||||
text: 'select typname, typnamespace, typowner, typlen, typbyval, typcategory, typispreferred, typisdefined, typdelim, typrelid, typelem, typarray from pg_type where typtypmod = $1 and typisdefined = $2',
|
||||
values: [-1, true],
|
||||
}
|
||||
|
||||
|
||||
@ -520,11 +520,11 @@ class Client extends EventEmitter {
|
||||
if (!query.callback) {
|
||||
result = new this._Promise((resolve, reject) => {
|
||||
query.callback = (err, res) => (err ? reject(err) : resolve(res))
|
||||
}).catch(err => {
|
||||
}).catch((err) => {
|
||||
// replace the stack trace that leads to `TCP.onStreamRead` with one that leads back to the
|
||||
// application that created the query
|
||||
Error.captureStackTrace(err);
|
||||
throw err;
|
||||
Error.captureStackTrace(err)
|
||||
throw err
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
@ -5,5 +5,5 @@ if (useLegacyCrypto) {
|
||||
// We are on an old version of Node.js that requires legacy crypto utilities.
|
||||
module.exports = require('./utils-legacy')
|
||||
} else {
|
||||
module.exports = require('./utils-webcrypto');
|
||||
module.exports = require('./utils-webcrypto')
|
||||
}
|
||||
|
||||
@ -174,9 +174,9 @@ Client.prototype.query = function (config, values, callback) {
|
||||
result = new this._Promise((resolve, reject) => {
|
||||
resolveOut = resolve
|
||||
rejectOut = reject
|
||||
}).catch(err => {
|
||||
Error.captureStackTrace(err);
|
||||
throw err;
|
||||
}).catch((err) => {
|
||||
Error.captureStackTrace(err)
|
||||
throw err
|
||||
})
|
||||
query.callback = (err, res) => (err ? rejectOut(err) : resolveOut(res))
|
||||
}
|
||||
|
||||
@ -137,8 +137,7 @@ class Query extends EventEmitter {
|
||||
if (this.callback) {
|
||||
try {
|
||||
this.callback(null, this._results)
|
||||
}
|
||||
catch(err) {
|
||||
} catch (err) {
|
||||
process.nextTick(() => {
|
||||
throw err
|
||||
})
|
||||
|
||||
@ -10,20 +10,20 @@ process.on('unhandledRejection', function (e) {
|
||||
const suite = new helper.Suite()
|
||||
|
||||
// these tests will only work for if --async-stack-traces is on, which is the default starting in node 16.
|
||||
const NODE_MAJOR_VERSION = +process.versions.node.split('.')[0];
|
||||
const NODE_MAJOR_VERSION = +process.versions.node.split('.')[0]
|
||||
if (NODE_MAJOR_VERSION >= 16) {
|
||||
suite.testAsync('promise API async stack trace in pool', async function outerFunction() {
|
||||
async function innerFunction() {
|
||||
const pool = new pg.Pool()
|
||||
await pool.query('SELECT test from nonexistent');
|
||||
await pool.query('SELECT test from nonexistent')
|
||||
}
|
||||
try {
|
||||
await innerFunction();
|
||||
throw Error("should have errored");
|
||||
await innerFunction()
|
||||
throw Error('should have errored')
|
||||
} catch (e) {
|
||||
const stack = e.stack;
|
||||
if(!e.stack.includes("innerFunction") || !e.stack.includes("outerFunction")) {
|
||||
throw Error("async stack trace does not contain wanted values: " + stack);
|
||||
const stack = e.stack
|
||||
if (!e.stack.includes('innerFunction') || !e.stack.includes('outerFunction')) {
|
||||
throw Error('async stack trace does not contain wanted values: ' + stack)
|
||||
}
|
||||
}
|
||||
})
|
||||
@ -31,21 +31,21 @@ if (NODE_MAJOR_VERSION >= 16) {
|
||||
suite.testAsync('promise API async stack trace in client', async function outerFunction() {
|
||||
async function innerFunction() {
|
||||
const client = new pg.Client()
|
||||
await client.connect();
|
||||
await client.connect()
|
||||
try {
|
||||
await client.query('SELECT test from nonexistent');
|
||||
await client.query('SELECT test from nonexistent')
|
||||
} finally {
|
||||
client.end();
|
||||
client.end()
|
||||
}
|
||||
}
|
||||
try {
|
||||
await innerFunction();
|
||||
throw Error("should have errored");
|
||||
await innerFunction()
|
||||
throw Error('should have errored')
|
||||
} catch (e) {
|
||||
const stack = e.stack;
|
||||
if(!e.stack.includes("innerFunction") || !e.stack.includes("outerFunction")) {
|
||||
throw Error("async stack trace does not contain wanted values: " + stack);
|
||||
const stack = e.stack
|
||||
if (!e.stack.includes('innerFunction') || !e.stack.includes('outerFunction')) {
|
||||
throw Error('async stack trace does not contain wanted values: ' + stack)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
@ -79,7 +79,7 @@ suite.testAsync('sasl/scram fails when password is empty', async () => {
|
||||
...config,
|
||||
// We use a password function here so the connection defaults do not
|
||||
// override the empty string value with one from process.env.PGPASSWORD
|
||||
password: () => '',
|
||||
password: () => '',
|
||||
})
|
||||
let usingSasl = false
|
||||
client.connection.once('authenticationSASL', () => {
|
||||
|
||||
@ -8,13 +8,19 @@ suite.testAsync('result fields with the same name should pick the last value', a
|
||||
const client = new helper.pg.Client()
|
||||
await client.connect()
|
||||
|
||||
const { rows: [shouldBeNullRow] } = await client.query('SELECT NULL AS test, 10 AS test, NULL AS test')
|
||||
const {
|
||||
rows: [shouldBeNullRow],
|
||||
} = await client.query('SELECT NULL AS test, 10 AS test, NULL AS test')
|
||||
assert.equal(shouldBeNullRow.test, null)
|
||||
|
||||
const { rows: [shouldBeTwelveRow] } = await client.query('SELECT NULL AS test, 10 AS test, 12 AS test')
|
||||
const {
|
||||
rows: [shouldBeTwelveRow],
|
||||
} = await client.query('SELECT NULL AS test, 10 AS test, 12 AS test')
|
||||
assert.equal(shouldBeTwelveRow.test, 12)
|
||||
|
||||
const { rows: [shouldBeAbcRow] } = await client.query(`SELECT NULL AS test, 10 AS test, 12 AS test, 'ABC' AS test`)
|
||||
const {
|
||||
rows: [shouldBeAbcRow],
|
||||
} = await client.query(`SELECT NULL AS test, 10 AS test, 12 AS test, 'ABC' AS test`)
|
||||
assert.equal(shouldBeAbcRow.test, 'ABC')
|
||||
|
||||
await client.end()
|
||||
|
||||
@ -4,8 +4,7 @@ const pool = new helper.pg.Pool()
|
||||
|
||||
pool.connect(function (err, client) {
|
||||
var q = {
|
||||
name:
|
||||
'This is a super long query name just so I can test that an error message is properly spit out to console.error without throwing an exception or anything',
|
||||
name: 'This is a super long query name just so I can test that an error message is properly spit out to console.error without throwing an exception or anything',
|
||||
text: 'SELECT NOW()',
|
||||
}
|
||||
client.query(q, function () {
|
||||
|
||||
@ -20,8 +20,7 @@ var testLit = function (testName, input, expected) {
|
||||
var actual = Client.prototype.escapeLiteral(input)
|
||||
assert.equal(expected, actual)
|
||||
})
|
||||
|
||||
|
||||
|
||||
test('utils.' + testName, function () {
|
||||
var actual = utils.escapeLiteral(input)
|
||||
assert.equal(expected, actual)
|
||||
@ -39,8 +38,7 @@ var testIdent = function (testName, input, expected) {
|
||||
var actual = Client.prototype.escapeIdentifier(input)
|
||||
assert.equal(expected, actual)
|
||||
})
|
||||
|
||||
|
||||
|
||||
test('utils.' + testName, function () {
|
||||
var actual = utils.escapeIdentifier(input)
|
||||
assert.equal(expected, actual)
|
||||
|
||||
@ -256,9 +256,17 @@ testEscapeLiteral('escapeLiteral: contains backslashes only', 'hello \\ world',
|
||||
|
||||
testEscapeLiteral('escapeLiteral: contains single quotes and double quotes', 'hello \' " world', "'hello '' \" world'")
|
||||
|
||||
testEscapeLiteral('escapeLiteral: contains double quotes and backslashes', 'hello \\ " world', " E'hello \\\\ \" world'")
|
||||
testEscapeLiteral(
|
||||
'escapeLiteral: contains double quotes and backslashes',
|
||||
'hello \\ " world',
|
||||
" E'hello \\\\ \" world'"
|
||||
)
|
||||
|
||||
testEscapeLiteral('escapeLiteral: contains single quotes and backslashes', "hello \\ ' world", " E'hello \\\\ '' world'")
|
||||
testEscapeLiteral(
|
||||
'escapeLiteral: contains single quotes and backslashes',
|
||||
"hello \\ ' world",
|
||||
" E'hello \\\\ '' world'"
|
||||
)
|
||||
|
||||
testEscapeLiteral(
|
||||
'escapeLiteral: contains single quotes, double quotes, and backslashes',
|
||||
@ -281,11 +289,23 @@ testEscapeIdentifier('escapeIdentifier: contains single quotes only', "hello ' w
|
||||
|
||||
testEscapeIdentifier('escapeIdentifier: contains backslashes only', 'hello \\ world', '"hello \\ world"')
|
||||
|
||||
testEscapeIdentifier('escapeIdentifier: contains single quotes and double quotes', 'hello \' " world', '"hello \' "" world"')
|
||||
testEscapeIdentifier(
|
||||
'escapeIdentifier: contains single quotes and double quotes',
|
||||
'hello \' " world',
|
||||
'"hello \' "" world"'
|
||||
)
|
||||
|
||||
testEscapeIdentifier('escapeIdentifier: contains double quotes and backslashes', 'hello \\ " world', '"hello \\ "" world"')
|
||||
testEscapeIdentifier(
|
||||
'escapeIdentifier: contains double quotes and backslashes',
|
||||
'hello \\ " world',
|
||||
'"hello \\ "" world"'
|
||||
)
|
||||
|
||||
testEscapeIdentifier('escapeIdentifier: contains single quotes and backslashes', "hello \\ ' world", '"hello \\ \' world"')
|
||||
testEscapeIdentifier(
|
||||
'escapeIdentifier: contains single quotes and backslashes',
|
||||
"hello \\ ' world",
|
||||
'"hello \\ \' world"'
|
||||
)
|
||||
|
||||
testEscapeIdentifier(
|
||||
'escapeIdentifier: contains single quotes, double quotes, and backslashes',
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user