DEV Community

Lee Yi Jie Joel
Lee Yi Jie Joel

Posted on • Edited on

Setting up FastAPI with SupabaseDB

What is Supabase?

Supabase is an open source Firebase alternative. It comes with a Postgres database, Authentication, instant APIs, Edge Functions, Realtime subscriptions, and Storage.

What is FastAPI?

FastAPI is a modern, fast (high-performance), web framework for building APIs with Python. It's commonly used to rapidly get an API up and running

Here we demonstrate how to make use of Supabase as a Postgres Database and connect it to FastAPI so that we can manage migrations. We will make use of Alembic as a migrations versioning tool. This example also heavily draws reference from the example provided by FastAPI

Setup

Let's first set up a virtual environment and install some dependencies



virtualenv .env
pip3 install supabase fastapi psycopg2 alembic


Enter fullscreen mode Exit fullscreen mode

Create Base Files

In the supafast directory, create the core files by running
touch app.py __init__.py database.py models.py schema.py
the key point to note is that models.py contains pydantic models while schema.py contains the database models which are used to generate the database schema.

Populating the models and schema file

Here, we create a users model and schema model so that we can create a users table in the database. The working of both files are best explained by FastAPI's fantastic tutorial so please refer there instead.

Configure Database URL

Head to SQLALCHEMY_DATABASE_URL and change the URL to what's found on your Supabase Dashboard (Screenshot of what it looks like at time of writing)

Image description

Run alembic init migrations to generate a migrations folder containing all alembic migrations.

Linking Database models to Alembic

We need to let Alembic view the FastAPI database models we defined in models.py. In this case we only have a User model so let's import that in out migrations/env.py file by adding



from supafast.models import User


Enter fullscreen mode Exit fullscreen mode

in migrations/env.py

Generating and applying the migrations

From here, we can go ahead and generate an initial migration by running

alembic revision --autogenerate -m "generate initial migration"

This should create a new version of the migration under migrations/versions/

We can then run alembic upgrade head to apply the migration changes to Supabase DB. Thereafter, we should be able to see the newly created table in the table editor.

Image description

Conclusion

You can find the final state of the source code in this Github repository. If you prefer a video form of this tutorial you can view the video

For a comprehensive overview of Authentication, Functions, Storage and other items in the Supabase Python SDK, you can also check out the Supabase Python Crash Course kindly put together by Patrick.

If you have any questions, feel free to leave them in the comments below.

References:
[1] https://fastapi.tiangolo.com/tutorial/

Top comments (0)