Custom SQLAlchemy JSONB column types backed by Pydantic models
Project description
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)
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 af_db_types_json-0.0.1.tar.gz.
File metadata
- Download URL: af_db_types_json-0.0.1.tar.gz
- Upload date:
- Size: 4.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
fa56a8998e1a40f475b70f488372ea8b8009390ff77254410d6230a2453baaa6
|
|
| MD5 |
9af6bbc8e289c7b272ffa094338d4329
|
|
| BLAKE2b-256 |
c467eabaf221c449b5a1c693c2ca91c4ac22ce6a7828d7efc02e757637b28f2a
|
Provenance
The following attestation bundles were made for af_db_types_json-0.0.1.tar.gz:
Publisher:
af-db-types-json-publish.yml on travelallfly/allfly-py-libs
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
af_db_types_json-0.0.1.tar.gz -
Subject digest:
fa56a8998e1a40f475b70f488372ea8b8009390ff77254410d6230a2453baaa6 - Sigstore transparency entry: 1518984523
- Sigstore integration time:
-
Permalink:
travelallfly/allfly-py-libs@8aacec2d45376751fa6ddb18e3fad0b0cff5797a -
Branch / Tag:
refs/heads/master - Owner: https://github.com/travelallfly
-
Access:
private
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
af-db-types-json-publish.yml@8aacec2d45376751fa6ddb18e3fad0b0cff5797a -
Trigger Event:
workflow_dispatch
-
Statement type:
File details
Details for the file af_db_types_json-0.0.1-py3-none-any.whl.
File metadata
- Download URL: af_db_types_json-0.0.1-py3-none-any.whl
- Upload date:
- Size: 5.9 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 |
a0f7daad05e3f472dd0f59fb23bfb09f3edac4dc901963564dd688783e473d61
|
|
| MD5 |
978dafaf824365b67418f77f2bb1b496
|
|
| BLAKE2b-256 |
6a7b58d0f5f17a06a0cd1cec080c0648945e461f54d71d6bd921595e43e0a276
|
Provenance
The following attestation bundles were made for af_db_types_json-0.0.1-py3-none-any.whl:
Publisher:
af-db-types-json-publish.yml on travelallfly/allfly-py-libs
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
af_db_types_json-0.0.1-py3-none-any.whl -
Subject digest:
a0f7daad05e3f472dd0f59fb23bfb09f3edac4dc901963564dd688783e473d61 - Sigstore transparency entry: 1518984533
- Sigstore integration time:
-
Permalink:
travelallfly/allfly-py-libs@8aacec2d45376751fa6ddb18e3fad0b0cff5797a -
Branch / Tag:
refs/heads/master - Owner: https://github.com/travelallfly
-
Access:
private
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
af-db-types-json-publish.yml@8aacec2d45376751fa6ddb18e3fad0b0cff5797a -
Trigger Event:
workflow_dispatch
-
Statement type: