All Projects → Richie765 → meteor-pg

Richie765 / meteor-pg

Licence: other
Use PostgreSQL reactively in Meteor.

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to meteor-pg

Simpleddp
An easy to use DDP client library
Stars: ✭ 130 (+441.67%)
Mutual labels:  reactive, meteor
meteor-two-factor
🔐 Two factor authentication package for accounts-password
Stars: ✭ 80 (+233.33%)
Mutual labels:  meteor
ency
Enhanced concurrency primitives for Javascript.
Stars: ✭ 32 (+33.33%)
Mutual labels:  reactive
unity3d-ddp-client
Lightweight DDP client for Unity3D
Stars: ✭ 18 (-25%)
Mutual labels:  meteor
Client-Storage
🗄 Bulletproof persistent Client storage, works with disabled Cookies and/or localStorage
Stars: ✭ 15 (-37.5%)
Mutual labels:  meteor
lila-ws
Lichess' websocket server
Stars: ✭ 99 (+312.5%)
Mutual labels:  reactive
redrock
Typesafe, reactive redux
Stars: ✭ 14 (-41.67%)
Mutual labels:  reactive
meteor-boilerplate
A Meteor ready-to-use app for Blaze + FlowRouter, with a bunch a handy functions, objects, packages, etc
Stars: ✭ 14 (-41.67%)
Mutual labels:  meteor
bassdrum
reactive, type safe components with preact and rxjs.
Stars: ✭ 44 (+83.33%)
Mutual labels:  reactive
awesome-vulcan
🗒 A list of resources to learn awesome VulcanJS 🖖
Stars: ✭ 15 (-37.5%)
Mutual labels:  meteor
servable
"simple" observable implementation based off RxJS & kefir Docs
Stars: ✭ 14 (-41.67%)
Mutual labels:  reactive
meteorpp
Meteor DDP & Minimongo implementation in C++.
Stars: ✭ 22 (-8.33%)
Mutual labels:  meteor
Meteor-logger-file
🔖 Meteor Logging: Store application log messages into file (FS)
Stars: ✭ 24 (+0%)
Mutual labels:  meteor
netifi-quickstart-java
Project to assist you in getting started using Netifi.
Stars: ✭ 23 (-4.17%)
Mutual labels:  reactive
ReactiveAPI
Write clean, concise and declarative network code relying on URLSession, with the power of RxSwift. Inspired by Retrofit.
Stars: ✭ 79 (+229.17%)
Mutual labels:  reactive
spring-boost
Introduce Clojure and live-coding power to your Spring Boot application!
Stars: ✭ 23 (-4.17%)
Mutual labels:  reactive
realar
5 kB Advanced state manager for React
Stars: ✭ 41 (+70.83%)
Mutual labels:  reactive
sample-spring-reactive
sample reactive microservices using spring 5, spring boot, spring webflux and project reactor
Stars: ✭ 26 (+8.33%)
Mutual labels:  reactive
nau-jukebox
Nâu Jukebox - share the song you love with the team, one person as host will play the song to listen together
Stars: ✭ 28 (+16.67%)
Mutual labels:  meteor
synthesis
🔥 Synthesis is Meteor + Polymer
Stars: ✭ 28 (+16.67%)
Mutual labels:  meteor

meteor-pg

This package allows you to use PostgreSQL reactively with Meteor as seamlessly as possible.

It provides a method to publish PostgreSQL queries on the server. The query result will be available reactively on the client in a Minimongo collection. The collection can be used in the usual way.

Data modifications (UPDATE, INSERT) can only be made from the server side. There are methods available that you can call from your server-side methods.

It has been used in a small scale production environment quite successfully.

For a full working example take a look at: https://github.com/Richie765/meteor-pg-leaderboard

Requires PostgresSQL version 9.3 or above.

Installation

meteor npm install meteor-pg --save

Configuration

There are two environment variables used to configure your database connection.

export PG_URL=postgres://user:password@host:port/db
export PG_CHANNEL=your_channel
meteor run

The channel is used for LISTEN/NOTIFY on the PostgreSQL database and cannot be used by more than one application on the same database.

Initialization

On the server side, import the package early on to establish the database connection. Your /server/main.js file would be a good place to do this.

import 'meteor-pg';

Usage - publication

Only use this for read-only SELECT queries that you want to be reactive. For non-reactive (SELECT) queries you can use a method.

Within a publish function on the server:

return mpg.select(collection, query, params, triggers);
Parameter Description
collection The name of the Minimongo collection where the results will be stored on the client-side.
query SELECT query to run and observe. May contain placeholders following pg-promise. Each row must contain a unique _id field as described below.
params The parameters to the query, following pg-promise. Single values will be $1. Array elements will be $1..$n. Object properties will be $*property* where ** is one of (), [], {} or //. See pg-promise for details.
triggers function(change). The trigger function, see below.

Unique _id field

Your query must always return an _id field which uniquely identifies each row returned. This is needed so that the changed rows can be identified. For simple queries, this could just be an alias to the PK.

For multi-table queries, this could be a combination of different PK's, eg:

SELECT CONCAT(userid, '-', taskid) AS _id, * FROM user, task;

This does not mean you have to include the PK's of all the tables involved. You just need to uniquely identify each row returned.

triggers function

This function will be called whenever there is a change to one of the underlying tables of the query. You should determine if this change requires a rerun of the query. If so, you should return true.

One parameter is passed, change. It contains the following fields:

Field Description
table String, name of the table that changed. This will always be in lowercase.
insert For INSERT, true
delete For DELETE, true
update For UPDATE, an object that contains the old and new values of each changed column. If a column score changed from 10 to 20, change.update.score.from would be 10 and change.update.score.to would be 20.
row The row values, for UPDATE, the NEW row values
old For UPDATE, the OLD row values

ES6 syntax makes it easy to write your trigger function, e.g.:

function trigger({ table, row }) {
  if(table === 'user' && row.name === 'name') return true;
  if(table === 'task' && row.status === 'completed') return true;
}

Example - publication

// Server side

import mpg from 'meteor-pg';

Meteor.publish('allPlayers', function() {
  let sql = `
    SELECT id AS _id, *
    FROM players
    ORDER BY score DESC
  `;

  function triggers() {
    // This function is rather important.
    // For now, just trigger any change
    return true;
  }

  return mpg.select('players', sql, undefined, triggers);
});

// Client side

Players = new Mongo.Collection('players');

Template.leaderboard.onCreated(function () {
  this.subscribe('allPlayers');
});

Template.leaderboard.helpers({
  players: function () {
    // Still need to sort client-side since record order is not preserved
    return Players.find({}, { sort: { score: -1, name: 1 } });
  },
});

Usage - methods

The mpg object provides the following methods that you can call within your Meteor methods to execute INSERT and UPDATE (and non-reactive SELECT) statements: query, none, one, many, oneOrNone, manyOrNone, any, result, stream, func, proc, map, each, task, tx.

These methods take the same parameters as the methods of pg-promise. The difference is that these are called within a fiber using Promise.await, so they wait for the statement to be executed. You can use the return value directly, it is not a 'promise'.

Example

// Server side

import mpg from 'meteor-pg';

Meteor.methods({
  'incScore': function(id, amount){
    let sql = `
      UPDATE players
      SET score = score + $[amount]
      WHERE id = $[id]
    `;

    mpg.none(sql, { id, amount });
  }
});

// Client side

Meteor.methods({
  // Optional stub for latency compensation, see note below

  'incScore': function(id, amount){
    Players.update(id, { $inc: { score: amount } });    
  }
});

Template.leaderboard.events({
  'click .inc': function () {
    Meteor.call('incScore', Session.get("selectedPlayer"), 5);
  }
});

Advanced usage

For running complex queries, involving multiple JOIN's and/or additional processing in JavaScript, you can use mpg.table_observer and mpg.query_observer directly. For the documentation see https://github.com/richie765/pg-table-observer and https://github.com/richie765/pg-query-observer.

If you need direct access to the pg-promise object or database handle, they are available as mpg.pgp and mpg.db.

To do, known issues

  • There is some flicker when using latancy compensation (client-side methods).
  • MongoDB is still required for Accounts (pull requests welcome)
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].