Data layer

The data layer is a CRUD interface between resource manager and data. It is a very flexible system to use any ORM or data storage. You can even create a data layer that uses multiple ORMs and data storage to manage your own objects. The data layer implements a CRUD interface for objects and relationships. It also manages pagination, filtering and sorting.

FastAPI-JSONAPI has a full-featured data layer that uses the popular ORM SQLAlchemy.

To configure the data layer you have to set its required parameters in the resource manager.

Example:

from fastapi import FastAPI

from fastapi_jsonapi import RoutersJSONAPI
from fastapi_jsonapi.data_layers.base import BaseDataLayer
from fastapi_jsonapi.data_layers.sqla_orm import SqlalchemyDataLayer
from fastapi_jsonapi.views.detail_view import DetailViewBase
from fastapi_jsonapi.views.list_view import ListViewBase


class MyCustomDataLayer(BaseDataLayer):
    """Overload abstract methods here"""

    ...


class MyCustomSqlaDataLayer(SqlalchemyDataLayer):
    """Overload any methods here"""

    async def before_delete_objects(self, objects: list, view_kwargs: dict):
        raise Exception("not allowed to delete objects")


class UserDetailView(DetailViewBase):
    data_layer_cls = MyCustomDataLayer


class UserListView(ListViewBase):
    data_layer_cls = MyCustomSqlaDataLayer


app = FastAPI()
RoutersJSONAPI(
    app,
    # ...
    class_detail=UserDetailView,
    class_list=UserListView,
    # ...
)