SQL Types
Custom type decorators for SQLModel/SQLAlchemy with Pydantic validation
Installation
pip install sqltypes
Quick Start
from typing import Sequence
from pydantic import BaseModel
from sqlmodel import Field, SQLModel
from sqltypes import ValidatedJSON, SpaceDelimitedList
class User(BaseModel):
name: str
age: int
class Article(SQLModel, table=True):
id: int | None = Field(default=None, primary_key=True)
tags: Sequence[str] = Field(sa_type=SpaceDelimitedList)
author: User = Field(sa_type=ValidatedJSON(User))
Available Types
ValidatedJSON(T, name?)
Stores any Pydantic model or complex type as JSON with automatic validation.
config: Config = Field(sa_type=ValidatedJSON(Config))
SpaceDelimitedList
Stores sequences as space-delimited strings.
tags: Sequence[str] = Field(sa_type=SpaceDelimitedList)
# Database: "python sql database"
# Python: ["python", "sql", "database"]
ValidatedStr(LiteralType)
Validates strings against Pydantic literal types.
from typing import Literal
Status = Literal["pending", "active", "completed"]
status: Status = Field(sa_type=ValidatedStr(Status))
Custom Types
Use CustomTypeMeta to create your own types:
from sqltypes import CustomTypeMeta
from sqlalchemy.types import String
CommaSeparatedList = CustomTypeMeta(
'CommaSeparatedList',
(), {},
Impl=String,
dump=lambda lst: ','.join(lst),
parse=lambda s: s.split(',')
)
Or use CustomStringMeta for string-based types:
from sqltypes import CustomStringMeta
class CommaSeparatedList(metaclass=CustomStringMeta,
dump=lambda lst: ','.join(lst),
parse=lambda s: s.split(',')):
...
Links
- PyPI: https://pypi.org/project/sqltypes/
- Repository: https://github.com/marciclabas/sqltypes
Metadata
Release files for sqltypes 0.1.11
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| sqltypes-0.1.11.tar.gz | 3.1 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| sqltypes-0.1.11-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 7.1 kB
Release files / sqltypes-0.1.11.tar.gz
| Download URL | sqltypes-0.1.11.tar.gz |
|---|---|
| Size | 3.1 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
6ee94b4f5dcd37de58dfeae84cd3d06e6e680f7debd2010667af92d1c7156b5f
|
|
BLAKE2b-256 checksum How to use checksums |
78b64f3ef66099f9df692168cfedf69656ac12635fedf08cfa233ddf0e078a14
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.11.10
|
Release files / sqltypes-0.1.11-py3-none-any.whl
| Download URL | sqltypes-0.1.11-py3-none-any.whl |
|---|---|
| Size | 4.0 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
45cabbe32801e866e63c2a26dff70cd8492c958ea7379739753da466191d51e4
|
|
BLAKE2b-256 checksum How to use checksums |
af2f35d88656f4034b5b68382baba73abd68c1f59b629c32716d7377a41a0e17
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.11.10
|