Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/nacosdev/sqlalchemy_api
Library that exposes serialized and validated REST endpoint for SQLAlchemy models.
https://github.com/nacosdev/sqlalchemy_api
crud fastapi sqlalchemy starlette
Last synced: 18 days ago
JSON representation
Library that exposes serialized and validated REST endpoint for SQLAlchemy models.
- Host: GitHub
- URL: https://github.com/nacosdev/sqlalchemy_api
- Owner: nacosdev
- License: mit
- Created: 2023-08-24T02:36:31.000Z (about 1 year ago)
- Default Branch: main
- Last Pushed: 2023-08-30T21:39:44.000Z (about 1 year ago)
- Last Synced: 2024-10-15T20:17:51.518Z (about 1 month ago)
- Topics: crud, fastapi, sqlalchemy, starlette
- Language: Python
- Homepage: https://nacosdev.github.io/sqlalchemy_api/
- Size: 911 KB
- Stars: 7
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.txt
Awesome Lists containing this project
README
SQLAlchemy 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/).
---
**Documentation**: https://nacosdev.github.io/sqlalchemy_api
**Source Code**: https://github.com/nacosdev/sqlalchemy_api
---
**Table of Contents**
- [Features](#features)
- [Requirements](#requirements)
- [Installation](#installation)
- [Example](#example)
- [License](#license)---
### Features
- [x] Mount CRUD endpoints for a SQLAlchemy model.
- [x] Automatic serialization and validation of the data using Pydantic.
- [x] Automatic pagination of the data.
- [x] Allow querying the data using different operators depending on the column data type.
- [x] Support [Starlette](https://github.com/encode/starlette)
- [x] Support Support [FastAPI](https://github.com/tiangolo/fastapi)
- [ ] Support [Blacksheep](https://github.com/Neoteroi/BlackSheep) 🚧
- [ ] Support custom queries. 🚧
- [ ] Autentication. 🚧---
### Requirements
- Python>=3.7
- SQLAlchemy>=1.4
- Pydantic>=2---
### Installation
```bash
pip install sqlalchemy-api
```---
### Example
#### Create it
- 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:
```python
from sqlalchemy_api.adapters.fastapi_crud import APICrud
from sqlalchemy import create_engine, ForeignKey
from sqlalchemy.orm import relationship, mapped_column, Mapped
from sqlalchemy.ext.declarative import declarative_base
from fastapi import FastAPI
from typing import ListBase = declarative_base()
engine = create_engine(
"sqlite:///example.db",
connect_args={"check_same_thread": False},
)class User(Base):
__tablename__ = "user"
id: Mapped[int] = mapped_column(primary_key=True)
name: Mapped[str] = mapped_column(default="John Doe")
age: Mapped[int] = mapped_column(nullable=False)
posts: Mapped[List['Post']] = relationship(back_populates="user")class Post(Base):
__tablename__ = "post"
id: Mapped[int] = mapped_column(primary_key=True)
title: Mapped[str] = mapped_column()
content: Mapped[str] = mapped_column()
user_id: Mapped[int] = mapped_column(ForeignKey("user.id"), nullable=False)
user: Mapped['User'] = relationship(back_populates="posts")Base.metadata.create_all(engine) # Create tables
user_crud_router = APICrud(User, engine)
post_crud_router = APICrud(Post, engine)app = FastAPI()
app.include_router(user_crud_router, prefix="/user", tags=["User"])
app.include_router(post_crud_router, prefix="/post", tags=["Post"])
```You will also need an ASGI server and FastAPI to be able to run this app, both are optional dependencies of SQLAlchemy API:
```bash
pip install sqlalchemy-api[fastapi]
```#### Run it
```bash
uvicorn main:app --reload
```#### Use it
Endpoints 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:SQLAlchemyAPI also provides different operators depending on the column data type, to filter the data:
The data returned is automatically paginated and serialized, including the relationships defined in the models:
Post 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:
---
### License
`sqlalchemy-api` is distributed under the terms of the [MIT](https://spdx.org/licenses/MIT.html) license.