Skip to main content

klauthed_data/
lib.rs

1#![deny(unsafe_code)]
2#![deny(missing_docs)]
3#![cfg_attr(
4    not(test),
5    deny(clippy::unwrap_used, clippy::expect_used, clippy::panic, clippy::indexing_slicing)
6)]
7
8//! Data-layer connectors for klauthed services.
9//!
10//! This crate turns the typed configuration sections from
11//! [`klauthed_core::config`] into **real, connected resources** — database
12//! pools, cache clients — so a service never hand-rolls connection wiring.
13//!
14//! Every backend lives behind a Cargo feature, so a service compiles only the
15//! drivers it actually uses:
16//!
17//! (Plain code spans below rather than intra-doc links, since these items are
18//! feature-gated and absent from a default-feature doc build.)
19//!
20//! | Feature        | Provides                                              |
21//! |----------------|-------------------------------------------------------|
22//! | `postgres`     | `db::connect` for PostgreSQL (implies `sql`)         |
23//! | `mysql`        | `db::connect` for MySQL/MariaDB (implies `sql`)      |
24//! | `sqlite`       | `db::connect` for SQLite (implies `sql`)             |
25//! | `redis`        | `cache::connect_redis`                               |
26//! | `cache-memory` | `cache::build_memory_cache` (moka, in-process)      |
27//! | `nats`         | `messaging::connect_nats` (async-nats)              |
28//! | `rabbitmq`     | `messaging::connect_rabbitmq` (lapin / AMQP)        |
29//! | `kafka`        | `messaging::connect_kafka` (rskafka, pure Rust)     |
30//! | `storage`      | `storage::connect` for local filesystem             |
31//! | `storage-s3`   | `storage::connect` for S3 / S3-compatible           |
32//! | `storage-gcs`  | `storage::connect` for Google Cloud Storage         |
33//! | `storage-azure`| `storage::connect` for Azure Blob Storage           |
34//! | `mongodb`      | `db::mongo::connect` for MongoDB                    |
35//!
36//! ```no_run
37//! # async fn run() -> Result<(), Box<dyn std::error::Error>> {
38//! use klauthed_core::config::Config;
39//!
40//! let config = Config::load().await?;
41//!
42//! # #[cfg(feature = "sql")]
43//! let pool = klauthed_data::db::connect(&config.database()?).await?;
44//! # Ok(())
45//! # }
46//! ```
47
48pub mod error;
49
50// ── Reliability patterns ──────────────────────────────────────────────────────
51//
52// Backend-agnostic traits with in-memory implementations. These are always
53// compiled (no feature gate) since they carry no driver dependencies; real
54// Postgres/Redis backends are in sub-modules gated by their own features.
55
56pub mod idempotency;
57pub mod locks;
58pub mod outbox;
59pub mod rate_limit;
60
61// The `db` module houses the relational connector (sql feature) and the
62// MongoDB sub-module (mongodb feature). It is compiled whenever any of those
63// features is active.
64#[cfg(any(feature = "sql", feature = "mongodb"))]
65pub mod db;
66
67#[cfg(any(feature = "redis", feature = "cache-memory"))]
68pub mod cache;
69
70#[cfg(any(feature = "nats", feature = "rabbitmq", feature = "kafka"))]
71pub mod messaging;
72
73#[cfg(feature = "storage")]
74pub mod storage;
75
76// Embedded, versioned schema migrations over a relational pool.
77#[cfg(feature = "sql")]
78pub mod migrate;
79
80// Auto-configuration starter: build the SQL pool from config into an AppContext.
81#[cfg(feature = "sql")]
82pub mod starter;
83
84// ── Stub modules reserved for future implementation ───────────────────────────
85pub mod eventbus;
86pub mod pagination;
87pub mod saga;
88pub mod transaction;
89
90pub use error::DataError;
91
92#[cfg(feature = "sql")]
93pub use migrate::{Migration, Migrator};
94#[cfg(feature = "sql")]
95pub use sqlx::AnyPool;
96#[cfg(feature = "sql")]
97pub use starter::DataStarter;
98
99pub use idempotency::{
100    IdempotencyRecord, IdempotencyStatus, IdempotencyStore, InMemoryIdempotencyStore, Outcome,
101};
102pub use locks::{InMemoryLockManager, LockGuard, LockManager, LockToken};
103pub use outbox::{InMemoryOutbox, Outbox, OutboxEntry, OutboxId};
104
105#[cfg(feature = "sql")]
106pub use outbox::SqlOutbox;
107
108#[cfg(feature = "redis")]
109pub use locks::redis::RedisLockManager;
110
111#[cfg(feature = "redis")]
112pub use idempotency::redis::RedisIdempotencyStore;
113
114#[cfg(feature = "mongodb")]
115pub use outbox::MongoOutbox;
116
117#[cfg(feature = "mongodb")]
118pub use locks::mongo::MongoLockManager;
119
120#[cfg(feature = "mongodb")]
121pub use idempotency::mongo::MongoIdempotencyStore;