Skip to content

moonanddoris/mongomorphism

 
 

Repository files navigation

Introduction

mongomorphism provides a simple interface to MongoDB that mimics the standard python dictionary (it strives to be an “isomorphism” between a MongoDB document and a python dictionary). In addition, mongomorphism supports ACID-like transactions on top of MongoDB by using the python transaction package, and provides a thin “ORM” layer allowing you to define your models as python objects. The underlying pymongo connection may be used for more complex operations.

Features:

  • Provides transaction-like semantics (all-or-nothing commit behavior)
  • Provides basic ORM functionality while still remaining schema-free
  • Transparent support for Mongo references (DBRefs):
doc1['friend'] = doc2 # doc2 is stored as a DBRef
  • Transparent support for arbitrary objects as values (not just dicts, lists, and other BSON data)
myobj = SomeClass()
doc1['obj'] = myobj # behind the scenes, this instance will be serialized to JSON in mongodb when committed
print doc1['obj'] # later when retrieved from the db, this will automatically deserialize to the python object

Usage

Transactional:

from mongomorphism.datamanager import MongoDocument
from mongomorphism.config import Session
import transaction
session = Session('my_db') # create a session by passing the name of the database to use
doc = MongoDocument(session, 'my_collection') # automatically joins the current transaction
doc['name'] = 'Sid'
doc['location'] = 'San Francisco'
transaction.commit() # commit all changes that are part of this transaction. If there are errors none of the changes will be persisted

Note: This mode operates by implementing a data manager for the python transaction package.

Non-transactional:

from mongomorphism.datamanager import MongoDocument
from mongomorphism.config import Session
session = Session('my_db', transactional=False) # creates a non-transactional session
doc = MongoDocument(session, 'my_collection')
doc['name'] = 'Sid'
doc['location'] = 'San Francisco'
doc.save() # doc is persisted to mongodb when save() is called

To use as an “ORM”:

from mongomorphism.orm import MongoObject
from mongomorphism.config import Session
import transaction
# define your models by extending MongoObject
class User(MongoObject):
    __collection__ = 'users'
    __requiredfields__ = ('name','location')
session = Session('my_db') # create a session by passing the name of the database to use
user = User(session) # automatically joins the current transaction
user['name'] = 'Sid'
user['location'] = 'San Francisco'
transaction.commit()

You could also use the ORM in non-transactional mode similar to the previous example. That is, create the session with transactional=False, and then call save() when you want to persist the object.

Retrieve an existing document from MongoDB:

Just pass a ‘retrieve’ dictionary to match against while creating the MongoDocument or MongoObject.

non-ORM:

doc = MongoDocument(session, 'my_collection', retrieve={'name':'Sid'})

ORM:

doc = User(session, retrieve={'name':'Sid'})

About

A transaction manager and "ORM" for MongoDB

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Python 100.0%