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

Is there a way to retrieve all sqlalchemy backend stored sessions? #97

Closed vinodMS closed 4 years ago

vinodMS commented 5 years ago

I'd like to access the db model to be able to query it and retrieve all the records.

gimbo commented 4 years ago

Quick and dirty but does the job, I think, assuming session is your table name:

import pickle
[
    dict(
        row_id=row[0],
        session_id=row[1],
        session=pickle.loads(bytes(row[2])),
        expiry=row[3]
    )
    for row in db.session.execute('SELECT * FROM session')
]

(Might possibly want to pass an encoding parameter to pickle.loads() depending on what's in your session too — IIRC the default encoding is ascii whereas utf-8 might be more suitable...)