Skip to main content

Compile constrained Pydantic models into SQLAlchemy WHERE-rule dictionaries.

Project description

SQLRules

Compile constrained Pydantic models into SQLAlchemy WHERE-rule dictionaries.

One package. One job. Deterministic output. Zero database dependency.

from typing import Annotated
from pydantic import BaseModel, Field
from sqlalchemy import Table, Column, Integer, String, MetaData

import sqlrules

metadata = MetaData()

users = Table(
    "users",
    metadata,
    Column("age", Integer),
    Column("name", String),
)

class UserFilter(BaseModel):
    age: Annotated[int, Field(ge=18, le=65)]
    name: Annotated[str, Field(min_length=2)]

rules = sqlrules.compile(UserFilter, users)
# {
#     "age": [users.c.age >= 18, users.c.age <= 65],
#     "name": [func.length(users.c.name) >= 2],
# }

stmt = users.select().where(*sqlrules.where(rules))

Install

pip install sqlrules

Requires Python 3.10+, Pydantic v2, and SQLAlchemy 2.x.

Supported constraints (0.2)

Constraint SQLAlchemy expression
gt / ge / lt / le column > / >= / < / <= value
multiple_of column % value == 0
min_length / max_length func.length(column) >= / <= value
Literal[...] column.in_(...)
Enum column.in_(...)

pattern is extracted into IR but has no portable core translator.

Unsupported constraints raise UnsupportedConstraintError by default. Use on_unsupported="warn" or "ignore" to change that policy for unknown constraint operators (unsupported types always raise).

Public API

sqlrules.compile(model, table, *, column_map=None, on_unsupported="raise", cache=True)
sqlrules.where(rules)    # flatten all expressions
sqlrules.flatten(rules)  # alias of where()

Non-goals

SQLRules is not an ORM, validator, query builder, SQL string generator, migration tool, or database client. It only compiles supported Pydantic constraints into SQLAlchemy expressions.

Documentation

See docs/index.md for the full documentation set, including the spec, API, architecture, and roadmap.

Development

pip install -e ".[dev]"
pytest
ruff check .
mypy src/sqlrules
python -m benchmarks.bench_compile

License

MIT

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

sqlrules-0.2.0.tar.gz (37.8 kB view details)

Uploaded Source

Built Distribution

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

sqlrules-0.2.0-py3-none-any.whl (13.9 kB view details)

Uploaded Python 3

File details

Details for the file sqlrules-0.2.0.tar.gz.

File metadata

  • Download URL: sqlrules-0.2.0.tar.gz
  • Upload date:
  • Size: 37.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for sqlrules-0.2.0.tar.gz
Algorithm Hash digest
SHA256 4a1c1057e239af80324203080efd271128688761b8e751705ac55891f9e4d2fd
MD5 ad0ca931bab57d67d2f1be3342615518
BLAKE2b-256 8f8490f687f388fd8671e5873d66b3822295b5e71d1c745942b01847fed88131

See more details on using hashes here.

File details

Details for the file sqlrules-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: sqlrules-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 13.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for sqlrules-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 e4a96d784295e57f74a7de98195a7170a0a1f1235afae328f3e7a2dca44b568d
MD5 71ea4ea3d734112d5eb1d8b8965af8b6
BLAKE2b-256 d85beefb45ee711aba95f45fb6d44bd94328ceb0138af41cf9a7ba1493e85b56

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