PgREST @ debminiconf Taiwan 2013

41
PgRest Enable REST in PostgreSQL @hychen Debian Mini Conf Taiwan 2013, 泰雅巴萊部落村

Transcript of PgREST @ debminiconf Taiwan 2013

Page 1: PgREST @ debminiconf Taiwan 2013

PgRest

Enable REST in PostgreSQL

@hychen

Debian Mini Conf Taiwan 2013, 泰雅巴萊部落村

Page 2: PgREST @ debminiconf Taiwan 2013

Who am I..

● 10+ years of experiences in participating with open source communities and projects.

● Python/LiveScript● Back to Web development

recently ...I was a PHP web programmer in 2009.

Page 3: PgREST @ debminiconf Taiwan 2013

Web ….

IOS

Android

Web

NoSql

Ruby On Rails

Backend

PHP

Python + DjangoRMDB

DB Frontend

Page 4: PgREST @ debminiconf Taiwan 2013

SPA - Single Page Application

Credit: http://blog.nodejitsu.com/

Page 5: PgREST @ debminiconf Taiwan 2013

RESTful Backend.

IOS

Android

Web

NoSql

Ruby On Rails

Restful Backend

PHP

Python + Django

RMDB

DB Frontend

REST

Page 6: PgREST @ debminiconf Taiwan 2013

Cut Middleware with RESTful NoSQL

IOS

Android

Web

Restful DB Frontend

MongoLab(rest mongodb)

FireBase

REST

Page 7: PgREST @ debminiconf Taiwan 2013

Summary

● All webs should be intractable.– Allow 3rd to read/wite the data by API.

● More and more features can be implemented in frontend.

● Database As service.● JavaScript + HTML everywhere.

Page 8: PgREST @ debminiconf Taiwan 2013

Why not have

RESTful RDBMS???

Page 9: PgREST @ debminiconf Taiwan 2013

Supports JSON DataType …..

Page 10: PgREST @ debminiconf Taiwan 2013

JSON

Page 11: PgREST @ debminiconf Taiwan 2013

In Database...

Page 12: PgREST @ debminiconf Taiwan 2013

JavaScript As SQL Procedure

Page 13: PgREST @ debminiconf Taiwan 2013

plv8++

Page 14: PgREST @ debminiconf Taiwan 2013

-------------- "Taiwan"(1 row)

JS Function in DB

Page 15: PgREST @ debminiconf Taiwan 2013

plv8x++

+

@clkao++

Page 16: PgREST @ debminiconf Taiwan 2013

JavaScript

Has bad^H^H^H good parts...

and Tax.

Page 17: PgREST @ debminiconf Taiwan 2013

LiveScript++

● Avoid JavaScript bad parts● Improved Readability● Less Typing● Support Backcall● Object Oriented Syntax Sugar● Thinking in a Functional Programming

manner

Page 18: PgREST @ debminiconf Taiwan 2013

Plv8x: operators

SELECT entry |> 'this.location' FROM debminiconf;– 'taiwan'

Page 19: PgREST @ debminiconf Taiwan 2013

Plv8x: operators

SELECT entry ~> '@location' FROM debminiconf;– 'taiwan'

Page 20: PgREST @ debminiconf Taiwan 2013

Plv8x: operators

SELECT entry '@locatoin' <~ FROM debminiconf;– 'taiwan'

Page 21: PgREST @ debminiconf Taiwan 2013

Plv8x: operators

SELECT ~> 'new Date';

[ { '?column?': '"2013-11-09T07:23:59.772Z"' } ]

Page 22: PgREST @ debminiconf Taiwan 2013

Plv8x: command line $ npm i -g plv8x;

$ export PLV8XCONN=dbname;

$ plv8x -r script.ls # js works too.

$ plv8x -E 'plv8.execute(“SELECT entry FROM debminiconf”)'.0.entry.location

Page 23: PgREST @ debminiconf Taiwan 2013

Plv8x: modules$ npm i -g qs

$ plv8x -i qs # same as: plv8x -i qs:/path/to/qs/package.json

$ psql debname

# parse a query stringtest=# select ~>'require("qs").parse("foo=bar&baz=1")' as qs; qs------------------------- {"foo":"bar","baz":"1"}

Page 24: PgREST @ debminiconf Taiwan 2013

Plv8x: functions$ plv8x -f 'plv8x.json parse_qs(text)=qs:parse'ok plv8x.json parse_qs(text)

# Now parse_qs is a postgresql function:test=# select parse_qs('foo=bar&baz=1') as qs; qs------------------------- {"foo":"bar","baz":"1"}

Page 25: PgREST @ debminiconf Taiwan 2013

Summary

● V8V8: JavaScript engine● PLV8PLV8: Stored procedure in JavaScript● Plv8xPlv8x: Package manager for PLV8

– Turn NPMNPM module into SQLSQL functions– JSON expressions with ~>~> and <~<~

● Code reuse for browserbrowser + serverserver + databasedatabase

Page 26: PgREST @ debminiconf Taiwan 2013

OK!

We are good to talk PgREST now.

Page 27: PgREST @ debminiconf Taiwan 2013

PgREST is ….

● JSON document store● Running inside PostgreSQL● Working with existing relational data● Capable of loading Node.js modules● Comparable with MongoLab's REST

API● = LiveScript + PLV8 + plv8x

Page 28: PgREST @ debminiconf Taiwan 2013

$ npm i -g pgrst

$ pgrest –mydb dbnameinfo: Available collections:debminiconfinfo: Serving `tcp://postgres@localhost/mydb` on http://127.0.0.1:3000/collections

Start pgrest..

Page 29: PgREST @ debminiconf Taiwan 2013

$ curl localhost:3000/collections/debminiconf{"paging":{"count":1,"l":30,"sk":0},"entries":[{"event":{"location":"Taiwan","year":"2013","people":[{"name":"hychen","country":"Taiwan"},{"name":"jserv","country":"Taiwan"},{"name":"paulliu","country":"Taiwan"}]}}]}

Reading

Page 30: PgREST @ debminiconf Taiwan 2013

$ curl -H 'Content-Type: application/json' \ -X POST -d @data.json \ http://localhost:3000/collections/debminiconf

Writing

{"event":{"location": "bali","year":"2012","people":[]}

Page 31: PgREST @ debminiconf Taiwan 2013

HTTP Request in REST...

GET => get a entry/entriesPOST => add a entry/entriesPUT => update a entryDELETE => delete a entry

Page 32: PgREST @ debminiconf Taiwan 2013

Maps HTTP Requests in DB...

GET => SELECTPOST => INSERTPUT => UPDATEDELETE => DELETE

Page 33: PgREST @ debminiconf Taiwan 2013

Access Control?

Page 34: PgREST @ debminiconf Taiwan 2013

Use Schema

Public / Private

Page 35: PgREST @ debminiconf Taiwan 2013

DO $$ BEGIN IF NOT EXISTS( SELECT schema_name FROM information_schema.schemata WHERE schema_name = 'other' ) THEN EXECUTE 'CREATE SCHEMA other'; END IF; END $$;

$ pgrest –db dbname –schema other

Page 36: PgREST @ debminiconf Taiwan 2013

•Define a view as an endpoint•

• Use rule system to redefine Create/Update/Read/Delete

Custom Request Handler

Page 37: PgREST @ debminiconf Taiwan 2013

Exmaple of The ruleCREATE OR REPLACE RULE tags_add AS ON INSERT TO kuansim.tags

DO INSTEADWITH auth as (select pgrest_getauth() as auth_id)

INSERT INTO public.tags (name, author_id) VALUES(NEW.name, (SELECT auth_id FROM auth));

Page 38: PgREST @ debminiconf Taiwan 2013

Cutting out the middleware

● Serve JSON API from SQL● Share models & validatoin code● Put Bussiness Logic into DB● Perfect fit for Medium Data

Page 39: PgREST @ debminiconf Taiwan 2013

Come to join us.

● Website: http://postgre.st/● VCS: https://github.com/clkao/pgrest● IRC: freenode.net #pgrest

Page 40: PgREST @ debminiconf Taiwan 2013

謝謝聆聽

Thanks!!

Page 41: PgREST @ debminiconf Taiwan 2013

Reference● PgREST – Node.js in Database, Audrey Tang, osdc.tw.

● Single Page Apps with Node.js., Paolo Fragomeni

● plv8 - V8 Engine Javascript Procedural Language add-on for PostgreSQL

● Plv8x - Helpers for managing plv8 javascript modules