Overview
Plugins are classes that implement thePlugin protocol. You can hook into various stages of the CRUD lifecycle:
- Create:
BEFORE_CREATE,AFTER_CREATE - Update:
BEFORE_UPDATE,AFTER_UPDATE - Delete:
BEFORE_DELETE,AFTER_DELETE - Get:
BEFORE_GET,AFTER_GET - Query:
BEFORE_QUERY(modify query),AFTER_QUERY(transform results)
Creating a Plugin
The recommended way to create a plugin is to inherit fromBasePlugin. This class provides default no-op implementations for all hooks, so you only need to override the methods you care about.
Registering Plugins
Pass a list of plugin instances when initializing your repository:Lifecycle Hooks
Data Transformation Hooks
These hooks allow you to modify data before it reaches the database or is returned to the user.on_before_create(self, model, data) -> dict: Modify creation data.on_before_update(self, model, db_obj, data) -> dict: Modify update data.on_before_query(self, query) -> Select: Modify the SQLAlchemy query object (e.g., add filters).on_after_query(self, results) -> Sequence: Modify or enrich the list of results.
Side-Effect Hooks
These hooks are for actions that don’t modify the data flow, like logging or notifications. They returnNone.
on_after_create(self, model, instance)on_after_update(self, model, instance)on_before_delete(self, model, instance)on_after_delete(self, model, instance)on_before_get(self, model, id)on_after_get(self, model, instance)
Example: Timestamp Plugin
Here is a simple plugin that automatically setscreated_at and updated_at timestamps.

