Session
mydborm.session.Session
Unit of Work + Identity Map session.
Tracks object lifecycle, detects dirty fields, and coordinates atomic flushes to the database. Use as context manager for automatic commit/rollback.
Usage
As context manager
with Session() as session: user = session.get(User, id=1) user.username = "updated" # auto-committed on exit
Manual
session = Session() session.configure(dialect="mysql", ...) user = session.get(User, id=1) user.username = "updated" session.flush() session.commit() session.close()
add(model_class, **kwargs)
Queue a new object for insertion on next flush.
Usage
session.add(User, username="alice", email="a@x.com") session.add(Order, user_id=1, total=99.99) session.flush() # both inserted atomically
all(model_class)
Load all records, registering each in the identity map.
Usage
users = session.all(User)
close()
Close the session and release all tracked objects.
commit()
Commit the current database transaction.
delete(instance)
Mark an instance for deletion on next flush.
Usage
user = session.get(User, id=1) session.delete(user) session.flush()
dirty_fields(instance)
Return list of modified field names for an instance.
expunge(instance)
Remove an instance from the session without deleting it.
expunge_all()
Remove all instances from the session.
filter(model_class, **kwargs)
Filter records, registering each in the identity map.
Usage
active_users = session.filter(User, active=True)
flush()
Write all pending changes to the database without committing.
Order: INSERTs → UPDATEs → DELETEs
get(model_class, **kwargs)
Get a record — returns cached instance if already loaded.
Usage
user = session.get(User, id=1) same = session.get(User, id=1) assert user is same # identical object
is_dirty(instance)
Check if an instance has unsaved changes.
original_value(instance, field)
Return the original DB value of a field before modification.
rollback()
Rollback pending changes — clear queued inserts/deletes and reset dirty tracking.
stats()
Return session statistics.