FastAPI configuration and setup
pnpm run dev
command from a terminal within the api
directory. Alternatively, you can directly run the run.py
file.
src
directory is where all the application code sits. Below briefly explains each folder/file
Item | Description |
---|---|
api | The API endpoints for the application |
crud | The CRUD operations used within the application |
schemas | The schemas used within the application |
config.py | Main application configuration |
main.py | Application |
Item | Description |
---|---|
package.json | Not typical in a Python programme; used due to the nature of the monorepo. Running pnpm run dev at the project root will execute the dev script in this package.json |
vercel.json | The configuration for deploying the FastAPI aspect of the application to Vercel. |
*.csv | Simple seed data for the database, sourced from the Harry Potter API |
3.9
must be used as this is the latest supported version of Python. For more information, read the official documentation.
If deploying to somewhere other than Vercel, check which version of Python you may use and adjust the project according to your needs.
supabase-py-async
is already
included as a project dependency within the pyproject.toml
. If you are not
using Supabase, this can be removed.requirements.txt
has been generated to enable the installation of Python packages via the pip install
command.
If you do not use Poetry, you can remove the poetry.lock
and pyproject.toml
files.
requirements.txt
when deploying. Vercel also accepts a Pipenv
file if you use Pipenv, otherwise, you’ll need the requirements.txt
for the api
to build correctlyAdd a schema
schemas
directory.table_name
value needs to be included in the base Spell
class. This must be the same as the name of the table created in Supabase. It is used in the CRUD operations to identify the table to work with.Item | Description |
---|---|
Spell | The base class describing the columns that will be in the Supabase table |
SpellCreate | The class for creating a new Spell |
SpellUpdate | The class for updating an existing Spell |
SpellSearchResults | Describes how data will be returned in the API response. It will be a Sequence of Spell |
Add to __init__.py
Spell
classes throughout our application, they need to be added to the src/schemas/__init__.py
file.src/schemas
like so:src/crud/base.py
can also be used without modification.
Create CRUD file
CRUDSpell
class inherits from the CRUDBase
class located in src/crud/base.py
. The Spell
, SpellCreate
and SpellUpdate
classes are passed to the CRUDBase
class to specify the types of data that will be used in the CRUD operations.The operations that will be used in the API endpoints are functions of the CRUDSpell
class.Finally, spell
is an instance of CRUDSpell
. We import this instance to use in the API endpoints like so: spell.get_all(db)
or spell.get(db, id=id)
.SpellCreate
and SpellUpdate
classes are created in the schema regardless as they are required by the CRUDBase
model’s function arguments. CRUDBase
can be refactored to have optional parameters, or a read-only version of the CRUDBase
class can be created. This is beyond the scope of this project.Add to __init__.py
CRUDSpell
class needs to be added to the src/crud/__init__.py
file.Create the endpoints
src/api/api_v1/endpoints/spells.py
file.spell
object which we setup in step 2.The SessionDep
(located in src/api/deps.py
) dependency is used to access the database.The response_model
parameter is used to specify the type of data that will be returned from the endpoint. This is used to generate TypeScript types for the frontend.Add the endpoint to the router
src/api/api_v1/api.py
.Create the table
table_name
value in the Spell
class in src/schemas/spell.py
.Columns should be added to match the Spell
class. For example, we have the following Spell
class:spells
(table names are case-sensitive) with the columns id
, name
, and description
. id
can be automatically assigned - in this example, it is set to a uuid
. The name
and description
fields are strings, therefore their column’s type is set to text
. The id
column should be the primary key.
table_name
column as this is used in the FastAPI code to identify the table to work with.Seed the table with data
harry-potter-db-seed-spells.csv
file. For a more detailed explanation, please see the official documentationConfigure table security
RLS
off. However, for true CRUD operations, it should be configured to use authentication which is beyond the scope of this project.RLS
, in the table settings, select configure RLS
and then disable RLS
.\Add table connection to .env
DB_URL
and DB_API_KEY
parameters are populated in your .env
file located in the root of the API
directory.settings
and then API
. Copy the Project URL (DB_URL
) and the Project API Key (DB_API_KEY
).DB_USERNAME
and DB_PASSWORD
should also included in the .env
as they are configured in the src/config.py
. If you do not include these you will receive an error from Pydantic.