FutureMind / hug-sqlalchemy-template

Boiler template for a Python hug API application with SQLAlchemy, alembic and pytest
MIT License
12 stars 3 forks source link

Accessing session #2

Open ldelossa opened 6 years ago

ldelossa commented 6 years ago

With this repository how can I access the session within a handler to make use of the .add() method ?

arybin93 commented 4 years ago

Hello @ldelossa , maybe so:

from api.app import db

db.connect()
db.session.begin()
db.session.add(user1)
db.session.commit()
db.close()
ianmacartney commented 2 years ago

I'm new to SQLAlchemy & hug, but this answer doesn't seem to leverage the scoped session that is being created in the middleware. In fact there's a few things about the middleware that concern me:

I think a session could be saved to the context once per request with a @context_factory and used by name in endpoints via a custom @directive. If I get it working I'll submit a PR