Your SlideShare is downloading. ×
PgREST @ debminiconf Taiwan 2013
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

PgREST @ debminiconf Taiwan 2013

19,828

Published on

Published in: Technology
0 Comments
3 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
19,828
On Slideshare
0
From Embeds
0
Number of Embeds
12
Actions
Shares
0
Downloads
19
Comments
0
Likes
3
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. PgRest Enable REST in PostgreSQL @hychen Debian Mini Conf Taiwan 2013, 泰雅巴萊部落村
  • 2. 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.
  • 3. Web …. IOS PHP Android Ruby On Rails NoSql RMDB Web Python + Django Frontend Backend DB
  • 4. SPA - Single Page Application Credit: http://blog.nodejitsu.com/
  • 5. RESTful Backend. IOS Android PHP NoSql REST Ruby On Rails RMDB Web Python + Django Frontend Restful Backend DB
  • 6. Cut Middleware with RESTful NoSQL MongoLab (rest mongodb) IOS Android REST FireBase Web Frontend Restful DB
  • 7. 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.
  • 8. Why not have RESTful RDBMS???
  • 9. Supports JSON DataType …..
  • 10. JSON
  • 11. In Database...
  • 12. JavaScript As SQL Procedure
  • 13. plv8++
  • 14. JS Function in DB -------------"Taiwan" (1 row)
  • 15. + plv8x++ @clkao++
  • 16. JavaScript Has bad^H^H^H good parts... and Tax.
  • 17. LiveScript++ ● Avoid JavaScript bad parts ● Improved Readability ● Less Typing ● Support Backcall ● Object Oriented Syntax Sugar ● Thinking in a Functional Programming manner
  • 18. Plv8x: operators SELECT entry |> 'this.location' FROM debminiconf; – 'taiwan'
  • 19. Plv8x: operators SELECT entry ~> '@location' FROM debminiconf; – 'taiwan'
  • 20. Plv8x: operators SELECT entry '@locatoin' <~ FROM debminiconf; – 'taiwan'
  • 21. Plv8x: operators SELECT ~> 'new Date'; [ { '?column?': '"20131109T07:23:59.772Z"' } ]
  • 22. 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
  • 23. Plv8x: modules $ npm i -g qs $ plv8x -i qs # same as: plv8x -i qs:/path/to/qs/package.json $ psql debname # parse a query string test=# select ~>'require("qs").parse("foo=bar&baz=1")' as qs; qs ------------------------{"foo":"bar","baz":"1"}
  • 24. 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"}
  • 25. Summary ● V8: JavaScript engine V8 ● PLV8: Stored procedure in JavaScript PLV8 ● Plv8x: Package manager for PLV8 Plv8x – – ● Turn NPM module into SQL functions JSON expressions with ~> and <~ Code reuse for browser + server + database
  • 26. OK! We are good to talk PgREST now.
  • 27. 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
  • 28. Start pgrest.. $ npm i -g pgrst $ pgrest –mydb dbname info: Available collections: debminiconf info: Serving `tcp://postgres@localhost/mydb` on http://127.0.0.1:3000/collections
  • 29. Reading $ 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"}]}}]}
  • 30. Writing $ curl -H 'Content-Type: application/json' -X POST -d @data.json http://localhost:3000/collections/debminiconf {"event": { "location": "bali", "year":"2012", "people":[] }
  • 31. HTTP Request in REST... GET => get a entry/entries POST => add a entry/entries PUT => update a entry DELETE => delete a entry
  • 32. Maps HTTP Requests in DB... GET => SELECT POST => INSERT PUT => UPDATE DELETE => DELETE
  • 33. Access Control?
  • 34. Use Schema Public / Private
  • 35. 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
  • 36. Custom Request Handler • Define a view as an endpoint • • Use rule system to redefine Create/Update/Read/Delete
  • 37. Exmaple of The rule CREATE OR REPLACE RULE tags_add AS ON INSERT TO kuansim.tags DO INSTEAD WITH auth as (select pgrest_getauth() as auth_id) INSERT INTO public.tags (name, author_id) VALUES(NEW.name, (SELECT auth_id FROM auth));
  • 38. Cutting out the middleware ● Serve JSON API from SQL ● Share models & validatoin code ● Put Bussiness Logic into DB ● Perfect fit for Medium Data
  • 39. Come to join us. ● Website: http://postgre.st/ ● VCS: https://github.com/clkao/pgrest ● IRC: freenode.net #pgrest
  • 40. 謝謝聆聽 Thanks !!
  • 41. Reference ● PgREST – Node.js in Database, Audrey Tang, osdc.tw. ● Single Page Apps with Node.js., Paolo Fragomeni ● ● plv8 - V8 Engine Javascript Procedural Language addon for PostgreSQL Plv8x - Helpers for managing plv8 javascript modules

×