-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Make async/await
the primary examples in the docs
#2932
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
805f8e1
Correctly capitalize GitHub
rijkvanzanten 3a33c25
Add note on callbacks to index
rijkvanzanten e908c10
Add note on error handling
rijkvanzanten 385fba4
Update client examples to use promises
rijkvanzanten c12a419
Update pooling examples
rijkvanzanten e84650a
Fix readme link
rijkvanzanten a246723
Update cursor docs
rijkvanzanten 035c183
Update connecting examples
rijkvanzanten 61d7b08
Update Queries
rijkvanzanten cfcc942
Update examples in pooling
rijkvanzanten 231ff6d
Update trx examples
rijkvanzanten 2b45232
Update SSL example
rijkvanzanten 39fe3bc
Update example
rijkvanzanten fcd53f9
Use ESM instead of CJS
rijkvanzanten 9c2697c
Merge branch 'master' into docs
rijkvanzanten 52d9cb6
Update docs/pages/apis/cursor.mdx
rijkvanzanten 48c1637
Update docs/pages/apis/cursor.mdx
rijkvanzanten 7667548
Update docs/pages/apis/pool.mdx
rijkvanzanten 2f184f9
Update docs/pages/apis/pool.mdx
rijkvanzanten 8955b2d
Update docs/pages/apis/pool.mdx
rijkvanzanten d3b2dce
Update docs/pages/features/connecting.mdx
rijkvanzanten e7f4a77
Update docs/pages/features/connecting.mdx
rijkvanzanten 14acfcf
Update docs/pages/features/ssl.mdx
rijkvanzanten File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -79,7 +79,7 @@ _If you find `pg` valuable to you or your business please consider [supporting]( | |
|
||
After a _very_ long time on my todo list I've ported the docs from my old hand-rolled webapp running on route53 + elb + ec2 + dokku (I know, I went overboard!) to [gatsby](https://www.gatsbyjs.org/) hosted on [netlify](https://www.netlify.com/) which is _so_ much easier to manage. I've released the code at [https://github.com/brianc/node-postgres-docs](https://github.com/brianc/node-postgres-docs) and invite your contributions! Let's make this documentation better together. Any time changes are merged to master on the documentation repo it will automatically deploy. | ||
|
||
If you see an error in the docs, big or small, use the "edit on github" button to edit the page & submit a pull request right there. I'll get a new version out ASAP with your changes! If you want to add new pages of documentation open an issue if you need guidance, and I'll help you get started. | ||
If you see an error in the docs, big or small, use the "edit on GitHub" button to edit the page & submit a pull request right there. I'll get a new version out ASAP with your changes! If you want to add new pages of documentation open an issue if you need guidance, and I'll help you get started. | ||
|
||
I want to extend a special **thank you** to all the [supporters](https://github.com/brianc/node-postgres/blob/master/SPONSORS.md) and [contributors](https://github.com/brianc/node-postgres/graphs/contributors) to the project that have helped keep me going through times of burnout or life "getting in the way." ❤️ | ||
|
||
|
@@ -116,7 +116,7 @@ [email protected] | |
To demonstrate the issue & see if you are vunerable execute the following in node: | ||
|
||
```js | ||
const { Client } = require('pg') | ||
import { Client } from 'pg' | ||
const client = new Client() | ||
client.connect() | ||
|
||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -29,7 +29,7 @@ type Config = { | |
example to create a client with specific connection information: | ||
|
||
```js | ||
const { Client } = require('pg') | ||
import { Client } from 'pg' | ||
|
||
const client = new Client({ | ||
host: 'my.database-server.com', | ||
|
@@ -42,33 +42,13 @@ const client = new Client({ | |
|
||
## client.connect | ||
|
||
Calling `client.connect` with a callback: | ||
|
||
```js | ||
const { Client } = require('pg') | ||
import { Client } from 'pg' | ||
const client = new Client() | ||
client.connect((err) => { | ||
if (err) { | ||
console.error('connection error', err.stack) | ||
} else { | ||
console.log('connected') | ||
} | ||
}) | ||
``` | ||
|
||
Calling `client.connect` without a callback yields a promise: | ||
|
||
```js | ||
const { Client } = require('pg') | ||
const client = new Client() | ||
client | ||
.connect() | ||
.then(() => console.log('connected')) | ||
.catch((err) => console.error('connection error', err.stack)) | ||
await client.connect() | ||
``` | ||
|
||
_note: connect returning a promise only available in [email protected] or above_ | ||
|
||
## client.query | ||
|
||
### QueryConfig | ||
|
@@ -95,77 +75,43 @@ type QueryConfig { | |
} | ||
``` | ||
|
||
### callback API | ||
|
||
```ts | ||
client.query(text: string, values?: any[], callback?: (err: Error, result: QueryResult) => void) => void | ||
``` | ||
|
||
**Plain text query with a callback:** | ||
|
||
```js | ||
const { Client } = require('pg') | ||
const client = new Client() | ||
client.connect() | ||
client.query('SELECT NOW()', (err, res) => { | ||
if (err) throw err | ||
console.log(res) | ||
client.end() | ||
}) | ||
client.query(text: string, values?: any[]) => Promise<Result> | ||
``` | ||
|
||
**Parameterized query with a callback:** | ||
**Plain text query** | ||
|
||
```js | ||
const { Client } = require('pg') | ||
import { Client } from 'pg' | ||
const client = new Client() | ||
client.connect() | ||
client.query('SELECT $1::text as name', ['brianc'], (err, res) => { | ||
if (err) throw err | ||
console.log(res) | ||
client.end() | ||
}) | ||
``` | ||
|
||
### Promise API | ||
await client.connect() | ||
|
||
If you call `client.query` with query text and optional parameters but **don't** pass a callback, then you will receive a `Promise` for a query result. | ||
const result = await client.query('SELECT NOW()') | ||
console.log(result) | ||
|
||
```ts | ||
client.query(text: string, values?: any[]) => Promise<Result> | ||
await client.end() | ||
``` | ||
|
||
**Plain text query with a promise** | ||
**Parameterized query** | ||
|
||
```js | ||
const { Client } = require('pg') | ||
import { Client } from 'pg' | ||
const client = new Client() | ||
client.connect() | ||
client | ||
.query('SELECT NOW()') | ||
.then((result) => console.log(result)) | ||
.catch((e) => console.error(e.stack)) | ||
.then(() => client.end()) | ||
``` | ||
|
||
**Parameterized query with a promise** | ||
await client.connect() | ||
|
||
```js | ||
const { Client } = require('pg') | ||
const client = new Client() | ||
client.connect() | ||
client | ||
.query('SELECT $1::text as name', ['brianc']) | ||
.then((result) => console.log(result)) | ||
.catch((e) => console.error(e.stack)) | ||
.then(() => client.end()) | ||
const result = await client.query('SELECT $1::text as name', ['brianc']) | ||
console.log(result) | ||
|
||
await client.end() | ||
``` | ||
|
||
```ts | ||
client.query(config: QueryConfig) => Promise<Result> | ||
``` | ||
|
||
**client.query with a QueryConfig and a callback** | ||
**client.query with a QueryConfig** | ||
|
||
If you pass a `name` parameter to the `client.query` method, the client will create a [prepared statement](/features/queries#prepared-statements). | ||
|
||
|
@@ -177,42 +123,18 @@ const query = { | |
rowMode: 'array', | ||
} | ||
|
||
client.query(query, (err, res) => { | ||
if (err) { | ||
console.error(err.stack) | ||
} else { | ||
console.log(res.rows) // ['brianc'] | ||
} | ||
}) | ||
``` | ||
|
||
**client.query with a QueryConfig and a Promise** | ||
const result = await client.query(query) | ||
console.log(result.rows) // ['brianc'] | ||
|
||
```js | ||
const query = { | ||
name: 'get-name', | ||
text: 'SELECT $1::text', | ||
values: ['brianc'], | ||
rowMode: 'array', | ||
} | ||
|
||
// promise | ||
client | ||
.query(query) | ||
.then((res) => { | ||
console.log(res.rows) // ['brianc'] | ||
}) | ||
.catch((e) => { | ||
console.error(e.stack) | ||
}) | ||
await client.end() | ||
``` | ||
|
||
**client.query with a `Submittable`** | ||
|
||
If you pass an object to `client.query` and the object has a `.submit` function on it, the client will pass it's PostgreSQL server connection to the object and delegate query dispatching to the supplied object. This is an advanced feature mostly intended for library authors. It is incidentally also currently how the callback and promise based queries above are handled internally, but this is subject to change. It is also how [pg-cursor](https://github.com/brianc/node-pg-cursor) and [pg-query-stream](https://github.com/brianc/node-pg-query-stream) work. | ||
|
||
```js | ||
const Query = require('pg').Query | ||
import { Query } from 'pg' | ||
const query = new Query('select $1::text as name', ['brianc']) | ||
|
||
const result = client.query(query) | ||
|
@@ -222,9 +144,11 @@ assert(query === result) // true | |
query.on('row', (row) => { | ||
console.log('row!', row) // { name: 'brianc' } | ||
}) | ||
|
||
query.on('end', () => { | ||
console.log('query done') | ||
}) | ||
|
||
query.on('error', (err) => { | ||
console.error(err.stack) | ||
}) | ||
|
@@ -237,25 +161,10 @@ query.on('error', (err) => { | |
Disconnects the client from the PostgreSQL server. | ||
|
||
```js | ||
client.end((err) => { | ||
console.log('client has disconnected') | ||
if (err) { | ||
console.log('error during disconnection', err.stack) | ||
} | ||
}) | ||
await client.end() | ||
console.log('client has disconnected') | ||
``` | ||
|
||
Calling end without a callback yields a promise: | ||
|
||
```js | ||
client | ||
.end() | ||
.then(() => console.log('client has disconnected')) | ||
.catch((err) => console.error('error during disconnection', err.stack)) | ||
``` | ||
|
||
_note: end returning a promise is only available in pg7.0 and above_ | ||
|
||
## events | ||
|
||
### error | ||
|
@@ -264,7 +173,7 @@ _note: end returning a promise is only available in pg7.0 and above_ | |
client.on('error', (err: Error) => void) => void | ||
``` | ||
|
||
When the client is in the process of connecting, dispatching a query, or disconnecting it will catch and foward errors from the PostgreSQL server to the respective `client.connect` `client.query` or `client.end` callback/promise; however, the client maintains a long-lived connection to the PostgreSQL back-end and due to network partitions, back-end crashes, fail-overs, etc the client can (and over a long enough time period _will_) eventually be disconnected while it is idle. To handle this you may want to attach an error listener to a client to catch errors. Here's a contrived example: | ||
When the client is in the process of connecting, dispatching a query, or disconnecting it will catch and foward errors from the PostgreSQL server to the respective `client.connect` `client.query` or `client.end` promise; however, the client maintains a long-lived connection to the PostgreSQL back-end and due to network partitions, back-end crashes, fail-overs, etc the client can (and over a long enough time period _will_) eventually be disconnected while it is idle. To handle this you may want to attach an error listener to a client to catch errors. Here's a contrived example: | ||
|
||
```js | ||
const client = new pg.Client() | ||
|
@@ -301,7 +210,7 @@ type Notification { | |
|
||
```js | ||
const client = new pg.Client() | ||
client.connect() | ||
await client.connect() | ||
|
||
client.query('LISTEN foo') | ||
|
||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This doesn’t work, see #2534.