Skip to main content

voltwire-db-session

A configurable PostgreSQL session factory for Python applications. Wraps SQLAlchemy connection pool setup and pydantic-settings configuration into a single reusable package — install it, point it at your .env, and get sessions.

ORM-agnostic core

DatabaseSessionFactory, RODatabaseSessionFactory, and Session (exported from voltwire.db.session) are Protocols, not concrete classes. TransactionContext and DatabaseAutoConfiguration are written entirely against these interfaces and never import sqlalchemy — only voltwire.db.session.backends.sqlalchemy does. Today that's the only backend (SqlAlchemyDatabaseSessionFactory/SqlAlchemyRODatabaseSessionFactory, built via build_session_factory/build_ro_session_factory), but a future ORM backend only needs to satisfy the same Protocols — no changes required to TransactionContext, DatabaseAutoConfiguration, or downstream packages like voltwire-fastapi-db-txs, which already depend only on the abstraction.

Installation

pip install voltwire-db-session
# or with Poetry:
poetry add voltwire-db-session

A PostgreSQL driver is not included — install whichever you prefer alongside it:

pip install psycopg2-binary   # most common
pip install psycopg           # psycopg3

Quickstart

from voltwire.db.session import DatabaseSettings, build_session_factory

settings = DatabaseSettings()           # reads DB_* vars from .env
factory = build_session_factory(settings)

session = factory.get_session()
try:
    result = session.execute(...)
    session.commit()
finally:
    session.close()

# On app shutdown
factory.close()

Configuration

All settings are loaded from environment variables with a DB_ prefix. By default the library reads from a .env file in the working directory.

Environment variables

Variable Default Description
DB_HOST localhost Primary database host
DB_PORT 5432 Database port
DB_DATABASE postgres Database name
DB_USERNAME postgres Database username
DB_PASSWORD postgres Database password
DB_SCHEMA_NAME public PostgreSQL schema (used for search_path)
DB_DRIVER psycopg2 SQLAlchemy driver name
DB_RO_HOST (unset) Read-only replica host; falls back to DB_HOST
DB_POOL_SIZE 10 Minimum connections in pool
DB_MAX_POOL_SIZE 20 Maximum connections in pool
DB_POOL_TIMEOUT 30 Seconds to wait for a connection from pool
DB_POOL_RECYCLE 299 Recycle connections after this many seconds
DB_APPLICATION_NAME app Application name reported to PostgreSQL

Choosing your env file

# Standard .env (default)
settings = DatabaseSettings()

# Custom env file — e.g. .env.local, .env.production
settings = DatabaseSettings.from_env(".env.local")

# No file — reads only from real environment variables
settings = DatabaseSettings.from_env(None)

# No file, with inline overrides
settings = DatabaseSettings.from_env(None, host="db.internal", database="myapp")

Using a different driver

# psycopg3
settings = DatabaseSettings.from_env(".env", driver="psycopg")

# or via env var
# DB_DRIVER=psycopg

The driver value is used as the SQLAlchemy URL scheme: postgresql+{driver}://.... The corresponding package must be installed in your environment.

Read-only replica

from voltwire.db.session import DatabaseSettings, build_ro_session_factory

settings = DatabaseSettings()       # set DB_RO_HOST to point at your replica
ro_factory = build_ro_session_factory(settings)

session = ro_factory.get_session()  # writes will be rejected by PostgreSQL

If DB_RO_HOST is not set, RODatabaseSessionFactory falls back to the primary host but still enforces read-only mode at the PostgreSQL level.

FastAPI example

from contextlib import asynccontextmanager
from fastapi import FastAPI
from voltwire.db.session import DatabaseSettings, build_session_factory

settings = DatabaseSettings.from_env(".env.local")

@asynccontextmanager
async def lifespan(app: FastAPI):
    app.state.db = build_session_factory(settings)
    yield
    app.state.db.close()

app = FastAPI(lifespan=lifespan)

@app.get("/items")
def list_items():
    session = app.state.db.get_session()
    try:
        return session.execute(...).all()
    finally:
        session.close()

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

voltwire_db_session-0.0.2.tar.gz (15.9 kB view details)

Uploaded Source

Built Distribution

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

voltwire_db_session-0.0.2-py3-none-any.whl (14.0 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for voltwire_db_session-0.0.2.tar.gz
Algorithm Hash digest
SHA256 3136ab3da1633389a2fd9a78cebdaac336e80376189b44c9b3d055fca4086457
MD5 5c7ba83335d7b872b8f1784b1070857d
BLAKE2b-256 eb4564d66b21a181115925f711898d687c8e1792d5f622d18d0b0726ecab50e8

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for voltwire_db_session-0.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 db8cb0dd0eb55bec1fc175097c7ce3e57b178e1546d59f6afd31de602e0ef52b
MD5 b6800c23a931d9b77011b74952b2ebfb
BLAKE2b-256 fa511f251146cea5a28095279c81704110714e57a031f1ca4ac4e4860560ec10

See more details on using hashes here.

Release history Release notifications | RSS feed

This release

0.0.2 This release

2 files

0.0.1

2 files

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