Skip to content

update docs - clean up interface #2863

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 2 commits into from
Nov 23, 2022
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
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ jobs:
options: --health-cmd pg_isready --health-interval 10s --health-timeout 5s --health-retries 5
strategy:
matrix:
node: ['8', '10', '12', '14', '16', '18']
node: ['10', '12', '14', '16', '18']
os: [ubuntu-latest, windows-latest, macos-latest]
name: Node.js ${{ matrix.node }} (${{ matrix.os }})
steps:
Expand Down
2 changes: 2 additions & 0 deletions SPONSORS.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ node-postgres is made possible by the helpful contributors from the community as
- [@BLUE-DEVIL1134](https://github.com/BLUE-DEVIL1134)
- [bubble.io](https://bubble.io/)
- GitHub[https://github.com/github]
- loveland [https://github.com/loveland]

# Supporters

Expand Down Expand Up @@ -48,3 +49,4 @@ node-postgres is made possible by the helpful contributors from the community as
- [Scout APM](https://github.com/scoutapm-sponsorships)
- [Sideline Sports](https://github.com/SidelineSports)
- [Gadget](https://github.com/gadget-inc)
- [Sentry](https://sentry.io/welcome/)
91 changes: 43 additions & 48 deletions docs/pages/apis/client.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,6 @@ const client = new Client({

## client.connect

### `client.connect(callback: (err: Error) => void) => void`

Calling `client.connect` with a callback:

```js
Expand All @@ -57,8 +55,6 @@ client.connect((err) => {
})
```

### `client.connect() => Promise<void>`

Calling `client.connect` without a callback yields a promise:

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

## client.query

### `client.query` - text, optional values, and callback.
### QueryConfig

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

```ts
client.query(
text: string,
values?: Array<any>,
callback: (err: Error, result: Result) => void
) => void
type QueryConfig {
// the raw query text
text: string;

// an array of query parameters
values?: Array<any>;

// name of the query - used for prepared statements
name?: string;

// by default rows come out as a key/value pair for each row
// pass the string 'array' here to receive rows as an array of values
rowMode?: string;

// custom type parsers just for this query result
types?: Types;
}
```

That is a kinda gross type signature but it translates out to this:
### callback API

```ts
client.query(text: string, values?: any[], callback?: (err: Error, result: QueryResult) => void) => void
```

**Plain text query with a callback:**

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

### `client.query` - text, optional values: Promise
### Promise API

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.

```ts
client.query(
text: string,
values?: Array<any>
) => Promise<Result>
client.query(text: string, values?: any[]) => Promise<Result>
```

**Plain text query with a promise**
Expand Down Expand Up @@ -151,30 +160,8 @@ client
.then(() => client.end())
```

### `client.query(config: QueryConfig, callback: (err?: Error, result?: Result) => void) => void`

### `client.query(config: QueryConfig) => Promise<Result>`

You can pass an object to `client.query` with the signature of:

```ts
type QueryConfig {
// the raw query text
text: string;

// an array of query parameters
values?: Array<any>;

// name of the query - used for prepared statements
name?: string;

// by default rows come out as a key/value pair for each row
// pass the string 'array' here to receive rows as an array of values
rowMode?: string;

// custom type parsers just for this query result
types?: Types;
}
client.query(config: QueryConfig) => Promise<Result>
```

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

## client.end

### client.end(cb?: (err?: Error) => void) => void

Disconnects the client from the PostgreSQL server.

```js
Expand All @@ -259,8 +244,6 @@ client.end((err) => {
})
```

### `client.end() => Promise<void>`

Calling end without a callback yields a promise:

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

## events

### client.on('error', (err: Error) => void) => void
### error

```ts
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:

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

### client.on('end') => void
### end

```ts
client.on('end') => void
```

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

### client.on('notification', (notification: Notification) => void) => void
### notification

Used for `listen/notify` events:

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

### client.on('notice', (notice: Error) => void) => void
### notice

```ts
client.on('notice', (notice: Error) => void) => void
```

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

Expand Down
8 changes: 6 additions & 2 deletions docs/pages/apis/pool.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,9 @@ const pool = new Pool({

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.

`pool.query() => Promise<pg.Result>`
```ts
pool.query(text: string, values?: any[]) => Promise<pg.Result>
```

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

Callbacks are also supported:

`pool.query(callback: (err?: Error, result: pg.Result)) => void`
```ts
pool.query(text: string, values?: any[], callback?: (err?: Error, result: pg.Result)) => void
```

```js
const { Pool } = require('pg')
Expand Down
15 changes: 1 addition & 14 deletions docs/pages/index.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,7 @@ $ npm install pg

## Supporters

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:

<div className="sponsors">
<div className="sponsor">
<a href="https://www.crate.io">
<img src="crate-io.png" style={{ height: 80 }} alt="crate.io" />
</a>
</div>
<div className="sponsor">
<a href="https://www.eaze.com/">
<img src="eaze.png" style={{ height: 80 }} alt="eaze.com" />
</a>
</div>
</div>
node-postgres continued development and support is made possible by the many [supporters](https://github.com/brianc/node-postgres/blob/master/SPONSORS.md).

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!

Expand Down