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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
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 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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b97a2591861347c078a8e02a58944c2092c6b46617b5dffa15de49065c78706a
|
|
| MD5 |
745ecdd443005b550e8eb2748c2ae4fc
|
|
| BLAKE2b-256 |
f284ad8b2e7962e7372ed4c315866be56b282bb5055cff59d7d486741a8347a6
|
Provenance
The following attestation bundles were made for fastfoundry_postgres-1.0.1.tar.gz:
Publisher:
release.yml on Drozdetskiy/fastfoundry-postgres
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
fastfoundry_postgres-1.0.1.tar.gz -
Subject digest:
b97a2591861347c078a8e02a58944c2092c6b46617b5dffa15de49065c78706a - Sigstore transparency entry: 1818758588
- Sigstore integration time:
-
Permalink:
Drozdetskiy/fastfoundry-postgres@3ff76941bcf9f30d5e461ed6272b888e10936c7c -
Branch / Tag:
refs/heads/main - Owner: https://github.com/Drozdetskiy
-
Access:
private
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@3ff76941bcf9f30d5e461ed6272b888e10936c7c -
Trigger Event:
push
-
Statement type:
File details
Details for the file fastfoundry_postgres-1.0.1-py3-none-any.whl.
File metadata
- Download URL: fastfoundry_postgres-1.0.1-py3-none-any.whl
- Upload date:
- Size: 7.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
0f103291e3038ed1c6bbc2f850cf633221a93dd3bc6c6ce8917fab2b69913045
|
|
| MD5 |
774ac5a39d7b31a432bae185aa28e1f2
|
|
| BLAKE2b-256 |
c6b89cc33813ecd2c522bcd5e5b3843c6b6ca3f72a23745e1ba151883f4cfa80
|
Provenance
The following attestation bundles were made for fastfoundry_postgres-1.0.1-py3-none-any.whl:
Publisher:
release.yml on Drozdetskiy/fastfoundry-postgres
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
fastfoundry_postgres-1.0.1-py3-none-any.whl -
Subject digest:
0f103291e3038ed1c6bbc2f850cf633221a93dd3bc6c6ce8917fab2b69913045 - Sigstore transparency entry: 1818758715
- Sigstore integration time:
-
Permalink:
Drozdetskiy/fastfoundry-postgres@3ff76941bcf9f30d5e461ed6272b888e10936c7c -
Branch / Tag:
refs/heads/main - Owner: https://github.com/Drozdetskiy
-
Access:
private
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@3ff76941bcf9f30d5e461ed6272b888e10936c7c -
Trigger Event:
push
-
Statement type: