Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor database design #25

Open
GOATS2K opened this issue Nov 29, 2021 · 3 comments
Open

Refactor database design #25

GOATS2K opened this issue Nov 29, 2021 · 3 comments
Labels
application design This issue regards application design and is not tied to the user-facin functionality of the project
Milestone

Comments

@GOATS2K
Copy link
Owner

GOATS2K commented Nov 29, 2021

  • Use an ORM
  • Refactor database table (user_id instead of user_snowflake in `streams`` for example)
  • Set up some sort of migrations system
@GOATS2K GOATS2K added this to To do in Migrate to FastAPI Dec 19, 2021
@GOATS2K GOATS2K added this to the v1.0.0 milestone Dec 19, 2021
@natehalsey
Copy link

natehalsey commented Jan 23, 2022

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

@GOATS2K
Copy link
Owner Author

GOATS2K commented Jan 23, 2022

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 GOATS2K added application design This issue regards application design and is not tied to the user-facin functionality of the project and removed long term goal labels Jan 23, 2022
@natehalsey
Copy link

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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
application design This issue regards application design and is not tied to the user-facin functionality of the project
Projects
v1.0.0
Awaiting triage
Development

No branches or pull requests

2 participants