Expand description
SQLx-facing integration crate for HydraCache database result caching.
The database-neutral query cache API lives in hydracache-db. This crate
keeps SQLx users on a convenient import path while avoiding a hard conceptual
dependency between the generic adapter and SQLx itself.
§Example
use hydracache::HydraCache;
use hydracache_sqlx::{DbCache, HydraCacheEntity, SqlxQueryExt};
#[derive(serde::Serialize, serde::Deserialize, HydraCacheEntity)]
#[hydracache(entity = "user", collection = "users", id = i64)]
struct User {
id: i64,
name: String,
}
let local = HydraCache::local().build();
// SQLx users may import DbCache from this crate, but the type itself is
// database-neutral and comes from hydracache-db.
let queries = DbCache::new(local, "db");
let user: User = queries
.for_entity::<User>(42)
.fetch_with(move || async move {
let (id, name): (i64, String) =
sqlx::query_as("select id, name from users where id = $1")
.bind(42_i64)
.fetch_one(&pool)
.await?;
Ok::<_, sqlx::Error>(User { id, name })
})
.await?;
assert_eq!(user.id, 42);
assert!(!user.name.is_empty());Use DbQuery::fetch_with when you need SQLx macros, transactions, or a
repository function instead of a pool-like executor.
Re-exports§
pub use sqlx;
Structs§
- DbCache
- A database-oriented view over a
HydraCacheinstance. - DbQuery
- A cacheable database query descriptor.
Enums§
- DbCache
Error - Error type returned by database cache adapter helpers.
- Sqlx
Cache Error - Error type returned by SQLx-facing cache helpers.
Traits§
- Cache
Entity - Static cache metadata for a domain entity.
- Sqlx
Query Ext - Convenience SQLx execution methods for
DbQuery.
Type Aliases§
- DbResult
- Database cache adapter result type.
- Result
- SQLx adapter result type.
- Sqlx
Cache - SQLx-specific compatibility name for
DbCache. - Sqlx
Query - SQLx-specific compatibility name for
DbQuery.
Derive Macros§
- Hydra
Cache Entity - Derive
CacheEntitymetadata for database result-cache helpers.