pub trait StorageProvider: Send + Sync {
type Error: Error + Send + Sync + 'static;
// Required methods
fn put(
&self,
key: StorageKey,
data: Value,
) -> impl Future<Output = Result<Value, Self::Error>> + Send;
fn get(
&self,
key: StorageKey,
) -> impl Future<Output = Result<Option<Value>, Self::Error>> + Send;
fn delete(
&self,
key: StorageKey,
) -> impl Future<Output = Result<bool, Self::Error>> + Send;
fn list(
&self,
prefix: StoragePrefix,
offset: usize,
limit: usize,
) -> impl Future<Output = Result<Vec<(StorageKey, Value)>, Self::Error>> + Send;
fn find_by_attribute(
&self,
prefix: StoragePrefix,
attribute: &str,
value: &str,
) -> impl Future<Output = Result<Vec<(StorageKey, Value)>, Self::Error>> + Send;
fn exists(
&self,
key: StorageKey,
) -> impl Future<Output = Result<bool, Self::Error>> + Send;
fn count(
&self,
prefix: StoragePrefix,
) -> impl Future<Output = Result<usize, Self::Error>> + Send;
fn list_tenants(
&self,
) -> impl Future<Output = Result<Vec<String>, Self::Error>> + Send;
fn list_resource_types(
&self,
tenant_id: &str,
) -> impl Future<Output = Result<Vec<String>, Self::Error>> + Send;
fn list_all_resource_types(
&self,
) -> impl Future<Output = Result<Vec<String>, Self::Error>> + Send;
fn clear(&self) -> impl Future<Output = Result<(), Self::Error>> + Send;
fn stats(
&self,
) -> impl Future<Output = Result<StorageStats, Self::Error>> + Send;
}
Expand description
Core trait for storage providers that handle pure data persistence operations.
This trait defines a protocol-agnostic interface for storing and retrieving JSON data with tenant isolation. Implementations should focus solely on data persistence and retrieval without any SCIM-specific logic.
§Design Principles
- PUT/GET/DELETE Model: Simple, fundamental operations
- PUT Returns Data: Supports SCIM requirement to return resource state after operations
- DELETE Returns Boolean: Indicates whether resource existed (for proper HTTP status codes)
- Tenant Isolation: All operations are scoped to a specific tenant via StorageKey
- Protocol Agnostic: No awareness of SCIM structures or semantics
- Async First: All operations return futures for scalability
- Error Transparency: Storage errors are clearly separated from protocol errors
§Key Design Decisions
- No separate CREATE/UPDATE: Both are just PUT operations. Business logic determines whether this should be treated as create vs update.
- PUT returns stored data: This enables SCIM providers to return the complete resource state after modifications without a separate GET call.
- DELETE returns boolean: Allows proper HTTP status code handling (204 vs 404).
Required Associated Types§
Required Methods§
Sourcefn put(
&self,
key: StorageKey,
data: Value,
) -> impl Future<Output = Result<Value, Self::Error>> + Send
fn put( &self, key: StorageKey, data: Value, ) -> impl Future<Output = Result<Value, Self::Error>> + Send
Store data at the specified key and return the stored data.
§Arguments
key
- The storage key identifying the resource locationdata
- The JSON data to store
§Returns
The data that was actually stored (may include storage-level metadata).
§Behavior
- If a resource with the same key already exists, it is completely replaced
- The storage implementation should ensure atomic operations where possible
- No validation is performed on the data structure
- The returned data should be exactly what would be retrieved by
get()
Sourcefn get(
&self,
key: StorageKey,
) -> impl Future<Output = Result<Option<Value>, Self::Error>> + Send
fn get( &self, key: StorageKey, ) -> impl Future<Output = Result<Option<Value>, Self::Error>> + Send
Sourcefn delete(
&self,
key: StorageKey,
) -> impl Future<Output = Result<bool, Self::Error>> + Send
fn delete( &self, key: StorageKey, ) -> impl Future<Output = Result<bool, Self::Error>> + Send
Delete data by key.
§Arguments
key
- The storage key identifying the resource
§Returns
true
if the resource was deleted, false
if it didn’t exist.
§Note
This follows SCIM/HTTP semantics where DELETE operations don’t return resource data. The boolean return value allows proper HTTP status code selection (204 vs 404).
Sourcefn list(
&self,
prefix: StoragePrefix,
offset: usize,
limit: usize,
) -> impl Future<Output = Result<Vec<(StorageKey, Value)>, Self::Error>> + Send
fn list( &self, prefix: StoragePrefix, offset: usize, limit: usize, ) -> impl Future<Output = Result<Vec<(StorageKey, Value)>, Self::Error>> + Send
List resources matching a prefix with pagination.
§Arguments
prefix
- The storage prefix (tenant + resource type)offset
- The number of resources to skip (0-based)limit
- The maximum number of resources to return
§Returns
A vector of (key, data) pairs.
§Behavior
- Results should be consistently ordered (e.g., by resource ID)
- If
offset
exceeds the total count, an empty vector should be returned - If
limit
is 0, an empty vector should be returned
Sourcefn find_by_attribute(
&self,
prefix: StoragePrefix,
attribute: &str,
value: &str,
) -> impl Future<Output = Result<Vec<(StorageKey, Value)>, Self::Error>> + Send
fn find_by_attribute( &self, prefix: StoragePrefix, attribute: &str, value: &str, ) -> impl Future<Output = Result<Vec<(StorageKey, Value)>, Self::Error>> + Send
Find resources by a specific attribute value.
§Arguments
prefix
- The storage prefix (tenant + resource type)attribute
- The JSON path of the attribute to search (e.g., “userName”, “emails.0.value”)value
- The exact value to match
§Returns
A vector of (key, data) pairs for matching resources.
§Behavior
- Performs exact string matching on the specified attribute
- Supports nested attributes using dot notation
- Returns all matching resources (no pagination)
- Empty vector if no matches found
Sourcefn exists(
&self,
key: StorageKey,
) -> impl Future<Output = Result<bool, Self::Error>> + Send
fn exists( &self, key: StorageKey, ) -> impl Future<Output = Result<bool, Self::Error>> + Send
Sourcefn count(
&self,
prefix: StoragePrefix,
) -> impl Future<Output = Result<usize, Self::Error>> + Send
fn count( &self, prefix: StoragePrefix, ) -> impl Future<Output = Result<usize, Self::Error>> + Send
Sourcefn list_tenants(
&self,
) -> impl Future<Output = Result<Vec<String>, Self::Error>> + Send
fn list_tenants( &self, ) -> impl Future<Output = Result<Vec<String>, Self::Error>> + Send
List all tenant IDs that currently have data in storage.
Returns tenant IDs for all tenants that contain at least one resource of any type. This method enables dynamic tenant discovery without requiring hardcoded tenant patterns.
§Returns
A vector of tenant ID strings. Empty vector if no tenants have data.
§Errors
Returns storage-specific errors if the discovery operation fails.
§Examples
use scim_server::storage::{StorageProvider, InMemoryStorage};
let storage = InMemoryStorage::new();
let tenants = storage.list_tenants().await?;
println!("Found {} tenants", tenants.len());
Sourcefn list_resource_types(
&self,
tenant_id: &str,
) -> impl Future<Output = Result<Vec<String>, Self::Error>> + Send
fn list_resource_types( &self, tenant_id: &str, ) -> impl Future<Output = Result<Vec<String>, Self::Error>> + Send
List all resource types for a specific tenant.
Returns resource type names (e.g., “User”, “Group”) that exist within the specified tenant. Only resource types with at least one stored resource are included.
§Arguments
tenant_id
- The tenant ID to query for resource types
§Returns
A vector of resource type strings. Empty vector if tenant doesn’t exist or has no resources.
§Errors
Returns storage-specific errors if the query operation fails.
§Examples
use scim_server::storage::{StorageProvider, InMemoryStorage};
let storage = InMemoryStorage::new();
let types = storage.list_resource_types("tenant1").await?;
for resource_type in types {
println!("Tenant has resource type: {}", resource_type);
}
Sourcefn list_all_resource_types(
&self,
) -> impl Future<Output = Result<Vec<String>, Self::Error>> + Send
fn list_all_resource_types( &self, ) -> impl Future<Output = Result<Vec<String>, Self::Error>> + Send
List all resource types across all tenants.
Returns a deduplicated collection of all resource type names found across all tenants in storage. This provides a global view of resource types without tenant boundaries.
§Returns
A vector of unique resource type strings. Empty vector if no resources exist.
§Errors
Returns storage-specific errors if the discovery operation fails.
§Examples
use scim_server::storage::{StorageProvider, InMemoryStorage};
let storage = InMemoryStorage::new();
let all_types = storage.list_all_resource_types().await?;
println!("System supports {} resource types", all_types.len());
Sourcefn clear(&self) -> impl Future<Output = Result<(), Self::Error>> + Send
fn clear(&self) -> impl Future<Output = Result<(), Self::Error>> + Send
Clear all data from storage.
Removes all resources from all tenants, effectively resetting the storage to an empty state. This operation is primarily intended for testing scenarios and should be used with caution in production environments.
§Returns
Ok(())
on successful clearing, or a storage-specific error on failure.
§Errors
Returns storage-specific errors if the clear operation fails partially or completely.
§Behavior
- Removes all resources from all tenants atomically where possible
- After successful clearing,
list_tenants
should return an empty vector - Primarily intended for testing scenarios
§Examples
use scim_server::storage::{StorageProvider, InMemoryStorage};
let storage = InMemoryStorage::new();
// ... populate storage with data ...
storage.clear().await?;
let tenants = storage.list_tenants().await?;
assert_eq!(tenants.len(), 0);
Sourcefn stats(
&self,
) -> impl Future<Output = Result<StorageStats, Self::Error>> + Send
fn stats( &self, ) -> impl Future<Output = Result<StorageStats, Self::Error>> + Send
Get storage statistics for debugging and monitoring.
Returns statistics about storage usage including tenant count, resource type count, and total number of resources across all tenants.
§Returns
A StorageStats
struct containing usage metrics.
§Errors
Returns storage-specific errors if the stats collection operation fails.
§Examples
use scim_server::storage::{StorageProvider, InMemoryStorage};
let storage = InMemoryStorage::new();
let stats = storage.stats().await?;
println!("Total resources: {}", stats.total_resources);
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.