RESTful Web API and MongoDB go for a pic nic

RESTful Web API and MongoDB go for a pic nic

Why and how MongoDB is a perfect match for building your next RESTful Web API.

E3550767c858c787c35c280047ff789c?s=128

Nicola Iarocci

October 26, 2013
Tweet

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. your typical old school desktop application gestionaleamica.com

  7. None
  8. gestionaleamica.com now going web and mobile

  9. gestionaleamica.com “ we need a remote API to keep everything

    in sync ” A-ha moment
  10. Before Client LAN/SQL Database Desktop! Application

  11. Initial draft Clients “Cloud” Database RESTful ! Web API API

    iOS Android Website Desktop Client ? ?
  12. Constraints • minimum viable product first • add features over

    time • frequent database schema updates • avoid downtime as much as possible
  13. So we started exploring new paths

  14. MongoDB and REST ! or why we picked MongoDB for

    our REST API
  15. true selling point for me JSON-style data store

  16. all client to API communication is going to be JSON

    JSON for transport
  17. JSON & RESTful API JSON! accepted media type Client JSON!

    (BSON) Mongo GET maybe we can push directly to client?
  18. JSON & RESTful API JSON! accepted media type Client JSON!

    (BSON) Mongo JSON! subset of python dict! (kinda) API GET almost.
  19. JSON & RESTful API JSON! objects Client JSON! (BSON) Mongo

    JSON/dict! maps to python dict! (validation layer) API POST also works when posting (adding) items to the database
  20. made NoSQL easy to grasp (even for a dumbhead like

    me) Similarity with RDBMS
  21. Terminology RDBMS Mongo Database Database Table Collection Rows(s) JSON Document

    Index Index Join Embedding & Linking
  22. Queries in MongoDB are represented as JSON-style objects What about

    Queries? // select * from things where x=3 and y="foo" db.things.find({x: 3, y: "foo”});
  23. Filtering and Sorting native! Mongo! query syntax Client JSON! (BSON)

    Mongo (very) thin parsing! & validation layer API let’s simply expose MongoDB syntax ?where={x: 3, y: "foo”}
  24. mapping to and from the database feels more natural JSON

    all along the pipeline
  25. Where we’re going we don’t need ORMs. ORM

  26. dynamic documents allow for painless, progressive evolution Schema-less

  27. MongoDB drivers are beautiful. Really. PyMongo

  28. Also in MongoDB • setup is a breeze • lightweight

    • fast inserts, updates and queries • excellent documentation • great support by 10gen • great community
  29. REST in practice ! with some MongoDB love

  30. Collections API entry point + plural nouns api.example.com/contacts Maps to

    a Mongo collection
  31. Document API entry point + plural nouns + ID api.example.com/contacts/4f46445fc88e201858000000

    Maps to a collection ObjectID
  32. Retrieving Resoruce Data GET

  33. def get_collection(collection):! documents = []! cursor = db(collection).find(where, projection)! for

    document in cursor:! documents.append(document)! return documents Resource GET find() accepts a python dict as query expression, and returns a cursor we can iterate /contacts?where={“age”: {“$gt”: 20}}&projection={“lastname”: 1}
  34. def get_collection(collection):! documents = []! cursor = db(collection).find(where, projection)! for

    document in cursor:! documents.append(document)! return documents Resource GET find() accepts a python dict as query expression, and returns a cursor we can iterate /contacts?where={“age”: {“$gt”: 20}}&projection={“lastname”: 1}
  35. def get_collection(collection):! documents = []! cursor = db(collection).find(where, projection)! for

    document in cursor:! documents.append(document)! return documents Resource GET find() accepts a python dict as query expression, and returns a cursor we can iterate /contacts?where={“age”: {“$gt”: 20}}&projection={“lastname”: 1}
  36. JSON Rendering

  37. JSON Rendering straight from Mongo

  38. JSON Rendering

  39. Editing a Document PATCH

  40. PATCHing mongo update() method commits updates to the database. def

    patch_document(collection, original):! (...)! # Perform the update! db(collection).update({"_Id": ObjectId(object_id)}, ! {"$set": updates})!
  41. PATCHing udpate() takes the unique Id of the document to

    update def patch_document(collection, original):! (...)! # Perform the update! db(collection).update({"_Id": ObjectId(object_id)}, ! {"$set": updates})!
  42. 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
  43. Creating Resources POST

  44. def post(collection):! (...)! for key, item in docs.items():! response[ID_FIELD] =

    db(collection).insert(item) POSTing Take #1 push document and get its ObjectId back from Mongo. like other CRUD operations, inserting is trivial in mongo.
  45. POSTing Take #2 Bulk inserts!! (let’s look at the code)

  46. after a lot of tinkering we released an ambitious open

    source project
  47. Eve REST API Framework powered by Flask, MongoDB and good

    intentions python-eve.org
  48. Beta 0.2 • 1.000+ stargazers • 120 forks • 24

    contributors • 7.935 downloads
  49. Eve Extensions contributed by the community • Eve-ElasticSearch • Eve-SQLAlchemy

    • Eve-Docs • Eve-Mocks
  50. Wanna see it running? Hopefully it won’t explode right into

    my face
  51. Initial draft Clients “Cloud” Database RESTful ! Web API API

    iOS Android Website Desktop Client ? ?
  52. Clients MongoDB Database Adam! eve instance API iOS Android Website

    Desktop Client Production
  53. MongoDB Rocks! your RESTful Web API

  54. Thank you. nicolaiarocci