Skip to main content

sqlphilosophy

Current source and planned customer-subdomain impact: current-state summary.

Portable SQLAlchemy repository kit: sync and async CRUD, fluent statement builders, sort/pagination, Core SQL helpers, and optional audit listeners.

PyPI sqlphilosophy
GitHub SignalSafeSoftware/sqlphilosophy
Import sqlphilosophy (__version__ only) — use explicit submodules for APIs
Python 3.12+
License MIT — see LICENSE

Documentation

Resource Description
Repository guide Entry point: overview, transaction model, links to usage pages
Usage examples Focused sync/async code examples by feature area
Strongly typed repositories Typed subclasses, factories, protocols, and service patterns
Service factory composition Compose sqlPhilosophy SQL repos with servicePhilosophy business services
Before/after SQLAlchemy Migration examples: direct SQLAlchemy vs repository-centered code
Feature matrix Full sync/async capability map
Typed repository (sync) Runnable factory + domain repo example
Typed repository (async) Async counterpart

What this package does

  • Repository pattern for a single mapped model (BaseRepository, AsyncBaseRepository).
  • Fluent query builders with pagination/sort (StatementQueryBuilder, ListQuery, SortConfig).
  • SQL helpers for row mapping, partial updates, and developer-defined fragments via sqlphilosophy.trusted_sql.
  • Opt-in strict row decoders via sqlphilosophy.strict for non-coercing scalars, timestamps, enums, collections, UUIDs, and recursive JSON.
  • Optional audit listeners and timestamp mixins.

What this package does not do

  • Migrations, schema design, or connection pooling configuration.
  • Authorization, multi-tenant isolation, or query sandboxing.
  • Automatic commits for normal CRUD — see Transaction ownership.

Install

pip install sqlphilosophy

Async ORM (AsyncSession) also needs greenlet:

pip install sqlphilosophy[async]

Requires Python 3.12+ and SQLAlchemy 2.x.

Quick start (sync)

from sqlalchemy import String, create_engine
from sqlalchemy.orm import DeclarativeBase, Mapped, Session, mapped_column, sessionmaker

from sqlphilosophy.sorting import ListQuery
from sqlphilosophy.sync.repository import BaseRepository


class Base(DeclarativeBase):
    pass


class Widget(Base):
    __tablename__ = "widget"
    id: Mapped[int] = mapped_column(primary_key=True)
    name: Mapped[str] = mapped_column(String(64))


engine = create_engine("sqlite:///:memory:", future=True)
Base.metadata.create_all(engine)
SessionLocal = sessionmaker(bind=engine, expire_on_commit=False)

with SessionLocal() as session:
    repo = BaseRepository(Widget, session)
    repo.create(name="alpha")  # stages + flush; does not commit
    session.commit()

    rows, total = repo.statement().fetch_page(ListQuery.from_page(page=1, size=20))
    assert total >= 1

Async: use AsyncSession, AsyncBaseRepository from sqlphilosophy.aio.repository, and await on repository/builder terminals. See the repository guide.

Package layout

Module Contents
sqlphilosophy __version__ only
sqlphilosophy.types Portable typing aliases
sqlphilosophy.sql Row mapping, partial updates, Core helpers (re-exports trusted_sql)
sqlphilosophy.strict Opt-in non-coercing row decoders and strict JSON validation
sqlphilosophy.trusted_sql Developer-trusted SQL fragments — see SECURITY.md
sqlphilosophy.sorting ListQuery, SortConfig, SortSpec
sqlphilosophy.sync / sqlphilosophy.aio Repositories, query builders, factory protocols
sqlphilosophy.audit Optional listeners and timestamp mixins

Transaction ownership

  • create / add / update_partial / remove / delete_* / update_where — flush or execute DML; caller commits.
  • delete_all() — bulk delete; does not commit.
  • batched_purge_ids(..., batch_size=...) — deletes in batches and commits after each batch; requires batch_size >= 1. Authorize in application code first.

Raw SQL trust boundaries

Identifiers and SQL fragments (table/column names, ORDER BY text, sort allowlists) must be developer-defined, never built from end-user input. User values use bind parameters.

Import trusted helpers from sqlphilosophy.trusted_sql (sql_table, col_eq, col_icontains, col_range, literal_order_expr, …). The same names are re-exported from sqlphilosophy.sql. Details: SECURITY.md and the repository guide.

Development

This repo uses uv and Ruff:

uv sync --extra dev
uv run pytest
uv run ruff check src tests docs/examples
uv run ruff format src tests docs/examples

# Optional: validate runnable docs examples (SQLite in-memory; CI runs these in smoke-package)
uv run --extra dev python docs/examples/typed_repository_sync.py
uv run --extra dev python docs/examples/typed_repository_async.py

uv run python -m build

Security

See SECURITY.md for vulnerability reporting and SQL trust boundaries.

Releasing

See RELEASING.md for GitHub + PyPI trusted publishing. See CHANGELOG.md.

License

MIT — see LICENSE.

Expression lookups and scope overrides (0.3.1)

Both repository bases provide find_entity(criteria=..., lock=False), find_row(criteria=..., columns=..., lock=False) and count_scoped(criteria=...). Criteria must not be empty; projected lookups also require columns. Entity lookups return a mapped object, projected lookups return RowMapping, and absence returns None. These methods never commit.

Override apply_scope(statement) for additional predicates on these helpers. It runs after projection initialization, because select_columns/select_entity reset the builder's SELECT. This hook does not implicitly scope existing CRUD helpers or arbitrary builder calls: keep ownership checks in named application repository methods. Existing first, get_by_id, count and statement APIs remain available and unchanged.

The 0.3.1 artifact is a local candidate until separately published. Dependent candidate packages use a vendored wheel during validation; publish sqlPhilosophy first before replacing their local sources with registry-only dependencies.

Metadata

Release files for sqlphilosophy 0.3.3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for sqlphilosophy 0.3.3
File Size Uploaded Provenance
sqlphilosophy-0.3.3.tar.gz 31.4 kB With provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for sqlphilosophy 0.3.3
File Interpreter ABI Platform
sqlphilosophy-0.3.3-py3-none-any.whl Python 3 none any Details

Total release size: 67.0 kB

Release files / sqlphilosophy-0.3.3.tar.gz

Download URL sqlphilosophy-0.3.3.tar.gz
Size 31.4 kB
Tags Source
SHA-256 checksum
How to use checksums
b5b23bd414028aa2e7be9d3fb73ae21916689c175eb9732cad896f3191575d66
BLAKE2b-256 checksum
How to use checksums
8139d10847903cb245a020b57739bb73fcebdbbc6e60570e6fe4fd01e00117b7
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 21, 2026.

Transparency log

Release files / sqlphilosophy-0.3.3-py3-none-any.whl

Download URL sqlphilosophy-0.3.3-py3-none-any.whl
Size 35.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
b14b54d66b47c116ecaff41522c4305ff2c20d48cd25ef42a5615a304a2d4e50
BLAKE2b-256 checksum
How to use checksums
d90567a90da058356e55bc508385d768bc9d8d1b4aa0d70262ebeca91adb8eac
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 21, 2026.

Transparency log

Security for sqlphilosophy 0.3.3

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

All 2 files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for sqlphilosophy-0.3.3.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 21, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page