{"id":25284076,"url":"https://github.com/bigrivi/better_crud","last_synced_at":"2025-06-25T22:05:14.426Z","repository":{"id":248338161,"uuid":"822838797","full_name":"bigrivi/better_crud","owner":"bigrivi","description":"Better crud for fastapi, without constraints","archived":false,"fork":false,"pushed_at":"2025-06-16T05:14:45.000Z","size":1818,"stargazers_count":2,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-06-16T06:23:55.869Z","etag":null,"topics":["crud-generator","curd","fastapi","library","pydanic","python","restful-api"],"latest_commit_sha":null,"homepage":"https://bigrivi.github.io/better_crud/","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/bigrivi.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,"zenodo":null}},"created_at":"2024-07-01T23:56:17.000Z","updated_at":"2025-06-16T05:12:49.000Z","dependencies_parsed_at":"2024-07-14T06:41:18.221Z","dependency_job_id":"8eb7f40c-eebb-429f-a2fe-ad88ee67b259","html_url":"https://github.com/bigrivi/better_crud","commit_stats":null,"previous_names":["bigrivi/fastapi_crud","bigrivi/better_crud"],"tags_count":18,"template":false,"template_full_name":null,"purl":"pkg:github/bigrivi/better_crud","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bigrivi%2Fbetter_crud","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bigrivi%2Fbetter_crud/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bigrivi%2Fbetter_crud/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bigrivi%2Fbetter_crud/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/bigrivi","download_url":"https://codeload.github.com/bigrivi/better_crud/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bigrivi%2Fbetter_crud/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":261960505,"owners_count":23236572,"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":["crud-generator","curd","fastapi","library","pydanic","python","restful-api"],"created_at":"2025-02-12T20:20:00.337Z","updated_at":"2025-06-25T22:05:14.413Z","avatar_url":"https://github.com/bigrivi.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"\u003cdiv align=\"center\"\u003e\n  \u003ch1\u003eBetterCRUD\u003c/h1\u003e\n\u003c/div\u003e\n\u003cp align=\"center\" markdown=1\u003e\n  \u003ci\u003eA better CRUD library for FastAPI.\u003c/i\u003e\u003c/br\u003e\n  \u003csub\u003eFastAPI CRUD routing library based on class view, you can control everything\u003c/sub\u003e\n\u003c/p\u003e\n\u003cp align=\"center\" markdown=1\u003e\n\u003ca href=\"https://github.com/bigrivi/better_crud/actions/workflows/pytest.yml\" target=\"_blank\"\u003e\n  \u003cimg src=\"https://github.com/bigrivi/better_crud/actions/workflows/pytest.yml/badge.svg\" alt=\"Tests\"/\u003e\n\u003c/a\u003e\n\u003ca href=\"https://pypi.org/project/better_crud/\" target=\"_blank\"\u003e\n  \u003cimg src=\"https://img.shields.io/pypi/v/better_crud?color=%2334D058\u0026label=pypi%20package\" alt=\"PyPi Version\"/\u003e\n\u003c/a\u003e\n\u003ca href=\"https://pypi.org/project/better_crud/\" target=\"_blank\"\u003e\n  \u003cimg src=\"https://img.shields.io/pypi/pyversions/better_crud.svg?color=%2334D058\" alt=\"Supported Python Versions\"/\u003e\n\u003c/a\u003e\n\u003ca href=\"https://codecov.io/github/bigrivi/better_crud\" target=\"_blank\"\u003e\n \u003cimg src=\"https://codecov.io/github/bigrivi/better_crud/graph/badge.svg?token=MEMUT1FH4K\"/\u003e\n \u003c/a\u003e\n\u003c/p\u003e\n\n---\n\n**Documentation**: \u003ca href=\"https://bigrivi.github.io/better_crud/\" target=\"_blank\"\u003ehttps://bigrivi.github.io/better_crud/\u003c/a\u003e\n\n**Source Code**: \u003ca href=\"https://github.com/bigrivi/better_crud\" target=\"_blank\"\u003ehttps://github.com/bigrivi/better_crud\u003c/a\u003e\n\n---\n\nBetterCRUD is a library that can quickly generate CRUD routes for you without any intrusion to your code. You can still control everything. When you are troubled by a large number of repeated CRUD routes, I believe it can help you, saving you a lot of time and allowing you to focus more on business logic.\n\nBetterCRUD is reliable, fully tested, and used in project production environments.\n\nBetterCRUD is a way to dynamically generate routes by combining your model with the crud decorator,I believe bring you a different development experience\n\nYou only need to configure some crud options and define your model to produce powerful CRUD functions\n\n```python\n@crud(\n    router,\n    dto={\n        \"create\": PetCreate,\n        \"update\": PetUpdate\n    },\n    serialize={\n        \"base\": PetPublic,\n    },\n    **other_options\n)\nclass PetController():\n    service: PetService = Depends(PetService)\n\n```\n\n## Requirements\n- **Python:** Version 3.9 or newer.\n- **FastAPI:** BetterCRUD is built to work with FastAPI, so having FastAPI in your project is essential.\n- \u003cb\u003eSQLAlchemy:\u003c/b\u003e Version 2.0.30 or newer. BetterCRUD uses SQLAlchemy for database operations.\n- \u003cb\u003ePydantic:\u003c/b\u003e Version 2.7.3 or newer. BetterCRUD leverages Pydantic models for data validation and serialization.\n\n## Installation\n```bash\npip install better-crud\n```\n\n## Features\n- Fully Async, Synchronization is not supported\n- Less boilerplate code\n- Configuring static type support\n- More flexible custom configuration，Less invasive\n- Compatible with both class views and functional views\n- Rich filter, pagination, and sorting support\n- Automated relationship support, query and storage\n- Extensible custom backend\n\n\n## Default Routes\n\n| Route                | Method     | Description |\n| -------------------- | ---------- | ----------- |\n| /resource            | **GET**    | Get Many    |\n| /resource/{id}       | **GET**    | Get One     |\n| /resource            | **POST**   | Create One  |\n| /resource/bulk       | **POST**   | Create Many |\n| /resource/{id}       | **PUT**    | Update One  |\n| /resource/{ids}/bulk | **PUT**    | Update Many |\n| /resource/{ids}      | **DELETE** | Delete Many |\n\n\n\n## Minimal Example\n\nPrerequisites,Prepare our db, Only asynchronous mode is supported,aiomysql or aiosqlite\n**db.py**\n```python\nfrom sqlalchemy.orm import DeclarativeBase, declared_attr\nfrom typing import AsyncGenerator\nfrom sqlalchemy.orm import sessionmaker\nfrom sqlalchemy.pool import NullPool\nfrom sqlalchemy.ext.asyncio import AsyncSession, create_async_engine\nDATABASE_URL = \"sqlite+aiosqlite:///crud.db\"\n\nclass MappedBase(DeclarativeBase):\n    @declared_attr.directive\n    def __tablename__(cls) -\u003e str:\n        return cls.__name__.lower()\n\n\nclass Base(MappedBase):\n    __abstract__ = True\n\n\nengine = create_async_engine(\n    DATABASE_URL,\n    echo=False,\n    poolclass=NullPool\n)\n\nSessionLocal = sessionmaker(\n    autocommit=False,\n    autoflush=False,\n    bind=engine,\n    class_=AsyncSession,\n    expire_on_commit=False,\n)\n\n\nasync def get_session() -\u003e AsyncGenerator[AsyncSession, None]:\n    async with SessionLocal() as session:\n        yield session\n\n\nasync def init_db():\n    async with engine.begin() as conn:\n        await conn.run_sync(MappedBase.metadata.create_all)\n```\n\nFirst Define Your Model And Schema\n\n**model.py**\n```python\nfrom sqlalchemy import String, Integer, ForeignKey\nfrom sqlalchemy.orm import Mapped, mapped_column\nfrom .db import Base\n\n\nclass Pet(Base):\n    __tablename__ = \"pet\"\n    id: Mapped[int] = mapped_column(Integer, primary_key=True)\n    name: Mapped[str] = mapped_column(String(100))\n    description: Mapped[str] = mapped_column(String(100))\n\n```\n\n**schema.py**\n```python\nfrom typing import Optional, List\nfrom pydantic import BaseModel\n\n\nclass PetBase(BaseModel):\n    name: Optional[str] = None\n    description: Optional[str] = None\n\n\nclass PetPublic(PetBase):\n    id: int\n\n\nclass PetCreate(PetBase):\n    pass\n\n\nclass PetUpdate(PetBase):\n    pass\n\n```\n\nNext we need to create a service:\n\n**service.py**\n```python\nfrom better_crud.service.sqlalchemy import SqlalchemyCrudService\nfrom .model import Pet\n\n\nclass PetService(SqlalchemyCrudService[Pet]):\n    def __init__(self):\n        super().__init__(Pet)\n\n```\n\nNext we need to define the controller and decorate it with the crud decorator\nSure the controller is just a normal class,The crud decorator gives it super powers\n**controller.py**\n```python\nfrom fastapi import APIRouter, Depends\nfrom better_crud import crud\nfrom .schema import PetCreate, PetUpdate, PetPublic\nfrom .service import PetService\n\npet_router = APIRouter()\n\n\n@crud(\n    pet_router,\n    dto={\n        \"create\": PetCreate,\n        \"update\": PetUpdate\n    },\n    serialize={\n        \"base\": PetPublic,\n    }\n)\nclass PetController():\n    service: PetService = Depends(PetService)\n\n```\n\nNext we can register router to the fastapi routing system\n\n**main.py**\n```python\nfrom better_crud import BetterCrudGlobalConfig\nfrom fastapi import FastAPI\nfrom contextlib import asynccontextmanager\nfrom .db import get_session, init_db\n\nBetterCrudGlobalConfig.init(\n    backend_config={\n        \"sqlalchemy\": {\n            \"db_session\": get_session\n        }\n    }\n)\n\n\n@asynccontextmanager\nasync def lifespan(_: FastAPI):\n    await init_db()\n    # Shutdown\n    yield\n\napp = FastAPI(lifespan=lifespan)\n\n\ndef register_router():\n    from app.controller import pet_router\n    app.include_router(pet_router, prefix=\"/pet\")\n\n\nregister_router()\n\n\n```\n\nCongratulations, your first CRUD route has been created！\n\n\n![OpenAPI Route Overview](https://raw.githubusercontent.com/bigrivi/better_crud/main/resources/RouteOverview.png)\n\n## Author\n\n👤 **bigrivi**\n* GitHub: [bigrivi](https://github.com/bigrivi)\n\n## 🤝 Contributing\n\nContributions are what make the open source community such an amazing place to learn, inspire, and create. Any contributions you make are **greatly appreciated**.\n\nIf you have a suggestion that would make this better, please fork the repo and create a pull request. You can also simply open an issue with the tag \"enhancement\".\nDon't forget to give the project a star! Thanks again!\n\n1. Fork the Project\n2. Create your Feature Branch (`git checkout -b feature/AmazingFeature`)\n3. Commit your Changes (`git commit -m 'Add some AmazingFeature'`)\n4. Push to the Branch (`git push origin feature/AmazingFeature`)\n5. Open a Pull Request\n\n## Credits\n\nThis project draws inspiration from the following frameworks:\n\n- [nestjsx-crud](https://github.com/nestjsx/crud)\n\n## UseCases\n\nBetterCrud was used in the following projects:\n\n- [black-panther](https://github.com/bigrivi/black-panther)\n\n## License\n\n[MIT](LICENSE)","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbigrivi%2Fbetter_crud","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbigrivi%2Fbetter_crud","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbigrivi%2Fbetter_crud/lists"}