Skip to content

pavdwest/fastapi-postgres-tortoise-template

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

12 Commits
 
 
 
 
 
 

Repository files navigation

Overview

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.

Getting Started

Docker

Requires:

  • Docker
  • Docker-Compose

docker-compose up --build

Local

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

View Swagger docs

http://127.0.0.1:8000/docs

Tests

NB! Navigate to the app folder:

    cd fastapi-postgres-tortoise-template/services/backend/app
    pytest -vv

Adding New Models/Modules

Model

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

Schemas

src/modules/thingy/schemas.py -> Create your Get, Create, Update etc. schemas

Routes

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

About

Simple template for a project built with FastAPI, PostgreSQL and TortoiseORM

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published