Skip to content
Sylvain edited this page Jul 11, 2016 · 6 revisions

pg-manati documentation

pg-manati is an advanced access to a PostgreSQL database using REST API. It is based on Node.js.

Introduction

Most development of web application involves a frontend programmer and a backend developer, sometime a Database Administrator. Most backend development often consists of creating simple CRUD access to the database, and for that most programmer will use an ORM. One of the main feature is this abstraction of the SQL language that allows you to manipulate row easily and change database like you change configuration.

However this bring an important problem, you cannot use your database to its fullest because the ORM abstraction is based on a common denominator of all the databases it supports, and you have to learn the ORM documentation. Besides you actually never change database. In fact this should be the last thing you change in your application, as it would require a major migration and code refactoring (even with an ORM!).

Goals

  • Bring PostgreSQL powerfulness directly to the Frontend developer through the REST paradigm.
  • Allow maximum freedom of development and design for the database programmer.

Installation

npm install pg-manati --save

Setup

var manati = require('pg-manati');
var app = manati(
  process.env.DATABASE_URL || 'postgres://user@localhost/database', // your database connection string
  'info' // the minumum log level that will be output
);

You can extend Koa.js with your own generator. To add CORS for instance. See http://koajs.com/ for more info

app.koa.use(require('koa-cors')());

Add plugins, a single

// add authentication routes to the list
app.addPlugin('authentication');

// add authentication middleware for all routes in the data router (route starting with /data)
app.addPlugin('authorization');

// add your own plugin
app.addPlugin(require('manati-plugin'));

Setup manati

if (process.argv[2] === '--setup') {
  app.setup().then(() => {
    console.log('Installation successfull');
    process.exit(0);
  })
}

Initialize manati

app.init();

Start manati

app.start();

Example

Get some users data

GET /data/users?limit=2&name=like::J*
[{"name": "John", "age": 22}, {"name": "Jessie", "age": 30}]

Update some users data

PATCH /data/users?name=eq::Jessie
Content-Type application/json
{
  "age": 23
}
[{"name": "John", "age": 22}, {"name": "Jessie", "age": 23}]

Create some new data

POST /data/users
Content-Type application/json
{
  "name": "Joe",
  "age": 21
}
[{"name": "Joe", "age": 21}]

Delete some users data

DELETE /data/users?name=eq::John
[{"name": "John", "age": 22}]