SqlAlchemy, encapsulate, or just sprinkle everywhere?

Viewed 33

I'm writing a small application using SQLAlchemy + SQLite3. I'm trying to wrap SQLAlchemy in classes like the snip below. I find I'm ending up with a tedious amount of very thin wrappers around SQLAlch. Is it typical to try and wrap SQLAlchemy? or is the convention just to use embrace it fully and sprinkle sessions and SA query objects throughout a codebase? The syntax of sqlalchemy is somehow just awkward enough that I end up wanting to wrap a lot of one liners. Opinions? Any good code examples would be appreciated.

class MyDB:
...
    def query(self, table_class, query):
        if isinstance(query, str):
            query = text(query)

        with self.make_session() as sesh:
            res = sesh.execute(select(table_class).where(query)).all()
        return res
...
db = MyDB(db_path="path_to_db.sqlite")

results = db.query(table_class = MyOrmTableClass, query = "where device_id="FOOBAR")
0 Answers
Related