Koa.js middleware REST API for PostgreSQL with automatic API endpoints matching your tables or views.
// Instantiate your own koa.js app
var app = require('koa')();
// use your own logger
var logger = app.logger = require('bunyan').createLogger({
name: config.get('logger.name'),
streams: [{stream: process.stdout, level: 'info'}]
});
// inject it in the nuodata middleware, along with other options
var nuodata = require('nuodata-db-api')(logger, {
// exclude some verbs if not allowed (e.g. use ['GET', 'POST'] if users
// are not allowed to delete or update records at all via the API, you can
// still control authorization under PostgreSQL even if the HTTP verb is allowed though)
methods: ['GET', 'POST', 'PATCH', 'DELETE']
});
var router = require('koa-router')();
router.get('/v1', nuodata);
app
.use(router.routes())
.use(router.allowedMethods());
app.listen(3000);
GET /data/users?limit=2&name=like::J*
[{"name": "John", "age": 22}, {"name": "Jessie", "age": 30}]
GET /data/users/count/name?name=like::J*
{"count":"2"}
PATCH /data/users?name=eq::Jessie
Content-Type application/json
{
"age": 23
}
[{"name": "John", "age": 22}, {"name": "Jessie", "age": 23}]
POST /data/users
Content-Type application/json
{
"name": "Joe",
"age": 21
}
[{"name": "Joe", "age": 21}]
GET /data/users?name=eq::John
[{"name": "John", "age": 22}]
Check the full documentation at http://docs.nuodata.io