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.1.tar.gz (6.4 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.1-py3-none-any.whl (7.7 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: fastfoundry_postgres-1.0.1.tar.gz
  • Upload date:
  • Size: 6.4 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.1.tar.gz
Algorithm Hash digest
SHA256 b97a2591861347c078a8e02a58944c2092c6b46617b5dffa15de49065c78706a
MD5 745ecdd443005b550e8eb2748c2ae4fc
BLAKE2b-256 f284ad8b2e7962e7372ed4c315866be56b282bb5055cff59d7d486741a8347a6

See more details on using hashes here.

Provenance

The following attestation bundles were made for fastfoundry_postgres-1.0.1.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.1-py3-none-any.whl.

File metadata

File hashes

Hashes for fastfoundry_postgres-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 0f103291e3038ed1c6bbc2f850cf633221a93dd3bc6c6ce8917fab2b69913045
MD5 774ac5a39d7b31a432bae185aa28e1f2
BLAKE2b-256 c6b89cc33813ecd2c522bcd5e5b3843c6b6ca3f72a23745e1ba151883f4cfa80

See more details on using hashes here.

Provenance

The following attestation bundles were made for fastfoundry_postgres-1.0.1-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