Skip to main content

SQLAlchemy async backend for varco — runtime ORM generation, repository, schema guard, and Alembic helpers

Project description

varco-sa

PyPI version Python License: Apache 2.0 GitHub

SQLAlchemy async backend for varco.

Generates SQLAlchemy ORM classes at runtime from your DomainModel subclasses — no hand-written ORM models needed. Requires varco-core.


Install

pip install varco-sa

# With PostgreSQL async driver:
pip install "varco-sa[postgresql]"

# With SQLite (tests / local dev):
pip install "varco-sa[sqlite]"

Features

  • Zero-boilerplate ORMSAModelFactory generates DeclarativeBase subclasses at runtime; no duplication between domain and ORM layers
  • Full async repositoryAsyncSQLAlchemyRepository implements AsyncRepository (CRUD, exists(), stream_by_query() with server-side cursor)
  • Unit of WorkSQLAlchemyUnitOfWork manages AsyncSession lifecycle and atomic commits
  • One-liner bootstrapSAFastrestApp + SAConfig wire engine, session factory, ORM generation, and table creation
  • Alembic integrationget_target_metadata() and print_create_ddl() helpers for migration scripts
  • Schema GuardSchemaGuard detects drift between ORM metadata and the live database schema
  • Query integration — accepts varco-core QueryParams / QueryBuilder AST natively; translates to SQLAlchemy where() clauses

What's in the package

Module Purpose
factory.py SAModelFactory — generates DeclarativeBase subclasses at runtime; SAModelRegistry — escape hatch
repository.py AsyncSQLAlchemyRepositoryAsyncSession-backed CRUD + exists() + stream_by_query()
uow.py SQLAlchemyUnitOfWork — session lifecycle + atomic commits
provider.py SQLAlchemyRepositoryProvider — wires factory + repos + UoW
bootstrap.py SAConfig, SAFastrestApp — one-liner app setup
alembic_helpers.py get_target_metadata, print_create_ddl — Alembic integration
schema_guard.py SchemaGuard — drift detection between ORM metadata and live DB

Quick start

Bootstrap (one-liner)

from sqlalchemy.ext.asyncio import create_async_engine
from sqlalchemy.orm import DeclarativeBase
from varco_sa import SAConfig, SAFastrestApp

class Base(DeclarativeBase): pass

engine = create_async_engine("postgresql+asyncpg://user:pass@localhost/mydb")

app = SAFastrestApp(SAConfig(
    engine=engine,
    base=Base,
    entity_classes=(User, Post),
))

await app.create_all()              # CREATE TABLE IF NOT EXISTS ...
uow_provider = app.uow_provider     # ready to inject as IUoWProvider

Manual setup

from sqlalchemy.ext.asyncio import async_sessionmaker
from varco_sa import SQLAlchemyRepositoryProvider

sessions = async_sessionmaker(engine, expire_on_commit=False)
provider = SQLAlchemyRepositoryProvider(engine=engine, session_factory=sessions)
provider.register(User, Post)
await provider.create_all()

async with provider.make_uow() as uow:
    user = await uow.users.save(User(name="Edo", email="edo@example.com"))
    print(user.pk)

Query integration

from varco_core import QueryBuilder, QueryParams

async with provider.make_uow() as uow:
    # exists() — uses SA identity-map cache, no full ORM load when cached
    if await uow.posts.exists(post_id):
        ...

    # stream_by_query() — server-side cursor, constant memory regardless of result size
    params = QueryParams(node=QueryBuilder().eq("active", True).build())
    async for post in uow.posts.stream_by_query(params):
        await process(post)

Alembic integration

# alembic/env.py
from varco_sa import get_target_metadata
from myapp.models import User, Post

target_metadata = get_target_metadata(User, Post)

Preview the DDL before running a migration:

from varco_sa import print_create_ddl

print(print_create_ddl(User, Post, dialect="postgresql"))

Schema Guard — detect drift

from varco_sa import SchemaGuard

guard = SchemaGuard(engine, User, Post)
differences = await guard.check()
if differences:
    print("Schema drift detected:", differences)

Access the generated SA model (escape hatch)

from varco_sa import SAModelRegistry
from sqlalchemy.orm import relationship

UserORM = SAModelRegistry.get(User)
UserORM.posts = relationship("PostORM", back_populates="author")

Related packages

Package Description
varco-core Domain model, service layer, query AST, JWT — required dependency
varco-beanie Beanie / Motor MongoDB backend (alternative to this package)

Links

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

varco_sa-0.0.2.tar.gz (67.0 kB view details)

Uploaded Source

Built Distribution

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

varco_sa-0.0.2-py3-none-any.whl (29.9 kB view details)

Uploaded Python 3

File details

Details for the file varco_sa-0.0.2.tar.gz.

File metadata

  • Download URL: varco_sa-0.0.2.tar.gz
  • Upload date:
  • Size: 67.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.10.12 {"installer":{"name":"uv","version":"0.10.12","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Debian GNU/Linux","version":"12","id":"bookworm","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for varco_sa-0.0.2.tar.gz
Algorithm Hash digest
SHA256 f50c41e191a00c115f9b53b478c28347dce7ffa1f483df6d1ede702c4981ed5e
MD5 d48444b8f448f7894d526c3de865cbf7
BLAKE2b-256 1b0b3bf0e6a071d267373a4edb8875cadcde53f6737c0e279cd482b58ded61f2

See more details on using hashes here.

File details

Details for the file varco_sa-0.0.2-py3-none-any.whl.

File metadata

  • Download URL: varco_sa-0.0.2-py3-none-any.whl
  • Upload date:
  • Size: 29.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.10.12 {"installer":{"name":"uv","version":"0.10.12","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Debian GNU/Linux","version":"12","id":"bookworm","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for varco_sa-0.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 131e2f6194ce3f38b12c3fc5cef5a7888f3e5111e9bbf25c436b20e91f0464b6
MD5 c77d2e11e70d32a6d3f03b23a3c372ad
BLAKE2b-256 242a95ba9d940ce88edbdee2d76edb7c458cbd702d1c63c54b15bb49ef5cbf84

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