anonlink-entity-service icon indicating copy to clipboard operation
anonlink-entity-service copied to clipboard

Replace low level database operations with an ORM

Open hardbyte opened this issue 6 years ago • 2 comments

I quite like the look of peewee although SqlAlchemy is the standard.

For example we would define models for our tables:

class Project(BaseModel):
    access_token = pw.TextField()
    chunk_size = pw.BigIntegerField(constraints=[pw.SQL("DEFAULT '-1'::integer")])
    notes = pw.TextField(null=True)
    parties = pw.IntegerField(constraints=[pw.SQL("DEFAULT 2")], null=True)
    ready = pw.BooleanField(constraints=[pw.SQL("DEFAULT false")])
    resource = pw.CharField(column_name='resource_id', unique=True)
    result_type = UnknownField()  # Or use USER-DEFINED
    schema = BinaryJSONField()
    threshold = pw.FloatField()
    time_added = pw.DateTimeField(constraints=[pw.SQL("DEFAULT CURRENT_TIMESTAMP")], null=True)
    time_completed = pw.DateTimeField(null=True)
    time_started = pw.DateTimeField(null=True)

    class Meta:
        table_name = 'projects'

We can then create tables:

def create_tables():
    db.connect()
    Project.create_table(True)

Our database queries change to ORM code - example from the docs:

for tweet in Tweet.select().where(Tweet.user == user, Tweet.is_published == True):
    print(tweet.user.username, '->', tweet.message)

hardbyte avatar Apr 18 '18 07:04 hardbyte

I will vote for sqlalchemy, if only because Alembic has been so useful for me in other projects!

brendony avatar Oct 23 '18 01:10 brendony

This is partially done - sqlalchemy and alembic are used to create and migrate the database. However we are not using the ORM in the main codebase.

hardbyte avatar Apr 18 '21 01:04 hardbyte