DataclassQL
DataclassQL 是一个基于 平凡 dataclass 的 ORM 生成器, 可生成类型提示完整精巧的数据库客户端.
模型文件保持干净、直观, 无需起手加一堆导入, 也没有 mapped_column()、Annotation 或额外的基类继承, 大部分时候只需要 @dataclass
设计目标
- 静态类型友好: 全程可获得完美的补全体验。本库作为 prisma client python 的精神继承者, 致力于完成如下体验:
- 最小语法负担: 模型定义是平凡的 Python dataclass, Python 本身就是 DSL
- 约定大于配置: 常用定义只需写少量代码
- 零成本抽象: 工具生成的数据库转换代码与手写版本有相同的速度
示例
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 = Client()
client.user.insert({
"name": "Alice",
"email": "test@example.com",
# 这里缺少 last_login
})
将在类型空间得到报错:
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
Metadata
Release files for dclassql 0.5.2
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| dclassql-0.5.2.tar.gz | 49.3 kB | Without provenance | Details for dclassql-0.5.2.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| dclassql-0.5.2-py3-none-any.whl | Python 3 | none | any | Details for dclassql-0.5.2-py3-none-any.whl |
Total release size: 115.5 kB
Release files / dclassql-0.5.2.tar.gz
| Download URL | dclassql-0.5.2.tar.gz |
|---|---|
| Size | 49.3 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
45b86b65bb7c26078fc5c038af914a94916b3c2d4246e56611cd76353f246522
|
|
BLAKE2b-256 checksum How to use checksums |
43c44e7be4000e7958b78206243d5e98654c17fa90f606b7566645bd14672e22
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.12.10 {"installer":{"name":"uv","version":"0.12.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}
|
Release files / dclassql-0.5.2-py3-none-any.whl
| Download URL | dclassql-0.5.2-py3-none-any.whl |
|---|---|
| Size | 66.2 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
3bfa477dacfb1479554182b3f42de806756a69c36d3bf3da7a74a4997338b157
|
|
BLAKE2b-256 checksum How to use checksums |
f58284d897d03c2eda3f074abd45a219b9bf5f13c9d088b089980969bf355317
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.12.10 {"installer":{"name":"uv","version":"0.12.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}
|
Security for dclassql 0.5.2
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.