MongoKit vs MongoEngine vs Flask-MongoAlchemy for Flask

PythonMongodbSqlalchemyFlask

Python Problem Overview


Anyone has experiences with MongoKit, MongoEngine or Flask-MongoAlchemy for Flask?

Which one do you prefer? Positive or negative experiences?. Too many options for a Flask-Newbie.

Python Solutions


Solution 1 - Python

I have invested a lot of time evaluating the popular Python ORMs for MongoDB. This was an exhaustive exercise, as I really wanted to pick one.

My conclusion is that an ORM removes the fun out of MongoDB. None feels natural, they impose restrictions similar to the ones which made me move away from relational databases in the first place.

Again, I really wanted to use an ORM, but now I am convinced that using pymongo directly is the way to go. Now, I follow a pattern which embraces MongoDB, pymongo, and Python.

A Resource Oriented Architecture leads to very natural representations. For instance, take the following User resource:

from werkzeug.wrappers import Response
from werkzeug.exceptions import NotFound

Users = pymongo.Connection("localhost", 27017)["mydb"]["users"]


class User(Resource):

    def GET(self, request, username):
        spec = {
            "_id": username,
            "_meta.active": True
        }
        # this is a simple call to pymongo - really, do
        # we need anything else?
        doc = Users.find_one(spec)
        if not doc:
            return NotFound(username)
        payload, mimetype = representation(doc, request.accept)
        return Response(payload, mimetype=mimetype, status=200)

    def PUT(self, request, username):
        spec = {
            "_id": username,
            "_meta.active": True
        }
        operation = {
            "$set": request.json,
        }
        # this call to pymongo will return the updated document (implies safe=True)
        doc = Users.update(spec, operation, new=True)
        if not doc:
            return NotFound(username)
        payload, mimetype = representation(doc, request.accept)
        return Response(payload, mimetype=mimetype, status=200)

The Resource base class looks like

class Resource(object):

    def GET(self, request, **kwargs):
        return NotImplemented()

    def HEAD(self, request, **kwargs):
        return NotImplemented()

    def POST(self, request, **kwargs):
        return NotImplemented()

    def DELETE(self, request, **kwargs):
        return NotImplemented()

    def PUT(self, request, **kwargs):
        return NotImplemented()

    def __call__(self, request, **kwargs):
        handler = getattr(self, request.method)
        return handler(request, **kwargs)

Notice that I use the WSGI spec directly, and leverage Werkzeug where possible (by the way, I think that Flask adds an unnecessary complication to Werkzeug).

The function representation takes the request's Accept headers, and produces a suitable representation (for example, application/json, or text/html). It is not difficult to implement. It also adds the Last-Modified header.

Of course, your input needs to be sanitized, and the code, as presented, will not work (I mean it as an example, but it is not difficult to understand my point).

Again, I tried everything, but this architecture made my code flexible, simple, and extensible.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionoscarmlageView Question on Stackoverflow
Solution 1 - PythonEscualoView Answer on Stackoverflow