Package fastapi_jsonapi index
fastapi_jsonapi.data_layers.fields.mixins module
Enum mixin module.
- class fastapi_jsonapi.data_layers.fields.mixins.MixinEnum(new_class_name, /, names, *, module=None, qualname=None, type=None, start=1, boundary=None)
Bases:
EnumExtension over enum class from standard library.
- classmethod inverse()
Return all inverted items sequence.
- classmethod keys()
Get all field keys from Enum.
- classmethod names()
Get all field names.
- classmethod value_to_enum(value)
Convert value to enum.
- classmethod values()
Get all values from Enum.
fastapi_jsonapi.data_layers.sqla module
fastapi_jsonapi.data_layers.sqla.base_model module
- class fastapi_jsonapi.data_layers.sqla.base_model.BaseSQLA
Bases:
object- async classmethod all(session: AsyncSession, stmt: Select) Type[TypeModel] | Any
- async classmethod count(session: AsyncSession, stmt: Select) int
- async classmethod create(session: AsyncSession, model: TypeModel, resource_type: str, commit: bool = True, id_: str | None = None, **kwargs) TypeModel
- async classmethod delete(session: AsyncSession, model: TypeModel, filters: list[BinaryExpression | bool], resource_type: str, commit: bool = True, id_: str | None = None, **kwargs) None
- async classmethod one_or_raise(session: AsyncSession, model: TypeModel, filters: list[BinaryExpression | bool], stmt: Select) TypeModel | Any
- classmethod query(model: TypeModel, fields: list | None = None, select_from: TypeModel | None = None, distinct_: bool = False, filters: list[BinaryExpression | bool] | None = None, for_update: dict | None = None, join: list[tuple[TypeModel, Any]] | None = None, jsonapi_join: list[RelationshipInfo] | None = None, number: int | None = None, options: Iterable = (), order: str | UnaryExpression | None = None, size: int | None = None, stmt: Select | None = None) Select
- async classmethod update(session: AsyncSession, model: TypeModel, resource_type: str, commit: bool = True, id_: str | None = None, **kwargs) TypeModel
fastapi_jsonapi.data_layers.sqla.orm module
This module is a CRUD interface between resource managers and the sqlalchemy ORM
- class fastapi_jsonapi.data_layers.sqla.orm.SqlalchemyDataLayer(model: Type[TypeModel], session: AsyncSession, resource_type: str, disable_collection_count: bool = False, default_collection_count: int = -1, id_name_field: str | None = None, url_id_field: str = 'id', eagerload_includes: bool = True, query: Select | None = None, auto_convert_id_to_column_type: bool = True, **kwargs: Any)
Bases:
BaseDataLayerSqlalchemy data layer
- async after_create_object(obj: TypeModel, model_kwargs: dict, view_kwargs: dict)
Provide additional data after object creation.
- Parameters:
obj – an object from data layer.
model_kwargs – the data validated by pydantic.
view_kwargs – kwargs from the resource view.
- async after_create_relationship(obj: Any, updated: bool, json_data: dict, relationship_field: str, related_id_field: str, view_kwargs: dict)
Make work after to create a relationship.
- Parameters:
obj – an object from data layer.
updated – True if object was updated else False.
json_data – the request params.
relationship_field – the model attribute used for relationship.
related_id_field – the identifier field of the related model.
view_kwargs – kwargs from the resource view.
- Return boolean:
True if relationship have changed else False.
- async after_delete_object(obj: TypeModel, view_kwargs: dict)
Make work after delete object.
- Parameters:
obj – an object from data layer.
view_kwargs – kwargs from the resource view.
- async after_delete_objects(objects: list[TypeModel], view_kwargs: dict)
Any actions after deleting objects.
- Parameters:
objects – an object from data layer.
view_kwargs – kwargs from the resource view.
- async after_delete_relationship(obj: Any, updated: bool, json_data: dict, relationship_field: str, related_id_field: str, view_kwargs: dict)
Make work after to delete a relationship.
- Parameters:
obj – an object from data layer.
updated – True if object was updated else False.
json_data – the request params.
relationship_field – the model attribute used for relationship.
related_id_field – the identifier field of the related model.
view_kwargs – kwargs from the resource view.
- async after_get_collection(collection: Iterable, qs: QueryStringManager, view_kwargs: dict)
Make work after to retrieve a collection of objects.
- Parameters:
collection – the collection of objects.
qs – a querystring manager to retrieve information from url.
view_kwargs – kwargs from the resource view.
- async after_get_object(obj: Any, view_kwargs: dict)
Make work after to retrieve an object.
- Parameters:
obj – an object from data layer.
view_kwargs – kwargs from the resource view.
- async after_get_relationship(obj: Any, related_objects: Iterable, relationship_field: str, related_type_: str, related_id_field: str, view_kwargs: dict)
Make work after to get information about a relationship.
- Parameters:
obj – an object from data layer.
related_objects – related objects of the object.
relationship_field – the model attribute used for relationship.
related_type – the related resource type.
related_id_field – the identifier field of the related model.
view_kwargs – kwargs from the resource view.
- Return tuple:
the object and related object(s).
- async after_update_object(obj: Any, model_kwargs: dict, view_kwargs: dict)
Make work after update object.
- Parameters:
obj – an object from data layer.
model_kwargs – the data validated by schemas.
view_kwargs – kwargs from the resource view.
- async after_update_relationship(obj: Any, updated: bool, json_data: dict, relationship_field: str, related_id_field: str, view_kwargs: dict)
Make work after to update a relationship.
- Parameters:
obj – an object from data layer.
updated – True if object was updated else False.
json_data – the request params.
relationship_field – the model attribute used for relationship.
related_id_field – the identifier field of the related model.
view_kwargs – kwargs from the resource view.
- Return boolean:
True if relationship have changed else False.
- async apply_relationships(obj: TypeModel, data_create: BaseJSONAPIItemInSchema, action_trigger: Literal['create', 'update']) tuple[dict[str, TypeModel | None], dict[str, list[TypeModel]]]
Handles relationships passed in request
- Parameters:
obj
data_create
action_trigger – indicates which one operation triggered relationships applying
- Returns:
- async atomic_end(success: bool = True, exception: Exception | None = None)
- async atomic_start(previous_dl: SqlalchemyDataLayer | None = None)
- async before_create_object(model_kwargs: dict, view_kwargs: dict)
Provide additional data before object creation.
- Parameters:
model_kwargs – the data validated by pydantic.
view_kwargs – kwargs from the resource view.
- async before_create_relationship(json_data: dict, relationship_field: str, related_id_field: str, view_kwargs: dict)
Make work before to create a relationship.
- Parameters:
json_data – the request params.
relationship_field – the model attribute used for relationship.
related_id_field – the identifier field of the related model.
view_kwargs – kwargs from the resource view.
- Return boolean:
True if relationship have changed else False.
- async before_delete_object(obj: TypeModel, view_kwargs: dict)
Make checks before delete object.
- Parameters:
obj – an object from data layer.
view_kwargs – kwargs from the resource view.
- async before_delete_objects(objects: list[TypeModel], view_kwargs: dict)
Make checks before deleting objects.
- Parameters:
objects – an object from data layer.
view_kwargs – kwargs from the resource view.
- async before_delete_relationship(json_data: dict, relationship_field: str, related_id_field: str, view_kwargs: dict)
Make work before to delete a relationship.
- Parameters:
json_data – the request params.
relationship_field – the model attribute used for relationship.
related_id_field – the identifier field of the related model.
view_kwargs – kwargs from the resource view.
- async before_get_collection(qs: QueryStringManager, view_kwargs: dict)
Make work before to retrieve a collection of objects.
- Parameters:
qs – a querystring manager to retrieve information from url.
view_kwargs – kwargs from the resource view.
- async before_get_object(view_kwargs: dict)
Make work before to retrieve an object.
- Parameters:
view_kwargs – kwargs from the resource view.
- async before_get_relationship(relationship_field: str, related_type_: str, related_id_field: str, view_kwargs: dict)
Make work before to get information about a relationship.
- Parameters:
relationship_field (str) – the model attribute used for relationship.
related_type (str) – the related resource type.
related_id_field (str) – the identifier field of the related model.
view_kwargs (dict) – kwargs from the resource view.
- Return tuple:
the object and related object(s).
- async before_update_object(obj: Any, model_kwargs: dict, view_kwargs: dict)
Make checks or provide additional data before update object.
- Parameters:
obj – an object from data layer.
model_kwargs – the data validated by schemas.
view_kwargs – kwargs from the resource view.
- async before_update_relationship(json_data: dict, relationship_field: str, related_id_field: str, view_kwargs: dict)
Make work before to update a relationship.
- Parameters:
json_data – the request params.
relationship_field – the model attribute used for relationship.
related_id_field – the identifier field of the related model.
view_kwargs – kwargs from the resource view.
- Return boolean:
True if relationship have changed else False.
- async classmethod check_object_has_relationship_or_raise(obj: TypeModel, relation_name: str)
Checks that there is relationship with relation_name in obj
- Parameters:
obj
relation_name
- async create_object(data_create: BaseJSONAPIItemInSchema, view_kwargs: dict) TypeModel
Create an object through sqlalchemy.
- Parameters:
data_create – the data validated by pydantic.
view_kwargs – kwargs from the resource view.
- Returns:
- async create_relationship(json_data: dict, relationship_field: str, related_id_field: str, view_kwargs: dict) bool
Create a relationship.
- Parameters:
json_data – the request params.
relationship_field – the model attribute used for relationship.
related_id_field – the identifier field of the related model.
view_kwargs – kwargs from the resource view.
- Returns:
True if relationship have changed else False.
- async delete_object(obj: TypeModel, view_kwargs: dict)
Delete an object through sqlalchemy.
- Parameters:
obj – an item from sqlalchemy.
view_kwargs – kwargs from the resource view.
- async delete_objects(objects: list[TypeModel], view_kwargs: dict)
- async delete_relationship(json_data: dict, relationship_field: str, related_id_field: str, view_kwargs: dict)
Delete a relationship.
- Parameters:
json_data – the request params.
relationship_field – the model attribute used for relationship.
related_id_field – the identifier field of the related model.
view_kwargs – kwargs from the resource view.
- eagerload_includes(qs: QueryStringManager)
Use eagerload feature of sqlalchemy to optimize data retrieval for include querystring parameter.
- Parameters:
qs – a querystring manager to retrieve information from url.
- Returns:
the query with includes eagerloaded.
- async get_collection(qs: QueryStringManager, view_kwargs: dict | None = None, relationship_request_info: RelationshipRequestInfo | None = None) tuple[int, list]
Retrieve a collection of objects through sqlalchemy.
- Parameters:
qs – a querystring manager to retrieve information from url.
view_kwargs – kwargs from the resource view.
relationship_request_info – indicates that method was called in fetch relationship request and contains some related data
- Returns:
the number of object and the list of objects.
- get_fields_options(resource_type: str, qs: QueryStringManager, required_to_load: set | None = None) set
- get_filter_expressions(qs: QueryStringManager) list[BinaryExpression] | None
- static get_load_only_options(resource_type: str, field_names: Iterable[str]) set
- async get_object(view_kwargs: dict, qs: QueryStringManager | None = None, relationship_request_info: RelationshipRequestInfo | None = None) TypeModel
Retrieve an object through sqlalchemy.
- Parameters:
view_kwargs – kwargs from the resource view
qs
relationship_request_info
- Return DeclarativeMeta:
an object from sqlalchemy
Fetch related objects (many)
- Parameters:
related_model
related_id_field
ids
- Returns:
- async get_relationship(relationship_field: str, related_type_: str, related_id_field: str, view_kwargs: dict) tuple[Any, Any]
Get a relationship.
- Parameters:
relationship_field – the model attribute used for relationship.
related_type – the related resource type.
related_id_field – the identifier field of the related model.
view_kwargs – kwargs from the resource view.
- Returns:
the object and related object(s).
- get_relationship_request_filters(model_id_field: InstrumentedAttribute, parent_obj_id: Any, parent_resource_type: str, relationship_name: str) list[BinaryExpression]
- get_sort_expressions(qs: QueryStringManager) list | None
- prepare_id_value(col: InstrumentedAttribute, value: Any) Any
Convert value to the required python type.
Type is declared on the SQLA column.
- Parameters:
col
value
- Returns:
- async update_object(obj: TypeModel, data_update: BaseJSONAPIItemInSchema, view_kwargs: dict) bool
Update an object through sqlalchemy.
- Parameters:
obj – an object from sqlalchemy.
data_update – the data validated by pydantic.
view_kwargs – kwargs from the resource view.
- Returns:
True if object have changed else False.
- async update_relationship(json_data: dict, relationship_field: str, related_id_field: str, view_kwargs: dict) bool
Update a relationship
- Parameters:
json_data – the request params.
relationship_field – the model attribute used for relationship.
related_id_field – the identifier field of the related model.
view_kwargs – kwargs from the resource view.
- Returns:
True if relationship have changed else False.
fastapi_jsonapi.data_layers.sqla.query_building module
Helper to create sqlalchemy filters according to filter querystring parameter
- class fastapi_jsonapi.data_layers.sqla.query_building.RelationshipInfo(*, target_schema: Type[TypeSchema], model: Type[TypeModel], aliased_model: AliasedClass, join_column: InstrumentedAttribute)
Bases:
BaseModel- aliased_model: AliasedClass
- join_column: InstrumentedAttribute
- model: Type[TypeModel]
- model_config: ClassVar[ConfigDict] = {'arbitrary_types_allowed': True}
Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].
- target_schema: Type[TypeSchema]
- class fastapi_jsonapi.data_layers.sqla.query_building.RelationshipInfoStorage
Bases:
object- get_info(resource_type: str, path: str) RelationshipInfo
- has_info(resource_type: str, path: str) bool
- set_info(resource_type: str, path: str, info: RelationshipInfo)
- fastapi_jsonapi.data_layers.sqla.query_building.build_filter_expression(schema_field: FieldInfo, model_column: InstrumentedAttribute, operator: str, value: Any) BinaryExpression
Builds sqlalchemy filter expression, like YourModel.some_field == value
Custom sqlalchemy filtering logic can be created in a schemas field for any operator To implement a new filtering logic (override existing or create a new one) create a method inside a field following this pattern: _<your_op_name>_sql_filter_
- Parameters:
schema_field – schemas field instance
model_column – sqlalchemy column instance
operator – your operator, for example: “eq”, “in”, “ilike_str_array”, …
value – filtering value
- fastapi_jsonapi.data_layers.sqla.query_building.build_filter_expressions(filter_item: dict, target_schema: Type[TypeSchema], target_model: Type[TypeModel], entrypoint_resource_type: str) BinaryExpression
Return sqla expressions.
Builds sqlalchemy expression which can be use in where condition: query(Model).where(build_filter_expressions(…))
- fastapi_jsonapi.data_layers.sqla.query_building.build_sort_expressions(sort_items: list[dict], target_schema: Type[TypeSchema], target_model: Type[TypeModel], entrypoint_resource_type: str)
- fastapi_jsonapi.data_layers.sqla.query_building.build_terminal_node_filter_expressions(filter_item: dict, target_schema: Type[TypeSchema], target_model: Type[TypeModel], entrypoint_resource_type: str)
- fastapi_jsonapi.data_layers.sqla.query_building.cast_value_with_schema(field_types: list[Type], value: Any) tuple[Any, list[str]]
- fastapi_jsonapi.data_layers.sqla.query_building.gather_relationship_paths(item: dict | list[dict]) set[str]
Extracts relationship paths from query filter
- fastapi_jsonapi.data_layers.sqla.query_building.gather_relationships(entrypoint_resource_type: str, entrypoint_model: Type[TypeModel], schema: Type[TypeSchema], relationship_paths: set[str]) set[str]
- fastapi_jsonapi.data_layers.sqla.query_building.gather_relationships_info(model: Type[TypeModel], schema: Type[TypeSchema], entrypoint_resource_type: str, relationship_path: list[str], target_relationship_idx: int = 0, prev_aliased_model: Any | None = None) dict[str, RelationshipInfo]
- fastapi_jsonapi.data_layers.sqla.query_building.get_model_column(model: Type[TypeModel], schema: Type[TypeSchema], field_name: str) InstrumentedAttribute
- fastapi_jsonapi.data_layers.sqla.query_building.get_operator(model_column: InstrumentedAttribute, operator_name: str) str
Get the function operator from his name
- Return callable:
a callable to make operation on a column
- fastapi_jsonapi.data_layers.sqla.query_building.is_filtering_terminal_node(filter_item: dict) bool
If node shape is:
- {
“name: …, “op: …, “val: …,
}
- fastapi_jsonapi.data_layers.sqla.query_building.is_relationship_filter(name: str) bool
- fastapi_jsonapi.data_layers.sqla.query_building.prepare_relationships_info(model: Type[TypeModel], schema: Type[TypeSchema], resource_type: str, filter_info: list, sorting_info: list) set[str]
Return set with request relationship paths in dot separated format.
Gathers information about all relationships involved to request and save them data for skip extra computations for the next time.
- For the filter like this:
- filter_info = [
{“field”: “foo.bar.field_name”, “op”: “eq”, “val”: “”}, {“field”: “baz.field_name”, “op”: “eq”, “val”: “”},
]
- It returns:
(“foo.bar”, “baz”)
fastapi_jsonapi.data_layers.base module
The base class of a data layer.
If you want to create your own data layer you must inherit from this base class
- class fastapi_jsonapi.data_layers.base.BaseDataLayer(request: Request, model: Type[TypeModel], schema: Type[TypeSchema], resource_type: str, url_id_field: str = 'id', disable_collection_count: bool = False, default_collection_count: int = -1, **kwargs)
Bases:
objectBase class of a data layer
- async after_create_object(obj, data, view_kwargs)
Provide additional data after object creation
- Parameters:
obj – an object from data layer
data – the data validated by schemas
view_kwargs – kwargs from the resource view
- async after_create_relationship(obj, updated, json_data, relationship_field, related_id_field, view_kwargs)
Make work after to create a relationship
- Parameters:
obj – an object from data layer
updated (bool) – True if object was updated else False
json_data – the request params
relationship_field (str) – the model attribute used for relationship
related_id_field (str) – the identifier field of the related model
view_kwargs – kwargs from the resource view
- Return boolean:
True if relationship have changed else False
- async after_delete_object(obj: TypeModel, view_kwargs)
Make work after delete object
- Parameters:
obj – an object from data layer
view_kwargs – kwargs from the resource view
- async after_delete_objects(objects: list[TypeModel], view_kwargs: dict)
Any action after deleting objects.
- Parameters:
objects – an object from data layer.
view_kwargs – kwargs from the resource view.
- async after_delete_relationship(obj, updated, json_data, relationship_field, related_id_field, view_kwargs)
Make work after to delete a relationship
- Parameters:
obj – an object from data layer
updated (bool) – True if object was updated else False
json_data – the request params
relationship_field (str) – the model attribute used for relationship
related_id_field (str) – the identifier field of the related model
view_kwargs – kwargs from the resource view
- async after_get_collection(collection, qs, view_kwargs)
Make work after to retrieve a collection of objects
- Parameters:
collection (iterable) – the collection of objects
qs – a querystring manager to retrieve information from url
view_kwargs – kwargs from the resource view
- async after_get_object(obj, view_kwargs)
Make work after to retrieve an object
- Parameters:
obj – an object from data layer
view_kwargs – kwargs from the resource view
- async after_get_relationship(obj, related_objects, relationship_field, related_type_, related_id_field, view_kwargs)
Make work after to get information about a relationship
- Parameters:
obj – an object from data layer
related_objects (iterable) – related objects of the object
relationship_field (str) – the model attribute used for relationship
related_type (str) – the related resource type
related_id_field (str) – the identifier field of the related model
view_kwargs – kwargs from the resource view
- Return tuple:
the object and related object(s)
- async after_update_object(obj: TypeModel, data, view_kwargs)
Make work after update object
- Parameters:
obj – an object from data layer
data – the data validated by schemas
view_kwargs – kwargs from the resource view
- async after_update_relationship(obj, updated, json_data, relationship_field, related_id_field, view_kwargs)
Make work after to update a relationship
- Parameters:
obj – an object from data layer
updated (bool) – True if object was updated else False
json_data – the request params
relationship_field (str) – the model attribute used for relationship
related_id_field (str) – the identifier field of the related model
view_kwargs – kwargs from the resource view
- Return boolean:
True if relationship have changed else False
- async atomic_end(success: bool = True, exception: Exception | None = None)
- async atomic_start(previous_dl: BaseDataLayer | None = None)
- async before_create_object(data, view_kwargs)
Provide additional data before object creation
- Parameters:
data – the data validated by schemas
view_kwargs – kwargs from the resource view
- async before_create_relationship(json_data, relationship_field, related_id_field, view_kwargs)
Make work before to create a relationship
- Parameters:
json_data – the request params
relationship_field (str) – the model attribute used for relationship
related_id_field (str) – the identifier field of the related model
view_kwargs – kwargs from the resource view
- Return boolean:
True if relationship have changed else False
- async before_delete_object(obj: TypeModel, view_kwargs)
Make checks before delete object
- Parameters:
obj – an object from data layer
view_kwargs – kwargs from the resource view
- async before_delete_objects(objects: list[TypeModel], view_kwargs: dict)
Make checks before deleting objects.
- Parameters:
objects – an object from data layer.
view_kwargs – kwargs from the resource view.
- async before_delete_relationship(json_data, relationship_field, related_id_field, view_kwargs)
Make work before to delete a relationship
- Parameters:
json_data – the request params
relationship_field (str) – the model attribute used for relationship
related_id_field (str) – the identifier field of the related model
view_kwargs – kwargs from the resource view
- async before_get_collection(qs, view_kwargs)
Make work before to retrieve a collection of objects
- Parameters:
qs – a querystring manager to retrieve information from url
view_kwargs – kwargs from the resource view
- async before_get_object(view_kwargs)
Make work before to retrieve an object
- Parameters:
view_kwargs – kwargs from the resource view
- async before_get_relationship(relationship_field, related_type_, related_id_field, view_kwargs)
Make work before to get information about a relationship
- Parameters:
relationship_field (str) – the model attribute used for relationship
related_type (str) – the related resource type
related_id_field (str) – the identifier field of the related model
view_kwargs – kwargs from the resource view
- Return tuple:
the object and related object(s)
- async before_update_object(obj, data, view_kwargs)
Make checks or provide additional data before update object
- Parameters:
obj – an object from data layer
data – the data validated by schemas
view_kwargs – kwargs from the resource view
- async before_update_relationship(json_data, relationship_field, related_id_field, view_kwargs)
Make work before to update a relationship
- Parameters:
json_data – the request params
relationship_field (str) – the model attribute used for relationship
related_id_field (str) – the identifier field of the related model
view_kwargs – kwargs from the resource view
- Return boolean:
True if relationship have changed else False
- async create_object(data_create: BaseJSONAPIItemInSchema, view_kwargs: dict) TypeModel
Create an object
- Parameters:
data_create – validated data
view_kwargs – kwargs from the resource view
- Return DeclarativeMeta:
an object
- async create_relationship(json_data, relationship_field, related_id_field, view_kwargs)
Create a relationship
- Parameters:
json_data – the request params
relationship_field (str) – the model attribute used for relationship
related_id_field (str) – the identifier field of the related model
view_kwargs – kwargs from the resource view
- Return boolean:
True if relationship have changed else False
- async delete_object(obj, view_kwargs)
Delete an item through the data layer
- Parameters:
obj (DeclarativeMeta) – an object
view_kwargs – kwargs from the resource view
- async delete_objects(objects: list[TypeModel], view_kwargs)
- async delete_relationship(json_data, relationship_field, related_id_field, view_kwargs)
Delete a relationship
- Parameters:
json_data – the request params
relationship_field (str) – the model attribute used for relationship
related_id_field (str) – the identifier field of the related model
view_kwargs – kwargs from the resource view
- async get_collection(qs: QueryStringManager, view_kwargs: dict | None = None, relationship_request_info: RelationshipRequestInfo | None = None) tuple[int, list]
Retrieve a collection of objects
- Parameters:
qs – a querystring manager to retrieve information from url
view_kwargs – kwargs from the resource view
relationship_request_info
- Return tuple:
the number of object and the list of objects
- async get_object(view_kwargs