Skip to main content

radixdb_orm/
session.rs

1use crate::{DynamicRecord, GeneratedRecord, IrDocument};
2use std::future::Future;
3use std::pin::Pin;
4
5/// Runtime-neutral owned future used by async ORM session traits.
6pub type OrmFuture<'a, T, E> = Pin<Box<dyn Future<Output = Result<T, E>> + 'a>>;
7
8/// Synchronous execution boundary shared by embedded and TCP sessions.
9///
10/// Implementations are provided for borrowed session objects, so executing an
11/// ORM builder never creates or owns a connection and cannot escape the
12/// caller's transaction/cursor state.
13pub trait OrmSession {
14    type CommandOutput;
15    type QueryOutput;
16    type Error;
17
18    fn execute_document(self, document: &IrDocument) -> Result<Self::CommandOutput, Self::Error>;
19    fn query_document(self, document: &IrDocument) -> Result<Self::QueryOutput, Self::Error>;
20}
21
22/// Async execution boundary over the caller-owned connection/session.
23///
24/// The boxed future keeps this crate independent from a particular async
25/// runtime while preserving exactly the same IR and result ownership as the
26/// synchronous [`OrmSession`].
27pub trait AsyncOrmSession {
28    type CommandOutput;
29    type QueryOutput;
30    type Error;
31
32    fn execute_document_async<'a>(
33        &'a mut self,
34        document: &'a IrDocument,
35    ) -> OrmFuture<'a, Self::CommandOutput, Self::Error>;
36
37    fn query_document_async<'a>(
38        &'a mut self,
39        document: &'a IrDocument,
40    ) -> OrmFuture<'a, Self::QueryOutput, Self::Error>;
41}
42
43#[derive(Debug, Clone, Copy, PartialEq, Eq)]
44pub enum RecordMutation {
45    Insert,
46    Save,
47    Update,
48    Delete,
49}
50
51/// Transport-specific `RETURNING *` hydration while preserving one session.
52pub trait OrmRecordSession {
53    type Error;
54
55    fn mutate_record(
56        self,
57        record: &mut DynamicRecord,
58        mutation: RecordMutation,
59    ) -> Result<(), Self::Error>;
60}
61
62pub trait AsyncOrmRecordSession {
63    type Error;
64
65    fn mutate_record_async<'a>(
66        &'a mut self,
67        record: &'a mut DynamicRecord,
68        mutation: RecordMutation,
69    ) -> OrmFuture<'a, (), Self::Error>;
70}
71
72/// Transport-owned execution for deterministic generated records.
73pub trait OrmGeneratedRecordSession {
74    type Error;
75
76    fn mutate_generated_record<R: GeneratedRecord>(
77        self,
78        record: &mut R,
79        mutation: RecordMutation,
80    ) -> Result<(), Self::Error>;
81}
82
83pub trait OrmGeneratedQuerySession {
84    type Error;
85
86    fn query_generated_records<R: GeneratedRecord + Default>(
87        self,
88        document: &IrDocument,
89    ) -> Result<Vec<R>, Self::Error>;
90}
91
92/// Async counterpart used by transport clients without introducing a runtime
93/// dependency into the language-neutral ORM crate.
94pub trait AsyncOrmGeneratedRecordSession {
95    type Error;
96
97    fn mutate_generated_record_async<'a, R: GeneratedRecord + 'a>(
98        &'a mut self,
99        record: &'a mut R,
100        mutation: RecordMutation,
101    ) -> OrmFuture<'a, (), Self::Error>;
102}
103
104pub trait AsyncOrmGeneratedQuerySession {
105    type Error;
106
107    fn query_generated_records_async<'a, R: GeneratedRecord + Default + 'a>(
108        &'a mut self,
109        document: &'a IrDocument,
110    ) -> OrmFuture<'a, Vec<R>, Self::Error>;
111}