10BC0 GitHub - Jamie-Chang/sqlalchemy-builder: Build SQLAlchemy statements avoiding common footguns
[go: up one dir, main page]

Skip to content

Jamie-Chang/sqlalchemy-builder

Repository files navigation

sqlalchemy-builder

PyPI version pydantic.run

Build SQLAlchemy statements avoiding common footguns.

Check out the documentation

Example

Build select statements functionally using | and |=

select(Model).where(Model.id == 1)

Becomes:

from sqlalchemy_builder.select import where

select(Model) | where(Model.id == 1)

|= is also supported:

statement = select(Model)
if value:
    statement |= where(Model.value == value)

Installation

$ uv add sqlalchemy-builder

or

$ pip install sqlalchemy-builder

Why?

There is a common mistake when building sqlalchemy statements.

statement = select(Model)

if status is not None:
    statement.where(Model.status == status)

The statement.where does not do anything as it generates a new Select object.

To fix this we have to assign it:

statement = statement.where(...)

This can be a bit verbose though not the worst issue. By using implace |= we can avoid this issue without making the statement mutable.

It's also just a fun syntax to play with.

Development

Installing dependencies:

$ uv sync 

Running tests:

$ uv run pytest

About

Build SQLAlchemy statements avoiding common footguns

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

0