autocrud


Nameautocrud JSON
Version 0.1.3 PyPI version JSON
download
home_pageNone
Summary自動產生 CRUD API 的 Python 函式庫
upload_time2025-07-24 17:25:46
maintainerNone
docs_urlNone
authorNone
requires_python>=3.11
licenseMIT License Copyright (c) 2025 HYChou0515 Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
keywords api automation crud fastapi rest
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # AutoCRUD

自動化 CRUD 系統,解決重複性 CRUD 操作的煩人問題。

## 目標

### 問題
CRUD 操作幾乎都長一樣,每次都要重複寫相同的代碼,很煩人。希望能有一個系統性的解決方案。

### 解決方案
建立一個自動化系統,輸入資料模型,自動產生完整的 CRUD API。

## 核心功能

### 1. 支援多種輸入格式
- `dataclasses` - Python 標準資料類
- `pydantic` - 資料驗證和序列化
- `typeddict` - 類型化字典

### 2. 自動產生 FastAPI CRUD 接口
- `GET /{resource}/{id}` - 取得單個資源
- `POST /{resource}` - 建立資源(自動產生 ID)
- `PUT /{resource}/{id}` - 更新資源
- `DELETE /{resource}/{id}` - 刪除資源

### 3. 靈活的儲存後端
支援簡單的 key-value 儲存,不一定要 SQL:
- **Memory** - 純內存儲存(快速、測試用、重啟後資料消失)
- **Disk** - 文件系統儲存(持久化、本地儲存)
- **S3** - 雲端對象儲存(未來實現)

### 4. 多種序列化格式支援
支援各種序列化方法,可根據需求選擇最適合的格式:
- **msgpack** - 高效二進制格式,體積小速度快
- **json** - 標準文本格式,易讀易調試
- **pickle** - Python 原生格式,支援複雜對象
- **其他** - 可擴展支援更多自訂格式

## 預期使用方式

```python
from dataclasses import dataclass
from autocrud import AutoCRUD, MemoryStorage, DiskStorage

@dataclass
class User:
    name: str
    email: str
    age: int

# 純內存儲存(演示用)
crud_memory = AutoCRUD(
    model=User,
    storage=MemoryStorage(),
    resource_name="users"
)

# 持久化磁碟儲存
crud_disk = AutoCRUD(
    model=User,
    storage=DiskStorage("./data"),
    resource_name="users"
)

# 產生 FastAPI 應用
app = crud_disk.create_fastapi_app(title="使用者管理 API")
```

## 開發計劃

### 第1步:資料類型轉換器 ✅
- ✅ 建立統一的資料類型轉換器
- ✅ 支援 dataclasses, pydantic, typeddict 轉換
- ✅ 實現多種序列化格式:msgpack, json, pickle

### 第2步:儲存抽象層 ✅
- ✅ 定義通用的 key-value 儲存接口
- ✅ 實現 Memory 儲存後端(純內存、演示用)
- ✅ 實現 Disk 儲存後端(文件系統持久化)
- 🔄 實現 S3 儲存後端
- ✅ 支援基本操作:get, set, delete, exists
- ✅ 可設定序列化格式

### 第3步:FastAPI 自動產生 ✅
- ✅ 基於資料模型自動產生 CRUD routing
- ✅ 自動 ID 產生和管理
- ✅ 統一錯誤處理和響應格式
- ✅ 自動產生 Pydantic 請求/響應模型
- ✅ 支援 OpenAPI 文件自動產生
- ✅ 健康檢查端點

## 快速開始

### 安裝dependency
```bash
pip install fastapi uvicorn
```

### 基本使用
```python
from dataclasses import dataclass
from autocrud import AutoCRUD, DiskStorage

@dataclass
class User:
    name: str
    email: str
    age: int

# 建立 CRUD 系統
storage = DiskStorage("./data")
crud = AutoCRUD(model=User, storage=storage, resource_name="users")

# 產生 FastAPI 應用
app = crud.create_fastapi_app(title="使用者管理 API")

# 啟動服務器
if __name__ == "__main__":
    import uvicorn
    uvicorn.run(app, host="0.0.0.0", port=8000)
```

### API 端點
- `POST /api/v1/users` - 建立使用者
- `GET /api/v1/users/{id}` - 取得使用者
- `PUT /api/v1/users/{id}` - 更新使用者  
- `DELETE /api/v1/users/{id}` - 刪除使用者
- `GET /api/v1/users` - 列出所有使用者
- `GET /health` - 健康檢查

### 自動產生文件
- Swagger UI: `http://localhost:8000/docs`
- ReDoc: `http://localhost:8000/redoc`

## 技術棧

- **FastAPI** - Web 框架
- **Pydantic** - 資料驗證
- **dependency-injector** - dependency注入
- **msgpack** - 高效序列化
- **json** - 標準序列化
- **pickle** - Python 原生序列化
            

Raw data

            {
    "_id": null,
    "home_page": null,
    "name": "autocrud",
    "maintainer": null,
    "docs_url": null,
    "requires_python": ">=3.11",
    "maintainer_email": null,
    "keywords": "api, automation, crud, fastapi, rest",
    "author": null,
    "author_email": "HYChou0515 <hychou.svm@example.com>",
    "download_url": "https://files.pythonhosted.org/packages/04/9f/c81f352ea9d8983e4a0b1257a6c581b7e853d329b82842e480e301e5750f/autocrud-0.1.3.tar.gz",
    "platform": null,
    "description": "# AutoCRUD\n\n\u81ea\u52d5\u5316 CRUD \u7cfb\u7d71\uff0c\u89e3\u6c7a\u91cd\u8907\u6027 CRUD \u64cd\u4f5c\u7684\u7169\u4eba\u554f\u984c\u3002\n\n## \u76ee\u6a19\n\n### \u554f\u984c\nCRUD \u64cd\u4f5c\u5e7e\u4e4e\u90fd\u9577\u4e00\u6a23\uff0c\u6bcf\u6b21\u90fd\u8981\u91cd\u8907\u5beb\u76f8\u540c\u7684\u4ee3\u78bc\uff0c\u5f88\u7169\u4eba\u3002\u5e0c\u671b\u80fd\u6709\u4e00\u500b\u7cfb\u7d71\u6027\u7684\u89e3\u6c7a\u65b9\u6848\u3002\n\n### \u89e3\u6c7a\u65b9\u6848\n\u5efa\u7acb\u4e00\u500b\u81ea\u52d5\u5316\u7cfb\u7d71\uff0c\u8f38\u5165\u8cc7\u6599\u6a21\u578b\uff0c\u81ea\u52d5\u7522\u751f\u5b8c\u6574\u7684 CRUD API\u3002\n\n## \u6838\u5fc3\u529f\u80fd\n\n### 1. \u652f\u63f4\u591a\u7a2e\u8f38\u5165\u683c\u5f0f\n- `dataclasses` - Python \u6a19\u6e96\u8cc7\u6599\u985e\n- `pydantic` - \u8cc7\u6599\u9a57\u8b49\u548c\u5e8f\u5217\u5316\n- `typeddict` - \u985e\u578b\u5316\u5b57\u5178\n\n### 2. \u81ea\u52d5\u7522\u751f FastAPI CRUD \u63a5\u53e3\n- `GET /{resource}/{id}` - \u53d6\u5f97\u55ae\u500b\u8cc7\u6e90\n- `POST /{resource}` - \u5efa\u7acb\u8cc7\u6e90\uff08\u81ea\u52d5\u7522\u751f ID\uff09\n- `PUT /{resource}/{id}` - \u66f4\u65b0\u8cc7\u6e90\n- `DELETE /{resource}/{id}` - \u522a\u9664\u8cc7\u6e90\n\n### 3. \u9748\u6d3b\u7684\u5132\u5b58\u5f8c\u7aef\n\u652f\u63f4\u7c21\u55ae\u7684 key-value \u5132\u5b58\uff0c\u4e0d\u4e00\u5b9a\u8981 SQL\uff1a\n- **Memory** - \u7d14\u5167\u5b58\u5132\u5b58\uff08\u5feb\u901f\u3001\u6e2c\u8a66\u7528\u3001\u91cd\u555f\u5f8c\u8cc7\u6599\u6d88\u5931\uff09\n- **Disk** - \u6587\u4ef6\u7cfb\u7d71\u5132\u5b58\uff08\u6301\u4e45\u5316\u3001\u672c\u5730\u5132\u5b58\uff09\n- **S3** - \u96f2\u7aef\u5c0d\u8c61\u5132\u5b58\uff08\u672a\u4f86\u5be6\u73fe\uff09\n\n### 4. \u591a\u7a2e\u5e8f\u5217\u5316\u683c\u5f0f\u652f\u63f4\n\u652f\u63f4\u5404\u7a2e\u5e8f\u5217\u5316\u65b9\u6cd5\uff0c\u53ef\u6839\u64da\u9700\u6c42\u9078\u64c7\u6700\u9069\u5408\u7684\u683c\u5f0f\uff1a\n- **msgpack** - \u9ad8\u6548\u4e8c\u9032\u5236\u683c\u5f0f\uff0c\u9ad4\u7a4d\u5c0f\u901f\u5ea6\u5feb\n- **json** - \u6a19\u6e96\u6587\u672c\u683c\u5f0f\uff0c\u6613\u8b80\u6613\u8abf\u8a66\n- **pickle** - Python \u539f\u751f\u683c\u5f0f\uff0c\u652f\u63f4\u8907\u96dc\u5c0d\u8c61\n- **\u5176\u4ed6** - \u53ef\u64f4\u5c55\u652f\u63f4\u66f4\u591a\u81ea\u8a02\u683c\u5f0f\n\n## \u9810\u671f\u4f7f\u7528\u65b9\u5f0f\n\n```python\nfrom dataclasses import dataclass\nfrom autocrud import AutoCRUD, MemoryStorage, DiskStorage\n\n@dataclass\nclass User:\n    name: str\n    email: str\n    age: int\n\n# \u7d14\u5167\u5b58\u5132\u5b58\uff08\u6f14\u793a\u7528\uff09\ncrud_memory = AutoCRUD(\n    model=User,\n    storage=MemoryStorage(),\n    resource_name=\"users\"\n)\n\n# \u6301\u4e45\u5316\u78c1\u789f\u5132\u5b58\ncrud_disk = AutoCRUD(\n    model=User,\n    storage=DiskStorage(\"./data\"),\n    resource_name=\"users\"\n)\n\n# \u7522\u751f FastAPI \u61c9\u7528\napp = crud_disk.create_fastapi_app(title=\"\u4f7f\u7528\u8005\u7ba1\u7406 API\")\n```\n\n## \u958b\u767c\u8a08\u5283\n\n### \u7b2c1\u6b65\uff1a\u8cc7\u6599\u985e\u578b\u8f49\u63db\u5668 \u2705\n- \u2705 \u5efa\u7acb\u7d71\u4e00\u7684\u8cc7\u6599\u985e\u578b\u8f49\u63db\u5668\n- \u2705 \u652f\u63f4 dataclasses, pydantic, typeddict \u8f49\u63db\n- \u2705 \u5be6\u73fe\u591a\u7a2e\u5e8f\u5217\u5316\u683c\u5f0f\uff1amsgpack, json, pickle\n\n### \u7b2c2\u6b65\uff1a\u5132\u5b58\u62bd\u8c61\u5c64 \u2705\n- \u2705 \u5b9a\u7fa9\u901a\u7528\u7684 key-value \u5132\u5b58\u63a5\u53e3\n- \u2705 \u5be6\u73fe Memory \u5132\u5b58\u5f8c\u7aef\uff08\u7d14\u5167\u5b58\u3001\u6f14\u793a\u7528\uff09\n- \u2705 \u5be6\u73fe Disk \u5132\u5b58\u5f8c\u7aef\uff08\u6587\u4ef6\u7cfb\u7d71\u6301\u4e45\u5316\uff09\n- \ud83d\udd04 \u5be6\u73fe S3 \u5132\u5b58\u5f8c\u7aef\n- \u2705 \u652f\u63f4\u57fa\u672c\u64cd\u4f5c\uff1aget, set, delete, exists\n- \u2705 \u53ef\u8a2d\u5b9a\u5e8f\u5217\u5316\u683c\u5f0f\n\n### \u7b2c3\u6b65\uff1aFastAPI \u81ea\u52d5\u7522\u751f \u2705\n- \u2705 \u57fa\u65bc\u8cc7\u6599\u6a21\u578b\u81ea\u52d5\u7522\u751f CRUD routing\n- \u2705 \u81ea\u52d5 ID \u7522\u751f\u548c\u7ba1\u7406\n- \u2705 \u7d71\u4e00\u932f\u8aa4\u8655\u7406\u548c\u97ff\u61c9\u683c\u5f0f\n- \u2705 \u81ea\u52d5\u7522\u751f Pydantic \u8acb\u6c42/\u97ff\u61c9\u6a21\u578b\n- \u2705 \u652f\u63f4 OpenAPI \u6587\u4ef6\u81ea\u52d5\u7522\u751f\n- \u2705 \u5065\u5eb7\u6aa2\u67e5\u7aef\u9ede\n\n## \u5feb\u901f\u958b\u59cb\n\n### \u5b89\u88dddependency\n```bash\npip install fastapi uvicorn\n```\n\n### \u57fa\u672c\u4f7f\u7528\n```python\nfrom dataclasses import dataclass\nfrom autocrud import AutoCRUD, DiskStorage\n\n@dataclass\nclass User:\n    name: str\n    email: str\n    age: int\n\n# \u5efa\u7acb CRUD \u7cfb\u7d71\nstorage = DiskStorage(\"./data\")\ncrud = AutoCRUD(model=User, storage=storage, resource_name=\"users\")\n\n# \u7522\u751f FastAPI \u61c9\u7528\napp = crud.create_fastapi_app(title=\"\u4f7f\u7528\u8005\u7ba1\u7406 API\")\n\n# \u555f\u52d5\u670d\u52d9\u5668\nif __name__ == \"__main__\":\n    import uvicorn\n    uvicorn.run(app, host=\"0.0.0.0\", port=8000)\n```\n\n### API \u7aef\u9ede\n- `POST /api/v1/users` - \u5efa\u7acb\u4f7f\u7528\u8005\n- `GET /api/v1/users/{id}` - \u53d6\u5f97\u4f7f\u7528\u8005\n- `PUT /api/v1/users/{id}` - \u66f4\u65b0\u4f7f\u7528\u8005  \n- `DELETE /api/v1/users/{id}` - \u522a\u9664\u4f7f\u7528\u8005\n- `GET /api/v1/users` - \u5217\u51fa\u6240\u6709\u4f7f\u7528\u8005\n- `GET /health` - \u5065\u5eb7\u6aa2\u67e5\n\n### \u81ea\u52d5\u7522\u751f\u6587\u4ef6\n- Swagger UI: `http://localhost:8000/docs`\n- ReDoc: `http://localhost:8000/redoc`\n\n## \u6280\u8853\u68e7\n\n- **FastAPI** - Web \u6846\u67b6\n- **Pydantic** - \u8cc7\u6599\u9a57\u8b49\n- **dependency-injector** - dependency\u6ce8\u5165\n- **msgpack** - \u9ad8\u6548\u5e8f\u5217\u5316\n- **json** - \u6a19\u6e96\u5e8f\u5217\u5316\n- **pickle** - Python \u539f\u751f\u5e8f\u5217\u5316",
    "bugtrack_url": null,
    "license": "MIT License\n        \n        Copyright (c) 2025 HYChou0515\n        \n        Permission is hereby granted, free of charge, to any person obtaining a copy\n        of this software and associated documentation files (the \"Software\"), to deal\n        in the Software without restriction, including without limitation the rights\n        to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\n        copies of the Software, and to permit persons to whom the Software is\n        furnished to do so, subject to the following conditions:\n        \n        The above copyright notice and this permission notice shall be included in all\n        copies or substantial portions of the Software.\n        \n        THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\n        IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\n        FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\n        AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\n        LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\n        OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\n        SOFTWARE.",
    "summary": "\u81ea\u52d5\u7522\u751f CRUD API \u7684 Python \u51fd\u5f0f\u5eab",
    "version": "0.1.3",
    "project_urls": {
        "Documentation": "https://HYChou0515.github.io/autocrud/",
        "Homepage": "https://github.com/HYChou0515/autocrud",
        "Issues": "https://github.com/HYChou0515/autocrud/issues",
        "Repository": "https://github.com/HYChou0515/autocrud.git"
    },
    "split_keywords": [
        "api",
        " automation",
        " crud",
        " fastapi",
        " rest"
    ],
    "urls": [
        {
            "comment_text": null,
            "digests": {
                "blake2b_256": "783ec4a583dc8a3bb10b00674655eb70146c52251e1171bcdd53429d85724aab",
                "md5": "2434310d982e527737d8247148952e06",
                "sha256": "167a98de8a447c97df1fd094c6bca6468d65488f573eabfbd74666677e25d4a8"
            },
            "downloads": -1,
            "filename": "autocrud-0.1.3-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "2434310d982e527737d8247148952e06",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.11",
            "size": 24354,
            "upload_time": "2025-07-24T17:25:44",
            "upload_time_iso_8601": "2025-07-24T17:25:44.551031Z",
            "url": "https://files.pythonhosted.org/packages/78/3e/c4a583dc8a3bb10b00674655eb70146c52251e1171bcdd53429d85724aab/autocrud-0.1.3-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": null,
            "digests": {
                "blake2b_256": "049fc81f352ea9d8983e4a0b1257a6c581b7e853d329b82842e480e301e5750f",
                "md5": "a6976d2517692e123e2cd9b5524b52f9",
                "sha256": "b46d67f60434aee9f7a8751a3fee681d29d73c69e108745bca944cf2bff756f0"
            },
            "downloads": -1,
            "filename": "autocrud-0.1.3.tar.gz",
            "has_sig": false,
            "md5_digest": "a6976d2517692e123e2cd9b5524b52f9",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.11",
            "size": 120363,
            "upload_time": "2025-07-24T17:25:46",
            "upload_time_iso_8601": "2025-07-24T17:25:46.406753Z",
            "url": "https://files.pythonhosted.org/packages/04/9f/c81f352ea9d8983e4a0b1257a6c581b7e853d329b82842e480e301e5750f/autocrud-0.1.3.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2025-07-24 17:25:46",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "HYChou0515",
    "github_project": "autocrud",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "lcname": "autocrud"
}
        
Elapsed time: 0.49774s