model
DataclassType
Bases: Protocol
Protocol for plain (non-pydantic) dataclass models.
Source code in src/embar/model.py
52 53 54 55 | |
HasAnnotations
Bases: Protocol
Protocol satisfied by any class that carries __annotations__ — i.e. every
Python class that declares at least one field-level type hint.
This is the minimal structural requirement for to_sql_columns and
load_dataclass to work: they only need get_type_hints() to succeed.
Source code in src/embar/model.py
58 59 60 61 62 63 64 65 66 67 | |
SelectAllDataclass
SelectAll version that doesn't validate (plain dataclass).
Source code in src/embar/model.py
81 82 83 84 85 86 | |
SelectAllPydantic
Bases: BaseModel
SelectAll version that validates with Pydantic.
Source code in src/embar/model.py
73 74 75 76 77 78 | |
generate_dataclass_model(cls)
Create a plain dataclass based on a Table (no Pydantic validation).
Fields are typed as Annotated[py_type, column] so that to_sql_columns
can discover the SQL column reference.
Note the new dataclass has the same exact name, maybe something to revisit.
from embar.table import Table
from embar.model import generate_dataclass_model
class MyTable(Table): ...
generate_dataclass_model(MyTable)
Source code in src/embar/model.py
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 | |
generate_pydantic_model(cls)
Create a Pydantic model based on a Table.
Note the new model has the same exact name, maybe something to revisit.
from embar.table import Table
from embar.model import generate_pydantic_model
class MyTable(Table): ...
generate_pydantic_model(MyTable)
Source code in src/embar/model.py
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 | |
load_dataclass(model, data)
Load a list of row dicts into plain dataclass/annotated-class instances (no Pydantic validation).
Handles nested dataclasses (from ManyTable/OneTable annotations) by recursively loading JSON objects/arrays from the database into the appropriate types.
Source code in src/embar/model.py
340 341 342 343 344 345 346 347 348 | |
load_results(model, data)
Load query result rows into model instances.
Dispatches between Pydantic validation (for BaseModel subclasses) and
the plain dict→dataclass loader for everything else.
Source code in src/embar/model.py
325 326 327 328 329 330 331 332 333 334 335 336 337 | |
upgrade_model_nested_fields(model, use_pydantic)
Upgrade a model so that nested ManyTable/OneTable fields are resolved to concrete models.
For Pydantic models, creates a new subclass via create_model.
For plain dataclasses/annotated classes, creates a new dataclass with upgraded field types.
use_pydantic controls whether nested table models are generated as Pydantic models
or plain dataclasses, and must be supplied explicitly by the caller.
Source code in src/embar/model.py
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 316 317 318 319 320 321 322 | |