Skip to main content

Custom types for SQLModel/SQLalchemy

Project description

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

Project details


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.9.tar.gz (3.0 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.9-py3-none-any.whl (4.0 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: sqltypes-0.1.9.tar.gz
  • Upload date:
  • Size: 3.0 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.9.tar.gz
Algorithm Hash digest
SHA256 0c7fcb844e76251852f0bbea218acd07f92c8e6710af0dec567832a1757b8743
MD5 0a20cd80adf89c214a05d7127912dc10
BLAKE2b-256 fbbd988e264025ace0d058559393fc2ba499751c1896421ab4f6e6a28c25ec24

See more details on using hashes here.

File details

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

File metadata

  • Download URL: sqltypes-0.1.9-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.9-py3-none-any.whl
Algorithm Hash digest
SHA256 c265da4fbd9cfa0f2f8d37d2cc26a1ec0ce6bf30d9d9ebd7be0f218a6af802bd
MD5 abf5d1820927c8d4bf2c31cb8673a73e
BLAKE2b-256 06e22c3bc2a24fca3a0939b987dc01e92231ef39036529385478a7d3658509fe

See more details on using hashes here.

Supported by

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