af-db-types-json
Custom SQLAlchemy column types for PostgreSQL JSONB columns, backed by Pydantic models. Handles serialisation, deserialisation, and validation transparently — your columns read and write Pydantic model instances directly.
Installation
pip install af-db-types-json
# or with Poetry:
poetry add af-db-types-json
Types
JSONList[T] — JSONB array as a list of Pydantic models
Returns an empty list when the column value is NULL.
from pydantic import BaseModel
from sqlalchemy.orm import mapped_column, Mapped
from allfly.db.types.json import JSONList
class Tag(BaseModel):
name: str
colour: str
class Article(Base):
__tablename__ = "articles"
tags: Mapped[list[Tag]] = mapped_column(JSONList(Tag), default=list)
JSONObject[T] — JSONB object as a single Pydantic model
Returns None when the column value is NULL or validation fails (logs an error, does not raise).
from allfly.db.types.json import JSONObject
class Address(BaseModel):
street: str
city: str
class User(Base):
__tablename__ = "users"
address: Mapped[Address | None] = mapped_column(JSONObject(Address), nullable=True)
VersionedJSONObject[T] — JSONB object with automatic version-based dispatch
Stores a __type discriminator alongside your data so the correct model version is always deserialised, even as your schema evolves.
from allfly.db.types.json import VersionedJSONObject, VersionedModel
class PayloadV1(VersionedModel):
version: float = 1.0
data: str
class PayloadV2(VersionedModel):
version: float = 2.0
data: str
extra: str
class Event(Base):
__tablename__ = "events"
payload: Mapped[PayloadV1 | PayloadV2 | None] = mapped_column(
VersionedJSONObject(PayloadV1), nullable=True
)
Rows written with PayloadV1 will deserialise as PayloadV1. Rows written with PayloadV2 will deserialise as PayloadV2. No migration needed.
VersionedModel — base class for versioned JSONB models
Subclasses must declare a version class attribute. The model_dump() method automatically injects a __type key (the class name) used for resolution.
from allfly.db.types.json import VersionedModel
class MyModelV1(VersionedModel):
version: float = 1.0
value: str
Configuring package scanning
By default VersionedModel.resolve_from_data() scans the src package (backwards compatible with the allfly-quest-core-api project). For any other project, call set_scan_packages once at app startup before any JSONB columns are read:
# In your app's startup / lifespan
from allfly.db.types.json import VersionedModel
import myapp
VersionedModel.set_scan_packages([myapp])
# Multiple packages are supported
VersionedModel.set_scan_packages([myapp, myapp_plugins])
# Disable scanning entirely (if you never use VersionedJSONObject)
VersionedModel.set_scan_packages([])
Logging
Validation errors in JSONObject and VersionedJSONObject are logged to the allfly.db.types.json.types logger namespace and return None rather than raising — this prevents a single bad row from crashing the application.
import logging
logging.getLogger("allfly.db.types.json").setLevel(logging.DEBUG)
Metadata
Release files for af-db-types-json 0.0.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | |
|---|---|---|---|---|
| af_db_types_json-0.0.1.tar.gz | 4.3 kB | With provenance | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| af_db_types_json-0.0.1-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 10.2 kB
Release files / af_db_types_json-0.0.1.tar.gz
| Download URL | af_db_types_json-0.0.1.tar.gz |
|---|---|
| Size | 4.3 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
fa56a8998e1a40f475b70f488372ea8b8009390ff77254410d6230a2453baaa6
|
|
BLAKE2b-256 checksum How to use checksums |
c467eabaf221c449b5a1c693c2ca91c4ac22ce6a7828d7efc02e757637b28f2a
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.13.12
|
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 May 12, 2026.
Transparency logRelease files / af_db_types_json-0.0.1-py3-none-any.whl
| Download URL | af_db_types_json-0.0.1-py3-none-any.whl |
|---|---|
| Size | 5.9 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
a0f7daad05e3f472dd0f59fb23bfb09f3edac4dc901963564dd688783e473d61
|
|
BLAKE2b-256 checksum How to use checksums |
6a7b58d0f5f17a06a0cd1cec080c0648945e461f54d71d6bd921595e43e0a276
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.13.12
|
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 May 12, 2026.
Transparency logSecurity for af-db-types-json 0.0.1
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
All 2 files in this release have been uploaded with provenance metadata.
Attestation for af_db_types_json-0.0.1.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 May 12, 2026.
Transparency logA 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.