MongoDB Europe 2016 - MongoDB 3.4 preview and introduction to MongoDB Atlas
RESTful Web API and MongoDB go for a pic nic
-
Upload
nicola-iarocci -
Category
Technology
-
view
10.826 -
download
0
description
Transcript of RESTful Web API and MongoDB go for a pic nic
RESTful Web APIsand MongoDB go for a picnic
Nicola Iarocci @nicolaiarocci
Nicola IarocciEve REST API Framework, Cerberus, Events
Co-founder @ gestionaleamica.com, Book Author,
MongoDB Master
Nicola IarocciTL;DR
Passionate full stack developer
Il Piccolo Libro di MongoDB
edizione italiana del libro di Karl Seguin disponibile per il download @ nicolaiarocci.com
gestionaleamica.cominvoicing & accounting
gestionaleamica.comnow going web and mobile
gestionaleamica.com“ we need a remote API to keep everything in sync ”
A-ha moment
Before
Client
LAN/SQL
Database
Desktop!Application
Initial draft
Clients
“Cloud”
Database
RESTful !Web API
API
iOS
Android
Website
Desktop Client
? ?
Constraints
• minimum viable product first
• add features over time
• frequent database schema updates
• avoid downtime as much as possible
So we started exploring new paths
MongoDB and REST!
or why we picked MongoDB for our REST API
true selling point for me
JSON-style data store
all client to API communication is going to be JSON
JSON for transport
JSON & RESTful API
JSON!accepted media type
Client
JSON!(BSON)
Mongo
GET
maybe we can push directly to client?
JSON & RESTful API
JSON!accepted media type
Client
JSON!(BSON)
Mongo
JSON!subset of python dict!
(kinda)
API
GET
almost.
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
made NoSQL easy to grasp (even for a dumbhead like me)
Similarity with RDBMS
Terminology
RDBMS Mongo
Database Database
Table Collection
Rows(s) JSON Document
Index Index
Join Embedding & Linking
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”});
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”}
mapping to and from the database feels more natural
JSON all along the pipeline
Where we’re going we don’t need ORMs.
ORM
dynamic documents allow for painless, progressive evolution
Schema-less
MongoDB drivers are beautiful. Really.
PyMongo
Also in MongoDB
• setup is a breeze
• lightweight
• fast inserts, updates and queries
• excellent documentation
• great support by 10gen
• great community
REST in practice!
with some MongoDB love
CollectionsAPI entry point + plural nounsapi.example.com/contacts
Maps to a Mongo collection
DocumentAPI entry point + plural nouns + ID
api.example.com/contacts/4f46445fc88e201858000000
Maps to a collection ObjectID
Retrieving Resoruce Data
GET
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}
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}
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}
JSON Rendering
JSON Rendering
straight from Mongo
JSON Rendering
Editing a Document
PATCH
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})!
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})!
PATCHingdef 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
Creating Resources
POST
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.
POSTing Take #2
Bulk inserts!!(let’s look at the code)
after a lot of tinkeringwe released an ambitious open source project
Eve REST API Framework powered by
Flask, MongoDB and good intentions
python-eve.org
Beta 0.2
• 1.000+ stargazers
• 120 forks
• 24 contributors
• 7.935 downloads
Eve Extensions contributed by the community
• Eve-ElasticSearch
• Eve-SQLAlchemy
• Eve-Docs
• Eve-Mocks
Wanna see it running?Hopefully it won’t explode right into my face
Initial draft
Clients
“Cloud”
Database
RESTful !Web API
API
iOS
Android
Website
Desktop Client
? ?
Clients
MongoDB
Database
Adam!eve instance
API
iOS
Android
Website
Desktop Client
Production
MongoDB Rocks!your RESTful Web API
Thank you.nicolaiarocci