Skip to content

Commit 12b9a69

Browse files
authored
update docs - clean up interface (brianc#2863)
* update docs - clean up interface * Remove node v8.x from test matrix
1 parent c7dc621 commit 12b9a69

File tree

5 files changed

+53
-65
lines changed

5 files changed

+53
-65
lines changed

.github/workflows/ci.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ jobs:
2020
options: --health-cmd pg_isready --health-interval 10s --health-timeout 5s --health-retries 5
2121
strategy:
2222
matrix:
23-
node: ['8', '10', '12', '14', '16', '18']
23+
node: ['10', '12', '14', '16', '18']
2424
os: [ubuntu-latest, windows-latest, macos-latest]
2525
name: Node.js ${{ matrix.node }} (${{ matrix.os }})
2626
steps:

SPONSORS.md

+2
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ node-postgres is made possible by the helpful contributors from the community as
1616
- [@BLUE-DEVIL1134](https://github.com/BLUE-DEVIL1134)
1717
- [bubble.io](https://bubble.io/)
1818
- GitHub[https://github.com/github]
19+
- loveland [https://github.com/loveland]
1920

2021
# Supporters
2122

@@ -48,3 +49,4 @@ node-postgres is made possible by the helpful contributors from the community as
4849
- [Scout APM](https://github.com/scoutapm-sponsorships)
4950
- [Sideline Sports](https://github.com/SidelineSports)
5051
- [Gadget](https://github.com/gadget-inc)
52+
- [Sentry](https://sentry.io/welcome/)

docs/pages/apis/client.mdx

+43-48
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,6 @@ const client = new Client({
4141

4242
## client.connect
4343

44-
### `client.connect(callback: (err: Error) => void) => void`
45-
4644
Calling `client.connect` with a callback:
4745

4846
```js
@@ -57,8 +55,6 @@ client.connect((err) => {
5755
})
5856
```
5957

60-
### `client.connect() => Promise<void>`
61-
6258
Calling `client.connect` without a callback yields a promise:
6359

6460
```js
@@ -74,19 +70,35 @@ _note: connect returning a promise only available in [email protected] or above_
7470

7571
## client.query
7672

77-
### `client.query` - text, optional values, and callback.
73+
### QueryConfig
7874

79-
Passing query text, optional query parameters, and a callback to `client.query` results in a type-signature of:
75+
You can pass an object to `client.query` with the signature of:
8076

8177
```ts
82-
client.query(
83-
text: string,
84-
values?: Array<any>,
85-
callback: (err: Error, result: Result) => void
86-
) => void
78+
type QueryConfig {
79+
// the raw query text
80+
text: string;
81+
82+
// an array of query parameters
83+
values?: Array<any>;
84+
85+
// name of the query - used for prepared statements
86+
name?: string;
87+
88+
// by default rows come out as a key/value pair for each row
89+
// pass the string 'array' here to receive rows as an array of values
90+
rowMode?: string;
91+
92+
// custom type parsers just for this query result
93+
types?: Types;
94+
}
8795
```
8896

89-
That is a kinda gross type signature but it translates out to this:
97+
### callback API
98+
99+
```ts
100+
client.query(text: string, values?: any[], callback?: (err: Error, result: QueryResult) => void) => void
101+
```
90102

91103
**Plain text query with a callback:**
92104

@@ -114,15 +126,12 @@ client.query('SELECT $1::text as name', ['brianc'], (err, res) => {
114126
})
115127
```
116128

117-
### `client.query` - text, optional values: Promise
129+
### Promise API
118130

119131
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.
120132

121133
```ts
122-
client.query(
123-
text: string,
124-
values?: Array<any>
125-
) => Promise<Result>
134+
client.query(text: string, values?: any[]) => Promise<Result>
126135
```
127136

128137
**Plain text query with a promise**
@@ -151,30 +160,8 @@ client
151160
.then(() => client.end())
152161
```
153162

154-
### `client.query(config: QueryConfig, callback: (err?: Error, result?: Result) => void) => void`
155-
156-
### `client.query(config: QueryConfig) => Promise<Result>`
157-
158-
You can pass an object to `client.query` with the signature of:
159-
160163
```ts
161-
type QueryConfig {
162-
// the raw query text
163-
text: string;
164-
165-
// an array of query parameters
166-
values?: Array<any>;
167-
168-
// name of the query - used for prepared statements
169-
name?: string;
170-
171-
// by default rows come out as a key/value pair for each row
172-
// pass the string 'array' here to receive rows as an array of values
173-
rowMode?: string;
174-
175-
// custom type parsers just for this query result
176-
types?: Types;
177-
}
164+
client.query(config: QueryConfig) => Promise<Result>
178165
```
179166

180167
**client.query with a QueryConfig and a callback**
@@ -246,8 +233,6 @@ query.on('error', (err) => {
246233

247234
## client.end
248235

249-
### client.end(cb?: (err?: Error) => void) => void
250-
251236
Disconnects the client from the PostgreSQL server.
252237

253238
```js
@@ -259,8 +244,6 @@ client.end((err) => {
259244
})
260245
```
261246

262-
### `client.end() => Promise<void>`
263-
264247
Calling end without a callback yields a promise:
265248

266249
```js
@@ -274,7 +257,11 @@ _note: end returning a promise is only available in pg7.0 and above_
274257

275258
## events
276259

277-
### client.on('error', (err: Error) => void) => void
260+
### error
261+
262+
```ts
263+
client.on('error', (err: Error) => void) => void
264+
```
278265

279266
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:
280267

@@ -291,11 +278,15 @@ client.on('error', (err) => {
291278
// process output: 'something bad has happened!' followed by stacktrace :P
292279
```
293280

294-
### client.on('end') => void
281+
### end
282+
283+
```ts
284+
client.on('end') => void
285+
```
295286

296287
When the client disconnects from the PostgreSQL server it will emit an end event once.
297288

298-
### client.on('notification', (notification: Notification) => void) => void
289+
### notification
299290

300291
Used for `listen/notify` events:
301292

@@ -321,7 +312,11 @@ client.on('notification', (msg) => {
321312
client.query(`NOTIFY foo, 'bar!'`)
322313
```
323314

324-
### client.on('notice', (notice: Error) => void) => void
315+
### notice
316+
317+
```ts
318+
client.on('notice', (notice: Error) => void) => void
319+
```
325320

326321
Used to log out [notice messages](https://www.postgresql.org/docs/9.6/static/plpgsql-errors-and-messages.html) from the PostgreSQL server.
327322

docs/pages/apis/pool.mdx

+6-2
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,9 @@ const pool = new Pool({
6363

6464
Often we only need to run a single query on the database, so as convenience the pool has a method to run a query on the first available idle client and return its result.
6565

66-
`pool.query() => Promise<pg.Result>`
66+
```ts
67+
pool.query(text: string, values?: any[]) => Promise<pg.Result>
68+
```
6769

6870
```js
6971
const { Pool } = require('pg')
@@ -78,7 +80,9 @@ pool
7880

7981
Callbacks are also supported:
8082

81-
`pool.query(callback: (err?: Error, result: pg.Result)) => void`
83+
```ts
84+
pool.query(text: string, values?: any[], callback?: (err?: Error, result: pg.Result)) => void
85+
```
8286

8387
```js
8488
const { Pool } = require('pg')

docs/pages/index.mdx

+1-14
Original file line numberDiff line numberDiff line change
@@ -13,20 +13,7 @@ $ npm install pg
1313

1414
## Supporters
1515

16-
node-postgres continued development and support is made possible by the many [supporters](https://github.com/brianc/node-postgres/blob/master/SPONSORS.md) with a special thanks to our featured supporters:
17-
18-
<div className="sponsors">
19-
<div className="sponsor">
20-
<a href="https://www.crate.io">
21-
<img src="crate-io.png" style={{ height: 80 }} alt="crate.io" />
22-
</a>
23-
</div>
24-
<div className="sponsor">
25-
<a href="https://www.eaze.com/">
26-
<img src="eaze.png" style={{ height: 80 }} alt="eaze.com" />
27-
</a>
28-
</div>
29-
</div>
16+
node-postgres continued development and support is made possible by the many [supporters](https://github.com/brianc/node-postgres/blob/master/SPONSORS.md).
3017

3118
If you or your company would like to sponsor node-postgres stop by [github sponsors](https://github.com/sponsors/brianc) and sign up or feel free to [email me](mailto:[email protected]) if you want to add your logo to the documentation or discuss higher tiers of sponsorship!
3219

0 commit comments

Comments
 (0)