{"id":16804099,"url":"https://github.com/cmhopesunshine/cherry-orm","last_synced_at":"2025-03-22T02:31:21.268Z","repository":{"id":192420865,"uuid":"686691149","full_name":"CMHopeSunshine/cherry-orm","owner":"CMHopeSunshine","description":"基于 SQLAlchemy 和 Pydantic 的异步 Python ORM / Python asynchronous ORM based on SQLAlchemy and Pydantic ","archived":false,"fork":false,"pushed_at":"2024-04-23T06:52:12.000Z","size":888,"stargazers_count":10,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2024-04-23T11:12:33.669Z","etag":null,"topics":["mysql","object-relational-mapper","orm","postgresql","pydantic","python","sqlalchemy","sqlite"],"latest_commit_sha":null,"homepage":"https://cherry.cherishmoon.top","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/CMHopeSunshine.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2023-09-03T16:25:48.000Z","updated_at":"2024-05-09T10:43:49.080Z","dependencies_parsed_at":null,"dependency_job_id":"cf03ead0-35ef-469b-b745-2cc5a3d65d90","html_url":"https://github.com/CMHopeSunshine/cherry-orm","commit_stats":null,"previous_names":["cmhopesunshine/cherry-orm"],"tags_count":9,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/CMHopeSunshine%2Fcherry-orm","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/CMHopeSunshine%2Fcherry-orm/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/CMHopeSunshine%2Fcherry-orm/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/CMHopeSunshine%2Fcherry-orm/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/CMHopeSunshine","download_url":"https://codeload.github.com/CMHopeSunshine/cherry-orm/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":244898010,"owners_count":20528331,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["mysql","object-relational-mapper","orm","postgresql","pydantic","python","sqlalchemy","sqlite"],"created_at":"2024-10-13T09:44:13.098Z","updated_at":"2025-03-22T02:31:20.657Z","avatar_url":"https://github.com/CMHopeSunshine.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"\u003cp align=\"center\"\u003e\n    \u003ch1 align=\"center\"\u003eCherry ORM\u003c/h1\u003e\n    \u003cp align=\"center\"\u003ePython 异步 ORM\u003c/p\u003e\n\u003c/p\u003e\n\u003cp align=\"center\"\u003e\n    \u003ca href=\"./LICENSE\"\u003e\n        \u003cimg src=\"https://img.shields.io/github/license/CMHopeSunshine/cherry-orm.svg\" alt=\"license\"\u003e\n    \u003c/a\u003e\n    \u003ca href=\"https://pypi.python.org/pypi/cherry-orm\"\u003e\n        \u003cimg src=\"https://img.shields.io/pypi/v/cherry-orm.svg\" alt=\"pypi\"\u003e\n    \u003c/a\u003e\n    \u003ca href=\"https://www.python.org/\"\u003e\n        \u003cimg src=\"https://img.shields.io/badge/python-3.9+-blue.svg\" alt=\"python\"\u003e\n    \u003c/a\u003e\n\u003c/p\u003e\n\n\u003cp align=\"center\"\u003e\n    \u003cstrong\u003e简体中文\u003c/strong\u003e\n    ·\n    \u003ca href=\"https://github.com/CMHopeSunshine/cherry-orm/blob/master/README_EN.md\"\u003eEnglish\u003c/a\u003e\n\u003c/p\u003e\n\n## 简介\n\n`Cherry ORM` 是一个 Python 的异步对象关系映射（ORM）库，它基于 [SQLAlchemy Core](https://www.sqlalchemy.org/) 和 [Pydantic V2](https://docs.pydantic.dev/latest/) 构建。\n\n它的一切设计都是为了简单易用，极大地减少开发者的数据库操作成本，提高开发效率，让开发者更专注于业务逻辑的实现。\n\n## 安装\n\n- 使用 pip: `pip install cherry-orm`\n- 使用 Poetry: `poetry add cherry-orm`\n- 使用 PDM: `pdm add cherry-orm`\n\n## 文档\n\n-\u003e [文档地址](https://cherry.cherishmoon.top)\n\n## 示例\n\n```python\nfrom datetime import date\nfrom typing import List, Optional\n\nimport cherry\n\ndb = cherry.Database(\"sqlite+aiosqlite:///:memory:\")\n\n\nclass Student(cherry.Model):\n    id: int = cherry.Field(primary_key=True)\n    name: str = cherry.Field(unique=True, index=True)\n    age: int\n    birthday: date = cherry.Field(default_factory=date.today)\n    school: cherry.ForeignKey[Optional[\"School\"]] = None\n\n    cherry_config = cherry.CherryConfig(tablename=\"student\", database=db)\n\n\nclass School(cherry.Model):\n    id: cherry.PrimaryKey[int]\n    name: str = cherry.Field(unique=True, index=True)\n    students: cherry.ReverseRelation[List[Student]] = []\n\n    cherry_config = cherry.CherryConfig(tablename=\"school\", database=db)\n\n\nasync def main():\n    await db.init()\n\n    # 插入\n    school = await School(id=1, name=\"school 1\").insert()\n    student1 = await Student(id=1, name=\"student 1\", age=15, school=school).insert()\n    await Student(id=2, name=\"student 2\", age=18, school=school).insert()\n    await Student(id=3, name=\"student 3\", age=20, school=school).insert()\n\n    # 更新\n    student1.age += 1\n    await student1.save()\n    # or\n    await student1.update(age=19)\n\n    # 获取关联的模型\n    await school.fetch_related(School.students)\n    assert len(school.students) == 3\n\n    # 条件查询\n    # Pythonic 风格\n    student2: Student = await Student.filter(Student.name == \"student 2\").get()\n    # Django 风格\n    student2: Student = await Student.filter(name=\"student 2\").get()\n\n    students: List[Student] = await Student.filter(Student.age \u003e= 18).all()\n\n    # 聚合查询\n    student_nums: int = await Student.filter(Student.age \u003e= 18).count()\n    assert len(students) == student_nums\n    student_age_avg: Optional[int] = await Student.select().avg(Student.age)\n\n    # 查询时预取关联模型\n    student_with_school: Student = (\n        await Student.filter(Student.name == \"student 3\")\n        .prefetch_related(Student.school)\n        .get()\n    )\n\n    # 选择更新\n    await Student.select().update(birthday=date(2023, 10, 1))\n    # 选择删除\n    await Student.filter(Student.age \u003e= 20).delete()\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcmhopesunshine%2Fcherry-orm","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcmhopesunshine%2Fcherry-orm","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcmhopesunshine%2Fcherry-orm/lists"}