Skip to main content

PostgreSQL connection building blocks for the fastfoundry ecosystem.

Project description

fastfoundry-postgres

PostgreSQL connection building blocks for the fastfoundry ecosystem.

Part of the fastfoundry.* namespace (fastfoundry-settings, fastfoundry-redis, …).

An async SQLAlchemy engine and session lifecycle for a single PostgreSQL database (via asyncpg), a declarative model base, and a configuration model that plugs into fastfoundry.settings.

Install

pip install fastfoundry-postgres
# or
uv add fastfoundry-postgres

Requires Python 3.12+.

Configuration

DatabaseCfg holds the connection parameters. It is designed to nest inside a fastfoundry.settings.BaseAppSettings subclass, so the values load from the environment with the rest of your service configuration, then get handed to database_ctx:

from fastfoundry.settings import BaseAppSettings
from fastfoundry.postgres import DatabaseCfg


class Settings(BaseAppSettings):
    database: DatabaseCfg


settings = Settings()

With the FF_ prefix and __ nested delimiter that BaseAppSettings applies, the fields are read from the environment as FF_DATABASE__HOST, FF_DATABASE__PORT, FF_DATABASE__USER, and so on.

Engine & session lifecycle

Open database_ctx(settings.database) once for the application lifetime — it builds the engine and session factory, publishes them for ambient access (get_engine() and db_cfg()), and disposes the engine on exit. For FastAPI, wire it into the lifespan:

from contextlib import asynccontextmanager
from collections.abc import AsyncGenerator

from fastapi import FastAPI
from fastfoundry.postgres import database_ctx

from app.settings import settings


@asynccontextmanager
async def lifespan(_: FastAPI) -> AsyncGenerator[None, None]:
    async with database_ctx(settings.database):
        yield


app = FastAPI(lifespan=lifespan)

Acquire a session with database_session, and open transactions explicitly with transaction (sessions use autobegin=False, so nothing starts a transaction for you — even read-only work should be wrapped, as SQLAlchemy recommends). A nested transaction becomes a SAVEPOINT:

from fastfoundry.postgres import database_session, transaction


async with database_session() as session:
    async with transaction(session):
        session.add(user)

Models

Subclass BaseDBModel. The table name defaults to the lower-cased class name with a trailing s; set __table_name__ to override it:

from sqlalchemy.orm import Mapped, mapped_column

from fastfoundry.postgres import BaseDBModel


class User(BaseDBModel):          # -> table "users"
    id: Mapped[int] = mapped_column(primary_key=True)


class Account(BaseDBModel):
    __table_name__ = "accounts"   # explicit table name
    id: Mapped[int] = mapped_column(primary_key=True)

Development

mise run install     # uv sync --dev
mise run lint        # ruff check
mise run typecheck   # mypy --strict
mise run test        # pytest
mise run build       # uv build (sdist + wheel)

License

MIT

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

fastfoundry_postgres-1.0.0.tar.gz (55.6 kB view details)

Uploaded Source

Built Distribution

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

fastfoundry_postgres-1.0.0-py3-none-any.whl (7.7 kB view details)

Uploaded Python 3

File details

Details for the file fastfoundry_postgres-1.0.0.tar.gz.

File metadata

  • Download URL: fastfoundry_postgres-1.0.0.tar.gz
  • Upload date:
  • Size: 55.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for fastfoundry_postgres-1.0.0.tar.gz
Algorithm Hash digest
SHA256 75f86bff424f7c13164b539e8a3acefda185d29ba56ebca277b967ff96398f22
MD5 ad55ab3c3a327b53eacce6436e30a5f0
BLAKE2b-256 720f6bf957f0ee0e7889ca25e3d618a36e027205207221c38326baf45c5e0284

See more details on using hashes here.

Provenance

The following attestation bundles were made for fastfoundry_postgres-1.0.0.tar.gz:

Publisher: release.yml on Drozdetskiy/fastfoundry-postgres

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file fastfoundry_postgres-1.0.0-py3-none-any.whl.

File metadata

File hashes

Hashes for fastfoundry_postgres-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 cadbcb82dfbca81c48aaadfa300b7e6370b7c36d32156e12c063b4d34fcf1e77
MD5 65852067e76ef0ef847aff930c4ebf51
BLAKE2b-256 59b973f43d27d27c7016528af9602d373bf1964971fc57626749dc76b3305e44

See more details on using hashes here.

Provenance

The following attestation bundles were made for fastfoundry_postgres-1.0.0-py3-none-any.whl:

Publisher: release.yml on Drozdetskiy/fastfoundry-postgres

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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