Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/countvajhula/mongomorphism

A transaction manager and "ORM" for MongoDB
https://github.com/countvajhula/mongomorphism

Last synced: 22 days ago
JSON representation

A transaction manager and "ORM" for MongoDB

Awesome Lists containing this project

README

        

h1. 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":http://zodb.readthedocs.org/en/latest/transactions.html 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.

h3. 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):

bc. doc1['friend'] = doc2 # doc2 is stored as a DBRef

* Transparent support for arbitrary objects as values (not just dicts, lists, and other BSON data)

bc. myobj = SomeClass()
doc1['obj'] = myobj # behind the scenes, this instance will be serialized to JSON in mongodb when committed

bc. print doc1['obj'] # later when retrieved from the db, this will automatically deserialize to the python object

h1. Usage

h3. Transactional:

bc. from mongomorphism.datamanager import MongoDocument
from mongomorphism.config import Session
import transaction

bc. 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":http://zodb.readthedocs.org/en/latest/transactions.html#data-managers for the python "transaction":http://zodb.readthedocs.org/en/latest/transactions.html package.

h3. Non-transactional:

bc. from mongomorphism.datamanager import MongoDocument
from mongomorphism.config import Session

bc. 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

h3. To use as an "ORM":

bc. from mongomorphism.orm import MongoObject
from mongomorphism.config import Session
import transaction

bc. # define your models by extending MongoObject
class User(MongoObject):
__collection__ = 'users'
__requiredfields__ = ('name','location')

bc. 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.

h3. Retrieve an existing document from MongoDB:

p. Just pass a 'retrieve' dictionary to match against while creating the MongoDocument or MongoObject.

p. non-ORM:

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

p. ORM:

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

h1. "License"

This work is "part of the world." You are free to do whatever you like with it and it isn't owned by anybody, not even the creators. Attribution would be appreciated and would help, but it is not strictly necessary nor required. If you'd like to learn more about this way of doing things and how it could lead to a peaceful, efficient, and creative world (and how you can be involved), visit `drym.org `_.