mod error;
mod query_ext;
pub use error::{Result, SqlxCacheError};
pub use hydracache_db::{DbCache, DbCacheError, DbQuery, Result as DbResult};
pub use query_ext::SqlxQueryExt;
pub type SqlxCache<C = hydracache::PostcardCodec> = DbCache<C>;
pub type SqlxQuery<T, C = hydracache::PostcardCodec> = DbQuery<T, C>;
pub use sqlx;
#[cfg(test)]
mod tests {
use hydracache::HydraCache;
use serde::{Deserialize, Serialize};
use sqlx::postgres::PgPoolOptions;
use crate::{DbCache, SqlxCache, SqlxQueryExt};
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
struct User {
id: u64,
}
#[tokio::test]
async fn sqlx_cache_alias_matches_database_cache_api() {
let query = SqlxCache::new(HydraCache::local().build(), "sqlx")
.cached::<User>()
.key("user:1");
assert_eq!(query.physical_key(), Some("sqlx:user:1".to_owned()));
}
#[tokio::test]
async fn db_cache_reexport_is_available_from_sqlx_crate() {
let query = DbCache::new(HydraCache::local().build(), "db")
.cached::<User>()
.key("user:1");
assert_eq!(query.physical_key(), Some("db:user:1".to_owned()));
}
#[tokio::test]
async fn sqlx_helper_missing_key_returns_sqlx_cache_error() {
let pool = PgPoolOptions::new()
.connect_lazy("postgres://postgres:postgres@localhost/postgres")
.unwrap();
let result = DbCache::new(HydraCache::local().build(), "db")
.cached::<(i64,)>()
.fetch_one(pool, sqlx::query_as("select 1"))
.await;
let error = result.unwrap_err();
assert_eq!(
error.to_string(),
"database cached operation `db:unnamed` is missing an explicit cache key"
);
}
#[tokio::test]
async fn sqlx_cache_error_wraps_db_cache_errors() {
let error = hydracache_db::DbCacheError::MissingKey {
operation: "load-user".to_owned(),
};
let error = crate::SqlxCacheError::from(error);
assert_eq!(
error.to_string(),
"database cached operation `load-user` is missing an explicit cache key"
);
}
}