Skip to content

Commit a275ada

Browse files
committed
Create README.md
1 parent 9af987f commit a275ada

File tree

1 file changed

+92
-0
lines changed

1 file changed

+92
-0
lines changed

README.md

+92
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
node-pg-cursor
2+
==============
3+
4+
Use a PostgreSQL result cursor from node with an easy to use API.
5+
6+
### why?
7+
8+
Sometimes you need to itterate 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. The page size is dynamic and async.
10+
11+
### example
12+
13+
```js
14+
var Cursor = require('pg-cursor')
15+
var pg = require('pg')
16+
17+
pg.connect(function(err, client, done) {
18+
19+
//imagine some_table has 30,000,000 results where prop > 100
20+
//lets create a query cursor to efficiently deal with the huge result set
21+
var cursor = client.query(new Cursor('SELECT * FROM some_table WHERE prop > $1', [100])
22+
23+
//read the first 100 rows from this cursor
24+
cursor.read(100, function(err, rows) {
25+
if(err) {
26+
//cursor error - release the client
27+
//normally you'd do app-specific error handling here
28+
return done(err)
29+
}
30+
31+
//when the cursor is exhausted and all rows have been returned
32+
//all future calls to `cursor#read` will return an empty row array
33+
//so if we received no rows, release the client and be done
34+
if(!rows.length) return done()
35+
36+
//do something with your rows
37+
//when you're ready, read another chunk from
38+
//your result
39+
40+
41+
cursor.read(2000, function(err, rows) {
42+
//I think you get the picture, yeah?
43+
//if you dont...open an issue - I'd love to help you out!
44+
})
45+
})
46+
});
47+
```
48+
49+
### api
50+
51+
#### var Cursor = require('pg-cursor')
52+
53+
#### constructor Cursor(string queryText, array queryParameters)
54+
55+
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)
56+
57+
#### cursor#read(int rowCount, function callback(Error err, Array rows)
58+
59+
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.
60+
61+
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.
62+
63+
### install
64+
65+
```sh
66+
$ npm install pg-cursor
67+
```
68+
___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.
69+
70+
### license
71+
72+
The MIT License (MIT)
73+
74+
Copyright (c) 2013 Brian M. Carlson
75+
76+
Permission is hereby granted, free of charge, to any person obtaining a copy
77+
of this software and associated documentation files (the "Software"), to deal
78+
in the Software without restriction, including without limitation the rights
79+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
80+
copies of the Software, and to permit persons to whom the Software is
81+
furnished to do so, subject to the following conditions:
82+
83+
The above copyright notice and this permission notice shall be included in
84+
all copies or substantial portions of the Software.
85+
86+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
87+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
88+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
89+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
90+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
91+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
92+
THE SOFTWARE.

0 commit comments

Comments
 (0)