FastAPI-Async-SQLA


NameFastAPI-Async-SQLA JSON
Version 0.2.4 PyPI version JSON
download
home_pageNone
SummarySQLAlchemy extension for FastAPI with support for asynchronous SQLAlchemy sessions and pagination.
upload_time2025-01-27 19:42:36
maintainerNone
docs_urlNone
authorNone
requires_python>=3.12
licenseMIT License
keywords fastapi sqlalchemy asyncio
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # PROJECT HAS BEEN RENAMED TO FastSQLA

Check [FastSQLA on Github](https://github.com/hadrien/FastSQLA) or [PyPI](https://pypi.org/project/FastSQLA/)

# FastAPI-Async-SQLA

[![PyPI - Version](https://img.shields.io/pypi/v/FastAPI-Async-SQLA?color=brightgreen)](https://pypi.org/project/FastAPI-Async-SQLA/)
[![Conventional Commits](https://img.shields.io/badge/Conventional%20Commits-1.0.0-brightgreen.svg)](https://conventionalcommits.org)
[![codecov](https://codecov.io/gh/hadrien/fastapi-async-sqla/graph/badge.svg?token=XK3YT60MWK)](https://codecov.io/gh/hadrien/fastapi-async-sqla)

FastAPI-Async-SQLA is an [SQLAlchemy] extension for [FastAPI]. It supports asynchronous
SQLAlchemy sessions using SQLAlchemy >= 2.0 and provides pagination support.

# Installing

Using [pip](https://pip.pypa.io/):
```
pip install fastapi-async-sqla
```

# Quick Example

Assuming it runs against a DB with a table `user` with 2 columns `id` and `name`:

```python
# main.py
from fastapi import FastAPI, HTTPException
from fastapi_async_sqla import Base, Item, Page, Paginate, Session, lifespan
from pydantic import BaseModel
from sqlalchemy import select


app = FastAPI(lifespan=lifespan)


class User(Base):
    __tablename__ = "user"


class UserIn(BaseModel):
    name: str


class UserModel(UserIn):
    id: int


@app.get("/users", response_model=Page[UserModel])
async def list_users(paginate: Paginate):
    return await paginate(select(User))


@app.get("/users/{user_id}", response_model=Item[UserModel])
async def get_user(user_id: int, session: Session):
    user = await session.get(User, user_id)
    if user is None:
        raise HTTPException(404)
    return {"data": user}


@app.post("/users", response_model=Item[UserModel])
async def create_user(new_user: UserIn, session: Session):
    user = User(**new_user.model_dump())
    session.add(user)
    await session.flush()
    return {"data": user}
```

Creating a db using `sqlite3`:
```bash
sqlite3 db.sqlite <<EOF
CREATE TABLE user (
    id    INTEGER PRIMARY KEY AUTOINCREMENT,
    name  TEXT NOT NULL
);
EOF
```

Installing [aiosqlite] to connect to the sqlite db asynchronously:
```bash
pip install aiosqlite
```

Running the app:
```bash
sqlalchemy_url=sqlite+aiosqlite:///db.sqlite?check_same_thread=false uvicorn main:app
```

[aiosqlite]: https://github.com/omnilib/aiosqlite
[FastAPI]: https://fastapi.tiangolo.com/
[SQLAlchemy]: http://sqlalchemy.org/

            

Raw data

            {
    "_id": null,
    "home_page": null,
    "name": "FastAPI-Async-SQLA",
    "maintainer": null,
    "docs_url": null,
    "requires_python": ">=3.12",
    "maintainer_email": null,
    "keywords": "FastAPI, SQLAlchemy, AsyncIO",
    "author": null,
    "author_email": "Hadrien David <bonjour@hadriendavid.com>",
    "download_url": "https://files.pythonhosted.org/packages/20/53/f0fc6b39d6dc369e41212829329830d360d38b5a2ea4bc361cdc875bdea9/fastapi_async_sqla-0.2.4.tar.gz",
    "platform": null,
    "description": "# PROJECT HAS BEEN RENAMED TO FastSQLA\n\nCheck [FastSQLA on Github](https://github.com/hadrien/FastSQLA) or [PyPI](https://pypi.org/project/FastSQLA/)\n\n# FastAPI-Async-SQLA\n\n[![PyPI - Version](https://img.shields.io/pypi/v/FastAPI-Async-SQLA?color=brightgreen)](https://pypi.org/project/FastAPI-Async-SQLA/)\n[![Conventional Commits](https://img.shields.io/badge/Conventional%20Commits-1.0.0-brightgreen.svg)](https://conventionalcommits.org)\n[![codecov](https://codecov.io/gh/hadrien/fastapi-async-sqla/graph/badge.svg?token=XK3YT60MWK)](https://codecov.io/gh/hadrien/fastapi-async-sqla)\n\nFastAPI-Async-SQLA is an [SQLAlchemy] extension for [FastAPI]. It supports asynchronous\nSQLAlchemy sessions using SQLAlchemy >= 2.0 and provides pagination support.\n\n# Installing\n\nUsing [pip](https://pip.pypa.io/):\n```\npip install fastapi-async-sqla\n```\n\n# Quick Example\n\nAssuming it runs against a DB with a table `user` with 2 columns `id` and `name`:\n\n```python\n# main.py\nfrom fastapi import FastAPI, HTTPException\nfrom fastapi_async_sqla import Base, Item, Page, Paginate, Session, lifespan\nfrom pydantic import BaseModel\nfrom sqlalchemy import select\n\n\napp = FastAPI(lifespan=lifespan)\n\n\nclass User(Base):\n    __tablename__ = \"user\"\n\n\nclass UserIn(BaseModel):\n    name: str\n\n\nclass UserModel(UserIn):\n    id: int\n\n\n@app.get(\"/users\", response_model=Page[UserModel])\nasync def list_users(paginate: Paginate):\n    return await paginate(select(User))\n\n\n@app.get(\"/users/{user_id}\", response_model=Item[UserModel])\nasync def get_user(user_id: int, session: Session):\n    user = await session.get(User, user_id)\n    if user is None:\n        raise HTTPException(404)\n    return {\"data\": user}\n\n\n@app.post(\"/users\", response_model=Item[UserModel])\nasync def create_user(new_user: UserIn, session: Session):\n    user = User(**new_user.model_dump())\n    session.add(user)\n    await session.flush()\n    return {\"data\": user}\n```\n\nCreating a db using `sqlite3`:\n```bash\nsqlite3 db.sqlite <<EOF\nCREATE TABLE user (\n    id    INTEGER PRIMARY KEY AUTOINCREMENT,\n    name  TEXT NOT NULL\n);\nEOF\n```\n\nInstalling [aiosqlite] to connect to the sqlite db asynchronously:\n```bash\npip install aiosqlite\n```\n\nRunning the app:\n```bash\nsqlalchemy_url=sqlite+aiosqlite:///db.sqlite?check_same_thread=false uvicorn main:app\n```\n\n[aiosqlite]: https://github.com/omnilib/aiosqlite\n[FastAPI]: https://fastapi.tiangolo.com/\n[SQLAlchemy]: http://sqlalchemy.org/\n",
    "bugtrack_url": null,
    "license": "MIT License",
    "summary": "SQLAlchemy extension for FastAPI with support for asynchronous SQLAlchemy sessions and pagination.",
    "version": "0.2.4",
    "project_urls": {
        "Changelog": "https://github.com/hadrien/fastapi-async-sqla/releases",
        "Documentation": "https://github.com/hadrien/fastapi-async-sqla",
        "Homepage": "https://github.com/hadrien/fastapi-async-sqla",
        "Issues": "https://github.com/hadrien/fastapi-async-sqla/issues",
        "Repository": "https://github.com/hadrien/fastapi-async-sqla"
    },
    "split_keywords": [
        "fastapi",
        " sqlalchemy",
        " asyncio"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "2053f0fc6b39d6dc369e41212829329830d360d38b5a2ea4bc361cdc875bdea9",
                "md5": "3907e00f8040dda3776956683819b32d",
                "sha256": "5f33439e69cefc74f165fc237442b49ace914db243e5fd5d18e80c551ed732c3"
            },
            "downloads": -1,
            "filename": "fastapi_async_sqla-0.2.4.tar.gz",
            "has_sig": false,
            "md5_digest": "3907e00f8040dda3776956683819b32d",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.12",
            "size": 5435,
            "upload_time": "2025-01-27T19:42:36",
            "upload_time_iso_8601": "2025-01-27T19:42:36.531631Z",
            "url": "https://files.pythonhosted.org/packages/20/53/f0fc6b39d6dc369e41212829329830d360d38b5a2ea4bc361cdc875bdea9/fastapi_async_sqla-0.2.4.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2025-01-27 19:42:36",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "hadrien",
    "github_project": "fastapi-async-sqla",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "lcname": "fastapi-async-sqla"
}
        
Elapsed time: 0.38732s