sqlalchemy,封装还是撒在任何地方?
我正在使用SQLalchemy + SQLite 3编写一个小应用程序。我正在尝试将Sqlalchemy包裹在下面的剪辑中。我发现我最终得到了Sqlalch周围非常乏味的包装纸。尝试包裹Sqlalchemy是典型的吗?还是可以完全使用它的约定,并在整个代码库中撒上会话和SA查询对象? Sqlalchemy的语法在某种程度上很尴尬,以至于我最终想包裹很多衬里。意见?任何好的代码示例都将不胜感激。
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")
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")
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论