Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

License

NotificationsYou must be signed in to change notification settings

rhoboro/async-fastapi-sqlalchemy

Repository files navigation

This is a sample project of Async Web API with FastAPI + SQLAlchemy 2.0.It includes asynchronous DB access using asyncpg and test code covering them.

This sample project is explained in this blog (written in Japanese).
https://www.rhoboro.com/2021/06/12/async-fastapi-sqlalchemy.html

If you want to use prisma instead of sqlalchemy, seerhoboro/async-fastapi-prisma.

Run with docker compose

After start-up, you can accesslocalhost:8000/docs to see the api documentation.

$ docker compose up -w

Setup a database and create tables

$ docker composeexec app uv run alembic upgrade head

Test

$ docker composeexec app uv sync --frozen --group testing$ docker composeexec app uv run black app$ docker composeexec app uv run ruff check app$ docker composeexec app uv run mypy app$ docker composeexec app uv run pytest app

Cleanup

$ docker compose down -v

Run without docker compose

This project usesuv.

Install dependencies

$ uv sync --frozen

Setup a database and create tables

$ docker run -d --name db \  -e POSTGRES_PASSWORD=password \  -e PGDATA=/var/lib/postgresql/data/pgdata \  -v pgdata:/var/lib/postgresql/data/pgdata \  -p 5432:5432 \  postgres:16.3-alpine# Cleanup database# $ docker stop db# $ docker rm db# $ docker volume rm pgdata# If your database host is not localhost, edit `DB_URI` in `app/config/local.env`.$ APP_CONFIG_FILE=local uv run alembic upgrade headINFO  [alembic.runtime.migration] Context impl PostgresqlImpl.INFO  [alembic.runtime.migration] Will assume transactional DDL.INFO  [alembic.runtime.migration] Running upgrade  -> a8483365f505, initial_emptyINFO  [alembic.runtime.migration] Running upgrade a8483365f505 -> 24104b6e1e0c, add_tables

Run

$ APP_CONFIG_FILE=local uv run fastapi devINFO:     Will watchforchangesin these directories: ['/Users/rhoboro/go/src/github.com/rhoboro/async-fastapi-sqlalchemy/app']INFO:     Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)INFO:     Started reloader process [49448] using WatchFilesINFO:     Started server process [49450]INFO:     Waitingfor application startup.INFO:     Application startup complete.INFO     Using path app/main.pyINFO     Resolved absolute path /Users/rhoboro/go/src/github.com/rhoboro/async-fastapi-sqlalchemy/app/main.pyINFO     Searchingfor package file structure from directories with __init__.py filesINFO     Importing from /Users/rhoboro/go/src/github.com/rhoboro/async-fastapi-sqlalchemy ╭─ Python package file structure ─╮ │                                 │ │  📁 app                         │ │  ├── 🐍 __init__.py             │ │  └── 🐍 main.py                 │ │                                 │ ╰─────────────────────────────────╯INFO     Importing module app.mainINFO     Found importable FastAPI app ╭── Importable FastAPI app ──╮ │                            │ │  from app.main import app  │ │                            │ ╰────────────────────────────╯INFO     Using import string app.main:app ╭────────── FastAPI CLI - Development mode ───────────╮ │                                                     │ │  Serving at: http://127.0.0.1:8000                  │ │                                                     │ │  API docs: http://127.0.0.1:8000/docs               │ │                                                     │ │  Runningin development mode,for production use:   │ │                                                     │ │  fastapi run                                        │ │                                                     │ ╰─────────────────────────────────────────────────────╯INFO:     Will watchforchangesin these directories: ['/Users/rhoboro/go/src/github.com/rhoboro/async-fastapi-sqlalchemy']INFO:     Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)INFO:     Started reloader process [47967] using WatchFilesINFO:     Started server process [47969]INFO:     Waitingfor application startup.INFO:     Application startup complete.

Test

$# Rewrite `db` to `localhost` in app/config/test.env$ uv sync --frozen --group testing$ uv run black app$ uv run ruff check app$ uv run mypy app$ uv run pytest app

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

[8]ページ先頭

©2009-2025 Movatter.jp