{"id":16026961,"url":"https://github.com/nacosdev/sqlalchemy_api","last_synced_at":"2025-03-18T03:32:07.305Z","repository":{"id":190325396,"uuid":"682364289","full_name":"nacosdev/sqlalchemy_api","owner":"nacosdev","description":"Library that exposes serialized and validated REST endpoint for SQLAlchemy models.","archived":false,"fork":false,"pushed_at":"2023-08-30T21:39:44.000Z","size":933,"stargazers_count":7,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-02-28T06:52:51.342Z","etag":null,"topics":["crud","fastapi","sqlalchemy","starlette"],"latest_commit_sha":null,"homepage":"https://nacosdev.github.io/sqlalchemy_api/","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/nacosdev.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE.txt","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-08-24T02:36:31.000Z","updated_at":"2024-05-30T15:32:46.000Z","dependencies_parsed_at":"2024-10-27T17:17:32.076Z","dependency_job_id":"57c55b6f-3fae-4f65-b09d-63c8c90dec5f","html_url":"https://github.com/nacosdev/sqlalchemy_api","commit_stats":{"total_commits":21,"total_committers":2,"mean_commits":10.5,"dds":"0.23809523809523814","last_synced_commit":"68847309debd3b6d602c3d75d4f4f7397df752e0"},"previous_names":["nacosdev/sqlalchemy_api"],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nacosdev%2Fsqlalchemy_api","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nacosdev%2Fsqlalchemy_api/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nacosdev%2Fsqlalchemy_api/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nacosdev%2Fsqlalchemy_api/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/nacosdev","download_url":"https://codeload.github.com/nacosdev/sqlalchemy_api/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243901527,"owners_count":20366252,"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","fastapi","sqlalchemy","starlette"],"created_at":"2024-10-08T20:04:20.677Z","updated_at":"2025-03-18T03:32:07.004Z","avatar_url":"https://github.com/nacosdev.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"\u003cp align=\"center\"\u003e\n  \u003ca\u003e\u003cimg src=\"https://raw.githubusercontent.com/nacosdev/sqlalchemy_api/main/docs/assets/images/sqlalchemy_api.png\" alt=\"SQLalchemyAPI\"\u003e\u003c/a\u003e\n\u003c/p\u003e\n\n\u003cp align=\"center\"\u003e\n  \u003ca href=\"https://github.com/nacosdev/sqlalchemy_api/actions?query=workflow%3ATests+event%3Apush+branch%3Amain\" target=\"_blank\"\u003e\n      \u003cimg src=\"https://github.com/nacosdev/sqlalchemy_api/workflows/Tests/badge.svg?event=push\u0026branch=main\" alt=\"Test\"\u003e\n  \u003c/a\u003e\n  \u003ca href=\"https://codecov.io/gh/nacosdev/sqlalchemy_api\"\u003e\n      \u003cimg src=\"https://codecov.io/gh/nacosdev/sqlalchemy_api/branch/main/graph/badge.svg\" alt=\"Coverage\"\u003e\n  \u003c/a\u003e\n  \u003ca href=\"https://pypi.org/project/sqlalchemy-api\" target=\"_blank\"\u003e\n      \u003cimg src=\"https://img.shields.io/pypi/v/sqlalchemy-api?color=%2334D058\u0026label=pypi%20package\" alt=\"Package version\"\u003e\n  \u003c/a\u003e\n  \u003ca href=\"https://pypi.org/project/sqlalchemy-api\" target=\"_blank\"\u003e\n      \u003cimg src=\"https://img.shields.io/pypi/pyversions/fastapi.svg?color=%2334D058\" alt=\"Supported Python versions\"\u003e\n  \u003c/a\u003e\n\u003c/p\u003e\n\nSQLAlchemy API is a library that helps to turn the [SQLAlchemy](https://www.sqlalchemy.org/) models into a REST API. It uses the power of [Pydantic 2](https://docs.pydantic.dev/dev-v2/), to validate and serialize the data. This is a framework-agnostic library that can be used with any web framework. Currently, it provides support for [Starlette](https://www.starlette.io/) and [FastAPI](https://fastapi.tiangolo.com/).\n\n---\n\n**Documentation**: \u003ca href=\"https://nacosdev.github.io/sqlalchemy_api\" target=\"_blank\"\u003ehttps://nacosdev.github.io/sqlalchemy_api\u003c/a\u003e\n\n**Source Code**: \u003ca href=\"https://github.com/nacosdev/sqlalchemy_api\" target=\"_blank\"\u003ehttps://github.com/nacosdev/sqlalchemy_api\u003c/a\u003e\n\n---\n\n**Table of Contents**\n\n- [Features](#features)\n- [Requirements](#requirements)\n- [Installation](#installation)\n- [Example](#example)\n- [License](#license)\n\n---\n\n### Features \n\n- [x] Mount CRUD endpoints for a SQLAlchemy model.\n- [x] Automatic serialization and validation of the data using Pydantic.\n- [x] Automatic pagination of the data.\n- [x] Allow querying the data using different operators depending on the column data type.\n- [x] Support [Starlette](https://github.com/encode/starlette)\n- [x] Support Support [FastAPI](https://github.com/tiangolo/fastapi)\n- [ ] Support [Blacksheep](https://github.com/Neoteroi/BlackSheep) 🚧\n- [ ] Support custom queries. 🚧\n- [ ] Autentication. 🚧\u003cbr/\u003e\n\n---\n\n### Requirements\n\n- Python\u003e=3.7\n- SQLAlchemy\u003e=1.4\n- Pydantic\u003e=2\n\n---\n\n### Installation\n\n```bash\npip install sqlalchemy-api\n```\n\n---\n\n### Example\n\n#### Create it\n\n- Create a file `main.py` with SQLAlchemy models and mount the crud using one of the adapters, in this example we will use the FastAPI adapter:\n\n```python\nfrom sqlalchemy_api.adapters.fastapi_crud import APICrud\nfrom sqlalchemy import create_engine, ForeignKey\nfrom sqlalchemy.orm import relationship, mapped_column, Mapped\nfrom sqlalchemy.ext.declarative import declarative_base\nfrom fastapi import FastAPI\nfrom typing import List\n\nBase = declarative_base()\n\nengine = create_engine(\n    \"sqlite:///example.db\",\n    connect_args={\"check_same_thread\": False},\n)\n\nclass User(Base):\n    __tablename__ = \"user\"\n    id: Mapped[int] = mapped_column(primary_key=True)\n    name: Mapped[str] = mapped_column(default=\"John Doe\")\n    age: Mapped[int] = mapped_column(nullable=False)\n    posts: Mapped[List['Post']] = relationship(back_populates=\"user\")\n\nclass Post(Base):\n    __tablename__ = \"post\"\n    id: Mapped[int] = mapped_column(primary_key=True)\n    title: Mapped[str] = mapped_column()\n    content: Mapped[str] = mapped_column()\n    user_id: Mapped[int] = mapped_column(ForeignKey(\"user.id\"), nullable=False)\n    user: Mapped['User'] = relationship(back_populates=\"posts\")\n\nBase.metadata.create_all(engine)  # Create tables\n\nuser_crud_router = APICrud(User, engine)\npost_crud_router = APICrud(Post, engine)\n\napp = FastAPI()\napp.include_router(user_crud_router, prefix=\"/user\", tags=[\"User\"])\napp.include_router(post_crud_router, prefix=\"/post\", tags=[\"Post\"])\n```\n\nYou will also need an ASGI server and FastAPI to be able to run this app, both are optional dependencies of SQLAlchemy API:\n\n```bash\npip install sqlalchemy-api[fastapi]\n```\n\n#### Run it\n\n```bash\nuvicorn main:app --reload\n```\n\n#### Use it\nEndpoints are automatically generated for the defined models and the FastAPI adapter provides automatic Swagger documentation, you can access [localhost:8000/docs](localhost:8000/docs) to interact with them:\n\n\u003cp align=\"center\"\u003e\n  \u003ca\u003e\u003cimg src=\"https://raw.githubusercontent.com/nacosdev/sqlalchemy_api/main/docs/assets/images/swagger-1.png\" alt=\"Swagger\"\u003e\u003c/a\u003e\n\u003c/p\u003e\n\nSQLAlchemyAPI also provides different operators depending on the column data type, to filter the data:\n\n\u003cp align=\"center\"\u003e\n  \u003ca\u003e\u003cimg src=\"https://raw.githubusercontent.com/nacosdev/sqlalchemy_api/main/docs/assets/images/swagger-2.png\" alt=\"Swagger2\"\u003e\u003c/a\u003e\n\u003c/p\u003e\n\nThe data returned is automatically paginated and serialized, including the relationships defined in the models:\n\n\u003cp align=\"center\"\u003e\n  \u003ca\u003e\u003cimg src=\"https://raw.githubusercontent.com/nacosdev/sqlalchemy_api/main/docs/assets/images/swagger-3.png\" alt=\"Swagger3\"\u003e\u003c/a\u003e\n\u003c/p\u003e\n\nPost data is automatically validated and serialized using Pydantic, for example, if you try to create a user wihout the required `age` field, you will get an error like this:\n\n\u003cp align=\"center\"\u003e\n  \u003ca\u003e\u003cimg src=\"https://raw.githubusercontent.com/nacosdev/sqlalchemy_api/main/docs/assets/images/swagger-4.png\" alt=\"Swagger4\"\u003e\u003c/a\u003e\n\u003c/p\u003e\n\n---\n\n### License\n\n`sqlalchemy-api` is distributed under the terms of the [MIT](https://spdx.org/licenses/MIT.html) license.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnacosdev%2Fsqlalchemy_api","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fnacosdev%2Fsqlalchemy_api","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnacosdev%2Fsqlalchemy_api/lists"}