Skip to main content

You deserve a...

Project description

QuickRest

A schema-first CRUD-generation framework for FastAPI and SQLAlchemy so your team can put your feet up (or get back to the interesting stuff).

License Coverage

Full Documentation: lucaskruitwagen.github.io/quickrest

Issues and Feature Requests: github.com/Lkruitwagen/quickrest/issues

Features

  • Mixins classes for SQLAlchemy declarative ORM classes that automatically build create, read, update, delete, and search (CRUD+Search) FastAPI endpoints and Pydantic models.
  • Full exposure of joined tables, either via serialized related objects or additional GET <resource>/<id>/<relationship> routes
  • Route-level protections via standard dependency injection
  • Row-level fine-grained access control via developer-defineable user generation
  • Many additional configuration options available via <Method>Config classes

See docs for more.

Installation

QuickRest is available from the python package index: pip install quickrest.

Quickstart

Simple mixin the Resource class with your ORM classes and you're good to go. Define some environment variables to set the default sessionmaker and user generator for the Resource class ()or use the build_resource method. Mixin the User, Private (via make_private), and Publishable (via make_publishabe) classes to add fine-grained access control.

Check the docs for the full range of customisation options.

from datetime import date
from typing import Optional

import uvicorn
from fastapi import FastAPI
from sqlalchemy import ForeignKey
from sqlalchemy.orm import Mapped, mapped_column, relationship

from quickrest import (
    Base,
    ReadConfig,
    ResourceConfig,
    RouterFactory,
    SearchConfig,
    Resource,
)


class Owner(
    Base,
    Resource,
):
    __tablename__ = "owners"
    first_name: Mapped[str] = mapped_column()
    last_name: Mapped[str] = mapped_column()

    pets: Mapped[list["Pet"]] = relationship(back_populates="owner")

    certifications: Mapped[list["Certification"]] = relationship(
        secondary="owner_certifications",
    )

    class resource_cfg(ResourceConfig):
        serialize = ["certifications"]

    class read_cfg(ReadConfig):
        # choose which relationships should be accessible via URL /<resource>/<id>/<relationship>
        routed_relationships = ["pets"]


class Specie(
    Base,
    Resource,
):
    __tablename__ = "species"

    common_name: Mapped[str] = mapped_column()
    scientific_name: Mapped[str] = mapped_column()


class Pet(Base, Resource):
    __tablename__ = "pets"
    # note: all Resource classes have an id and slug column by default
    name: Mapped[str] = mapped_column()
    vaccination_date: Mapped[Optional[date]] = mapped_column(nullable=True)

    species_id: Mapped[int] = mapped_column(ForeignKey("species.id"))
    owner_id: Mapped[int] = mapped_column(ForeignKey("owners.id"))

    owner: Mapped["Owner"] = relationship()
    specie: Mapped["Specie"] = relationship()
    notes: Mapped[list["Note"]] = relationship()

    class resource_cfg(ResourceConfig):
        # choose which relationships should be serialized on the reponse
        serialize = ["specie"]

    class search_cfg(SearchConfig):
        search_gte = ["vaccination_date"]  # greater than or equal to, list[str] | bool
        search_lt = ["vaccination_date"]  # less than, list[str] | bool


class Note(Base, Resource):
    __tablename__ = "notes"

    text: Mapped[str] = mapped_column()
    pet_id: Mapped[int] = mapped_column(ForeignKey("pets.id"))


class Certification(
    Base,
    Resource,
):
    __tablename__ = "certifications"
    # note: all Resource classes have an id and slug column by default
    name: Mapped[str] = mapped_column()
    description: Mapped[str] = mapped_column()


class OwnerCertifications(Base):
    __tablename__ = "owner_certifications"
    owner_id: Mapped[int] = mapped_column(ForeignKey("owners.id"), primary_key=True)
    certification_id: Mapped[int] = mapped_column(
        ForeignKey("certifications.id"), primary_key=True
    )


# instantiate a FastAPI app
app = FastAPI(title="QuickRest Quickstart", separate_input_output_schemas=False)

# build create, read, update, delete routers for each resource and add them to the app
RouterFactory.mount(app, [Owner, Pet, Specie, Note, Certification])

if __name__ == "__main__":
    # Base.metadata.create_all(Resource._sessionmaker.kw.get("bind"))  # uncomment this line to create the tables in db backend
    uvicorn.run(app, host="0.0.0.0", port=8000)

Contributing

Contributions welcome!

To set up for development simply clone the repo and then:

pip install -e .[dev]

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

fastapi_quickrest-0.0.0.tar.gz (27.6 kB view details)

Uploaded Source

Built Distribution

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

fastapi_quickrest-0.0.0-py3-none-any.whl (39.5 kB view details)

Uploaded Python 3

File details

Details for the file fastapi_quickrest-0.0.0.tar.gz.

File metadata

  • Download URL: fastapi_quickrest-0.0.0.tar.gz
  • Upload date:
  • Size: 27.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.0.1 CPython/3.12.8

File hashes

Hashes for fastapi_quickrest-0.0.0.tar.gz
Algorithm Hash digest
SHA256 50dedfd8ce90c7e18b0c279f7f26886824254af0fdf018cc76b678b076c980fe
MD5 a8409ae22437439c896b6c91f270584e
BLAKE2b-256 e15f2cd7aa4da9abf0eba38a2932e79dd897909bdeca91b5f30e6dded4c51926

See more details on using hashes here.

Provenance

The following attestation bundles were made for fastapi_quickrest-0.0.0.tar.gz:

Publisher: release-pypi.yaml on Lkruitwagen/quickrest

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file fastapi_quickrest-0.0.0-py3-none-any.whl.

File metadata

File hashes

Hashes for fastapi_quickrest-0.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 f6927d0eb69746b328f1038230a26953575d2503afc1d9333562f9669c4c003b
MD5 f0d894baff927f6ebe9f91529461aa1e
BLAKE2b-256 c70760b4450050629fd54c68049dd93be7895f79c942bdad3e92062ed1c4a3d2

See more details on using hashes here.

Provenance

The following attestation bundles were made for fastapi_quickrest-0.0.0-py3-none-any.whl:

Publisher: release-pypi.yaml on Lkruitwagen/quickrest

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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