My first Python package: pydantic-sqlalchemy-deco
Today, I released my first Python package published on pypi!
It doesn’t do much, but it’s something that has been bugging me for a while. I often include Pydantic models in SQLAlchemy-created databases. Normally, I just use JSONB and use the regular ol’ model_validate functions. While this works, I’d prefer it to be automatic. Here’s where pydantic-sqlalchemy-deco comes in.
It’s a simple TypeDecorator class that automatically converts between Pydantic and SQLAlchemy JSONB types.
Installation
Since it’s pip installable, you can use pip install pydantic_sqlalchemy_deco.
Usage
All you need to do is set the Mapped[] type and include PydanticJSON in the mapped_column(), like such:
from pydantic_sqlalchemy_deco.decorator import PydanticJSON
...
# Define Pydantic model somewhere:
class MyCustomModel(BaseModel):
custom_id: int = 0
# Define your SQLAlchemy model:
class MyEntry(Base):
custom_data: Mapped[MyCustomModel] = mapped_column(PydanticJSON(MyCustomModel))
Then it’s easy as doing
entry = MyEntry(
custom_data=MyCustomModel(custom_id=1234)
)
...
print(entry.custom_data.custom_id)
Pretty simple, eh?
Further remarks
I never realized how easy it is to build Python packages. It’s basically:
- write your code
- define your pyproject.toml file
- then setup the default Github action to push releases to pypi.
That’s it! I was expecting a whole mess of generating and keeping track of tokens, writing and rewriting Github actions, and some CLI work to get it published.
I’ll eventually need to do tests, but I can hold off on those til later.