Skip to content

Commit c0f5518

Browse files
authored
Update README.md
Add link to updated documentation
1 parent 796d141 commit c0f5518

File tree

1 file changed

+2
-68
lines changed

1 file changed

+2
-68
lines changed

README.md

Lines changed: 2 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -3,81 +3,15 @@ node-pg-cursor
33

44
Use a PostgreSQL result cursor from node with an easy to use API.
55

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(new Cursor('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-
return done(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) return done()
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.
49-
})
50-
})
51-
});
52-
```
53-
54-
### api
55-
56-
#### var Cursor = require('pg-cursor')
57-
58-
#### constructor Cursor(string queryText, array queryParameters)
59-
60-
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-
746
### install
757

768
```sh
779
$ npm install pg-cursor
7810
```
7911
___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.
8012

13+
### :star: [Documentation](https://node-postgres.com/api/cursor) :star:
14+
8115
### license
8216

8317
The MIT License (MIT)

0 commit comments

Comments
 (0)