| Crates.io | tower-sessions-seaorm-store |
| lib.rs | tower-sessions-seaorm-store |
| version | 0.1.1 |
| created_at | 2025-05-14 21:02:34.908056+00 |
| updated_at | 2025-07-15 13:40:30.480301+00 |
| description | SeaORM session store for `tower-sessions`. |
| homepage | https://github.com/Himmelschmidt/tower-sessions-seaorm-store |
| repository | https://github.com/Himmelschmidt/tower-sessions-seaorm-store |
| max_upload_size | |
| id | 1673966 |
| size | 130,165 |
Note: This project started due to a misunderstanding of how the tower-sessions sqlx crate works and how you can access sea-orm backend sqlx connections. The tower sessions sqlx crate has any functionality of this crate and some. As such this repo is inactive but kept open so I can learn more Rust. Please use tower sessions sqlx store, found here https://github.com/maxcountryman/tower-sessions-stores
A SeaORM session store implementation for tower-sessions, providing seamless session storage in PostgreSQL databases.
Add this to your Cargo.toml:
[dependencies]
tower-sessions-seaorm-store = "0.1.0"
postgres (default): Enables PostgreSQL support via SeaORMFuture support is planned for SQLite and MySQL databases.
use sea_orm::{Database, DbConn};
use time::Duration;
use tower_sessions::Expiry;
use tower_sessions_seaorm_store::PostgresStore;
// Connect to the database
let conn = Database::connect("postgres://postgres:postgres@localhost:5432/sessions").await?;
// Create a new PostgresStore
let store = PostgresStore::new(conn);
// Use the store with tower-sessions
let session_layer = tower_sessions::SessionManagerLayer::new(store)
.with_expiry(Expiry::OnInactivity(Duration::days(7)));
use axum::{
routing::get,
Router,
};
use sea_orm::Database;
use time::Duration;
use tower_sessions::{Expiry, SessionLayer};
use tower_sessions_seaorm_store::PostgresStore;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
// Connect to database
let conn = Database::connect("postgres://postgres:postgres@localhost:5432/sessions").await?;
// Create store
let store = PostgresStore::new(conn);
// Create session layer
let session_layer = SessionLayer::new(store)
.with_secure(false) // Set to true in production
.with_expiry(Expiry::OnInactivity(Duration::hours(24)));
// Build app with session layer
let app = Router::new()
.route("/", get(|| async { "Hello, World!" }))
.layer(session_layer);
// Run it
let addr = std::net::SocketAddr::from(([127, 0, 0, 1], 3000));
axum::Server::bind(&addr)
.serve(app.into_make_service())
.await?;
Ok(())
}
The PostgresStore can be configured with the following options:
// Create store with custom table name
let store = PostgresStore::new(conn)
.with_table_name("custom_sessions");
By default, the store uses a table named tower_sessions.
This crate includes a complete example application built with Axum, demonstrating how to:
To run the example:
# Set the DATABASE_URL environment variable
export DATABASE_URL=postgres://postgres:password@localhost:5432/sessions
# Run the example
cargo run --example axum_example
The server will start on http://127.0.0.1:3000, and you can test it with:
# Set a session value
curl -v -c cookies.txt -X POST "http://127.0.0.1:3000/set?name=username&value=john_doe"
# Get the session value
curl -v -b cookies.txt "http://127.0.0.1:3000/get?name=username"
# Clear the session
curl -v -b cookies.txt -c cookies.txt -X POST http://127.0.0.1:3000/clear
This project is licensed under the MIT License.