pallets-eco / flask-session

Server side session extension for Flask
https://flask-session.readthedocs.io
BSD 3-Clause "New" or "Revised" License
507 stars 238 forks source link

Add support for server side Flask-SQLAlchemy backed sessions #5

Closed mchiocca closed 9 years ago

mchiocca commented 9 years ago

The current Flask-Session implementation provides support for NoSQL options, such as MongoDB and Redis, but no support for SQL options, such as Oracle, MySQL, or SQLServer, etc.

Adding support for Flask-SQLAlchemy backed sessions adds support for all SQL options that Flask-SQLAlchemy supports. Here is a simple example of using Flask-SQLAlchemy with Flask-Session.

import os
from flask import Flask
from flask.ext.session import Session
from flask.ext.sqlalchemy import SQLAlchemy

SECRET_KEY = os.urandom(24)
SESSION_TYPE = 'sqlalchemy'
SQLALCHEMY_DATABASE_URI = 'sqlite:////tmp/database.db'

app = Flask(__name__)
app.config.from_object(__name__)
db = SQLAlchemy(app)
app.config['SESSION_SQLALCHEMY'] = db
Session(app)

Simply specify SESSION_TYPE = 'sqlalchemy' and set SESSION_SQLALCHEMY to an instance of Flask-SQLAlchemy. Instantiate the Session with the app set up to use Flask-SQLAlchemy.

Special thanks to Viswa Vutharkar for his effort in implementing the new Flask-SQLAlchemy backed session implementation. If fills a gap in Flask-Session by adding support for SQL datastores.

fengsp commented 9 years ago

I modify the code a little bit, you can check out the latest master, feel free to comment on it.