Basic template that includes:
- FastAPI
- PostgreSQL
- TortoiseORM (Aerich for migrations)
Ready to go.
- Project layout is inspired by zhanymkanov/fastapi-best-practices but deviates slightly by putting all 'modules' into a sub-folder and core/shared concerns in the core folder.
- Can be run locally or with docker-compose
- Has stubbed base classes for your app (AppModel, AppService, AppSchema etc.)
- The database is created and migrations run automatically on startup. No messing around with Aerich or migration tools unless you want to create a new model.
- The test database is a real database but is reset with each run (public schema dropped & recreated), so it's ephemeral for all practical purposes.
Requires:
- Docker
- Docker-Compose
docker-compose up --build
Requires:
- Python 3.10+
- Postgres
Clone repo:
git clone git@github.com:pavdwest/fastapi-postgres-tortoise-template.git
NB! Navigate to app folder:
cd fastapi-postgres-tortoise-template/services/backend/app
Create & Activate virtual environment:
python -m venv ./ignored/venv
source ./ignored/venv/bin/activate
Install dependencies:
pip install requirements/base.txt
Note that versions are intentionally omitted - lock them down when you start.
Run:
uvicorn src.main:app --reload --port 8000
NB! Navigate to the app folder:
cd fastapi-postgres-tortoise-template/services/backend/app
pytest -vv
Definition:
src/modules/thingy/models.py
ORM:
src/database/config.py
-> Add to APP_MODELS
aerich migrate
-> Generates migration. Double check & name correctly. Rename constraints
src/modules/thingy/schemas.py
-> Create your Get, Create, Update etc. schemas
src/modules/thingy/routes.py
-> Create your Router, CRUD and other routes, and add the router to mount_routes()
in src/core/route_manager.py