karenina.storage.auto_mapper¶
auto_mapper
¶
Auto-mapper for generating SQLAlchemy models from Pydantic models.
This module provides utilities to automatically generate SQLAlchemy ORM models from Pydantic BaseModel classes, with support for: - Flattening nested models into prefixed columns - Automatic type mapping (str→Text, int→Integer, etc.) - Index hints via Pydantic Field metadata - Handling Optional/Union types
Classes¶
ColumnSpec
¶
Specification for a generated SQLAlchemy column.
Source code in src/karenina/storage/auto_mapper.py
Functions¶
to_column
¶
Convert to SQLAlchemy Column.
Source code in src/karenina/storage/auto_mapper.py
PydanticSQLAlchemyMapper
¶
Auto-generates SQLAlchemy column specifications from Pydantic models.
This mapper introspects Pydantic BaseModel classes and generates corresponding SQLAlchemy column definitions with support for: - Recursive flattening of nested models - Automatic type mapping - Index hints from Field metadata - Nullable handling for Optional fields
Example
mapper = PydanticSQLAlchemyMapper() columns = mapper.generate_columns(VerificationResult, flatten_config={ ... "metadata": {"prefix": "metadata_"}, ... "template": {"prefix": "template_", "optional": True}, ... })
Source code in src/karenina/storage/auto_mapper.py
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 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 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 | |
Functions¶
__init__
¶
__init__(type_map: dict[type, Any] | None = None)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
type_map
¶ |
dict[type, Any] | None
|
Optional custom type mapping to extend/override defaults |
None
|
Source code in src/karenina/storage/auto_mapper.py
create_model_class
¶
create_model_class(
base: type[DeclarativeBase],
name: str,
tablename: str,
columns: dict[str, ColumnSpec],
extra_columns: dict[str, Column[Any]] | None = None,
relationships: dict[str, Any] | None = None,
table_args: tuple[Any, ...] | None = None,
) -> type
Dynamically create a SQLAlchemy ORM model class.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
base
¶ |
type[DeclarativeBase]
|
SQLAlchemy declarative base class |
required |
name
¶ |
str
|
Name for the generated class |
required |
tablename
¶ |
str
|
Database table name |
required |
columns
¶ |
dict[str, ColumnSpec]
|
Column specifications from generate_columns() |
required |
extra_columns
¶ |
dict[str, Column[Any]] | None
|
Additional columns (e.g., id, foreign keys) |
None
|
relationships
¶ |
dict[str, Any] | None
|
SQLAlchemy relationship definitions |
None
|
table_args
¶ |
tuple[Any, ...] | None
|
Additional table arguments (indexes, constraints) |
None
|
Returns:
| Type | Description |
|---|---|
type
|
Generated SQLAlchemy model class |
Source code in src/karenina/storage/auto_mapper.py
generate_columns
¶
generate_columns(
model: type[BaseModel],
prefix: str = "",
flatten_nested: bool = True,
parent_optional: bool = False,
) -> dict[str, ColumnSpec]
Generate SQLAlchemy column specifications from Pydantic model fields.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
¶ |
type[BaseModel]
|
The Pydantic model class to introspect |
required |
prefix
¶ |
str
|
Prefix to add to all column names (e.g., "metadata_") |
''
|
flatten_nested
¶ |
bool
|
Whether to recursively flatten nested models |
True
|
parent_optional
¶ |
bool
|
Whether the parent field was optional (affects nullability) |
False
|
Returns:
| Type | Description |
|---|---|
dict[str, ColumnSpec]
|
Dictionary mapping column names to ColumnSpec objects |
Source code in src/karenina/storage/auto_mapper.py
generate_columns_for_result
¶
generate_columns_for_result(
model: type[BaseModel],
flatten_config: dict[str, dict[str, Any]],
) -> dict[str, ColumnSpec]
Generate columns with custom flatten configuration per field.
This is designed for the VerificationResult model where each component (metadata, template, rubric, etc.) has its own prefix and optional status.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
¶ |
type[BaseModel]
|
The root Pydantic model class |
required |
flatten_config
¶ |
dict[str, dict[str, Any]]
|
Configuration for each field, e.g.: { "metadata": {"prefix": "metadata_", "optional": False}, "template": {"prefix": "template_", "optional": True}, } |
required |
Returns:
| Type | Description |
|---|---|
dict[str, ColumnSpec]
|
Dictionary mapping column names to ColumnSpec objects |
Source code in src/karenina/storage/auto_mapper.py
Functions¶
generate_indexes_from_columns
¶
generate_indexes_from_columns(
columns: dict[str, ColumnSpec],
tablename: str,
composite_indexes: list[tuple[str, ...]] | None = None,
) -> list[Index]
Generate SQLAlchemy Index objects from column specifications.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
dict[str, ColumnSpec]
|
Column specifications |
required |
|
str
|
Table name for naming indexes |
required |
|
list[tuple[str, ...]] | None
|
List of column name tuples for composite indexes |
None
|
Returns:
| Type | Description |
|---|---|
list[Index]
|
List of Index objects |
Source code in src/karenina/storage/auto_mapper.py
get_flat_field_mapping
¶
get_flat_field_mapping(
model: type[BaseModel],
flatten_config: dict[str, dict[str, Any]],
) -> dict[str, str]
Get mapping from nested field paths to flat column names.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
type[BaseModel]
|
The Pydantic model class |
required |
|
dict[str, dict[str, Any]]
|
Flatten configuration |
required |
Returns:
| Type | Description |
|---|---|
dict[str, str]
|
Dictionary mapping "component.field" to "prefix_field" |