Skip to main content

Starlite config plugin with SAQ and SQLAlchemy boilerplate

Project description

starlite-saqlalchemy

TopSport Pulse

PYPI: starlite-saqlalchemy License: MIT Python: supported versions pre-commit.ci status OpenSSF Best Practices Actions: CI

Reliability Rating Quality Gate Status Quality Gate Status Quality Gate Status Quality Gate Status Quality Gate Status Quality Gate Status

Configuration for a Starlite application that features:

  • SQLAlchemy 2.0
  • SAQ async worker
  • Lots of features!

Installation

This will install starlite-saqlalchemy with minimal dependencies.

poetry add starlite-saqlalchemy

You can also install additional dependencies depending on the features you need:

# Repository implementation, DTOs
poetry add starlite-saqlalchemy[sqlalchemy]
# Async worker using saq
poetry add starlite-saqlalchemy[worker]
# Redis cache backend
poetry add starlite-saqlalchemy[cache]
# Sentry integration for starlite
poetry add starlite-saqlalchemy[sentry]

# or to install them all:
poetry add starlite-saqlalchemy[all]

Example

from starlite import Starlite, get

from starlite_saqlalchemy import ConfigureApp


@get("/example")
def example_handler() -> dict:
    """Hello, world!"""
    return {"hello": "world"}


app = Starlite(route_handlers=[example_handler], on_app_init=[ConfigureApp()])

Features

The application configured in the above example includes the following configuration.

Logging after exception handler

Receives and logs any unhandled exceptions raised out of route handling.

Redis cache

Integrates a Redis cache backend with Starlite first-class cache support.

Collection route filters

Support filtering collection routes by created and updated timestamps, list of ids, and limit/offset pagination.

Includes an aggregate filters dependency to easily inject all filters into a route handler, e.g,:

from starlite import get
from starlite_saqlalchemy.dependencies import FilterTypes


@get()
async def get_collection(filters: list[FilterTypes]) -> list[...]:
    ...

Gzip compression

Configures Starlite's built-in Gzip compression support.

Exception handlers

Exception handlers that translate non-Starlite repository and service object exception types into Starlite's HTTP exceptions.

Health check

A health check route handler that returns some basic application info.

Logging

Configures logging for the application including:

  • Queue listener handler, appropriate for asyncio applications
  • Health check route filter so that health check requests don't clog your logs
  • An informative log format
  • Configuration for dependency logs

Openapi config

Configures OpenAPI docs for the application, including config by environment to allow for easy personalization per application.

Starlite Response class

A response class that can handle serialization of SQLAlchemy/Postgres UUID types.

Sentry configuration

Just supply the DSN via environment, and Sentry is configured for you.

SQLAlchemy

Engine, logging, pooling etc all configurable via environment. We configure starlite and include a custom before_send wrapper that inspects the outgoing status code to determine whether the transaction that represents the request should be committed, or rolled back.

Async SAQ worker config

A customized SAQ queue and worker that is started and shutdown using the Starlite lifecycle event hooks - no need to run your worker in another process, we attach it to the same event loop as the Starlite app uses. Be careful not to do things in workers that will block the loop!

Extra Features

In addition to application config, the library include:

Repository

An abstract repository object type and a SQLAlchemy repository implementation.

DTO Factory

A factory for building pydantic models from SQLAlchemy 2.0 style declarative classes. Use these to annotate the data parameter and return type of routes to control the data that can be modified per route, and the information included in route responses.

HTTP Client and Endpoint decorator

http.Client is a wrapper around httpx.AsyncClient with some extra features including unwrapping enveloped data, and closing the underlying client during shutdown of the Starlite application.

ORM Configuration

A SQLAlchemy declarative base class that includes:

  • a mapping of the builtin UUID type to the postgresql dialect UUID type.
  • an id column
  • a created timestamp column
  • an updated timestamp column
  • an automated __tablename__ attribute
  • a from_dto() class method, to ease construction of model types from DTO objects.

We also add:

  • a before_flush event listener that ensures that the updated timestamp is touched on instances on their way into the database.
  • a constraint naming convention so that index and constraint names are automatically generated.

Service object

A Service object that integrates with the Repository ABC and provides standard logic for typical operations.

Settings

Configuration by environment.

Contributing

All contributions big or small are welcome and appreciated! Please check out CONTRIBUTING.md for specific information about configuring your environment and workflows used by this project.

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

starlite_saqlalchemy-0.30.0.tar.gz (46.2 kB view details)

Uploaded Source

Built Distribution

starlite_saqlalchemy-0.30.0-py3-none-any.whl (60.7 kB view details)

Uploaded Python 3

File details

Details for the file starlite_saqlalchemy-0.30.0.tar.gz.

File metadata

  • Download URL: starlite_saqlalchemy-0.30.0.tar.gz
  • Upload date:
  • Size: 46.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.9.6 readme-renderer/37.3 requests/2.28.2 requests-toolbelt/0.10.1 urllib3/1.26.14 tqdm/4.65.0 importlib-metadata/6.0.0 keyring/23.13.1 rfc3986/2.0.0 colorama/0.4.6 CPython/3.11.2

File hashes

Hashes for starlite_saqlalchemy-0.30.0.tar.gz
Algorithm Hash digest
SHA256 1768015734ac9e155c023ad00b58c5cf588b8ca31aaaadd6f558408a36787532
MD5 806f0c4a20d08267614717f23db9f0d2
BLAKE2b-256 c7f1f2ec272c39789c2d01bae17f58e836abedba1f82fb64cc7dfaa0369a3cb5

See more details on using hashes here.

File details

Details for the file starlite_saqlalchemy-0.30.0-py3-none-any.whl.

File metadata

  • Download URL: starlite_saqlalchemy-0.30.0-py3-none-any.whl
  • Upload date:
  • Size: 60.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.9.6 readme-renderer/37.3 requests/2.28.2 requests-toolbelt/0.10.1 urllib3/1.26.14 tqdm/4.65.0 importlib-metadata/6.0.0 keyring/23.13.1 rfc3986/2.0.0 colorama/0.4.6 CPython/3.11.2

File hashes

Hashes for starlite_saqlalchemy-0.30.0-py3-none-any.whl
Algorithm Hash digest
SHA256 ca826dab832863674d6ab192c355ba62864336cfbbee90c7b8cbaba275db00f6
MD5 a8c99f1bbd22163cd55b55b31cdab16f
BLAKE2b-256 ad9b2930a0e974023a68fe0d9acf570baf093683d83ffa0a2aa9663750573be6

See more details on using hashes here.

Supported by

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