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 ApplicationBuilder
from fastapi_jsonapi.data_layers.base import BaseDataLayer
from fastapi_jsonapi.data_layers.sqla.orm import SqlalchemyDataLayer
from fastapi_jsonapi.views import ViewBase


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 UserView(ViewBase):
    data_layer_cls = MyCustomDataLayer


app = FastAPI()
builder = ApplicationBuilder(app)
builder.add_resource(
    # ...
    view=UserView,
    # ...
)