Skip to content

Commit 23948e7

Browse files
author
Brian Carlson
committed
Update more import statements
1 parent 3086b66 commit 23948e7

File tree

7 files changed

+52
-20
lines changed

7 files changed

+52
-20
lines changed

docs/pages/apis/cursor.mdx

+3-5
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,9 @@ const values = [10]
2828

2929
const cursor = client.query(new Cursor(text, values))
3030

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

3836
```ts

docs/pages/features/_meta.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,6 @@
66
"types": "Data Types",
77
"ssl": "SSL",
88
"native": "Native",
9-
"esm": "ESM"
9+
"esm": "ESM",
10+
"callbacks": "Callbacks"
1011
}

docs/pages/features/callbacks.mdx

+39
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/ssl.mdx

+1-2
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

+1-2
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()

docs/pages/guides/async-express.md

+1-2
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,7 @@ That's the same structure I used in the [project structure](/guides/project-stru
2222
My `db/index.js` file usually starts out like this:
2323

2424
```js
25-
import pg from 'pg'
26-
const { Pool } = pg
25+
import { Pool } from 'pg'
2726

2827
const pool = new Pool()
2928

docs/pages/guides/project-structure.md

+5-8
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,12 @@ The location doesn't really matter - I've found it usually ends up being somewha
2727
Typically I'll start out my `db/index.js` file like so:
2828

2929
```js
30-
import pg from 'pg'
31-
const { Pool } = pg
30+
import { Pool } from 'pg'
3231

3332
const pool = new Pool()
3433

35-
export const query = (text, params, callback) => {
36-
return pool.query(text, params, callback)
34+
export const query = (text, params) => {
35+
return pool.query(text, params)
3736
}
3837
```
3938

@@ -55,8 +54,7 @@ app.get('/:id', async (req, res, next) => {
5554
Imagine we have lots of routes scattered throughout many files under our `routes/` directory. We now want to go back and log every single query that's executed, how long it took, and the number of rows it returned. If we had required node-postgres directly in every route file we'd have to go edit every single route - that would take forever & be really error prone! But thankfully we put our data access into `db/index.js`. Let's go add some logging:
5655

5756
```js
58-
import pg from 'pg'
59-
const { Pool } = pg
57+
import { Pool } from 'pg'
6058

6159
const pool = new Pool()
6260

@@ -76,8 +74,7 @@ _note: I didn't log the query parameters. Depending on your application you migh
7674
Now what if we need to check out a client from the pool to run several queries in a row in a transaction? We can add another method to our `db/index.js` file when we need to do this:
7775

7876
```js
79-
import pg from 'pg'
80-
const { Pool } = pg
77+
import { Pool } from 'pg'
8178

8279
const pool = new Pool()
8380

0 commit comments

Comments
 (0)