bslocombe:pg

v1.0.8Published 6 months ago

benslocombe:pg

Reactive PostgreSQL for Meteor, Credits to original package by numtel, and inspired by vlasky mysql package, updated to get newer PG libraries, and refactored to not depend on other NPM package for simpler maintenance.

Provides Meteor an integrated live select class, bringing reactive SELECT statement result sets from PostgreSQL >= 9.3 using pg notify.

If you do not have PostgreSQL server already installed, you may use Meteor PG Server (only tested on osx) to bundle the PostgreSQL server directly to your Meteor application.

Server Implements

This package provides the LivePg class (moved into this package lib)

LivePg.prototype.select()

In this Meteor package, the SelectHandle object returned by the select() method is modified to act as a cursor that can be published.

1var liveDb = new LivePg(CONNECTION_STRING, CHANNEL);
2
3Meteor.publish('allPlayers', function(){
4  return liveDb.select('SELECT * FROM players ORDER BY score DESC');
5});

Client/Server Implements

Simply call Meteor.subscribe() as you would normally on the client.

Notes:

Closing connections between hot code-pushes

With Meteor's hot code-push feature, new triggers and functions on database server are created with each restart. In order to remove old items, a handler to your application process's SIGTERM signal event must be added that calls the cleanup() method on each LivePg instance in your application. Also, a handler for SIGINT can be used to close connections on exit.

On the server-side of your application, add event handlers like this:

1
2var liveDb = new LivePg(CONNECTION_STRING, CHANNEL);
3
4var closeAndExit = function() {
5  // Call process.exit() as callback
6  liveDb.cleanup(process.exit);
7};
8
9// Close connections on hot code push
10process.on('SIGTERM', closeAndExit);
11// Close connections on exit (ctrl + c)
12process.on('SIGINT', closeAndExit);

Tests / Benchmarks

** tests are broken at this time ** The test suite does not require a separate Postgres server installation as it uses Meteor PG Server (only tested on osx) to run the tests.

The database connection settings must be configured in test/settings/local.json.

The database specified should be an empty database with no tables because the tests will create and delete tables as needed.

# Install Meteor
$ curl -L https://install.meteor.com/ | /bin/sh

# Clone Repository
$ git clone https://github.com/numtel/meteor-pg.git
$ cd meteor-pg

# Optionally, configure port and data dir in test/settings/test.pg.json.
# If changing port, keep port value updated in test/index.es6 as well.

# Test database will be created in dbtest directory.

# Run test server
$ meteor test-packages ./

Example Usage Publication

1var liveDb = new LivePg(Meteor.settings.private.postgres, 'test');
2
3// uses the name of the publication as the name of the collection by default.
4// option to override so you can have more than one publication for the same "collection" 
5// this is the publication context.. 
6
7// this._collection_name = "entities"
8
9Meteor.publish('test', function(){
10  this._collection_name = "entities" // override the default collection name to merge results into the entities collection
11  let res = liveDb.select(
12    `SELECT * from entity`,
13    [], //query parameters if required
14    LivePgKeySelector.Columns(['id']), //how to index the _id column for the result set
15    [{table:'entity'}] //tables to create trigger functions in pg
16  );
17  return res;
18})
19
20var closeAndExit = function() {
21  // Call process.exit() as callback
22  liveDb.cleanup(process.exit);
23};
24
25// Close connections on hot code push
26process.on('SIGTERM', closeAndExit);
27// Close connections on exit (ctrl + c)
28process.on('SIGINT', closeAndExit);

License

MIT