Useful links:
- Clone the repository
git clone https://github.com/bandirom/fastapi-template.git- Install dependencies for local development
poetry install --extras dev- Create
.envfile
DATABASE_URI="postgresql+asyncpg://develop:develop@localhost/develop"
DEBUG=1
SECRET_KEY="someSecret"- Up only Postgresql and Redis in docker compose
docker-compose up -d db redis- Run project
python main.py- Init alembic (Already done in the project)
alembic init -t async alembic- Check if Alembic works well
alembic current- Generate migrations
alembic revision --autogenerate -m "migration_name"- Migrate
alembic upgrade head- Get users
from sqlalchemy import select
from sqlalchemy.ext.asyncio import AsyncSession
from db.models import User
async def get_users(session: AsyncSession):
result = await session.execute(select(User))
return result.scalars().all()- Edit
.envfile
DATABASE_URI=postgresql+asyncpg://develop:develop@db/develop- Build docker compose image
docker compose up -d --build- Build image
docker build -t fastapi-project -f docker/Dockerfile .- (Optional) Run container
docker run --rm -it --env-file .env -p 8080:8080 --name fastapi-project fastapi-project