navree-orm 1.2.2

Navree ORM: active-record models, a typed query builder, dirty tracking and pagination on PostgreSQL/TimescaleDB, MySQL and SQLite.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
//! [`CastContext`]: the app services casts work with (the encrypter, the
//! hasher, the clock, the configuration, the SQL dialect), and the scopes
//! that make them available while records are read and written.
//!
//! Casts run inside [`Model::from_row`](crate::Model::from_row) and
//! [`Model::values`](crate::Model::values), which are synchronous and take
//! no executor. The ORM therefore resolves the services a model's casts
//! need from the executor's app *before* it reads or writes the model's
//! records, and runs the reads and writes inside a scope that holds them:
//!
//! - queries, `save`, `delete`, `touch`, `refresh` and the increments
//!   resolve the context from the app of the `Db` or `Uow` they run on
//!   ([`CastContext::for_model`]), for models whose casts declare that they
//!   need one ([`Cast::CONTEXT`](crate::cast::Cast::CONTEXT));
//! - a record keeps the context it was read or saved under, and its dirty
//!   checks, fills and serialization use it when no scope is running
//!   (`create_from` and `force_create` resolve it before they fill);
//! - code that encodes or decodes outside those operations (a test, a
//!   script on a `Db` opened by hand) sets one itself with
//!   [`CastContext::scope`] or [`CastContext::sync_scope`];
//! - conditions are encoded where they are built (`Deal::CODE.eq(..)`),
//!   before the query runs: a condition on a cast that reads services is
//!   built inside such a scope (or under the process default);
//! - a process can install a fallback with [`CastContext::set_default`].
//!
//! A context set explicitly with `scope` / `sync_scope` wins over the app's
//! services, so tests can pin keys:
//!
//! ```
//! use navree_crypto::{AppKey, Encrypter};
//! use navree_orm::attributes::CastContext;
//! use navree_orm::cast::{Cast, Encrypted};
//!
//! let context = CastContext::new().with_encrypter(Encrypter::new(AppKey::generate()));
//! let payload = context.clone().sync_scope(|| <Encrypted as Cast<String>>::set(&"secret".to_owned()))?;
//! assert!(payload.starts_with("v1."));
//! let plain: String = context.sync_scope(|| Encrypted::get(payload))?;
//! assert_eq!(plain, "secret");
//! # Ok::<(), navree_orm::cast::CastError>(())
//! ```

use std::fmt;
use std::future::Future;
use std::sync::{Arc, LazyLock, RwLock};

use navree_config::Config;
use navree_contracts::Clock;
use navree_crypto::{Encrypter, Hasher};
use navree_kernel::AppContext;

use crate::cast::CastError;
use crate::error::{OrmError, Result};
use crate::exec::ExecContext;
use crate::model::Model;
use crate::sql::Dialect;

tokio::task_local! {
    /// The context of the running scope.
    static CURRENT: Arc<CastContext>;
}

/// The process-wide fallback ([`CastContext::set_default`]).
static DEFAULT: RwLock<Option<Arc<CastContext>>> = RwLock::new(None);

/// The context outside every scope when no default is installed.
static EMPTY: LazyLock<Arc<CastContext>> = LazyLock::new(|| Arc::new(CastContext::new()));

/// The services casts work with, for the model being read or written.
///
/// Built-in casts use the [`Encrypter`] (`"encrypted"`, `"encrypted_json"`),
/// the [`Hasher`] (`"hashed"`) and the dialect (`"vector"`); custom casts
/// read whatever they need in [`Cast::get_with`](crate::cast::Cast::get_with),
/// [`Cast::set_with`](crate::cast::Cast::set_with) and the asynchronous
/// [`Cast::prepare`](crate::cast::Cast::prepare), which can also resolve
/// any other service from [`app`](CastContext::app).
///
/// Cloning is cheap: the services are shared.
#[derive(Clone, Default)]
pub struct CastContext {
    /// The model being read or written.
    model: Option<&'static str>,
    /// The encrypter name the model declares (`#[model(encrypter = "..")]`).
    encrypter_name: Option<&'static str>,
    encrypter: Option<Arc<Encrypter>>,
    named_encrypters: Vec<(&'static str, Arc<Encrypter>)>,
    hasher: Option<Arc<Hasher>>,
    clock: Option<Arc<dyn Clock>>,
    config: Option<Arc<Config>>,
    app: Option<AppContext>,
    dialect: Option<Dialect>,
    /// Set by [`CastContext::scope`] / [`CastContext::sync_scope`] (called
    /// by application code, not by the ORM's own operations): such a
    /// context's services win over the app's.
    explicit: bool,
}

impl CastContext {
    /// An empty context: casts that need a service report which one is
    /// missing.
    pub fn new() -> Self {
        CastContext::default()
    }

    /// Uses `encrypter` for encrypted casts.
    pub fn with_encrypter(mut self, encrypter: impl Into<Arc<Encrypter>>) -> Self {
        self.encrypter = Some(encrypter.into());
        self
    }

    /// Uses `encrypter` for the models that declare `#[model(encrypter =
    /// "name")]` with this `name`.
    pub fn with_named_encrypter(
        mut self,
        name: &'static str,
        encrypter: impl Into<Arc<Encrypter>>,
    ) -> Self {
        self.named_encrypters.retain(|(known, _)| *known != name);
        self.named_encrypters.push((name, encrypter.into()));
        self
    }

    /// Uses `hasher` for hashed casts.
    pub fn with_hasher(mut self, hasher: impl Into<Arc<Hasher>>) -> Self {
        self.hasher = Some(hasher.into());
        self
    }

    /// Uses `clock` for casts that read the time.
    pub fn with_clock(mut self, clock: Arc<dyn Clock>) -> Self {
        self.clock = Some(clock);
        self
    }

    /// Uses `config` for casts that read configuration.
    pub fn with_config(mut self, config: impl Into<Arc<Config>>) -> Self {
        self.config = Some(config.into());
        self
    }

    /// Encodes for `dialect` (casts whose stored form differs between
    /// databases).
    pub fn with_dialect(mut self, dialect: Dialect) -> Self {
        self.dialect = Some(dialect);
        self
    }

    /// The model being read or written, e.g. `Patient` (for messages).
    pub fn model(&self) -> Option<&'static str> {
        self.model
    }

    /// The encrypter of the model being read or written: the one its
    /// `#[model(encrypter = "...")]` names, else the app's.
    ///
    /// The error names what to add: navree-crypto's `CryptoModule` (with an
    /// `app.key`), a named `Encrypter` provider, or a context set with
    /// [`CastContext::scope`].
    pub fn encrypter(&self) -> Result<&Encrypter, CastError> {
        match self.encrypter_name {
            Some(name) => self
                .named_encrypters
                .iter()
                .find(|(known, _)| *known == name)
                .map(|(_, encrypter)| encrypter.as_ref())
                .ok_or_else(|| {
                    CastError::new(format!(
                        "no Encrypter named `{name}` is available: register one in the app with `m.provider_named(\"{name}\", ..)`, or set one with CastContext::with_named_encrypter"
                    ))
                }),
            None => self.encrypter.as_deref().ok_or_else(|| {
                CastError::new(
                    "no Encrypter is available: run the query on a Db or Uow of an app with navree-crypto's CryptoModule (and an `app.key`), or set one with CastContext::scope",
                )
            }),
        }
    }

    /// The hasher; the error names what to add (navree-crypto's
    /// `CryptoModule`, or a context set with [`CastContext::scope`]).
    pub fn hasher(&self) -> Result<&Hasher, CastError> {
        self.hasher.as_deref().ok_or_else(|| {
            CastError::new(
                "no Hasher is available: run the query on a Db or Uow of an app with navree-crypto's CryptoModule, or set one with CastContext::scope",
            )
        })
    }

    /// The app's clock, when it registers one.
    pub fn clock(&self) -> Option<&dyn Clock> {
        self.clock.as_deref()
    }

    /// The current time: the app's clock when it registers one, else the
    /// system clock.
    pub fn now(&self) -> chrono::DateTime<chrono::Utc> {
        self.clock
            .as_ref()
            .map_or_else(chrono::Utc::now, |clock| clock.now())
    }

    /// The app's configuration, when the context comes from an app with
    /// `ConfigModule`.
    pub fn config(&self) -> Option<&Config> {
        self.config.as_deref()
    }

    /// The app, when the context comes from one (resolve further services
    /// in [`Cast::prepare`](crate::cast::Cast::prepare), which is
    /// asynchronous).
    pub fn app(&self) -> Option<&AppContext> {
        self.app.as_ref()
    }

    /// The SQL dialect of the database being written to or read from;
    /// `None` on a document store or outside an operation.
    pub fn dialect(&self) -> Option<Dialect> {
        self.dialect
    }

    /// The context for reading and writing `M`'s records in `exec`: `None`
    /// when no cast of `M` needs one (nothing to resolve, no scope to set).
    ///
    /// Services come, in this order of precedence, from an explicit
    /// enclosing scope ([`scope`](Self::scope)), the executor's app, and
    /// then an enclosing operation's scope or the process default
    /// ([`set_default`](Self::set_default)). Fails when the model names an
    /// encrypter (`#[model(encrypter = "...")]`) that neither an explicit
    /// scope nor the app provides.
    pub async fn for_model<M: Model>(exec: &ExecContext) -> Result<Option<CastContext>> {
        let meta = M::meta();
        if !meta.uses_cast_context() {
            return Ok(None);
        }
        let outer = CastContext::current_arc();
        let mut context = CastContext {
            model: Some(meta.name()),
            encrypter_name: meta.encrypter_name(),
            dialect: exec.dialect(),
            app: exec.app().cloned(),
            explicit: outer.explicit,
            ..CastContext::default()
        };
        if outer.explicit {
            context.fill_from(&outer);
        }
        if let Some(app) = exec.app() {
            if context.encrypter.is_none() {
                context.encrypter = app.try_get::<Encrypter>().await;
            }
            if let Some(name) = meta.encrypter_name()
                && context.named_encrypter(name).is_none()
            {
                let encrypter = app.get_named::<Encrypter>(name).await.map_err(|error| {
                    OrmError::Cast {
                        model: meta.name().into(),
                        column: String::new(),
                        message: format!(
                            "{} declares #[model(encrypter = \"{name}\")], but the app provides no Encrypter named `{name}`: register one with `m.provider_named(\"{name}\", ..)` ({error})",
                            meta.name()
                        ),
                    }
                })?;
                context.named_encrypters.push((name, encrypter));
            }
            if context.hasher.is_none() {
                context.hasher = app.try_get::<Hasher>().await;
            }
            if context.clock.is_none() {
                context.clock = app.try_get::<dyn Clock>().await;
            }
            if context.config.is_none() {
                context.config = app.try_get::<Config>().await;
            }
        }
        if !outer.explicit {
            context.fill_from(&outer);
        }
        if let Some(default) = CastContext::default_context() {
            context.fill_from(&default);
        }
        Ok(Some(context))
    }

    /// Takes `other`'s services where this context lacks them.
    fn fill_from(&mut self, other: &CastContext) {
        if self.encrypter.is_none() {
            self.encrypter.clone_from(&other.encrypter);
        }
        for (name, encrypter) in &other.named_encrypters {
            if self.named_encrypter(name).is_none() {
                self.named_encrypters.push((name, Arc::clone(encrypter)));
            }
        }
        if self.hasher.is_none() {
            self.hasher.clone_from(&other.hasher);
        }
        if self.clock.is_none() {
            self.clock.clone_from(&other.clock);
        }
        if self.config.is_none() {
            self.config.clone_from(&other.config);
        }
        if self.dialect.is_none() {
            self.dialect = other.dialect;
        }
    }

    fn named_encrypter(&self, name: &str) -> Option<Arc<Encrypter>> {
        self.named_encrypters
            .iter()
            .find(|(known, _)| *known == name)
            .map(|(_, encrypter)| Arc::clone(encrypter))
    }

    /// Runs `future` with this context as the current one. Its services win
    /// over those of the app an operation inside runs in.
    pub async fn scope<F: Future>(mut self, future: F) -> F::Output {
        self.explicit = true;
        CURRENT.scope(Arc::new(self), future).await
    }

    /// Runs `f` with this context as the current one (for synchronous
    /// encoding and decoding: [`Model::values`](crate::Model::values),
    /// [`Cast::set`](crate::cast::Cast::set), ...).
    pub fn sync_scope<R>(mut self, f: impl FnOnce() -> R) -> R {
        self.explicit = true;
        CURRENT.sync_scope(Arc::new(self), f)
    }

    /// Runs `future` with this context as the current one, as the ORM does
    /// for its own operations.
    pub(crate) async fn operation_scope<F: Future>(self, future: F) -> F::Output {
        CURRENT.scope(Arc::new(self), future).await
    }

    /// Installs `context` as the process-wide fallback: what casts use
    /// outside every scope, and what operations fall back to for services
    /// their app lacks. Meant for processes with a single app or none
    /// (scripts and tools on a `Db` opened by hand); scopes are the precise
    /// tool everywhere else.
    pub fn set_default(context: CastContext) {
        let mut default = DEFAULT
            .write()
            .unwrap_or_else(std::sync::PoisonError::into_inner);
        *default = Some(Arc::new(context));
    }

    /// Removes the process-wide fallback.
    pub fn clear_default() {
        let mut default = DEFAULT
            .write()
            .unwrap_or_else(std::sync::PoisonError::into_inner);
        *default = None;
    }

    fn default_context() -> Option<Arc<CastContext>> {
        DEFAULT
            .read()
            .unwrap_or_else(std::sync::PoisonError::into_inner)
            .clone()
    }

    /// The current context: the running scope's, else the process default,
    /// else an empty one.
    pub fn current() -> CastContext {
        CastContext::current_arc().as_ref().clone()
    }

    fn current_arc() -> Arc<CastContext> {
        CURRENT
            .try_with(Arc::clone)
            .ok()
            .or_else(CastContext::default_context)
            .unwrap_or_else(|| Arc::clone(&EMPTY))
    }

    /// Calls `f` with the current context (see [`current`](Self::current))
    /// without cloning its services.
    pub fn with_current<R>(f: impl FnOnce(&CastContext) -> R) -> R {
        let context = CastContext::current_arc();
        f(&context)
    }

    /// The running scope's context (an operation's or an explicit one), for
    /// a record read or saved in it to keep.
    pub(crate) fn scoped() -> Option<Arc<CastContext>> {
        CURRENT.try_with(Arc::clone).ok()
    }

    /// Runs `f` with `context` (the one a record was read or saved under)
    /// as the current one when no scope is running; a running scope wins.
    pub(crate) fn with_record<R>(context: Option<&Arc<CastContext>>, f: impl FnOnce() -> R) -> R {
        match context {
            Some(context) if CURRENT.try_with(|_| ()).is_err() => {
                CURRENT.sync_scope(Arc::clone(context), f)
            }
            _ => f(),
        }
    }
}

impl fmt::Debug for CastContext {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("CastContext")
            .field("model", &self.model)
            .field("encrypter_name", &self.encrypter_name)
            .field("encrypter", &self.encrypter.is_some())
            .field(
                "named_encrypters",
                &self
                    .named_encrypters
                    .iter()
                    .map(|(name, _)| *name)
                    .collect::<Vec<_>>(),
            )
            .field("hasher", &self.hasher.is_some())
            .field("clock", &self.clock.is_some())
            .field("config", &self.config.is_some())
            .field("app", &self.app.is_some())
            .field("dialect", &self.dialect)
            .finish()
    }
}

/// Runs `future` in `M`'s cast context for `exec`, when `M`'s casts need
/// one: how the ORM's operations make the app's services available to the
/// casts they encode and decode with.
pub(crate) async fn in_model_scope<M, F, T>(exec: &ExecContext, future: F) -> Result<T>
where
    M: Model,
    F: Future<Output = Result<T>>,
{
    match CastContext::for_model::<M>(exec).await? {
        Some(context) => context.operation_scope(future).await,
        None => future.await,
    }
}

#[cfg(test)]
mod tests {
    use navree_crypto::AppKey;

    use super::*;

    #[test]
    fn scopes_nest_and_restore() {
        assert_eq!(CastContext::current().dialect(), None);
        let outer = CastContext::new().with_dialect(Dialect::Postgres);
        outer.sync_scope(|| {
            assert_eq!(CastContext::current().dialect(), Some(Dialect::Postgres));
            CastContext::new()
                .with_dialect(Dialect::MySql)
                .sync_scope(|| {
                    assert_eq!(CastContext::current().dialect(), Some(Dialect::MySql));
                });
            assert_eq!(CastContext::current().dialect(), Some(Dialect::Postgres));
        });
        assert_eq!(CastContext::current().dialect(), None);
    }

    #[test]
    fn missing_services_say_what_to_add() {
        let context = CastContext::new();
        let error = context.encrypter().unwrap_err().to_string();
        assert!(error.contains("CryptoModule"), "{error}");
        assert!(error.contains("CastContext::scope"), "{error}");
        assert!(context.hasher().unwrap_err().to_string().contains("Hasher"));
        let named = CastContext {
            encrypter_name: Some("pii"),
            ..CastContext::new()
        };
        let error = named.encrypter().unwrap_err().to_string();
        assert!(error.contains("no Encrypter named `pii`"), "{error}");
    }

    #[test]
    fn a_model_picks_its_named_encrypter() {
        let default = Encrypter::new(AppKey::generate());
        let pii = Encrypter::new(AppKey::generate());
        let pii_kid = pii.current_kid().map(str::to_owned);
        let context = CastContext::new()
            .with_encrypter(default)
            .with_named_encrypter("pii", Encrypter::new(AppKey::generate()))
            .with_named_encrypter("pii", pii);
        assert_eq!(context.named_encrypters.len(), 1, "the later one replaces");
        let for_model = CastContext {
            encrypter_name: Some("pii"),
            ..context.clone()
        };
        assert_eq!(
            for_model
                .encrypter()
                .unwrap()
                .current_kid()
                .map(str::to_owned),
            pii_kid
        );
        assert_ne!(
            context
                .encrypter()
                .unwrap()
                .current_kid()
                .map(str::to_owned),
            pii_kid
        );
        assert_eq!(
            format!("{context:?}"),
            r#"CastContext { model: None, encrypter_name: None, encrypter: true, named_encrypters: ["pii"], hasher: false, clock: false, config: false, app: false, dialect: None }"#
        );
    }
}