Skip to content

e-c-d/flask-sqlalchemy-session2024

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

61 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Flask-SQLAlchemySession2024

Run tests

This is a fork of the excellent Flask-SQLAlchemySession package by Dimitris Theodorou, which has unfortunately gone unpatched for a few years.

Provides an SQLAlchemy scoped session that creates unique sessions per Flask request, following the guidelines documented at Using Custom Created Scopes.

TODO

  • coverage
  • docs

Usage

Initialize a flask_scoped_session as you would a scoped_session, with the addition of a Flask app. Then use the resulting session to query models:

from flask import Flask, abort, jsonify
from flask_sqlalchemy_session import flask_scoped_session

app = Flask(__name__)
session = flask_scoped_session(session_factory, app)

@app.route("/users/<int:user_id>")
def user(user_id):
    user = session.query(User).get(user_id)
    if user is None:
        abort(404)
    return flask.jsonify(**user.to_dict())

The current_session is also provided as a convenient accessor to the session of the current request, in the same spirit of flask.request and flask.current_app.

from flask_sqlalchemy_session import current_session

@app.route("/users/<int:user_id>")
def user(user_id):
    user = current_session.query(User).get(user_id)
    if user is None:
        abort(404)
    return flask.jsonify(**user.to_dict())

Tests

You can run the tests by invoking PYTHONPATH=. py.test tests/ in the repository root.

About

SQL Alchemy scoped session for Flask.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Python 100.0%