RESTful Web API and MongoDB go for a pic nic

  • 9,982 views
Uploaded on

Why and how MongoDB is a perfect match for building your next RESTful Web API. Presented at: NoSQL Day 2013 (Udine, Italy), MongoTorino 2013 (Turin, Italy), A Morning With MongoDB (Milan).

Why and how MongoDB is a perfect match for building your next RESTful Web API. Presented at: NoSQL Day 2013 (Udine, Italy), MongoTorino 2013 (Turin, Italy), A Morning With MongoDB (Milan).

More in: Technology , Education
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
No Downloads

Views

Total Views
9,982
On Slideshare
0
From Embeds
0
Number of Embeds
2

Actions

Shares
Downloads
71
Comments
0
Likes
9

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. RESTful Web APIs and MongoDB go for a picnic Nicola Iarocci @nicolaiarocci
  • 2. Nicola Iarocci Eve REST API Framework, Cerberus, Events Co-founder @ gestionaleamica.com, Book Author, MongoDB Master
  • 3. Nicola Iarocci TL;DR Passionate full stack developer
  • 4. Il Piccolo Libro di MongoDB edizione italiana del libro di Karl Seguin disponibile per il download @ nicolaiarocci.com
  • 5. gestionaleamica.com invoicing & accounting
  • 6. gestionaleamica.com your typical old school desktop application
  • 7. gestionaleamica.com now going web and mobile
  • 8. gestionaleamica.com “ we need a remote API to keep everything in sync ” -ha A ent mo m
  • 9. Before Client Database Desktop! Application LAN/SQL
  • 10. Initial draft Clients API Database RESTful ! Web API “Cloud” ? ? iOS Android Website Desktop Client
  • 11. Constraints • minimum viable product first • add features over time • frequent database schema updates • avoid downtime as much as possible
  • 12. So we started exploring new paths
  • 13. MongoDB and REST ! or why we picked MongoDB for our REST API
  • 14. JSON-style data store true selling point for me
  • 15. JSON for transport all client to API communication is going to be JSON
  • 16. JSON & RESTful API GET Client Mongo JSON! JSON! accepted media type (BSON) maybe we can push directly to client?
  • 17. JSON & RESTful API GET Client API JSON! JSON! accepted media type subset of python dict! (kinda) almost. Mongo JSON! (BSON)
  • 18. JSON & RESTful API POST Client JSON! objects API JSON/dict! maps to python dict! (validation layer) Mongo JSON! (BSON) also works when posting (adding) items to the database
  • 19. Similarity with RDBMS made NoSQL easy to grasp (even for a dumbhead like me)
  • 20. Terminology RDBMS Mongo Database Database Table Collection Rows(s) JSON Document Index Index Join Embedding & Linking
  • 21. What about Queries? Queries in MongoDB are represented as JSON-style objects // select * from things where x=3 and y="foo" db.things.find({x: 3, y: "foo”});
  • 22. Filtering and Sorting let’s simply expose MongoDB syntax ?where={x: 3, y: "foo”} Client native! Mongo! query syntax API (very) thin parsing! & validation layer Mongo JSON! (BSON)
  • 23. JSON all along the pipeline mapping to and from the database feels more natural
  • 24. ORM Where we’re going we don’t need ORMs.
  • 25. Schema-less dynamic documents allow for painless, progressive evolution
  • 26. PyMongo MongoDB drivers are beautiful. Really.
  • 27. Also in MongoDB • setup is a breeze • lightweight • fast inserts, updates and queries • excellent documentation • great support by 10gen • great community
  • 28. REST in practice ! with some MongoDB love
  • 29. Collections API entry point + plural nouns api.example.com/contacts collection to a Mongo Maps
  • 30. Document API entry point + plural nouns + ID api.example.com/contacts/4f46445fc88e201858000000 on ObjectID o a collecti Maps t
  • 31. GET Retrieving Resoruce Data
  • 32. Resource GET /contacts?where={“age”: {“$gt”: 20}}&projection={“lastname”: 1} def get_collection(collection):! documents = []! cursor = db(collection).find(where, projection)! for document in cursor:! documents.append(document)! return documents find() accepts a python dict as query expression, and returns a cursor we can iterate
  • 33. Resource GET /contacts?where={“age”: {“$gt”: 20}}&projection={“lastname”: 1} def get_collection(collection):! documents = []! cursor = db(collection).find(where, projection)! for document in cursor:! documents.append(document)! return documents find() accepts a python dict as query expression, and returns a cursor we can iterate
  • 34. Resource GET /contacts?where={“age”: {“$gt”: 20}}&projection={“lastname”: 1} def get_collection(collection):! documents = []! cursor = db(collection).find(where, projection)! for document in cursor:! documents.append(document)! return documents find() accepts a python dict as query expression, and returns a cursor we can iterate
  • 35. JSON Rendering
  • 36. JSON Rendering from Mongo straight
  • 37. JSON Rendering
  • 38. PATCH Editing a Document
  • 39. PATCHing def patch_document(collection, original):! (...)! # Perform the update! db(collection).update({"_Id": ObjectId(object_id)}, ! {"$set": updates})! mongo update() method commits updates to the database.
  • 40. PATCHing def patch_document(collection, original):! (...)! # Perform the update! db(collection).update({"_Id": ObjectId(object_id)}, ! {"$set": updates})! udpate() takes the unique Id of the document to update
  • 41. PATCHing def patch_document(collection, original):! (...)! # Perform the update! db(collection).update({"_Id": ObjectId(object_id)}, ! {"$set": updates})! $set accepts a dict! with the updates for the db eg: {“active”: False}. updates are atomic
  • 42. POST Creating Resources
  • 43. POSTing Take #1 def post(collection):! (...)! for key, item in docs.items():! response[ID_FIELD] = db(collection).insert(item) push document and get its ObjectId back from Mongo. like other CRUD operations, inserting is trivial in mongo.
  • 44. POSTing Take #2 Bulk inserts!! (let’s look at the code)
  • 45. after a lot of tinkering we released an ambitious open source project
  • 46. Eve REST API Framework powered by Flask, MongoDB and good intentions python-eve.org
  • 47. Beta 0.2 • 1.000+ stargazers • 120 forks • 24 contributors • 7.935 downloads
  • 48. Eve Extensions contributed by the community • Eve-ElasticSearch • Eve-SQLAlchemy • Eve-Docs • Eve-Mocks
  • 49. Wanna see it running? Hopefully it won’t explode right into my face
  • 50. Initial draft Clients API Database RESTful ! Web API “Cloud” ? ? iOS Android Website Desktop Client
  • 51. Production Clients API Database Adam! MongoDB iOS Android Website Desktop Client eve instance
  • 52. MongoDB Rocks! your RESTful Web API
  • 53. Thank you. nicolaiarocci