Skip to main content

SQL Alchemy session scoped on Flask requests.

Project description

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_session2024 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_session2024 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.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

Flask-SQLAlchemy-Session2024-2.0.0.tar.gz (5.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

Flask_SQLAlchemy_Session2024-2.0.0-py3-none-any.whl (4.6 kB view details)

Uploaded Python 3

File details

Details for the file Flask-SQLAlchemy-Session2024-2.0.0.tar.gz.

File metadata

File hashes

Hashes for Flask-SQLAlchemy-Session2024-2.0.0.tar.gz
Algorithm Hash digest
SHA256 814cdfa4b0507c780cd2cb1ba4e9bb79c4da434c25442a7e77ad59bf5f2fbc0b
MD5 cc776a8b40dcb50c826f1e3db87010a4
BLAKE2b-256 9d53deb26923a9fe469719feb15e92348faf768813c08b3b73f36289c691a2cb

See more details on using hashes here.

File details

Details for the file Flask_SQLAlchemy_Session2024-2.0.0-py3-none-any.whl.

File metadata

File hashes

Hashes for Flask_SQLAlchemy_Session2024-2.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 debbdf0382de3086af370957af3b408a5d64cc15d7928ade5b15086e69015622
MD5 ba5caca1dbd41dfac6f57735daa3722f
BLAKE2b-256 40ca9a43f5f1e14b986c8b6b2bd1e30796ff024291e4884b34178b21f30864bc

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page