Expand description
Storage-agnostic database-per-tenant support for laravel-rust — the idiomatic Rust
equivalent of Rails’ activerecord-tenanted, with zero dependency on LiteVault.
Built natively on laravel::db::DatabaseManager, DatabaseManager::using_connection,
laravel::context, laravel::queue::Middleware, laravel::cache::Store, laravel::storage,
laravel::event, laravel::errors::Status, and Artisan commands so completely ordinary
laravel-rust code (#[model], create!(...), db::table(...), db::query!(...),
db::transaction(...), Route Model Binding, DB validation, queues, cache, file storage, and
logs) is automatically tenant-scoped across:
- Plain local SQLite (
sqlite://storage/tenants/{tenant}/main.sqlite3?mode=rwc, default), - PostgreSQL / MySQL (
postgres://user:pass@host/tenant_{tenant}), or - LiteVault S3/R2 SQLite VFS (
laravel_rust_litevault::tenanted_database_url("tenants/{tenant}/main")).
§Quick Start
Enable multi-tenancy in 1 line on Application::configure():
use laravel_tenanted::WithTenancy;
laravel::Application::configure()
.with_tenancy()
.with_routing(|routing, _| {
routing.routes(|route| {
route.get("/invoices", list_invoices);
});
})
.run()Or customize your subdomain / session resolver and tenant tables inline with
.with_tenancy_using(|t| ...) (no Box::pin, no manual provider/cache wiring):
use laravel_tenanted::WithTenancy;
laravel::Application::configure()
.with_tenancy_using(|t| {
t.subdomain("myapp.com") // or t.session("active_org") for Inertia apps
.create_table("invoices", |table| {
table.id();
table.string("label").unique();
table.big_integer("cents");
});
})
.with_routing(|routing, _| {
routing.routes(|route| {
route.get("/invoices", list_invoices);
});
})
.run()Inside any route, job, or using_tenant scope, write completely normal laravel-rust code:
use laravel::http::Json;
use laravel::model::model;
use laravel_tenanted::Tenant;
#[model]
pub struct Invoice { pub id: i64, pub label: String, pub cents: i64 }
async fn list_invoices(_tenant: Tenant) -> laravel::Result<Json<Vec<Invoice>>> {
Ok(Json(Invoice::query().get().await?))
}Re-exports§
pub use builder::TenancyBuilder;pub use builder::WithTenancy;pub use cache::TenantedCache;pub use cache::TenantedCacheSettings;pub use cache::TenantedCacheStore;pub use commands::TenantCreateCommand;pub use commands::TenantDeleteCommand;pub use commands::TenantEvictCommand;pub use commands::TenantListCommand;pub use commands::TenantMigrateCommand;pub use config::IntoTenantMigration;pub use config::MigrationFn;pub use config::MigrationFuture;pub use config::TenantConfig;pub use config::TenantDeleteFn;pub use config::TenantExistsFn;pub use config::TenantListerFn;pub use config::TenantMigration;pub use config::TenantPoolStats;pub use error::NO_TENANT_CONNECTION;pub use error::TenantedError;pub use error::sanitize_tenant_id;pub use events::TenantCreated;pub use events::TenantDeleted;pub use events::TenantEntered;pub use events::TenantEvicted;pub use events::TenantExited;pub use events::TenantMigrated;pub use extract::Tenant;pub use manager::TenantManager;pub use manager::global_manager;pub use manager::set_global_manager;pub use middleware::RequestTenantResolver;pub use middleware::ResolveTenantMiddleware;pub use provider::TenantedServiceProvider;pub use queue::TenantQueueMiddleware;pub use storage::TenantedStorage;
Modules§
- builder
- One-line, Taylor-Otwell-style automagic registration on
laravel::foundation::ApplicationBuilder. - cache
- Tenant-isolated caching for
laravel-rust: - commands
- Native
laravel-rustArtisan commands automatically registered bycrate::TenantedServiceProvider: - config
- Configuration, schema/SQL migration definitions, and pool statistics for
laravel-tenanted. - error
- Typed error hierarchy for
laravel-tenanted, modeled after Rails’ActiveRecord::Tenantederrors andlaravel-rust’s native#[derive(Status)]. - events
- Native
laravel::event::Eventlifecycle events dispatched as tenants are entered, exited, created, migrated, evicted, and deleted. - extract
- First-class
laravel-rustHTTP route extractor for the active tenant, mirroringlaravel::auth::Authenticatedandlaravel::http::RequestId. - manager
- Core
TenantManagerbuilt natively onlaravel::db::DatabaseManager,DatabaseManager::using_connection,laravel::context, andlaravel::event. - middleware
- First-class
laravel::http::Middlewareresolving a customer’s tenant per HTTP request and running the entire request insidecrate::TenantManager::run. - provider
- Drop-in
laravel-rustTenantedServiceProviderthat wires database-per-tenant support into the application’s DI container, HTTP middleware pipeline, cache drivers, and Artisan CLI. - queue
- Native
laravel::queue::Middlewarethat automatically restores the dispatching tenant’s database, cache, and log scope inside background queue workers! - storage
- Tenant-isolated file storage wrapping any
laravel::storage::Disk("local","public","s3","laravel_cloud", or"read_through"). - testing
- Ergonomic test helpers for isolated multi-tenant
laravel-rusttests.
Functions§
- cache
- Return a tenant-scoped cache wrapper around the application’s default cache store.
Fails closed with
TenantedError::NoTenantif no tenant is active. - cache_
store - Return a tenant-scoped cache wrapper around a named
laravel::cachestore (e.g."redis"). - central
- Alias for
without_tenantfamiliar tostancl/tenancyusers. - connection
- Obtain the native
laravel::db::AnyConnectionfor the currently active tenant (or fail withTenantedError::NoTenant). - connection_
name - Return the
DatabaseManagerconnection name for the active tenant (e.g."tenant_acme"). - create
- Explicitly provision and migrate a new tenant database.
- current
- Return the validated tenant ID active in the current scope (or
Nonewhen outside a tenant scope or insidewithout_tenant). - current_
connection_ name - Backwards-compatible alias for
connection_name. - current_
tenant - Backwards-compatible alias for
current. - delete
- Evict and permanently delete a tenant database.
- evict
- Evict one tenant’s connection pool from the LRU cache.
- exists
- Check whether
tenant_idalready exists. - for_
each - Iterate across all discovered tenants (or an explicit list via
for_each_in), executingfinsiderunfor each tenant. - for_
each_ in - Iterate across an explicit list of tenant IDs, executing
finsiderunfor each. - id
- Return the currently active tenant ID as a typed
Result<String, TenantedError>. - is_
active - Return
truewhen code is executing inside an active tenant scope. - landlord
- Alias for
without_tenantfamiliar tospatie/laravel-multitenancyusers. - list
- List all discovered and currently open tenants.
- manager
- Resolve the active
TenantManagerfrom the currentlaravel::Applicationcontainer (or fallback global manager). - migrate
- Run pending migrations on
tenant_id. - require
- Return the currently active tenant ID, or fail with
TenantedError::NoTenant. - run
- Run an async block strictly scoped to one validated tenant — exact Rust equivalent of
Rails’
ApplicationRecord.with_tenant("acme") { ... }. - storage
- Return a tenant-scoped storage wrapper around the application’s default
laravel::storage::Disk(automatically prefixing all paths with{storage_prefix}/{id}/and rejecting path traversal). Fails closed withTenantedError::NoTenantif no tenant is active. - storage_
disk - Return a tenant-scoped storage wrapper around a named
laravel::storage::Disk(e.g."s3","public","local", or"laravel_cloud"), automatically prefixing all paths with{storage_prefix}/{id}/and rejecting path traversal. - table
- Start a native
laravel::db::Builderscoped to the currently active tenant’s database. - using_
tenant - Alias for
runmatchinglaravel::db::using_connection. - with_
tenant - Alias for
runmatching Rails’with_tenant. - without_
tenant - Temporarily execute
futoutside any tenant scope on the central (shared) database — exact sibling oflaravel::model::without_eventsand Rails’without_tenant. Restores the surrounding tenant scope cleanly on exit.