Skip to main content

hexastack-db

hexastack-db

SQLAlchemy 2.0+ persistence layer, generic repositories, Unit of Work, declarative mixins, and Alembic migrations for Hexastack.

PyPI: hexastack-db Python 3.12+ Coverage Part of Hexastack Governed by Hexaqual Tested on Hexaqueue License: Apache 2.0

Part of the Hexastack Framework · Governed by Hexaqual.


1. Overview & Capabilities

hexastack-db provides a robust, decoupled database layer supporting both synchronous and asynchronous workflows:

  • Generic Repositories: SqlAlchemyRepository[T, ID] and AsyncSqlAlchemyRepository[T, ID] implementing Repository[E, ID] and AsyncRepository[E, ID].
  • Unit of Work: SqlAlchemyUnitOfWork and AsyncSqlAlchemyUnitOfWork providing contextual transaction boundaries (commit(), rollback()).
  • Declarative Base & Mixins:
    • HexastackBase: Shared DeclarativeBase for unified metadata management across applications and migration autogen.
    • UuidPrimaryKeyMixin: Portable Python-side uuid4() stored as a 36-character string primary key.
    • TimestampMixin: Automatic UTC created_at and updated_at timestamps.
  • Alembic Migration Engine: Programmatic migration execution (upgrade, downgrade, revision, current, history, stamp) and CLI integration without requiring manual alembic.ini authoring.
  • Auto Table Creation: Schema discovery and create_all() execution on bootstrap via register_metadata().

2. Package Anatomy & Key Components

hexastack_db/
├── domain/          # DatabaseError, EntityNotFoundError, UniqueConstraintViolationError
├── adapters/        # SqlAlchemyRepository, AsyncSqlAlchemyRepository, SqlAlchemyUnitOfWork, AsyncSqlAlchemyUnitOfWork
└── infra/
    ├── bootstrap.py # DatabaseBootstrapper (order=15)
    ├── config.py    # HexastackDatabaseConfig
    ├── engine.py    # Engine and sessionmaker factory functions
    ├── mixins.py    # HexastackBase, UuidPrimaryKeyMixin, TimestampMixin
    ├── migrations.py# Alembic migration helpers
    └── registries/  # metadata.py (register_metadata, get_registered_metadata)

Key Exports

Category Exports
Bootstrap DatabaseBootstrapper (order=15), HexastackDatabaseConfig
Migrations init_migrations, get_alembic_config, run_upgrade, run_downgrade, run_revision, run_current, run_history, stamp
Mixins & Base HexastackBase, UuidPrimaryKeyMixin, TimestampMixin
Registries register_metadata, get_registered_metadata, clear_metadata_registry
Repositories SqlAlchemyRepository, AsyncSqlAlchemyRepository
Unit of Work SqlAlchemyUnitOfWork, AsyncSqlAlchemyUnitOfWork

3. Monorepo & Sibling Relationships

graph TD
    subgraph SiblingConsumers ["Sibling Consumers"]
        CQRS["hexastack-cqrs (UnitOfWorkMiddleware)"]
        FASTAPI["hexastack-fastapi (DbSessionMiddleware)"]
        CLI["hexastack (hexastack db <cmd>)"]
    end

    subgraph DatabasePackage ["hexastack-db"]
        BOOT["DatabaseBootstrapper (order=15)"]
        UOW["SqlAlchemyUnitOfWork / AsyncSqlAlchemyUnitOfWork"]
        ENG["Engine & sessionmaker"]
        MIGR["Alembic Migration Engine"]
    end

    subgraph CoreContracts ["hexastack-core"]
        UOW_PORT["UnitOfWorkPort"]
        REPO_PORT["RepositoryPort"]
    end

    BOOT --> UOW
    BOOT --> ENG
    UOW -->|implements| UOW_PORT

    CQRS -. consumes UnitOfWorkPort from DI .-> UOW
    FASTAPI -. consumes sessionmaker from DI .-> ENG
    CLI -. invokes migration helpers .-> MIGR

Explicit Dependencies (Direct)

  • hexastack-core: Core ports (UnitOfWorkPort, Repository), exception types, and DI container.
  • sqlalchemy>=2.0.38: Core ORM and SQL toolkit.

Implied / Behavioral Relationships (DI-Mediated)

  • Unit of Work Binding: Binds UnitOfWorkPort into the DI container at order=15 (before CQRS order=20), allowing UnitOfWorkMiddleware to manage transactions automatically.
  • Session Injection: Injects sessionmaker / async_sessionmaker into DI, which hexastack-fastapi resolves in DbSessionMiddleware without requiring a direct package dependency.
  • Migration CLI Integration: The hexastack umbrella package exposes CLI subcommands (hexastack db init/migrate/revision/...) backed by hexastack_db.infra.migrations.

Optional Integrations (Extras)

  • [sqlite]: Installs aiosqlite>=0.20.0 for async SQLite support.
  • [postgresql]: Installs asyncpg>=0.30.0 and psycopg[binary]>=3.2.0.
  • [pgvector]: Installs pgvector>=0.3.0 and PostgreSQL drivers for VectorStorePort support.
  • [migrations]: Installs alembic>=1.13.0 for migration commands and autogeneration.
  • [all]: Installs all drivers, pgvector, and Alembic.

4. Installation

# Standalone install with SQLite async support
pip install "hexastack-db[sqlite]"

# Standalone with PostgreSQL and Alembic migrations
pip install "hexastack-db[postgresql,migrations]"

# With PostgreSQL vector search (pgvector)
pip install "hexastack-db[pgvector]"

# Via umbrella package
pip install "hexastack[db]"

5. Configuration Reference

[hexastack.db]
# Global Settings
url = "sqlite:///app.db" # "postgresql+asyncpg://user:pass@localhost:5432/dbname"
async_mode = false # Set to true for async engine & sessions
auto_create_tables = false # Runs create_all() on registered metadata at bootstrap
echo = false # Enables raw SQLAlchemy SQL logging
pool_size = 5
max_overflow = 10
pool_timeout = 30
pool_recycle = 1800

# SQLite-Specific Dialect Settings
[hexastack.db.sqlite]
foreign_keys = true # PRAGMA foreign_keys = ON
journal_mode = "WAL" # PRAGMA journal_mode = WAL
busy_timeout_ms = 5000 # PRAGMA busy_timeout = 5000
synchronous = "NORMAL"

# PostgreSQL-Specific Dialect Settings
[hexastack.db.postgres]
search_path = "public"
ssl_mode = "require"
server_side_cursors = false

# PostgreSQL pgvector Configuration
[hexastack.db.vector]
enabled = false # Auto-binds VectorStorePort in DI container
table_name = "hexastack_vectors"
dimension = 1536
distance_strategy = "cosine" # "cosine", "l2", "inner_product"
index_type = "hnsw" # "hnsw", "ivfflat", "none"
m = 16
ef_construction = 64

6. Quickstart Example

from sqlalchemy.orm import Mapped, mapped_column
from hexastack_core.infra.bootstrap import bootstrap
from hexastack_core.ports.unit_of_work import UnitOfWorkPort
from hexastack_db.utils.mixins import HexastackBase, UuidPrimaryKeyMixin, TimestampMixin
from hexastack_db.infra.registries.metadata import register_metadata
from hexastack_db.adapters.repository import SqlAlchemyRepository


# 1. Define Model
class UserRecord(UuidPrimaryKeyMixin, TimestampMixin, HexastackBase):
    __tablename__ = "users"
    email: Mapped[str] = mapped_column(unique=True)


register_metadata(HexastackBase.metadata)

# 2. Bootstrap using [hexastack.db] settings from TOML configuration
runtime = bootstrap(config_path="pyproject.toml")

# 3. Use Unit of Work and Repository
uow = runtime.container.resolve(UnitOfWorkPort)
with uow:
    repo = SqlAlchemyRepository(session=uow.session, model_cls=UserRecord)
    user = UserRecord(email="alice@hexastack.dev")
    repo.add(user)
    uow.commit()

print(f"Created user {user.id} at {user.created_at}")

Metadata

Release files for hexastack-db 0.13.0

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

Source distribution (sdist)

Source distribution for hexastack-db 0.13.0
File Size Uploaded Provenance Actions
hexastack_db-0.13.0.tar.gz 29.7 kB Without provenance Details for hexastack_db-0.13.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for hexastack-db 0.13.0
File Interpreter ABI Platform Actions
hexastack_db-0.13.0-py3-none-any.whl Python 3 none any Details for hexastack_db-0.13.0-py3-none-any.whl

Total release size: 68.1 kB

Release files / hexastack_db-0.13.0.tar.gz

Download URL hexastack_db-0.13.0.tar.gz
Size 29.7 kB
Tags Source
SHA-256 checksum
How to use checksums
fcd726ea9b5566b8879035bfc8bae7a73416c583b7a9c7666a39b0d7d54b5f61
BLAKE2b-256 checksum
How to use checksums
b217eab713b8c49e11acba675757821ecccbbdd089616b2f8bd0a4f2dcc4e614
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.16

Release files / hexastack_db-0.13.0-py3-none-any.whl

Download URL hexastack_db-0.13.0-py3-none-any.whl
Size 38.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
73a8dc230f04d609bc768aa88f3673fbbd14676def2bb1e7427be775fb884460
BLAKE2b-256 checksum
How to use checksums
b946ff0c5e20214c622f9b12569aa0fb0699ed11660d4c84a7df319bcb26964e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.16

Security for hexastack-db 0.13.0

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.

No provenance found

None of the files in this release were uploaded with provenance metadata.

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.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

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