Database
SeaORM-backed database integration for Better Auth.
Better Auth uses your application's SeaORM entities and DatabaseConnection
for auth persistence through the seaorm2 feature on better-auth.
The supported public path is:
- Enable the
seaorm2feature onbetter-auth. - Define auth entities in your app with
#[derive(AuthEntity)]frombetter_auth::seaorm. - Register them in an app schema with
#[derive(AuthSchema)]. - Manage auth migrations in your own SeaORM migrator.
- Build a
SeaOrmStorefrom the sharedDatabaseConnectionand pass it toBetterAuth::<YourSchema>::new(config).store(...).
Setup
use better_auth::{AuthConfig, AuthSchema, BetterAuth};
use better_auth::plugins::EmailPasswordPlugin;
use better_auth::seaorm::{AuthEntity, Database, DatabaseConnection, SeaOrmStore};
use std::sync::Arc;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let database = Database::connect("sqlite::memory:").await?;
let auth_database: DatabaseConnection = database.clone();
let config = AuthConfig::new("your-very-secure-secret-key-at-least-32-chars-long")
.base_url("http://localhost:3000");
let store = SeaOrmStore::<AppAuthSchema>::new(config.clone(), auth_database);
let auth = BetterAuth::<AppAuthSchema>::new(
config,
)
.store(store)
.plugin(EmailPasswordPlugin::new().enable_signup(true))
.build()
.await?;
// Keep using `database` for your app's own tables and queries.
println!("plugins: {:?}", auth.plugin_names());
Ok(())
}Existing Databases
If your application already has a users table with extra fields or non-string primary keys, keep the same public integration story:
- Reuse your app-owned SeaORM entities.
- Implement the public
AuthUser/SeaOrmUserModeltraits manually instead of relying on#[derive(AuthEntity)]. - Keep
sessions,accounts, andverificationsas app-owned SeaORM entities in the same database. - Run your own SeaORM migrations alongside the rest of the application schema.
The canonical worked example is examples/postgres_usage.rs, which demonstrates:
- an existing app-owned schema
- numeric user IDs
- seeded legacy users
- additional required app fields on the user table
- Better Auth mounted on top of the same
DatabaseConnection
Testing
Repo-local tests use SQLite through the same DatabaseConnection API and a bundled internal schema.
The dual-server and client SDK compatibility checks still validate behavior against the TypeScript
reference server.
Notes
- Better Auth no longer owns a public SeaORM migration path. Auth and app tables should be migrated together by the host application.
MemoryCacheAdapteris still available for cache/rate-limit use cases. It is separate from database persistence.