auto_rest.interfaces¶
Pydantic models are used to facilitate data validation and to define
interfaces for FastAPI endpoint handlers. The interfaces
module
provides utility functions for converting SQLAlchemy models into
Pydantic interfaces. Interfaces can be created using different modes
which force interface fields to be optional or read only.
Example: Creating an Interface
The create_interface_default
method creates an interface class
based on a SQLAlchemy table.
default_interface = create_interface(database_model)
required_interface = create_interface(database_model, mode="required")
optional_interface = create_interface(database_model, mode="optional")
create_field_definition(col, mode='default')
¶
Return a tuple with the type and default value for a database table column.
The returned tuple is compatible for use with Pydantic as a field definition
during dynamic model generation. The mode
argument modifies returned
values to enforce different behavior in the generated Pydantic interface.
Modes
default: Values are marked as (not)required based on the column schema. required: Values are always marked required. required: Values are always marked optional.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
col
|
Column
|
The column to return values for. |
required |
mode
|
MODE_TYPE
|
The mode to use when determining the default value. |
'default'
|
Returns:
Type | Description |
---|---|
tuple[type[any], any]
|
The default value for the column. |
Source code in auto_rest/interfaces.py
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
|
create_interface(table, pk_only=False, mode='default')
¶
Create a Pydantic interface for a SQLAlchemy model where all fields are required.
Modes
default: Values are marked as (not)required based on the column schema. required: Values are always marked required. required: Values are always marked optional.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
table
|
Table
|
The SQLAlchemy table to create an interface for. |
required |
pk_only
|
bool
|
If True, only include primary key columns. |
False
|
mode
|
MODE_TYPE
|
Whether to force fields to all be optional or required. |
'default'
|
Returns:
Type | Description |
---|---|
type[BaseModel]
|
A dynamically generated Pydantic model with all fields required. |
Source code in auto_rest/interfaces.py
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 |
|
iter_columns(table, pk_only=False)
¶
Iterate over the columns of a SQLAlchemy model.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
table
|
Table
|
The table to iterate columns over. |
required |
pk_only
|
bool
|
If True, only iterate over primary key columns. |
False
|
Yields:
Type | Description |
---|---|
Column
|
A column of the SQLAlchemy model. |
Source code in auto_rest/interfaces.py
29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
|