Skip to main content

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

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

sqltypes-0.1.11.tar.gz (3.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

sqltypes-0.1.11-py3-none-any.whl (4.0 kB view details)

Uploaded Python 3

File details

Details for the file sqltypes-0.1.11.tar.gz.

File metadata

  • Download URL: sqltypes-0.1.11.tar.gz
  • Upload date:
  • Size: 3.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.10

File hashes

Hashes for sqltypes-0.1.11.tar.gz
Algorithm Hash digest
SHA256 6ee94b4f5dcd37de58dfeae84cd3d06e6e680f7debd2010667af92d1c7156b5f
MD5 883d6610371fca514dcb92300264a72a
BLAKE2b-256 78b64f3ef66099f9df692168cfedf69656ac12635fedf08cfa233ddf0e078a14

See more details on using hashes here.

File details

Details for the file sqltypes-0.1.11-py3-none-any.whl.

File metadata

  • Download URL: sqltypes-0.1.11-py3-none-any.whl
  • Upload date:
  • Size: 4.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.10

File hashes

Hashes for sqltypes-0.1.11-py3-none-any.whl
Algorithm Hash digest
SHA256 45cabbe32801e866e63c2a26dff70cd8492c958ea7379739753da466191d51e4
MD5 71ff5343ba2239715e51ac8be5ea3a42
BLAKE2b-256 af2f35d88656f4034b5b68382baba73abd68c1f59b629c32716d7377a41a0e17

See more details on using hashes here.

Release history Release notifications | RSS feed

This release

0.1.11 This release

2 files

0.1.10

2 files

0.1.9

2 files

0.1.8

2 files

0.1.7

2 files

0.1.6

2 files

0.1.5

2 files

0.1.4

2 files

0.1.3

2 files

0.1.2

2 files

0.1.1

2 files

0.1.0

2 files

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page