Skip to content

Commit 0c1629b

Browse files
briancBrian Carlson
andauthored
Update docs - add ESM info
* Update docs - start * Add logo & discord * Start updating docs for esm style imports * Update docs with logo & info on pooling * Update more import statements --------- Co-authored-by: Brian Carlson <[email protected]>
1 parent 56e2862 commit 0c1629b

File tree

15 files changed

+194
-67
lines changed

15 files changed

+194
-67
lines changed

docs/README.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# node-postgres docs website
2+
3+
This is the documentation for node-postgres which is currently hosted at [https://node-postgres.com](https://node-postgres.com).
4+
5+
## Development
6+
7+
To run the documentation locally, you need to have [Node.js](https://nodejs.org) installed. Then, you can clone the repository and install the dependencies:
8+
9+
```bash
10+
cd docs
11+
yarn
12+
```
13+
14+
Once you've installed the deps, you can run the development server:
15+
16+
```bash
17+
yarn dev
18+
```
19+
20+
This will start a local server at [http://localhost:3000](http://localhost:3000) where you can view the documentation and see your changes.

docs/components/logo.tsx

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import React from 'react'
2+
3+
type Props = {
4+
src: string
5+
alt?: string
6+
}
7+
8+
export function Logo(props: Props) {
9+
const alt = props.alt || 'Logo'
10+
return <img src={props.src} alt={alt} width={100} height={100} style={{ width: 400, height: 'auto' }} />
11+
}

docs/pages/apis/client.mdx

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,7 @@ type Config = {
3333
example to create a client with specific connection information:
3434
3535
```js
36-
import pg from 'pg'
37-
const { Client } = pg
36+
import { Client } from 'pg'
3837

3938
const client = new Client({
4039
user: 'database-user',
@@ -48,8 +47,7 @@ const client = new Client({
4847
## client.connect
4948

5049
```js
51-
import pg from 'pg'
52-
const { Client } = pg
50+
import { Client } from 'pg'
5351
const client = new Client()
5452

5553
await client.connect()
@@ -91,8 +89,7 @@ client.query(text: string, values?: any[]) => Promise<Result>
9189
**Plain text query**
9290

9391
```js
94-
import pg from 'pg'
95-
const { Client } = pg
92+
import { Client } from 'pg'
9693
const client = new Client()
9794

9895
await client.connect()
@@ -106,8 +103,7 @@ await client.end()
106103
**Parameterized query**
107104

108105
```js
109-
import pg from 'pg'
110-
const { Client } = pg
106+
import { Client } from 'pg'
111107
const client = new Client()
112108

113109
await client.connect()
@@ -145,8 +141,7 @@ await client.end()
145141
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.
146142

147143
```js
148-
import pg from 'pg'
149-
const { Query } = pg
144+
import { Query } from 'pg'
150145
const query = new Query('select $1::text as name', ['brianc'])
151146

152147
const result = client.query(query)

docs/pages/apis/cursor.mdx

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,7 @@ $ npm install pg pg-cursor
1818
Instantiates a new Cursor. A cursor is an instance of `Submittable` and should be passed directly to the `client.query` method.
1919

2020
```js
21-
import pg from 'pg'
22-
const { Pool } = pg
21+
import { Pool } from 'pg'
2322
import Cursor from 'pg-cursor'
2423

2524
const pool = new Pool()
@@ -29,11 +28,9 @@ const values = [10]
2928

3029
const cursor = client.query(new Cursor(text, values))
3130

32-
cursor.read(100, (err, rows) => {
33-
cursor.close(() => {
34-
client.release()
35-
})
36-
})
31+
const { rows } = await cursor.read(100)
32+
console.log(rows.length) // 100 (unless the table has fewer than 100 rows)
33+
client.release()
3734
```
3835

3936
```ts
@@ -58,8 +55,7 @@ If the cursor has read to the end of the result sets all subsequent calls to cur
5855
Here is an example of reading to the end of a cursor:
5956

6057
```js
61-
import pg from 'pg'
62-
const { Pool } = pg
58+
import { Pool } from 'pg'
6359
import Cursor from 'pg-cursor'
6460

6561
const pool = new Pool()

docs/pages/apis/pool.mdx

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,7 @@ type Config = {
4848
example to create a new pool with configuration:
4949
5050
```js
51-
import pg from 'pg'
52-
const { Pool } = pg
51+
import { Pool } from 'pg'
5352

5453
const pool = new Pool({
5554
host: 'localhost',
@@ -69,8 +68,7 @@ pool.query(text: string, values?: any[]) => Promise<pg.Result>
6968
```
7069

7170
```js
72-
import pg from 'pg'
73-
const { Pool } = pg
71+
import { Pool } from 'pg'
7472

7573
const pool = new Pool()
7674

@@ -102,8 +100,7 @@ Acquires a client from the pool.
102100
- If the pool is 'full' and all clients are currently checked out will wait in a FIFO queue until a client becomes available by it being released back to the pool.
103101

104102
```js
105-
import pg from 'pg'
106-
const { Pool } = pg
103+
import { Pool } from 'pg'
107104

108105
const pool = new Pool()
109106

@@ -121,8 +118,7 @@ Client instances returned from `pool.connect` will have a `release` method which
121118
The `release` method on an acquired client returns it back to the pool. If you pass a truthy value in the `destroy` parameter, instead of releasing the client to the pool, the pool will be instructed to disconnect and destroy this client, leaving a space within itself for a new client.
122119

123120
```js
124-
import pg from 'pg'
125-
const { Pool } = pg
121+
import { Pool } from 'pg'
126122

127123
const pool = new Pool()
128124

@@ -134,8 +130,7 @@ client.release()
134130
```
135131

136132
```js
137-
import pg from 'pg'
138-
const { Pool } = pg
133+
import { Pool } from 'pg'
139134

140135
const pool = new Pool()
141136
assert(pool.totalCount === 0)
@@ -168,8 +163,7 @@ Calling `pool.end` will drain the pool of all active clients, disconnect them, a
168163

169164
```js
170165
// again both promises and callbacks are supported:
171-
import pg from 'pg'
172-
const { Pool } = pg
166+
import { Pool } from 'pg'
173167

174168
const pool = new Pool()
175169

docs/pages/features/_meta.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,7 @@
55
"transactions": "Transactions",
66
"types": "Data Types",
77
"ssl": "SSL",
8-
"native": "Native"
8+
"native": "Native",
9+
"esm": "ESM",
10+
"callbacks": "Callbacks"
911
}

docs/pages/features/callbacks.mdx

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
---
2+
title: Callbacks
3+
---
4+
5+
## Callback Support
6+
7+
`async` / `await` is the preferred way to write async code these days with node, but callbacks are supported in the `pg` module and the `pg-pool` module. To use them, pass a callback function as the last argument to the following methods & it will be called and a promise will not be returned:
8+
9+
10+
```js
11+
const { Pool, Client } = require('pg')
12+
13+
// pool
14+
const pool = new Pool()
15+
// run a query on an available client
16+
pool.query('SELECT NOW()', (err, res) => {
17+
console.log(err, res)
18+
})
19+
20+
// check out a client to do something more complex like a transaction
21+
pool.connect((err, client, release) => {
22+
client.query('SELECT NOW()', (err, res) => {
23+
release()
24+
console.log(err, res)
25+
pool.end()
26+
})
27+
28+
})
29+
30+
// single client
31+
const client = new Client()
32+
client.connect((err) => {
33+
if (err) throw err
34+
client.query('SELECT NOW()', (err, res) => {
35+
console.log(err, res)
36+
client.end()
37+
})
38+
})
39+
```

docs/pages/features/esm.mdx

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
---
2+
title: ESM
3+
---
4+
5+
## ESM Support
6+
7+
As of v8.15.x node-postgres supporters the __ECMAScript Module__ (ESM) format. This means you can use `import` statements instead of `require` or `import pg from 'pg'`.
8+
9+
CommonJS modules are still supported. The ESM format is an opt-in feature and will not affect existing codebases that use CommonJS.
10+
11+
The docs have been changed to show ESM usage, but in a CommonJS context you can still use the same code, you just need to change the import format.
12+
13+
If you're using CommonJS, you can use the following code to import the `pg` module:
14+
15+
```js
16+
const pg = require('pg')
17+
const { Client } = pg
18+
// etc...
19+
```
20+
21+
### ESM Usage
22+
23+
If you're using ESM, you can use the following code to import the `pg` module:
24+
25+
```js
26+
import { Client } from 'pg'
27+
// etc...
28+
```
29+
30+
31+
Previously if you were using ESM you would have to use the following code:
32+
33+
```js
34+
import pg from 'pg'
35+
const { Client } = pg
36+
// etc...
37+
```

docs/pages/features/ssl.mdx

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,7 @@ const config = {
2222
},
2323
}
2424

25-
import pg from 'pg'
26-
const { Client, Pool } = pg
25+
import { Client, Pool } from 'pg'
2726

2827
const client = new Client(config)
2928
await client.connect()

docs/pages/features/transactions.mdx

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,7 @@ To execute a transaction with node-postgres you simply execute `BEGIN / COMMIT /
1616
## Examples
1717

1818
```js
19-
import pg from 'pg'
20-
const { Pool } = pg
19+
import { Pool } from 'pg'
2120
const pool = new Pool()
2221

2322
const client = await pool.connect()

0 commit comments

Comments
 (0)