Expand description
§Tower Sessions Store for Sea-ORM with PostgreSQL
A robust and efficient session store implementation for tower-sessions
using Sea-ORM as the database abstraction layer.
This crate provides a PostgreSQL backend implementation for session storage in web applications built with Tower-compatible frameworks like Axum, Hyper, or any Tower-based service.
§Features
- Persistent session storage in PostgreSQL databases
- Sea-ORM integration for type-safe database operations
- Automatic session expiration and cleanup
- Automatic database migration support (with
migrationfeature) - Optimized upsert operations for better performance
- Comprehensive error handling with dedicated error types
- Serialization of session data using MessagePack for compact storage
§Quick Start
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);
// Run migrations to set up the database schema (requires "migration" feature)
store.migrate().await?;
// Use the store with tower-sessions
let session_layer = tower_sessions::SessionManagerLayer::new(store)
.with_expiry(Expiry::OnInactivity(Duration::days(7)));§Axum Integration Example
use axum::{Router, routing::get};
use sea_orm::{Database, DbConn};
use time::Duration;
use tower_sessions::{Expiry, Session, SessionManagerLayer};
use tower_sessions_seaorm_store::PostgresStore;
// Connect to the database
let conn = Database::connect("postgres://postgres:postgres@localhost:5432/sessions").await?;
// Create the store
let store = PostgresStore::new(conn);
// Run migrations to set up the database schema
store.migrate().await?;
// Configure session layer
let session_layer = SessionManagerLayer::new(store)
.with_expiry(Expiry::OnInactivity(Duration::days(1)))
.with_secure(true);
// Build your Axum application with session support
let app = Router::new()
.route("/", get(|| async { "Hello, world!" }))
.layer(session_layer);§Session Management
Once your application is set up with the session layer, you can use the session in your handlers:
use axum::extract::State;
use tower_sessions::Session;
// Set a value
session.insert("user_id", 123).await.map_err(|_| "Failed to insert")?;
// Get a value
let user_id: Option<u32> = session.get("user_id").await.map_err(|_| "Failed to get")?;
// Remove a value
session.remove("user_id").await.map_err(|_| "Failed to remove")?;
// Clear the entire session
session.flush().await.map_err(|_| "Failed to flush")?;Re-exports§
pub use sea_orm;
Modules§
- entity
- Database entity models for tower-sessions-seaorm-store.
- migration
- session_
store - Session storage error types and results
Structs§
- Id
- Session identifier type
- Postgres
Store - The main PostgreSQL store implementation for tower-sessions
- Record
- Session record type
- Session
- Session type for manipulating the current session
Enums§
- SeaOrm
Store Error - An error type for SeaORM stores.
Traits§
- Expired
Deletion - Trait for implementing session store expiration cleanup
- Session
Store - Trait for implementing session storage backends