Skip to main content

persistence-kit

The plumbing every one of our services rewrites: repositories, settings, object storage, authentication, an HTTP client and a cache. You describe your entities once and the kit gives you an async repository for them, backed by memory, MongoDB, PostgreSQL or DynamoDB, without your domain code knowing which one.

Nothing here knows about your business. Roles, permissions, and domain rules stay in your application.

  • Python 3.11+ · async everywhere · type hints shipped (py.typed)
  • Optional dependencies stay out of the graph until you ask for them

Five minutes

pip install persistence-kit
from dataclasses import dataclass, field
from uuid import UUID, uuid4

from persistence_kit import Database
from persistence_kit.repository_factory import get_repo, register_entity


@dataclass
class User:
    email: str
    name: str
    id: UUID = field(default_factory=uuid4)


register_entity("user", {
    "entity": User,
    "collection": "users",
    "database": Database.MEMORY,
    "unique": {"email": "email"},
})

repo = get_repo("user")
await repo.add(User(email="ada@example.org", name="Ada"))
ada = await repo.get_by_index("email", "ada@example.org")

The repository protocol is small and the same for every backend: add, get, update, delete, get_by_index, list, list_by_fields, count and count_by_fields.

Switch database to Database.POSTGRES and the same code talks to PostgreSQL. That is the whole point of the kit.

The one idea: the entity registry

Everything else hangs off a single dictionary. You register each entity once, usually in a register_defaults() function in your app, and from then on you ask for repositories by key.

from persistence_kit.repository_factory import set_registry_initializer

set_registry_initializer(register_defaults)   # called during startup

What goes in a registration:

Key Meaning
entity The dataclass this key maps to
collection Table or collection name in the backing store
database Database.MEMORY, MONGO, POSTGRES or DYNAMODB. Defaults to REPO_DATABASE
unique Indexed lookups, {"index_name": "field"}, used by get_by_index
indexes Composite, non-unique indexes for filters and counts, {"index_name": ["field", ...]}. Postgres and Mongo create them; memory ignores them; DynamoDB rejects them
relations How this entity joins to others, including many-to-many through a pivot

Relations are what get_repo_view(...) reads to return an entity with its related rows already populated, so you do not write joins by hand. The shapes and the traps are in docs/repositories_and_relations.md.

Four ways to reach a repository, all equivalent:

get_repo("user")                    # plain repository
get_repo_view("user")               # repository that populates relations
provide_repo("user")                # FastAPI dependency
provide_view_repo("user")           # FastAPI dependency, populated

Transactions

Each repository call commits on its own. When several writes have to succeed or fail together, put them inside transaction():

from persistence_kit.repository import transaction

async with transaction():
    counter = await counters.get("resolution", for_update=True)
    counter.value += 1
    await counters.update(counter)
    await resolutions.add(Resolution(number=counter.value, request_id=request.id))
  • Every repository used inside the block shares one connection. The block commits when it ends and rolls everything back if it raises.
  • get(..., for_update=True) locks the row until the block ends, so a second transaction that asks for the same row waits for the first one. That is how to hand out consecutive numbers with no repeats and no gaps. Outside a transaction it raises TransactionRequiredError.
  • A transaction() opened inside another one joins the outer one.
  • Postgres and memory take part. Memory behaves the same way for tests: it locks rows, undoes the writes when the block fails and, inside the block, returns copies, so a change only lands through update. Plain reads are not isolated in memory: another transaction can see a write before it commits.
  • MongoDB and DynamoDB repositories raise TransactionNotSupportedError when they write or lock a row inside the block.
  • Do not run work in parallel (asyncio.gather) inside one transaction: it shares a single connection.

Actions that must happen only once

Give the transaction an idempotency_key when an action must never run twice, such as approving a request:

from persistence_kit.repository import RepeatedActionError, transaction

async with transaction(idempotency_key=f"approve-request:{request.id}"):
    ...
  • The key is saved with the rest of the block, in the idempotency_keys table of the default database (REPO_DATABASE). If the block fails, the key is rolled back with it and the action can run again.
  • Once the block commits, running it again with the same key raises RepeatedActionError before the block starts. Its key attribute names the action.
  • When two arrive at the same time, the second waits for the first: if the first commits, the second gets RepeatedActionError; if it fails, the second runs.
  • Keys never expire. A key given to a nested transaction() belongs to the outer one.
  • Postgres and memory keep keys. With MongoDB or DynamoDB as the default database it raises TransactionNotSupportedError.

Installation and extras

The base install stays light. Each capability that needs a heavy dependency lives behind an extra, so a service that only talks to Mongo never installs boto3 or fastapi.

pip install "persistence-kit[api,security,restclient]"
Extra Gives you Pulls in
api FastAPI exceptions, pagination, route loading, error handlers fastapi, python-multipart
security Memory identity provider and JWT verifier fastapi, pyjwt, python-multipart
security-cognito The above plus AWS Cognito and JWKS + boto3
storage-s3 S3 object storage adapter boto3
storage-routes FastAPI route to serve local exports fastapi, python-multipart
dynamodb DynamoDB repository backend boto3
restclient The REST client and its cache httpx
encrypted Encrypted request and response payloads fastapi, cryptography
mail Mail templates rendered with Jinja2 (sending itself needs no extra) jinja2
tracing A span for every REST call, inside the request's trace opentelemetry-api
testing Everything the test suite needs fastapi, pyjwt, httpx, python-multipart, cryptography, jinja2, opentelemetry-api
all Every optional capability all of the above

Importing persistence_kit never loads an optional dependency by itself. Ask for something you did not install and you get a message telling you which extra to add, not an obscure ImportError.

What is in the box

Module What it does
contracts/ The protocols: Repository, ViewRepository, ObjectStorage, IdentityProvider
repository/ One implementation per backend: memory, Mongo, SQLAlchemy, DynamoDB
repository_factory/ The entity registry, the factory, and the view repository that populates relations
settings/ RepoSettings and PersistenceKitSettings, plus shared enums and parsers
storage/ Object storage: local directory or S3, with presigned URLs
security/ Identity providers and JWT verifiers, memory or Cognito, and the encrypted envelope format
restclient/ HTTP client with pluggable auth, endpoint resolution, retries and DTO decoding
cache/ Key-value cache with TTL and atomic counters: memory, Mongo or DynamoDB
mail/ Outbound mail through an SMTP relay, with templates that live in the app
excel/ .xlsx workbooks with several sheets, real dates and columns sized to their content
resilience/ Circuit breaker, shared by anything that calls out
api/ Reusable FastAPI pieces: error handlers, pagination, rate limiting, encrypted routes
bootstrap/ Startup helpers, configuration registry, seed orchestration
utils/ Small transversal helpers such as upserts

Import from persistence_kit when the public facade is enough. Reach into a subpackage only when you need something implementation-specific. The root exports about a hundred names, so let your editor complete them rather than keeping a list here.

Object storage

Writing generated files without your domain layer knowing where they land.

from persistence_kit.storage import LocalObjectStorage

storage = LocalObjectStorage(
    base_dir=".local",
    public_base_url="http://localhost:8000",
    signing_secret="dev-secret",
)

key = await storage.upload("exports/report.csv", b"id,name\n1,Ada\n", "text/csv")
url = await storage.generate_presigned_url(key)

LocalObjectStorage writes to a directory and signs its own download URLs. S3ObjectStorage uploads to S3 and returns AWS presigned URLs. Pick one with get_export_storage(settings), which reads your settings and caches the adapter. Give S3ObjectStorage a prefix and every key it writes, presigns, deletes or lists stays under that folder of the bucket, while the keys you pass and get back stay relative to it.

For files your users upload, build_media_storage(settings, namespace=..., url_prefix=...) checks type and size and keeps each namespace in its own folder: <LOCAL_MEDIA_STORAGE_DIR>/<namespace> with MEDIA_STORAGE_PROVIDER=local, and <namespace>/ inside AWS_S3_MEDIA_BUCKET with s3. Keys are the same on both, so moving to S3 does not change what your app stores.

media = build_media_storage(
    settings, namespace="avatars", url_prefix="/media/avatars", verify_signature=True
)
data = await media.read_upload(file)

verify_signature=True also checks the first bytes of PDF, PNG, JPEG, GIF and WebP files, so a file renamed to .png is rejected; other types are not checked. It is off by default. read_upload(file) takes a FastAPI UploadFile, or anything with an async read(size) and a content_type, reads at most max_bytes + 1 bytes, validates them and returns them.

When you also need the type, read_validated_upload(file) runs the same checks and returns a ValidatedUpload: the data and the content_type that passed them. Store the file with that type and it is served with it later.

upload = await media.read_validated_upload(file)
await media.upload(key, upload.data, upload.content_type)

Every storage keeps the content type it was given on upload and upload_file, and content_type_of(key) returns it, on the storage itself or on the media storage. S3 reads it from the object's head. LocalObjectStorage writes it to a .content-types/ folder at its root that mirrors your keys: .content-types/abc/main.png holds image/png. That folder is reserved: list_keys never returns it, delete and the scope cleanups remove the type with the object, and a key inside it raises StoragePathError. A local object saved before 3.21.0 has no stored type, so its type is guessed from the key's extension, or application/octet-stream when there is none. A key with nothing stored raises StorageNotFoundError.

read(key) gives back the bytes stored under a key, on the storage itself or on the media storage, the same way on disk and on S3. A key with nothing stored raises StorageNotFoundError; any other S3 failure raises StorageError. Use it when the service itself needs the content, such as filling a template; to hand a file to a browser, keep using generate_presigned_url or serve_media_object.

serve_media_object(storage, key) sends the local file with its stored type, or redirects to a presigned URL on S3, which sends the type S3 stored. Pass media_type= to send another one. A stored HTML, SVG or other XML type goes out as an attachment, so it never renders on your API's origin. A local key with no file raises NotFoundException, so it stays a 404 when a use case wrapped in handle_service_errors calls it.

FastAPI apps can mount build_local_export_storage_router(...) to serve local files, passing their settings provider, an optional current-user dependency, and an authorization callback. It sends each file with its stored type; a file stored without one keeps the type guess_export_media_type gives its extension.

Needs [storage-s3] for S3 and [storage-routes] for the route.

Security

from persistence_kit.security import MemorySecurityProvider, MemoryJwtVerifier

identity = MemorySecurityProvider(
    jwt_secret="dev-secret-with-enough-length",
    jwt_issuer="local-sandbox",
    seed_role_users=True,
    seed_role_codes=("admin", "operator"),
    seed_user_domain="example.org",
)
verifier = MemoryJwtVerifier(secret="dev-secret-with-enough-length", issuer="local-sandbox")

Two protocols, IdentityProvider and TokenVerifier, with a memory pair for tests and local work and a Cognito pair for real deployments. Registration, login, and password-reset results come back as dataclasses, not raw dicts. get_identity_provider(settings) and get_token_verifier(settings) build the right pair from your settings.

Your roles, authorization policies and route permission matrices stay in your application. The kit only answers "who is this".

Needs [security], or [security-cognito] for Cognito.

Auth rate limit

from fastapi import Depends

from persistence_kit.api import build_auth_rate_limit_dependency

login_limit = build_auth_rate_limit_dependency(
    scope="auth-login",
    limit_setting_name="auth_login_rate_limit",
    settings_dep=get_settings,
)


@router.post("/login", dependencies=[Depends(login_limit)])
async def login(payload: LoginRequest) -> LoginResponse:
    ...

The dependency caps attempts per client address and answers 429 with a Retry-After. TRUSTED_PROXY_HOPS decides which address is the client.

Unless the app puts its own limiter in app.state.auth_rate_limiter, it uses build_rate_limiter(): a CacheRateLimiter over get_cache("rate-limit"), so CACHE_BACKEND decides where the counts live. With CACHE_BACKEND=dynamodb (or mongo) every replica counts against the same limit and a deploy does not reset it; with memory each process counts on its own. CacheRateLimiter(cache) takes any other Cache as well.

It counts with an approximate sliding window: one counter per fixed window, kept by the cache for two windows, plus the previous window weighted by how much of it still falls inside the last AUTH_RATE_LIMIT_WINDOW_SECONDS. A rejected attempt counts too, so a client that keeps hammering stays blocked, and Retry-After is the first second its next attempt would pass.

InMemoryRateLimiter is still available for apps that set it explicitly; it keeps the count inside the process.

With STAGE=production the limit cannot be turned off and the cache cannot be memory; see Settings.

Login lockout

The rate limit counts every attempt from an address. LoginLockout counts only the failed ones, and blocks for AUTH_LOCKOUT_SECONDS once a scope reaches its cap:

Scope Counts Cap
pair failures of one username from one address AUTH_LOCKOUT_PAIR_ATTEMPTS (5)
user failures of one username from any address AUTH_LOCKOUT_USER_ATTEMPTS (20)
client failures from one address with any username AUTH_LOCKOUT_CLIENT_ATTEMPTS (20)
from persistence_kit import build_login_lockout

lockout = build_login_lockout(settings)

locked = await lockout.blocked(username=username, client_id=client_ip)
if locked:
    raise HTTPException(429, headers={"Retry-After": str(locked.retry_after)})
if not await credentials_are_valid(username, password):
    await lockout.fail(username=username, client_id=client_ip)
    raise HTTPException(401)
await lockout.succeed(username=username, client_id=client_ip)

Check blocked before checking the password, so a blocked attempt is refused even with the right one and the answer is the same whether the user exists or not. fail returns the Lockout it just started, if any, for the log. succeed clears the pair count only, so a login that works does not hand a distributed attacker a fresh user budget. A failure without a username (username=None) counts for the address alone. A cap of 0 turns that scope off.

The counts live in get_cache("login-lockout"), so CACHE_BACKEND decides whether every replica shares them, and the keys are SHA-256 digests: the cache never holds a username or an address. The lockout follows AUTH_RATE_LIMIT_ENABLED, off by default in local.

Encrypted payloads

For data that should not be readable between the browser and your handler, not even to a proxy that terminates TLS.

from fastapi import APIRouter, FastAPI

from persistence_kit import (
    EncryptedResponseMiddleware,
    build_encrypted_route,
    encrypted,
)

router = APIRouter(route_class=build_encrypted_route(get_settings))


@router.post("/login", openapi_extra=encrypted(fields=["password"], response_fields=["token"]))
async def login(payload: LoginRequest) -> LoginResponse:
    ...


app = FastAPI()
app.add_middleware(EncryptedResponseMiddleware)
app.include_router(router)

The client generates one AES-256-GCM key per request, wraps it with the server's public key, and sends it inside the envelope. The route unwraps it and opens the payload before your handler runs; the middleware encrypts whatever comes back with the same key, including the errors your own exception_handler builds. Your handler never sees an envelope, and both pieces are required.

Two modes. encrypted() encrypts the whole body and the whole response. encrypted(fields=[...]) encrypts named fields only, so the rest of the payload stays readable; paths may be nested and may cross lists, as in items[].price. Those paths are validated when the route is mounted — against your Pydantic body when there is one — so a typo fails at startup rather than quietly leaving the field unencrypted.

By default the private key is generated in memory at startup, so encrypted routes work with no configuration. Set ENCRYPTED_TYPE=local to load a fixed base64 PEM from ENCRYPTED_PRIVATE_KEY, or ENCRYPTED_TYPE=kms to keep it inside AWS KMS. Clients fetch the matching public key from your own endpoint, built on public_key_der_b64(provider).

Outside the local stage the kit logs a warning when ENCRYPTED_TYPE=memory or CACHE_BACKEND=memory: both keep per-process state that several replicas do not share. It warns and starts anyway, except that with STAGE=production the settings already refuse CACHE_BACKEND=memory.

Envelopes carry a timestamp, expire after sixty seconds, and each nonce is accepted once, so a captured request cannot be replayed. Anything malformed, expired, replayed or tampered with comes back as a 400.

In local stage a plain body is allowed through, so you can call your API from Swagger while developing.

Needs [encrypted].

REST client and cache

For calling somebody else's HTTP API without writing the same client again. Register a service once, then ask for its client:

from persistence_kit.restclient import get_rest_client, register_rest_service, ServiceConfig

register_rest_service(
    "billing",
    base_url="https://billing.example.org/api",
    config=ServiceConfig(
        default_headers={"X-Api-Key": settings.billing_key},
        timeout_seconds=10,
        cacheable=True,
        cache_ttl_seconds=300,
    ),
)

client = get_rest_client("billing")

It brings pluggable authentication (NoAuth, ApiKeyAuth, BasicAuth, BearerAuth, OAuth2ClientCredentials, LoginTokenAuth), endpoint resolution that can read a service directory, a retry policy, DTO decoding through decode, and optional response caching. MemoryRestClient stands in for the real one in tests.

Base URLs can be overridden per environment with REST_SERVICE_URLS, a JSON map of service name to URL, without touching the registration.

With [tracing] installed and a tracer provider in the app, every call is a client span inside the current trace, named <METHOD> <path> (GET /api/invoices) and tagged with the service in peer.service, with the host, the status and the retries. The query string is never recorded, and a failure keeps only the error type, not its message, so a password sent as a query parameter does not reach the traces. MemoryRestClient registered under a name produces the same spans, so they show up in local runs too.

The cache is a plain key-value store with TTL, chosen by CACHE_BACKEND the same way repositories are chosen by REPO_DATABASE. Set CACHE_NAMESPACE when several apps share one DynamoDB table or Mongo collection so their keys do not collide.

sent_today = await get_cache("mail").increment("sent:2026-11-21", ttl_seconds=86400)

increment adds one atomically and returns the new value. A missing or expired key starts at 1 with the given TTL; a live one keeps its expiry, so the counter covers a fixed window. It is atomic on every backend (a single pipeline update in Mongo, a conditional UpdateItem in DynamoDB), including a key the backend's TTL sweep has not deleted yet, so replicas can share a counter.

Both are covered in depth in docs/restclient_and_cache.md.

Needs [restclient].

Mail

Sending mail from an application without it knowing which relay carries it. The kit ships the machinery and no templates: the words belong to the app that sends them.

from persistence_kit.mail import MailMessage, build_mailer

mailer = build_mailer("app/templates/mail")

message = mailer.compose(
    "user_roles_changed",
    to="ana@udea.edu.co",
    cc=["coordinacion@udea.edu.co"],
    bcc="auditoria@udea.edu.co",
    context={"name": "Ana", "roles": ["Préstamos", "Reportes"]},
)
mailer.dispatch(message)

A template is a folder in the app's directory, user_roles_changed/, with subject.txt (required) and body.html or body.txt (at least one). The same files can also live loose in the directory as user_roles_changed.subject.txt, user_roles_changed.html and user_roles_changed.txt; the folder wins when both exist. They are Jinja2, so a layout can be shared with {% extends "shared/layout.html" %}, resolved from the directory root. HTML is autoescaped, and a variable missing from the context fails instead of rendering blank.

templates/mail/
  shared/layout.html
  images/logo.png
  user_roles_changed/
    subject.txt
    body.html
    body.txt
    sample.json

to, cc, bcc and reply_to take a string, a MailAddress or a list of them. Blind copies travel in the SMTP envelope only and never appear in the headers. Attachments go in attachments as MailAttachment(filename, content, content_type).

compose raises on a broken template, so a test that composes every template catches it. dispatch delivers in the background: if the relay fails, the error is logged and the request that triggered the mail carries on. Use await mailer.send(message) when the caller has to know, and await mailer.wait_for_pending() in tests or at shutdown.

Images are embedded, not linked: <img src="cid:logo.png"> in a template attaches images/logo.png from the same directory as an inline part, so it shows even where remote images are blocked. compose adds it on its own; a missing file or one that is not an image fails like a missing variable. MailAttachment(..., content_id=...) does the same for images the caller builds.

A template can carry a fourth file, sample.json (user_roles_changed.sample.json when loose), with an example context. MailTemplates.names() lists the templates and sample_context(name) reads that file, so one parametrized test covers them all. The same sample drives the preview, which renders without sending:

python -m persistence_kit.mail.preview app/templates/mail user_roles_changed

It writes the .html and the .txt to a temporary folder, prints the subject and opens the HTML in the browser. Without names it renders every template; --output changes the folder and --no-open skips the browser.

The sender is chosen by MAIL_BACKEND. memory keeps messages in MemoryMailSender.sent, for tests and for local runs where no relay accepts you. smtp delivers through SmtpMailSender, which upgrades with STARTTLS and logs in only if MAIL_SMTP_USERNAME is set, so a relay that authorizes by IP works as is.

Needs [mail] for templates. MailMessage and the senders use only the standard library.

Excel

Writing an .xlsx report without a spreadsheet library. Each ExcelSheet is one tab, and build_xlsx returns the workbook as bytes, ready to upload or to serve with EXCEL_MEDIA_TYPE.

from datetime import UTC, date, datetime
from zoneinfo import ZoneInfo

from persistence_kit.excel import EXCEL_MEDIA_TYPE, ExcelSheet, build_xlsx

content = build_xlsx(
    sheets=[
        ExcelSheet(
            name="Comisiones",
            headers=["Nombre", "Inicio", "Solicitud", "Aprobada"],
            rows=[["Ana", date(2026, 9, 29), datetime(2026, 9, 29, 14, 12, tzinfo=UTC), True]],
        ),
        ExcelSheet(name="Filtros", headers=["Filtro", "Valor"], rows=[["Unidad", "Física"]]),
    ],
    timezone=ZoneInfo("America/Bogota"),
)
await storage.upload("report.xlsx", content, content_type=EXCEL_MEDIA_TYPE)

Every sheet gets a bold header row that stays frozen while scrolling and carries an autofilter. Each column is as wide as its longest value, header included, between 8 and 60 characters, so nothing is cut and a long text does not swallow the screen. Text in a column that reaches 60 wraps inside its cell instead of hiding behind the next column.

A sheet name has 1 to 31 characters, none of [ ] : * ? / \, does not start or end with an apostrophe and is not repeated, ignoring case. Otherwise build_xlsx raises ValueError, since Excel would refuse to open the file.

A cell takes str, int, float, bool, date, datetime or None:

  • date and datetime are real Excel dates, shown as dd/mm/yyyy and dd/mm/yyyy hh:mm, so they sort and filter as dates, not as text.
  • A datetime with a timezone is moved to timezone before it is written, since a cell keeps only the clock time. Without timezone it raises ValueError: the kit does not guess, and a report in UTC would show every hour wrong. A naive datetime is written as it is.
  • A date before 1 March 1900 raises ValueError: Excel counts a 29 February 1900 that never existed, so it would show 28/02/1900 as 29/02/1900 and the earlier ones as #######. A datetime with a timezone is checked after it is moved to timezone.
  • bool is an Excel boolean, which each reader sees in their own language. Its column is wide enough for the longest word, such as VERDADERO.
  • None leaves the cell empty, and so does a row shorter than the headers.
  • Text is escaped, and the characters XML rejects are dropped: control characters, lone surrogates and U+FFFE and U+FFFF. NaN and infinities are written as text, because Excel cannot store them as numbers.

build_xlsx raises ValueError before writing anything when there are no sheets, a sheet has no headers, a row has more cells than headers, or a sheet name is empty, longer than 31 characters, repeated ignoring case, or uses [ ] : * ? / \.

Uses only the standard library, so it needs no extra.

Resilience

CircuitBreaker is domain-agnostic and shared by anything that calls out: after enough consecutive failures it opens, fails fast with CircuitOpenError instead of hammering a service that is already down, and closes again after a cooldown. RetryPolicy, which lives with the REST client, handles the retry side.

Settings

PersistenceKitSettings gathers what most services need: auth, storage, observability, AWS and job-service settings. Inherit from it and override only what is yours.

from persistence_kit import Database, PersistenceKitSettings


class Settings(PersistenceKitSettings):
    service_name: str = "my-api"
    key_status_history_database: Database = Database.MONGO
    memory_seed_role_codes: tuple[str, ...] = ("admin", "operator")

The factories read that object: get_identity_provider, get_token_verifier, get_export_storage, get_cache, get_mail_sender.

With STAGE=production the settings refuse to load, with a ValueError that names the variable, when CACHE_BACKEND is memory or AUTH_RATE_LIMIT_ENABLED is false. A per-process cache lets each replica count login attempts and accept encrypted envelopes on its own, and every deploy would reset them. dev and local start as before.

Environment variables

Variable Meaning
STAGE local (default), dev or production. production refuses CACHE_BACKEND=memory and AUTH_RATE_LIMIT_ENABLED=false
REPO_DATABASE memory, mongo, postgres or dynamodb. Default backend for every entity
CACHE_BACKEND memory, mongo or dynamodb. Defaults to memory, which production refuses. With dynamodb or mongo the auth rate limit is shared by every replica
CACHE_NAMESPACE Key prefix, so apps sharing a cache store do not collide
REST_SERVICE_URLS JSON map {"service": "base_url"} overriding registered REST base URLs
AUTH_RATE_LIMIT_ENABLED Turns the auth rate limit and the login lockout on or off. Off by default in local, on elsewhere; production refuses false
AUTH_LOCKOUT_PAIR_ATTEMPTS, AUTH_LOCKOUT_USER_ATTEMPTS, AUTH_LOCKOUT_CLIENT_ATTEMPTS Failed logins before the login lockout blocks a username from one address (5), a username from anywhere (20) and an address (20). 0 turns that scope off
AUTH_LOCKOUT_SECONDS How long the login lockout lasts, 900 by default
TRUSTED_PROXY_HOPS Proxies in front of the app that append to X-Forwarded-For. The auth rate limit counts the address the outermost one saw, so a forged header does not open a new bucket. 1 by default (one load balancer), 2 behind CloudFront and an ALB, 0 with no proxy
MONGO_DSN, MONGO_DB Mongo connection
POSTGRES_DSN Full DSN, if you prefer it to the pieces below
POSTGRES_USER, POSTGRES_PASSWORD, POSTGRES_HOST, POSTGRES_PORT, POSTGRES_DB Postgres connection
POSTGRES_SSL Set when the server requires TLS
DYNAMODB_REGION, DYNAMODB_TABLE_PREFIX DynamoDB connection
ENCRYPTED_PRIVATE_KEY Base64 PEM of the RSA private key that unwraps encrypted payloads
ENCRYPTED_TYPE memory (default, key generated at startup), local, or kms for AWS KMS
KMS_KEY_ID The KMS key that unwraps the AES key, required by kms
MAIL_BACKEND memory (default) or smtp
MAIL_FROM_ADDRESS, MAIL_FROM_NAME Default sender. The address is required by smtp
MAIL_SMTP_HOST, MAIL_SMTP_PORT The relay. Port defaults to 587
MAIL_SMTP_STARTTLS Upgrade the connection with STARTTLS. Defaults to true
MAIL_SMTP_USERNAME, MAIL_SMTP_PASSWORD Only for relays that require a login
MAIL_SMTP_TIMEOUT_SECONDS Connection and command timeout. Defaults to 30

Wiring it into an application

  1. Define your entities as dataclasses.
  2. Register them in a register_defaults() in your app.
  3. Call set_registry_initializer(register_defaults) at startup.
  4. Resolve repositories with get_repo(...), get_repo_view(...) or the FastAPI providers.
  5. Use ConfigRegistry and SeederProvider as bootstrap infrastructure only. What gets registered and seeded stays in your app.

Trying a change against an app on your machine

This is the everyday loop, and it needs no release, no PR and no TestPyPI. It assumes the kit and the app sit next to each other:

aux programación udea/
├── persistence_kit/          <- this repository
└── api_store_manager_v1/     <- the app that consumes it

Install the kit into the app's environment in editable mode, from the app:

cd api_store_manager_v1
.venv/Scripts/pip install -e ../persistence_kit      # Linux/macOS: .venv/bin/pip

From that moment the app imports your working copy. Edit the kit, rerun the app or its tests, and the change is already there. No reinstall between edits.

To go back to the published version:

.venv/Scripts/pip uninstall persistence-kit
poetry install

Two things that will confuse you if nobody warns you:

  • The reported version lies. In editable mode importlib.metadata.version("persistence-kit") keeps returning whatever the old dist-info said, even though the code running is your source. Trust persistence_kit.__file__, not the version string.
  • The app pins an exact version. api_store_manager_v1 asks for a pinned persistence-kit with its extras, so poetry install will happily undo your editable install. Redo it after any dependency change.

Run the kit's own tests with the app's interpreter, which already has every extra installed:

"../api_store_manager_v1/.venv/Scripts/python.exe" -m pytest -q

Only when the change has to be tried from a machine that is not yours does it make sense to publish a preview, which is the next section.

Adding something to the kit

Every capability here is built the same way. Follow the shape and your module will look like it was always part of the kit.

1. Start with the contract. Declare what the thing does before writing how. The repository pair uses ABC; everything else uses Protocol, so an implementation only has to match the shape, not inherit from anything.

from typing import Protocol


class Mailer(Protocol):
    async def send(self, *, to: str, subject: str, body: str) -> None: ...

Everything is async and arguments are keyword-only. Both rules exist so callers read clearly at the call site and so adding a parameter never breaks anyone.

2. Write two implementations, not one. A memory one and a real one. The memory one is not a toy: it is what tests and local development run against, and having it forces the contract to stay honest. InMemoryTTLCache next to the Mongo cache, MemorySecurityProvider next to Cognito, MemoryRestClient next to the httpx one.

3. Choose between them with a factory driven by settings. Never with an if scattered around the app. The factory reads an enum from settings, caches with lru_cache, and imports the provider lazily inside the builder so an unused backend never loads its dependency.

@lru_cache
def get_mailer(settings):
    if settings.mailer_backend is MailerBackend.SES:
        from persistence_kit.mailer.ses import SesMailer   # imported only if used
        return SesMailer(region=settings.aws_region)
    return InMemoryMailer()

4. Give the package its own error hierarchy. One base exception and specific subclasses, the way storage/errors.py and restclient/errors.py do it. Callers should be able to catch the whole family or one precise case.

5. Export it lazily. If it needs an optional dependency, add it to _OPTIONAL_EXPORTS in the root __init__.py and declare its extra in pyproject.toml. Someone who imports persistence_kit without that extra must get a message naming the extra, not an ImportError. tests/test_capabilities.py enforces this and will fail if you skip it.

6. Add tests next to the feature. Async tests carry @pytest.mark.asyncio.

Getting it merged. main is protected, so it goes through a pull request. Branch off main, keep the change to one capability, run the suite, and open the PR. Anything domain-specific, roles, business rules, product settings, belongs in the application that consumes the kit, not here.

Development

From the kit's root, with its own environment:

poetry lock
poetry install --with dev --all-extras
poetry run pytest -q

Current baseline: 901 tests passing (version 3.24.0). Async tests use pytest-asyncio in strict mode, so each one carries @pytest.mark.asyncio.

tests/test_cache_mongo.py runs the Mongo cache against a real server and is skipped unless PERSISTENCE_KIT_TEST_MONGO_DSN points at one, for example a throwaway docker run -d --rm -p 127.0.0.1:27047:27017 mongo:7 with PERSISTENCE_KIT_TEST_MONGO_DSN=mongodb://127.0.0.1:27047. Each test uses its own database and drops it.

tests/test_capabilities.py guards the lazy-import promise: it fails if merely importing persistence_kit drags in fastapi, pyjwt, boto3 or jinja2.

Releasing

The normal path, and the only one that produces an official version:

  1. Bump version in pyproject.toml inside the pull request.
  2. Merge it to main. The branch is protected, so it goes through a pull request.
  3. .github/workflows/publish-pypi.yml runs on every push to main. If there is no vX.Y.Z tag for the version in pyproject.toml, it builds, publishes to PyPI through Trusted Publishing once the pypi environment is approved, and then creates the GitHub Release vX.Y.Z on that commit. A merge that keeps the version publishes nothing. No tokens stored anywhere.

Publishing a GitHub Release by hand still works: the workflow checks that its tag points at main HEAD and publishes it.

For previews that others need to install before there is a release, use a X.Y.Z.devN version:

  • From GitHub: run the Publish Preview Package workflow, enter the version, and pick testpypi. It patches the version inside the CI run only, so no commit.
  • From your machine: bash ./scripts/publish-local.sh 3.9.2.dev1 testpypi, with a TestPyPI token exported as TWINE_PASSWORD.

Then, in the consuming project:

pip install --index-url https://test.pypi.org/simple/ \
            --extra-index-url https://pypi.org/simple persistence-kit==3.9.2.dev1

Neither PyPI nor TestPyPI lets you re-upload a version, so bump the devN on every iteration.

Further reading

Author: Andres Felipe Serrano Barrios · github.com/AndresFSerrano/persistence-kit

Metadata

Release files for persistence-kit 3.24.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for persistence-kit 3.24.0
File Size Uploaded Provenance Actions
persistence_kit-3.24.0.tar.gz 122.4 kB With provenance Details for persistence_kit-3.24.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for persistence-kit 3.24.0
File Interpreter ABI Platform Actions
persistence_kit-3.24.0-py3-none-any.whl Python 3 none any Details for persistence_kit-3.24.0-py3-none-any.whl

Total release size: 283.2 kB

Release files / persistence_kit-3.24.0.tar.gz

Download URL persistence_kit-3.24.0.tar.gz
Size 122.4 kB
Tags Source
SHA-256 checksum
How to use checksums
6eabe6aa51fca684b1a5819d9e67d6a1fe4c960216bff60460a200601be16319
BLAKE2b-256 checksum
How to use checksums
65a1e2ddec62a063872c509090b6d82c561f2e98bd74b4e5c7104c43b90c02fc
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 22, 2026.

Transparency log

Release files / persistence_kit-3.24.0-py3-none-any.whl

Download URL persistence_kit-3.24.0-py3-none-any.whl
Size 160.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
e693d4c670d5932a5103cdf97b73e781c24d8d34881b9af1a6e499cda656fd3c
BLAKE2b-256 checksum
How to use checksums
b95f01db4c8c0e187ab0fa1967cdcea3464d3c69be0bf70429f641b853d0ded0
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 22, 2026.

Transparency log

Security for persistence-kit 3.24.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

Both files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for persistence_kit-3.24.0.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 22, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

3.24.0 This release

2 release files Full provenance

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