Skip to main content

A type-safe ORM generator for Python, creating fully type-hinted database clients from plain dataclass definitions.

Project description

DataclassQL

DataclassQL 是一个基于 平凡 dataclass 定义 的 ORM 生成器, 可生成类型提示完整精巧的数据库客户端.

模型文件保持干净、直观, 无需起手加一堆导入, 也没有 mapped_column()Annotation 或额外的基类继承, 只需要 dataclass


设计目标

  • 最小语法负担: 模型定义仅是合法平凡的 Python dataclass, Python 即 DSL
  • 常用路径简洁: 常用的定义只需要写少量的代码
  • 静态转换模型: 类似 C++ 模板, 工具生成静态的序列化、反序列化模型代码, 不会比手写更慢. 这是一个生成例子, 其中包含静态枚举转换和可空字段判断: 静态序列化示意
  • 静态类型安全: 模型定义和生成代码全都类型安全. 本库作为 prisma client python 的精神继承者, 致力于完成如下体验:

prisma client python 示例


示例

from dataclasses import dataclass
from datetime import datetime

@dataclass
class User:
    id: int
    name: str
    email: str
    last_login: datetime

    def index(self):
        yield self.name
        yield self.last_login

    def unique_index(self):
        yield self.name, self.email

写出如下代码时:

from dclassql import client

client.user.insert({
    "name": "Alice",
    "email": "test@example.com",
})

将在类型空间得到报错:

error: Argument of type "dict[str, str]" cannot be assigned to parameter "data" of type "UserInsertDict" in function "insert"
    "last_login" is required in "UserInsertDict" (reportArgumentType)

安装

uv add dclassql

当前状态

DataclassQL 仍在早期开发阶段, 但不是无根浮萍, 我已经在另外两个项目里大量使用, 目前基于其他项目的反馈来更新.

一份更长的例子

class UserStatus(Enum):
    ACTIVE = "active"
    DISABLED = "disabled"

class UserType(StrEnum):
    ADMIN = "admin"
    MEMBER = "member"
    GUEST = "guest"

class UserVIPLevel(IntEnum):
    LEVEL_1 = 1
    LEVEL_2 = 2
    LEVEL_3 = 3

@dataclass
class Address:
    id: int
    location: str
    user_id: int
    user: 'User'

    def foreign_key(self):
        yield self.user.id == self.user_id, User.addresses


@dataclass
class BirthDay:
    user_id: int
    user: 'User'
    date: datetime

    def primary_key(self):
        return self.user_id

    def foreign_key(self):
        yield self.user.id == self.user_id, User.birthday


@dataclass
class Book:
    id: int
    name: str
    users: list['UserBook']

    def index(self):
        return self.name


@dataclass
class UserBook:
    user_id: int
    book_id: int
    user: 'User'
    book: Book
    created_at: datetime

    def primary_key(self):
        return (self.user_id, self.book_id)

    def index(self):
        yield self.created_at

    def foreign_key(self):
        yield self.user.id == self.user_id, User.books
        yield self.book.id == self.book_id, Book.users


@dataclass
class User:
    id: int
    name: str
    email: str
    last_login: datetime
    status: UserStatus
    type: UserType
    vip_level: UserVIPLevel | None

    birthday: BirthDay | None
    addresses: list[Address]
    books: list[UserBook]

    def index(self):
        yield self.name
        yield self.name, self.email
        yield self.last_login

    def unique_index(self):
        yield self.name, self.email

生成的代码请见: https://github.com/myuanz/dataclassql/blob/master/tests/results.py

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

dclassql-0.3.0.tar.gz (40.9 kB view details)

Uploaded Source

Built Distribution

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

dclassql-0.3.0-py3-none-any.whl (54.3 kB view details)

Uploaded Python 3

File details

Details for the file dclassql-0.3.0.tar.gz.

File metadata

  • Download URL: dclassql-0.3.0.tar.gz
  • Upload date:
  • Size: 40.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.10.10 {"installer":{"name":"uv","version":"0.10.10","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Manjaro Linux","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for dclassql-0.3.0.tar.gz
Algorithm Hash digest
SHA256 8406e914c11a82fbd5529dbe663b82bc94e60e0b7e1d93f43937716b40e9d76c
MD5 e59498065c84c0389a6b2cc0db6d4f6b
BLAKE2b-256 a84afcf51e6c5246274b23fe2a1492a70e567b0f9e1b0ae705eb3c901a800777

See more details on using hashes here.

File details

Details for the file dclassql-0.3.0-py3-none-any.whl.

File metadata

  • Download URL: dclassql-0.3.0-py3-none-any.whl
  • Upload date:
  • Size: 54.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.10.10 {"installer":{"name":"uv","version":"0.10.10","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Manjaro Linux","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for dclassql-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 3e01b3cdd062cca0a6afd3ed5db2cddf6c2b5a0b90090b1e2026e1349e65d578
MD5 6009c79ca14488fde75dad7ae8a547d9
BLAKE2b-256 db985f67ad4df112528b1d5ce11bcf59100b0cd8d70f344fd6d2db8655e9cba5

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