Skip to main content

Database infrastructure: async engine, session management, and composable SQLAlchemy mixins

Project description

ulfblk-db

Database infrastructure for the Bloques ecosystem: async engine factory, session management, and composable SQLAlchemy mixins.

Installation

uv add ulfblk-db

Quick Start

from ulfblk_db import (
    Base,
    DatabaseSettings,
    TimestampMixin,
    SoftDeleteMixin,
    create_async_engine,
    create_session_factory,
    get_db_session,
    db_health_check,
)

# 1. Configure
settings = DatabaseSettings()  # reads BLOQUE_DATABASE_URL from env

# 2. Create engine and session factory
engine = create_async_engine(settings)
SessionLocal = create_session_factory(engine)

# 3. Define models with composable mixins
class User(Base, TimestampMixin, SoftDeleteMixin):
    __tablename__ = "users"
    id = Column(Integer, primary_key=True)
    name = Column(String, nullable=False)

# 4. Use in FastAPI
from fastapi import Depends
from sqlalchemy.ext.asyncio import AsyncSession

app = FastAPI()
db_dep = get_db_session(SessionLocal)

@app.get("/users")
async def list_users(db: AsyncSession = Depends(db_dep)):
    result = await db.execute(select(User))
    return result.scalars().all()

Features

  • DatabaseSettings: Extends BloqueSettings with pool configuration, reads from env vars with BLOQUE_ prefix
  • create_async_engine: Wrapper with sensible defaults, auto-detects SQLite for testing
  • create_session_factory: Returns async_sessionmaker with expire_on_commit=False
  • get_db_session: FastAPI Depends() compatible async generator
  • db_health_check: SELECT 1 health check for HealthResponse.checks
  • Base: SQLAlchemy DeclarativeBase - no opinionated id column
  • TimestampMixin: created_at + updated_at with auto-defaults
  • SoftDeleteMixin: deleted_at + is_deleted property + soft_delete() / restore() helpers

Environment Variables

Variable Default Description
BLOQUE_DATABASE_URL postgresql+asyncpg://localhost:5432/bloquedb Database connection URL
BLOQUE_DB_POOL_SIZE 5 Connection pool size
BLOQUE_DB_MAX_OVERFLOW 10 Max overflow connections
BLOQUE_DB_POOL_TIMEOUT 30 Pool timeout in seconds
BLOQUE_DB_POOL_RECYCLE 1800 Connection recycle time in seconds
BLOQUE_DB_ECHO false Echo SQL statements

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

ulfblk_db-0.1.0.tar.gz (6.2 kB view details)

Uploaded Source

Built Distribution

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

ulfblk_db-0.1.0-py3-none-any.whl (7.9 kB view details)

Uploaded Python 3

File details

Details for the file ulfblk_db-0.1.0.tar.gz.

File metadata

  • Download URL: ulfblk_db-0.1.0.tar.gz
  • Upload date:
  • Size: 6.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.10.11 {"installer":{"name":"uv","version":"0.10.11","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

Hashes for ulfblk_db-0.1.0.tar.gz
Algorithm Hash digest
SHA256 da6b9fc6a3ce5b2151b3bc32895d9c3add79e2a40f4ef17957428eb0b60619ab
MD5 a39048326f5d04e4af36c2a852dfedc3
BLAKE2b-256 a810a0de55f781bfbbdd48050082c1d9aee255fecc2e9d28371bf401e2f30634

See more details on using hashes here.

File details

Details for the file ulfblk_db-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: ulfblk_db-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 7.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.10.11 {"installer":{"name":"uv","version":"0.10.11","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

Hashes for ulfblk_db-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 6760b9777744c5b3b2907c0f14798b636801d00ebdccef5612313d05ca9eb02f
MD5 697c7d74f39e19d0442360fbc90d2127
BLAKE2b-256 c048cd48caa35c06628d25e2122df443e65685746bfb0b1266ffb2345ef79e8a

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