BaseDatabaseModel
Base class for database models, the schemas that map aggregates to a specific storage technology. Database models are typically auto-generated but can be customized for adapter-specific optimizations.
Bases: Element, OptionsMixin
Base class for database models, which map an aggregate, entity or projection to the schema of one kind of database.
Protean generates a default database model for every aggregate. Declare
one with @domain.database_model only to change the mapping, for
example the table name or adapter-specific field types. See the custom
database models guide.
A subclass must implement from_entity(). The default to_entity()
maps each attribute back by name, using referenced_as when it is set.
Override to_entity() only when the storage needs special handling.
A concrete subclass satisfies DatabaseModelPort, which lists the class
methods the framework calls on an adapter's model class.
Meta Options
| Option | Type | Description |
|---|---|---|
part_of |
type or str |
The aggregate, entity or projection this model maps. Required. A string names the element and is resolved at domain.init(). |
database_type |
str |
The database type this model applies to (e.g. "sqlite", "elasticsearch"), not a provider name. Without it, the model is used for any database type that has no model of its own. |
schema_name |
str |
Override the storage table/collection name. |
derive_schema_name
classmethod
derive_schema_name() -> str
Derive the storage table/collection name.
Uses meta_.schema_name if explicitly set, otherwise falls back
to the schema name of the associated aggregate or entity.
| RETURNS | DESCRIPTION |
|---|---|
str
|
The resolved schema name.
TYPE:
|
Source code in src/protean/core/database_model.py
62 63 64 65 66 67 68 69 70 71 72 73 74 75 | |
from_entity
abstractmethod
classmethod
from_entity(entity: Any) -> Any
Convert a domain entity/aggregate into a database model instance.
Implementors should extract field values from the domain object and map them to the database-specific representation.
| PARAMETER | DESCRIPTION |
|---|---|
entity
|
The domain entity or aggregate to convert.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Any
|
The database model instance ready for persistence. |
Source code in src/protean/core/database_model.py
87 88 89 90 91 92 93 94 95 96 97 98 99 100 | |
to_entity
classmethod
to_entity(item: Any) -> BaseEntity
Convert a database record back into a domain entity/aggregate.
Iterates entity attributes, applies referenced_as remapping,
and reconstructs the domain object. Adapters with storage-specific
reconstruction needs (e.g. Elasticsearch meta.id extraction)
should override this method entirely.
Source code in src/protean/core/database_model.py
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 | |
to_records
classmethod
to_records(
items: list[Any], fields: list[str]
) -> list[Record]
Build read-only Record objects
from storage records, holding only the selected fields.
QuerySet.only uses this. It
builds no domain entities, so no validation or invariant runs.
Adapters with storage-specific record access override this method.
| PARAMETER | DESCRIPTION |
|---|---|
items
|
The raw storage records the adapter's
TYPE:
|
fields
|
Attribute names to read from each record. Each record is keyed by these names.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
list[Record]
|
One |
Source code in src/protean/core/database_model.py
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 | |