data61 / anonlink-entity-service

Privacy Preserving Record Linkage Service
Apache License 2.0
26 stars 8 forks source link

Replace low level database operations with an ORM #133

Open hardbyte opened 6 years ago

hardbyte commented 6 years ago

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)
brendony commented 6 years ago

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

hardbyte commented 3 years ago

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.