Skip to main content

rullst_orm/
lib.rs

1#[cfg(not(any(
2    feature = "strict-postgres",
3    feature = "strict-mysql",
4    feature = "strict-sqlite"
5)))]
6pub use sqlx::AnyPool as RullstPool;
7
8#[cfg(feature = "strict-postgres")]
9pub use sqlx::PgPool as RullstPool;
10
11#[cfg(all(feature = "strict-mysql", not(feature = "strict-postgres")))]
12pub use sqlx::MySqlPool as RullstPool;
13
14#[cfg(all(
15    feature = "strict-sqlite",
16    not(feature = "strict-postgres"),
17    not(feature = "strict-mysql")
18))]
19pub use sqlx::SqlitePool as RullstPool;
20
21#[cfg(not(any(
22    feature = "strict-postgres",
23    feature = "strict-mysql",
24    feature = "strict-sqlite"
25)))]
26use sqlx::any::install_default_drivers;
27
28use std::sync::OnceLock;
29use std::sync::atomic::{AtomicUsize, Ordering};
30
31// Hide underlying libraries for macro usage while keeping the public API clean
32#[doc(hidden)]
33pub use futures as _futures;
34#[doc(hidden)]
35pub use serde as _serde;
36#[doc(hidden)]
37pub use serde_json as _serde_json;
38#[doc(hidden)]
39pub use sqlx as _sqlx;
40
41#[cfg(feature = "redis")]
42#[doc(hidden)]
43pub use redis as _redis;
44pub mod admin;
45pub mod audit;
46pub mod collection;
47pub mod database;
48pub mod db;
49pub mod error;
50pub mod resource;
51pub mod schema;
52pub mod scout;
53pub mod tenant;
54pub mod types;
55
56// Export the custom Error enum to the root
57pub use error::RullstError as Error;
58
59// Re-exports
60pub use _sqlx::FromRow;
61pub use admin::dashboard_html;
62pub use collection::RullstCollection;
63pub use database::RullstDatabase;
64pub use resource::{ApiResource, JsonResource, ResourceCollection};
65pub use rullst_orm_macros::Orm;
66pub use scout::{SearchEngine, get_search_engine, set_search_engine};
67pub use tenant::{get_tenant_id, with_tenant};
68pub use types::Json;
69
70// Re-export async_trait so the macro can use it implicitly
71pub use async_trait::async_trait;
72
73// Re-export sqlx and FromRow for database mapping
74pub use schema::{JoinClause, SubqueryBuilder};
75
76/// The global connection pool
77static DB_POOL: OnceLock<RullstPool> = OnceLock::new();
78
79/// The driver identifier (postgres, mysql, sqlite) to help macro syntax formatting
80static DB_DRIVER: OnceLock<String> = OnceLock::new();
81
82/// The replica connection pools for read operations
83static REPLICA_POOLS: OnceLock<Vec<RullstPool>> = OnceLock::new();
84
85/// Atomic index for replica round-robin selection
86static REPLICA_INDEX: AtomicUsize = AtomicUsize::new(0);
87
88#[cfg(feature = "redis")]
89static REDIS_CLIENT: OnceLock<_redis::Client> = OnceLock::new();
90
91#[cfg(feature = "redis")]
92static REDIS_MANAGER: OnceLock<_redis::aio::ConnectionManager> = OnceLock::new();
93
94/// Enum dinâmico para encapsular qualquer tipo que possa ser associado ao banco de dados pelo Macro
95#[derive(Clone, Debug)]
96pub enum RullstValue {
97    String(String),
98    Int(i32),
99    Float(f64),
100    Bool(bool),
101}
102
103impl From<&str> for RullstValue {
104    fn from(s: &str) -> Self {
105        RullstValue::String(s.to_string())
106    }
107}
108impl From<String> for RullstValue {
109    fn from(s: String) -> Self {
110        RullstValue::String(s)
111    }
112}
113impl From<i32> for RullstValue {
114    fn from(i: i32) -> Self {
115        RullstValue::Int(i)
116    }
117}
118impl From<f64> for RullstValue {
119    fn from(f: f64) -> Self {
120        RullstValue::Float(f)
121    }
122}
123impl From<bool> for RullstValue {
124    fn from(b: bool) -> Self {
125        RullstValue::Bool(b)
126    }
127}
128
129impl TryFrom<RullstValue> for String {
130    type Error = &'static str;
131    fn try_from(val: RullstValue) -> Result<Self, Self::Error> {
132        match val {
133            RullstValue::String(s) => Ok(s),
134            _ => Err("Not a string"),
135        }
136    }
137}
138impl TryFrom<RullstValue> for i32 {
139    type Error = &'static str;
140    fn try_from(val: RullstValue) -> Result<Self, Self::Error> {
141        match val {
142            RullstValue::Int(i) => Ok(i),
143            _ => Err("Not an i32"),
144        }
145    }
146}
147impl TryFrom<RullstValue> for f64 {
148    type Error = &'static str;
149    fn try_from(val: RullstValue) -> Result<Self, Self::Error> {
150        match val {
151            RullstValue::Float(f) => Ok(f),
152            _ => Err("Not an f64"),
153        }
154    }
155}
156impl TryFrom<RullstValue> for bool {
157    type Error = &'static str;
158    fn try_from(val: RullstValue) -> Result<Self, Self::Error> {
159        match val {
160            RullstValue::Bool(b) => Ok(b),
161            _ => Err("Not a bool"),
162        }
163    }
164}
165
166/// Orm configuration structure
167pub struct Orm;
168
169impl Orm {
170    /// Initialize the global database connection pool using an agnostic URI
171    pub async fn init(database_url: &str) -> Result<(), crate::Error> {
172        #[cfg(not(any(
173            feature = "strict-postgres",
174            feature = "strict-mysql",
175            feature = "strict-sqlite"
176        )))]
177        install_default_drivers();
178
179        let pool = RullstPool::connect(database_url).await?;
180
181        if DB_POOL.set(pool).is_err() {
182            return Err(crate::Error::Internal(
183                "Orm has already been initialized".to_string(),
184            ));
185        }
186
187        let driver = if database_url.starts_with("postgres") {
188            "postgres"
189        } else if database_url.starts_with("mysql") {
190            "mysql"
191        } else {
192            "sqlite"
193        };
194
195        let _ = DB_DRIVER.set(driver.to_string());
196        let _ = REPLICA_POOLS.set(vec![]);
197
198        Ok(())
199    }
200
201    /// Initialize the global database connection pool and its read replicas
202    pub async fn init_with_replicas(
203        primary_url: &str,
204        replica_urls: Vec<&str>,
205    ) -> Result<(), crate::Error> {
206        #[cfg(not(any(
207            feature = "strict-postgres",
208            feature = "strict-mysql",
209            feature = "strict-sqlite"
210        )))]
211        install_default_drivers();
212
213        let pool = RullstPool::connect(primary_url).await?;
214
215        if DB_POOL.set(pool).is_err() {
216            return Err(crate::Error::Internal(
217                "Orm has already been initialized".to_string(),
218            ));
219        }
220
221        let driver = if primary_url.starts_with("postgres") {
222            "postgres"
223        } else if primary_url.starts_with("mysql") {
224            "mysql"
225        } else {
226            "sqlite"
227        };
228
229        let _ = DB_DRIVER.set(driver.to_string());
230
231        // Initialize all replica pools concurrently — each connect() is independent I/O.
232        let replica_futures: Vec<_> = replica_urls.into_iter().map(RullstPool::connect).collect();
233        let replicas = futures::future::try_join_all(replica_futures).await?;
234        let _ = REPLICA_POOLS.set(replicas);
235
236        Ok(())
237    }
238
239    /// Retrieve the global database connection pool (strictly for writes).
240    /// Returns `Err(Error::Internal)` if `Orm::init()` has not been called yet.
241    pub fn pool() -> Result<&'static RullstPool, crate::Error> {
242        DB_POOL.get().ok_or_else(|| {
243            crate::Error::Internal(
244                "Orm::init() must be called before any database operation".to_string(),
245            )
246        })
247    }
248
249    /// Retrieve the connection pool for read operations.
250    /// Performs round-robin load balancing over replicas if configured.
251    /// Returns `Err(Error::Internal)` if `Orm::init()` has not been called yet.
252    pub fn read_pool() -> Result<&'static RullstPool, crate::Error> {
253        if let Some(replicas) = REPLICA_POOLS.get()
254            && !replicas.is_empty()
255        {
256            let idx = REPLICA_INDEX.fetch_add(1, Ordering::Relaxed) % replicas.len();
257            return Ok(&replicas[idx]);
258        }
259        Self::pool()
260    }
261
262    /// Retrieve the active driver string ("postgres", "mysql", or "sqlite").
263    /// Returns `Err(Error::Internal)` if `Orm::init()` has not been called yet.
264    pub fn driver() -> Result<&'static str, crate::Error> {
265        DB_DRIVER.get().map(|s| s.as_str()).ok_or_else(|| {
266            crate::Error::Internal(
267                "Orm::init() must be called before any database operation".to_string(),
268            )
269        })
270    }
271
272    pub async fn begin_transaction() -> Result<crate::db::Transaction<'static>, crate::Error> {
273        let pool = Self::pool()?;
274        pool.begin().await.map_err(Into::into)
275    }
276
277    /// Run an array of seeders sequentially
278    pub async fn seed(seeders: Vec<Box<dyn Seeder>>) -> Result<(), crate::Error> {
279        for seeder in seeders {
280            seeder.run().await?;
281        }
282        Ok(())
283    }
284
285    /// Enable query logging to print all queries to the terminal
286    pub fn enable_query_log() {
287        crate::schema::enable_query_log();
288    }
289
290    /// Disable query logging
291    pub fn disable_query_log() {
292        crate::schema::disable_query_log();
293    }
294
295    /// Initialize Redis connection and connection manager for caching and events
296    #[cfg(feature = "redis")]
297    pub async fn init_redis(redis_url: &str) -> Result<(), crate::Error> {
298        let client = _redis::Client::open(redis_url)?;
299        let manager = _redis::aio::ConnectionManager::new(client.clone()).await?;
300        let _ = REDIS_CLIENT.set(client);
301        let _ = REDIS_MANAGER.set(manager);
302        Ok(())
303    }
304
305    /// Get reference to the global Redis client.
306    /// Returns `Err(Error::Internal)` if `Orm::init_redis()` has not been called yet.
307    #[cfg(feature = "redis")]
308    pub fn redis_client() -> Result<&'static _redis::Client, crate::Error> {
309        REDIS_CLIENT.get().ok_or_else(|| {
310            crate::Error::Internal(
311                "Orm::init_redis() must be called before using cache features".to_string(),
312            )
313        })
314    }
315
316    /// Get clone of the thread-safe connection manager for async Redis queries.
317    /// Returns `Err(Error::Internal)` if `Orm::init_redis()` has not been called yet.
318    #[cfg(feature = "redis")]
319    pub fn redis_manager() -> Result<_redis::aio::ConnectionManager, crate::Error> {
320        REDIS_MANAGER.get().cloned().ok_or_else(|| {
321            crate::Error::Internal(
322                "Orm::init_redis() must be called before using cache features".to_string(),
323            )
324        })
325    }
326}
327
328/// A database seeder trait for populating tables
329#[async_trait]
330pub trait Seeder: Send + Sync {
331    async fn run(&self) -> Result<(), crate::Error>;
332}
333
334/// The core trait that all Orm models will implement via #[derive(Orm)]
335#[async_trait]
336pub trait RullstModel {
337    fn table_name() -> &'static str;
338}
339
340/// Represents a paginated result set
341#[derive(Debug, Clone)]
342pub struct PaginationResult<T> {
343    pub data: Vec<T>,
344    pub total: i64,
345    pub per_page: usize,
346    pub current_page: usize,
347    pub last_page: usize,
348}
349
350#[cfg(test)]
351mod tests {
352    use super::*;
353
354    #[test]
355    fn test_rullst_value_conversions() {
356        let v: RullstValue = "test".into();
357        assert!(matches!(v, RullstValue::String(_)));
358        let v_int: RullstValue = 100.into();
359        assert!(matches!(v_int, RullstValue::Int(100)));
360        let v_bool: RullstValue = false.into();
361        assert!(matches!(v_bool, RullstValue::Bool(false)));
362    }
363
364    #[test]
365    fn test_enable_query_log_wrapper() {
366        // Orm::enable/disable_query_log delegate to schema — verify the delegation works.
367        Orm::disable_query_log();
368        assert!(!crate::schema::is_query_log_enabled());
369        Orm::enable_query_log();
370        assert!(crate::schema::is_query_log_enabled());
371        Orm::disable_query_log();
372        assert!(!crate::schema::is_query_log_enabled());
373    }
374
375    #[test]
376    fn test_disable_query_log_wrapper() {
377        Orm::enable_query_log();
378        Orm::disable_query_log();
379        assert!(!crate::schema::is_query_log_enabled());
380    }
381}