Tori Framework and Object
Relational Mapping with MongoDB
Juti Noppornpitak (@shiroyuki)
Senior Developer at Instaclick Inc. in Toronto
A contributor of some open-source projects in Python (mostly
mine), PHP (mostly Symfony Framework-related) and
T: @shiroyuki FB: juti.noppornpitak
What is object relational mapping?
For example, SQLAlchemy.
Well, Wikipedia and Google can
help you. But please save the fun
after this talk. :D
It is a web framework based on
However, nobody knows about this but it has
an object relational mapper for NoSQL
databases... Well, start with MongoDB
But why did I go through troubles in
order to create an object relational
mapper for MongoDB in Python?
Problems + Motivations
• Develop a database-agnostic ORM for
• Develop an ORM using the Unit of
Work pattern, instead of the popular
Active Record pattern. (Inspired by
• Emphasize on the code portability and
• Emphasize on the minimal class
inheritance. (Inspired by the concept
of pure old Java objects)
• Emphasize on the schema-less
feature of no-SQL databases.
Architecture of Tori’s Object
• Design Criteria
• Building Tools
•The model class must not extend
from any base classes to reduce the
number of required parent classes
and code overriding.
•Imitate InnoDB's transaction feature
to minimize the number of write
operations on the server and some
capabilities to revert the changes.
•Like any ORMs, handle associations
and cascading automatically.
•Unlike some ORMs, the model
should not enforce the type of the
value of the property, except the
property that is mapped to the
•Provide a common way to deal with
databases with minimal magic but
good enough to reduce human
Tori ORM Architecture / Entities
• Lightweight object
• Normal object (no extension required)
• Many entities can be mapped to the same collection.
• The constructor is used to initially set the data.
• Entity class can extend other entity classes as well as any entity classes.
• All public properties (without leading underscores) are mapped to the respective
• Use decorators to set up the metadata, including the associations and indexes.
Tori ORM Architecture / Entity
• Centralize the access point to the ORM functionality.
• Manage pseudo-transactional write operations to optimize the
• Similar to SQLAlchemy and Doctrine, internally use a UnitOfWork
to track all loaded references.