Working with async databases

Creating a config file using pydantic

Want more?

This lesson for enrolled students only. Join the course to unlock it!

You can see the code changes implemented in this lecture below.

If you have purchased the course in a different platform, you still have access to the code changes per lecture here on Teclado. The lecture video and lecture notes remain locked.
Join course for $30

New files

storeapi/config.py
# Most of this taken from Redowan Delowar's post on configurations with Pydantic
# https://rednafi.github.io/digressions/python/2020/06/03/python-configs.html
from typing import Optional

from pydantic_settings import BaseSettings, SettingsConfigDict

class BaseConfig(BaseSettings):
    """Loads the dotenv file. Including this is necessary to get
    pydantic to load a .env file."""

    model_config = SettingsConfigDict(env_file=".env", extra="ignore")

class GlobalConfig(BaseConfig):
    DATABASE_URL: Optional[str] = None
    DB_FORCE_ROLL_BACK: bool = False

Modified files

requirements.txt
--- 
+++ 
@@ -1,4 +1,5 @@
 fastapi
+pydantic-settings
 uvicorn[standard]
 sqlalchemy
 databases[aiosqlite]