Skip to main content

A highly opinionated framework based on FastAPI and Sqlalchemy.

Project description

Brickworks

Brickworks is a modular, "batteries included" backend Framework, build on FastAPI and SQLAlchemy.

Key features:

  • Modular and fully async, build on FastAPI
  • Multi-tenancy with schema separation
  • Policy Based Access Control (PBAC)
  • Role Based Access Control (RBAC)
  • Build in CLI for db migrations and common tasks

WARNING!!! Brickworks is currently in a very early development stage - expect things to change a lot. (You probably shouldn't use this yet.)

Basic usage

Brickworks requires your project to have a pyproject.yaml.

It is recommended to use a dependency management tool like Poetry or uv, which will create the pyproject.yaml for you and help you manage your projects dependencies.

Installation

uv init
uv add brickworks
uv pip install -e .

Create your first brick

Brickworks allows you to build your application out of individual Python modules called bricks. Each brick should encapsulate a specific functionality, making the codebase easier to understand, maintain and reuse.

To create a brick you can use the command line tool that comes with Brickworks: Mason.

mason brick create mybrick --namespace app
uv pip install -e

This will create the app package, containing the mybrick module and register your new brick in the pyproject.toml.

Add your first route

Brickworks is based on FastAPI, so routes are working the same way they would in FastAPI. Add routes to the app.mybrick.routes module...

from fastapi import APIRouter

r = APIRouter(prefix="/myroutes")

@r.get("/")
async def hello_world():
    return "Hello World"

... and register them in the brick.json

{
  "routers": ["app.mybrick.routers.r"],
  "middlewares": [],
  "loadme": []
}

Your first Database Model

Brickworks uses the ORM (Object Relational Mapper) from SQLAlchemy. All your models should inherit from the BaseDBModel class, which is an SQLAlchemy model class. This base class provides common fields and functionality that are useful for most applications, such as access control, attaching files, creating views and more!

from sqlalchemy import String, Integer
from sqlalchemy.orm import Mapped, mapped_column
from brickworks.core.models.base_dbmodel import BaseDBModel

class BookModel(BaseDBModel):
    __tablename__ = "mymodule_books"
    title: Mapped[str] = mapped_column(String(255), nullable=False)
    author: Mapped[str] = mapped_column(String(255), nullable=False)
    year_published: Mapped[int] = mapped_column(Integer, nullable=True)

# create a new book
book = await BookModel(
    title="The Hobbit", author="J.R.R. Tolkien", year_published=1937
    ).persist()

# get books by author
books = await BookModel.get_list(author="J.R.R. Tolkien")

To run database migrations you can just use Mason:

mason db upgrade

Which will create migration files using alembic and executes the migration.

Policy Based Access Control (PBAC)

Policies are classes that define rules for accessing or filtering resources. Policies are only applied if you access the models with the methods get_one_or_none_with_policies() or get_list_with_policies().

from brickworks.core.acl.base_policy import BasePolicy, PolicyTypeEnum
from apps.mybrick.models import BookModel

class NoTolkienBooksPolicy(BasePolicy):
    policy_type = PolicyTypeEnum.RESTRICTIVE

    async def filter(self, user_uuid: str | None, obj_class: type[BookModel]):
        # Prevent access to books authored by J.R.R. Tolkien
        return obj_class.author.notilike("J.R.R. Tolkien")


BookModel.__policies__.append(NoTolkienBooksPolicy())

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

brickworks-0.0.2.tar.gz (150.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

brickworks-0.0.2-py3-none-any.whl (47.4 kB view details)

Uploaded Python 3

File details

Details for the file brickworks-0.0.2.tar.gz.

File metadata

  • Download URL: brickworks-0.0.2.tar.gz
  • Upload date:
  • Size: 150.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.7.8

File hashes

Hashes for brickworks-0.0.2.tar.gz
Algorithm Hash digest
SHA256 3429b4d87617b2908e569e33a629302ef116e8fe2a03faa7179694930b9125ea
MD5 1c4827439d60d5bbdec9c4d48c3b7b79
BLAKE2b-256 dcfcd0f6838bcaf762da50cdedf1d7197a4a045db4af53e7b4f59bfc797ebc3f

See more details on using hashes here.

File details

Details for the file brickworks-0.0.2-py3-none-any.whl.

File metadata

  • Download URL: brickworks-0.0.2-py3-none-any.whl
  • Upload date:
  • Size: 47.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.7.8

File hashes

Hashes for brickworks-0.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 74eb75ae61a746083fd6c65a7c1cb5ae5799f6212f02a078defdcab139f40c36
MD5 4783fad257592c8c4baa2f07c3d76d46
BLAKE2b-256 747bf5a455f9e7489fe55b574b8880f1ad4a99c934dc47804a2becee6f564d55

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page