overpass
overpass copied to clipboard
Refactor database design
- Use an ORM
- Refactor database table (user_id instead of user_snowflake in `streams`` for example)
- Set up some sort of migrations system
Might I recommend ormar+sqlalchemy for this? We/you could set up some models using ormar that would directly translate to tables in your db. Further, I think you could use either alembic or django migrations to migrate the database after any changes to the schema.
An example using ormar:
# in base/schema.py
import sqlalchemy
import databases
import ormar
from ..config import Settings
DATABASE_URL = Settings.DATABASE
database = databases.Database(DATABASE_URL)
metadata = sqlalchemy.MetaData()
class BaseMeta(ormar.ModelMeta):
metadata = metadata
database = database
class Users(ormar.Model):
class Meta(BaseMeta):
tablename="users"
user_id: int = ormar.Integer(primary_key=True)
name: str = ormar.String(max_length=20)
This would also follow your future plans to use FastAPI as it works quite nicely together.
fastapi to get data from database:
from fastapi import APIRouter
from base.schema import Users
router = APIRouter()
@router.get("/{user_id}")
def get_user(user_id: str) -> Users:
user = await Users.objects.get(user_id=user_id)
return user
This looks pretty nice! I'm already planning to use Tortoise as my ORM, as it's made for an asynchronous environment - but I'm definitely open to suggestions if you know of anything that would work better for the project.
This looks pretty nice! I'm already planning to use Tortoise as my ORM, as it's made for an asynchronous environment - but I'm definitely open to suggestions if you know of anything that would work better for the project.
I think Tortoise looks great!