You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/pages/announcements.mdx
+2-2Lines changed: 2 additions & 2 deletions
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](
79
79
80
80
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.
81
81
82
-
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.
82
+
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.
83
83
84
84
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." ❤️
client.query('SELECT $1::text as name', ['brianc'], (err, res) => {
124
-
if (err) throw err
125
-
console.log(res)
126
-
client.end()
127
-
})
128
-
```
129
87
130
-
### Promise API
88
+
awaitclient.connect()
131
89
132
-
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.
**client.query with a QueryConfig and a callback**
114
+
**client.query with a QueryConfig**
169
115
170
116
If you pass a `name` parameter to the `client.query` method, the client will create a [prepared statement](/features/queries#prepared-statements).
171
117
@@ -177,42 +123,18 @@ const query = {
177
123
rowMode:'array',
178
124
}
179
125
180
-
client.query(query, (err, res) => {
181
-
if (err) {
182
-
console.error(err.stack)
183
-
} else {
184
-
console.log(res.rows) // ['brianc']
185
-
}
186
-
})
187
-
```
188
-
189
-
**client.query with a QueryConfig and a Promise**
126
+
constresult=awaitclient.query(query)
127
+
console.log(result.rows) // ['brianc']
190
128
191
-
```js
192
-
constquery= {
193
-
name:'get-name',
194
-
text:'SELECT $1::text',
195
-
values: ['brianc'],
196
-
rowMode:'array',
197
-
}
198
-
199
-
// promise
200
-
client
201
-
.query(query)
202
-
.then((res) => {
203
-
console.log(res.rows) // ['brianc']
204
-
})
205
-
.catch((e) => {
206
-
console.error(e.stack)
207
-
})
129
+
awaitclient.end()
208
130
```
209
131
210
132
**client.query with a `Submittable`**
211
133
212
134
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.
213
135
214
136
```js
215
-
constQuery=require('pg').Query
137
+
import { Query} from'pg'
216
138
constquery=newQuery('select $1::text as name', ['brianc'])
Disconnects the client from the PostgreSQL server.
238
162
239
163
```js
240
-
client.end((err) => {
241
-
console.log('client has disconnected')
242
-
if (err) {
243
-
console.log('error during disconnection', err.stack)
244
-
}
245
-
})
164
+
awaitclient.end()
165
+
console.log('client has disconnected')
246
166
```
247
167
248
-
Calling end without a callback yields a promise:
249
-
250
-
```js
251
-
client
252
-
.end()
253
-
.then(() =>console.log('client has disconnected'))
254
-
.catch((err) =>console.error('error during disconnection', err.stack))
255
-
```
256
-
257
-
_note: end returning a promise is only available in pg7.0 and above_
258
-
259
168
## events
260
169
261
170
### error
@@ -264,7 +173,7 @@ _note: end returning a promise is only available in pg7.0 and above_
264
173
client.on('error', (err:Error) =>void) =>void
265
174
```
266
175
267
-
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:
176
+
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:
Read `rowCount` rows from the cursor instance. The callback will be called when the rows are available, loaded into memory, parsed, and converted to JavaScript types.
54
54
@@ -57,25 +57,22 @@ If the cursor has read to the end of the result sets all subsequent calls to cur
57
57
Here is an example of reading to the end of a cursor:
58
58
59
59
```js
60
-
const { Pool } =require('pg')
61
-
constCursor=require('pg-cursor')
60
+
import { Pool } from'pg'
61
+
importCursorfrom'pg-cursor'
62
62
63
63
constpool=newPool()
64
64
constclient=awaitpool.connect()
65
65
constcursor=client.query(newCursor('select * from generate_series(0, 5)'))
66
-
cursor.read(100, (err, rows) => {
67
-
if (err) {
68
-
throw err
69
-
}
70
-
assert(rows.length==6)
71
-
cursor.read(100, (err, rows) => {
72
-
assert(rows.length==0)
73
-
})
74
-
})
66
+
67
+
let rows =awaitcursor.read(100)
68
+
assert(rows.length==6)
69
+
70
+
rows =awaitcursor.read(100)
71
+
assert(rows.length==0)
75
72
```
76
73
77
74
## close
78
75
79
-
### `cursor.close(callback: () => void) => void`
76
+
### `cursor.close() => Promise<void>`
80
77
81
78
Used to close the cursor early. If you want to stop reading from the cursor before you get all of the rows returned, call this.
0 commit comments