hexastack-db
SQLAlchemy 2.0+ persistence layer, generic repositories, Unit of Work, declarative mixins, and Alembic migrations for Hexastack.
1. Overview & Capabilities
hexastack-db provides a robust, decoupled database layer supporting both synchronous and asynchronous workflows:
- Generic Repositories:
SqlAlchemyRepository[T, ID]andAsyncSqlAlchemyRepository[T, ID]implementingRepository[E, ID]andAsyncRepository[E, ID]. - Unit of Work:
SqlAlchemyUnitOfWorkandAsyncSqlAlchemyUnitOfWorkproviding contextual transaction boundaries (commit(),rollback()). - Declarative Base & Mixins:
HexastackBase: SharedDeclarativeBasefor unified metadata management across applications and migration autogen.UuidPrimaryKeyMixin: Portable Python-sideuuid4()stored as a 36-character string primary key.TimestampMixin: Automatic UTCcreated_atandupdated_attimestamps.
- Alembic Migration Engine: Programmatic migration execution (
upgrade,downgrade,revision,current,history,stamp) and CLI integration without requiring manualalembic.iniauthoring. - Auto Table Creation: Schema discovery and
create_all()execution on bootstrap viaregister_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
UnitOfWorkPortinto the DI container atorder=15(before CQRSorder=20), allowingUnitOfWorkMiddlewareto manage transactions automatically. - Session Injection: Injects
sessionmaker/async_sessionmakerinto DI, whichhexastack-fastapiresolves inDbSessionMiddlewarewithout requiring a direct package dependency. - Migration CLI Integration: The
hexastackumbrella package exposes CLI subcommands (hexastack db init/upgrade/revision/...) backed byhexastack_db.infra.migrations.
Optional Integrations (Extras)
[sqlite]: Installsaiosqlite>=0.20.0for async SQLite support.[postgresql]: Installsasyncpg>=0.30.0andpsycopg[binary]>=3.2.0.[pgvector]: Installspgvector>=0.3.0and PostgreSQL drivers for VectorStorePort support.[migrations]: Installsalembic>=1.13.0for 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.infra.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 with auto-table creation
runtime = bootstrap(
config_overrides={
"db": {
"url": "sqlite:///:memory:",
"auto_create_tables": True,
}
}
)
# 3. Use Unit of Work and Repository
uow = runtime.container.get(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}")
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
hexastack_db-0.1.0.tar.gz
(17.4 kB
view details)
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file hexastack_db-0.1.0.tar.gz.
File metadata
- Download URL: hexastack_db-0.1.0.tar.gz
- Upload date:
- Size: 17.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via:
uv/0.12.3 {"installer":{"name":"uv","version":"0.12.3","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
302c5a4c0bb103e9e72279276e8e3172384027815cc5e6c782dd9c711756005f
|
|
| MD5 |
8b2b3798d0f685649169239786acedbb
|
|
| BLAKE2b-256 |
b19e2804c3c73b3eb80c7f1228266dc5c47b5c7a4076700e05222dbdc1616169
|
File details
Details for the file hexastack_db-0.1.0-py3-none-any.whl.
File metadata
- Download URL: hexastack_db-0.1.0-py3-none-any.whl
- Upload date:
- Size: 23.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via:
uv/0.12.3 {"installer":{"name":"uv","version":"0.12.3","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ccc46ceb6a0f5759fdc3e4c03eface24fd33fe214b590845dea307999eaef722
|
|
| MD5 |
fdc522b93dbf23626cdaba885bbc2ba1
|
|
| BLAKE2b-256 |
46eabf338d0f14cdc8c528570baef9f9640ab9cbdf6c35804bcd03d3d107b3b5
|