overpass icon indicating copy to clipboard operation
overpass copied to clipboard

Refactor database design

Open GOATS2K opened this issue 4 years ago • 3 comments

  • Use an ORM
  • Refactor database table (user_id instead of user_snowflake in `streams`` for example)
  • Set up some sort of migrations system

GOATS2K avatar Nov 29 '21 23:11 GOATS2K

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

natehalsey avatar Jan 23 '22 21:01 natehalsey

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.

GOATS2K avatar Jan 23 '22 22:01 GOATS2K

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!

natehalsey avatar Jan 23 '22 22:01 natehalsey