Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
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
- Host: GitHub
- URL: https://github.com/countvajhula/mongomorphism
- Owner: countvajhula
- Created: 2013-03-09T00:12:30.000Z (over 11 years ago)
- Default Branch: master
- Last Pushed: 2021-11-16T21:12:37.000Z (almost 3 years ago)
- Last Synced: 2024-05-10T00:04:54.626Z (6 months ago)
- Language: Python
- Homepage:
- Size: 30.3 KB
- Stars: 1
- Watchers: 2
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.textile
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 committedbc. 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 transactionbc. 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 persistedNote: 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 Sessionbc. 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 calledh3. To use as an "ORM":
bc. from mongomorphism.orm import MongoObject
from mongomorphism.config import Session
import transactionbc. # 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 `_.