Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/cmhopesunshine/cherry-orm
基于 SQLAlchemy 和 Pydantic 的异步 Python ORM / Python asynchronous ORM based on SQLAlchemy and Pydantic
https://github.com/cmhopesunshine/cherry-orm
mysql object-relational-mapper orm postgresql pydantic python sqlalchemy sqlite
Last synced: 11 days ago
JSON representation
基于 SQLAlchemy 和 Pydantic 的异步 Python ORM / Python asynchronous ORM based on SQLAlchemy and Pydantic
- Host: GitHub
- URL: https://github.com/cmhopesunshine/cherry-orm
- Owner: CMHopeSunshine
- License: mit
- Created: 2023-09-03T16:25:48.000Z (about 1 year ago)
- Default Branch: master
- Last Pushed: 2024-04-23T06:52:12.000Z (7 months ago)
- Last Synced: 2024-04-23T11:12:33.669Z (7 months ago)
- Topics: mysql, object-relational-mapper, orm, postgresql, pydantic, python, sqlalchemy, sqlite
- Language: Python
- Homepage: https://cherry.cherishmoon.top
- Size: 867 KB
- Stars: 10
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
Cherry ORM
Python 异步 ORM
简体中文
·
English## 简介
`Cherry ORM` 是一个 Python 的异步对象关系映射(ORM)库,它基于 [SQLAlchemy Core](https://www.sqlalchemy.org/) 和 [Pydantic V2](https://docs.pydantic.dev/latest/) 构建。
它的一切设计都是为了简单易用,极大地减少开发者的数据库操作成本,提高开发效率,让开发者更专注于业务逻辑的实现。
## 安装
- 使用 pip: `pip install cherry-orm`
- 使用 Poetry: `poetry add cherry-orm`
- 使用 PDM: `pdm add cherry-orm`## 文档
-> [文档地址](https://cherry.cherishmoon.top)
## 示例
```python
from datetime import date
from typing import List, Optionalimport cherry
db = cherry.Database("sqlite+aiosqlite:///:memory:")
class Student(cherry.Model):
id: int = cherry.Field(primary_key=True)
name: str = cherry.Field(unique=True, index=True)
age: int
birthday: date = cherry.Field(default_factory=date.today)
school: cherry.ForeignKey[Optional["School"]] = Nonecherry_config = cherry.CherryConfig(tablename="student", database=db)
class School(cherry.Model):
id: cherry.PrimaryKey[int]
name: str = cherry.Field(unique=True, index=True)
students: cherry.ReverseRelation[List[Student]] = []cherry_config = cherry.CherryConfig(tablename="school", database=db)
async def main():
await db.init()# 插入
school = await School(id=1, name="school 1").insert()
student1 = await Student(id=1, name="student 1", age=15, school=school).insert()
await Student(id=2, name="student 2", age=18, school=school).insert()
await Student(id=3, name="student 3", age=20, school=school).insert()# 更新
student1.age += 1
await student1.save()
# or
await student1.update(age=19)# 获取关联的模型
await school.fetch_related(School.students)
assert len(school.students) == 3# 条件查询
# Pythonic 风格
student2: Student = await Student.filter(Student.name == "student 2").get()
# Django 风格
student2: Student = await Student.filter(name="student 2").get()students: List[Student] = await Student.filter(Student.age >= 18).all()
# 聚合查询
student_nums: int = await Student.filter(Student.age >= 18).count()
assert len(students) == student_nums
student_age_avg: Optional[int] = await Student.select().avg(Student.age)# 查询时预取关联模型
student_with_school: Student = (
await Student.filter(Student.name == "student 3")
.prefetch_related(Student.school)
.get()
)# 选择更新
await Student.select().update(birthday=date(2023, 10, 1))
# 选择删除
await Student.filter(Student.age >= 20).delete()
```