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: README.md
+2-68Lines changed: 2 additions & 68 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -3,81 +3,15 @@ node-pg-cursor
3
3
4
4
Use a PostgreSQL result cursor from node with an easy to use API.
5
5
6
-
### why?
7
-
8
-
Sometimes you need to iterate through a table in chunks. It's extremely inefficient to use hand-crafted `LIMIT` and `OFFSET` queries to do this.
9
-
PostgreSQL provides built-in functionality to fetch a "cursor" to your results and page through the cursor efficiently fetching chunks of the results with full MVCC compliance.
10
-
11
-
This actually ends up pairing very nicely with node's _asyncness_ and handling a lot of data. PostgreSQL is rad.
12
-
13
-
### example
14
-
15
-
```js
16
-
var Cursor =require('pg-cursor')
17
-
var pg =require('pg')
18
-
19
-
pg.connect(function(err, client, done) {
20
-
21
-
//imagine some_table has 30,000,000 results where prop > 100
22
-
//lets create a query cursor to efficiently deal with the huge result set
23
-
var cursor =client.query(newCursor('SELECT * FROM some_table WHERE prop > $1', [100]))
24
-
25
-
//read the first 100 rows from this cursor
26
-
cursor.read(100, function(err, rows) {
27
-
if(err) {
28
-
//cursor error - release the client
29
-
//normally you'd do app-specific error handling here
30
-
returndone(err)
31
-
}
32
-
33
-
//when the cursor is exhausted and all rows have been returned
34
-
//all future calls to `cursor#read` will return an empty row array
35
-
//so if we received no rows, release the client and be done
36
-
if(!rows.length) returndone()
37
-
38
-
//do something with your rows
39
-
//when you're ready, read another chunk from
40
-
//your result
41
-
42
-
43
-
cursor.read(2000, function(err, rows) {
44
-
//I think you get the picture, yeah?
45
-
//if you dont...open an issue - I'd love to help you out!
46
-
47
-
//Also - you probably want to use some sort of async or promise library to deal with paging
48
-
//through your cursor results. node-pg-cursor makes no asumptions for you on that front.
Creates an instance of a query cursor. Pass this instance to node-postgres [`client#query`](https://github.com/brianc/node-postgres/wiki/Client#wiki-method-query-parameterized)
61
-
62
-
#### cursor#read(int rowCount, function callback(Error err, Array rows, Result result)
63
-
64
-
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.
65
-
66
-
If the cursor has read to the end of the result sets all subsequent calls to `cursor#read` will return a 0 length array of rows. I'm open to other ways to signal the end of a cursor, but this has worked out well for me so far.
67
-
68
-
`result` is a special [https://github.com/brianc/node-postgres/wiki/Query#result-object](Result) object that can be used to accumulate rows.
69
-
70
-
#### cursor#close(function callback(Error err))
71
-
72
-
Closes the backend portal before itterating through the entire result set. Useful when you want to 'abort' out of a read early but continue to use the same client for other queries after the cursor is finished.
73
-
74
6
### install
75
7
76
8
```sh
77
9
$ npm install pg-cursor
78
10
```
79
11
___note___: this depends on _either_`npm install pg` or `npm install pg.js`, but you __must__ be using the pure JavaScript client. This will __not work__ with the native bindings.
0 commit comments