ODM Event Interfaces¶
This section describes the various categories of events which can be intercepted within the Ming ODM.
Events can be trapped by registering MapperExtension
and SessionExtension
instances
which implement the event handlers for the events you want to trap.
Mapper Events¶
Mapper events are used to track To use MapperExtension, make your own subclass of it and just send it off to a mapper:
from ming.odm.mapper import MapperExtension
class MyExtension(MapperExtension):
def after_insert(self, obj, st, sess):
print "instance %s after insert !" % obj
class MyMappedClass(MappedClass):
class __mongometa__:
session = session
name = 'my_mapped_class'
extensions = [ MyExtension ]
Multiple extensions will be chained together and processed in order;
extensions = [ext1, ext2, ext3]
- class ming.odm.MapperExtension(mapper)
Base class that should be inherited to handle Mapper events.
- after_delete(instance, state, sess)
Receive an object instance and its current state after that instance is deleted.
- after_insert(instance, state, sess)
Receive an object instance and its current state after that instance is inserted into its collection.
- after_remove(sess, *args, **kwargs)
After a remove query is performed for this class
- after_update(instance, state, sess)
Receive an object instance and its current state after that instance is updated.
- before_delete(instance, state, sess)
Receive an object instance and its current state before that instance is deleted.
- before_insert(instance, state, sess)
Receive an object instance and its current state before that instance is inserted into its collection.
- before_remove(sess, *args, **kwargs)
Before a remove query is performed for this class
- before_update(instance, state, sess)
Receive an object instance and its current state before that instance is updated.
Session Events¶
The SessionExtension applies plugin points for Session objects and ODMCursor objects:
from ming.odm.base import state
from ming.odm.odmsession import SessionExtension
class MySessionExtension(SessionExtension):
def __init__(self, session):
SessionExtension.__init__(self, session)
self.objects_added = []
self.objects_modified = []
self.objects_deleted = []
def before_flush(self, obj=None):
if obj is None:
self.objects_added = list(self.session.uow.new)
self.objects_modified = list(self.session.uow.dirty)
self.objects_deleted = list(self.session.uow.deleted)
# do something
ODMSession = ThreadLocalODMSession(session,
extensions=[ProjectSessionExtension])
The same SessionExtension instance can be used with any number of sessions. It is possible to register extensions on an already created ODMSession using the register_extension(extension) method of the session itself. Even calling register_extension it is possible to register the extensions only before using the session for the first time.
- class ming.odm.SessionExtension(session)
Base class that should be inherited to handle Session events.
- after_cursor_next(cursor)
Cursor has advanced to next result
- after_delete(obj, st)
After an object gets deleted in this session
- after_flush(obj=None)
After the session is flushed for
obj
If
obj
isNone
it means all the objects in the UnitOfWork which can be retrieved by iterating overODMSession.uow
- after_insert(obj, st)
After an object gets inserted in this session
- after_remove(cls, *args, **kwargs)
After a remove query is performed session
- after_update(obj, st)
After an object gets updated in this session
- before_cursor_next(cursor)
Cursor is going to advance to next result
- before_delete(obj, st)
Before an object gets deleted in this session
- before_flush(obj=None)
Before the session is flushed for
obj
If
obj
isNone
it means all the objects in the UnitOfWork which can be retrieved by iterating overODMSession.uow
- before_insert(obj, st)
Before an object gets inserted in this session
- before_remove(cls, *args, **kwargs)
Before a remove query is performed session
- before_update(obj, st)
Before an object gets updated in this session
- cursor_created(cursor, action, *args, **kw)
New cursor with the results of a query got created