Backends

Connecting models to their data since 2020!.

Overview

The backend system acts as a flexible layer between models and their data sources. By changing the backend attached to a model, you change where the model fetches and saves data. This might be a database, an in-memory data store, a dynamodb table, an API, and more. This allows you to interact with a variety of data sources with the models acting as a standardized API. Since endpoints also rely on the models for their functionality, this means that you can easily build API endpoints and more for a variety of data sources with a minimal amount of code.

Of course, not all data sources support all functionality present in the model. Therefore, you do still need to have a fair understanding of how your data sources work.

Permissions

Backends support permission flags that control what operations are allowed. These can be set as class attributes or passed to the constructor:

  • can_create: Whether creating new records is allowed (default: True)
  • can_update: Whether updating existing records is allowed (default: True)
  • can_delete: Whether deleting records is allowed (default: True)
  • can_query: Whether querying/reading records is allowed (default: True)

When an operation is attempted that is not allowed, a ValueError will be raised with a descriptive message.

Example of restricting a backend to read-only by subclassing:

import clearskies
from clearskies.configs import Boolean


class ReadOnlyMemoryBackend(clearskies.backends.MemoryBackend):
    can_create = Boolean(default=False)
    can_update = Boolean(default=False)
    can_delete = Boolean(default=False)


class User(clearskies.Model):
    id_column_name = "id"
    backend = ReadOnlyMemoryBackend()

    id = clearskies.columns.Uuid()
    name = clearskies.columns.String()

Table of contents