Created
July 11, 2014 15:29
-
-
Save Chris927/3736b64a2ed1b278ddf9 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var EventsEmitter = require('events').EventEmitter; | |
| pg.connect(..., function(err, client, done) { | |
| var pageSize = 50; | |
| var emitter = new EventsEmitter(); | |
| emitter.on('next', function(offset) { | |
| callHandlersPaginated(offset); | |
| }); | |
| emitter.on('end', function() { | |
| done(); | |
| }); | |
| emitter.emit('next', /* initial offset */ 0); | |
| function callHandlersPaginated(offset) { | |
| var rowsProcessed = 0; | |
| client.query("select payload from events limit " + pageSize + " offset " + offset + " order by id"); | |
| client.on('row', function(row, result) { | |
| // do stuff | |
| rowsProcessed++; | |
| }); | |
| client.on('end', function(result) { | |
| // do stuff | |
| if (rowsProcessed >= pageSize) { | |
| emitter.emit('next', offset + pageSize); | |
| } else { | |
| emitter.emit('end'); | |
| } | |
| }); | |
| } | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment