Code Monkey home page Code Monkey logo

pg-gen's Introduction

pg-gen

Use ES6 Generators to paginate through large Postgres result sets You'll need to use Node 4.2.0 and up

Explaination

pg-gen uses a postgresql cursor for queries so that an entire result set is not returned and so memory is conserved. You utilize a generator to fetch the next n rows until there are no more results.

Why not stream the result set?

Streams are great assuming you are ready to receive said results. Otherwise, you'd have to manually manage flow using pause/resume. And this will still eventually max out the internal buffer. Generators actually pause function execution. There is no buffer, there is only ever the data you request. If streams push data, generators allow you to pull data. I advise you to use whichever is best for solving your particular problem.

Install

npm install pg-gen --save

Usage

  const PgGen   = require( 'pg-gen' );
  const pg      = require( 'pg' );
  const conn    = process.env.DATABASE_URL;
  const client  = new pg.Client( conn );
  
  // assume the necessary pg client connection stuff here ....

  const pgGen = PgGen( { pg: client } );
  const query = `select * from table_with_so_much_data`;
  const gen   = pgGen.atMost( 10 ).lazyQuery( query );

  gen().then( ( result ) => {

    console.log(result); // an array with at most, 10 items

    gen().then( ( result ) => {
      console.log(result); // an array with the NEXT 10 items
    });

    // Do this until result has a length of 0

  });

API

  atMost([numItems=int]) 

Each generator execution will return no more than the specified number of items. It may return less upon reaching the end of the result set. A return of 0 means there is no more data.

  lazyQuery([query=string], [params=object]) 

Tests

You'll need to have a (running) database and a table with data in order to run the tests. Create your test environment like so

$ echo "DATABASE_URL=postgres://user:$ecret@localhost/test_db" >> test/test.env
$ echo "TEST_TABLE=test_table" >> test/test.env

Then

$ npm test

pg-gen's People

Stargazers

 avatar Jeff Galbraith avatar Jeff Mealo avatar Tony Ghita avatar

Watchers

James Cloos avatar Jeff Mealo avatar

pg-gen's Issues

Why do this?

The only place where it makes sense to paginate data is with pg-query-stream.

Paginating query result that's already in memory makes it of little to no value.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.