type-bridge-orm 1.4.5

Async ORM for TypeDB built on type-bridge-core-lib
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
//! Shared test utilities for ORM integration tests.
//!
//! Provides a mock backend, helper functions, and common model definitions
//! used across multiple test files.

#![allow(dead_code)]

use std::sync::{Arc, Mutex};

use type_bridge_orm::session::backend::{BoxFuture, DriverBackend, QueryResult, TransactionOps};
use type_bridge_orm::*;

// ── Mock backend ─────────────────────────────────────────────────────

/// Records queries and returns pre-configured results (LIFO order).
pub struct MockBackend {
    responses: Arc<Mutex<Vec<QueryResult>>>,
    pub queries: Arc<Mutex<Vec<String>>>,
}

impl MockBackend {
    pub fn new(responses: Vec<QueryResult>) -> Self {
        Self {
            responses: Arc::new(Mutex::new(responses)),
            queries: Arc::new(Mutex::new(Vec::new())),
        }
    }
}

impl DriverBackend for MockBackend {
    fn open_transaction(
        &self,
        _database: &str,
        _tx_type: TxType,
    ) -> BoxFuture<'_, std::result::Result<Box<dyn TransactionOps>, OrmError>> {
        let responses = Arc::clone(&self.responses);
        let queries = Arc::clone(&self.queries);
        Box::pin(async move {
            Ok(Box::new(MockTransaction { responses, queries }) as Box<dyn TransactionOps>)
        })
    }

    fn is_open(&self) -> bool {
        true
    }
}

pub struct MockTransaction {
    responses: Arc<Mutex<Vec<QueryResult>>>,
    queries: Arc<Mutex<Vec<String>>>,
}

impl TransactionOps for MockTransaction {
    fn query(&mut self, typeql: &str) -> BoxFuture<'_, std::result::Result<QueryResult, OrmError>> {
        self.queries.lock().unwrap().push(typeql.to_string());
        let result = self
            .responses
            .lock()
            .unwrap()
            .pop()
            .unwrap_or(QueryResult::Ok);
        Box::pin(async move { Ok(result) })
    }

    fn commit(&mut self) -> BoxFuture<'_, std::result::Result<(), OrmError>> {
        Box::pin(async { Ok(()) })
    }
}

// ── Failing mock backend ─────────────────────────────────────────────

/// Mock backend whose transactions always fail with a configurable error.
pub struct FailingMockBackend {
    error_message: String,
}

impl FailingMockBackend {
    pub fn new(message: &str) -> Self {
        Self {
            error_message: message.to_string(),
        }
    }
}

impl DriverBackend for FailingMockBackend {
    fn open_transaction(
        &self,
        _database: &str,
        _tx_type: TxType,
    ) -> BoxFuture<'_, std::result::Result<Box<dyn TransactionOps>, OrmError>> {
        let msg = self.error_message.clone();
        Box::pin(async move {
            Ok(Box::new(FailingMockTransaction { error_message: msg }) as Box<dyn TransactionOps>)
        })
    }

    fn is_open(&self) -> bool {
        true
    }
}

pub struct FailingMockTransaction {
    error_message: String,
}

impl TransactionOps for FailingMockTransaction {
    fn query(
        &mut self,
        _typeql: &str,
    ) -> BoxFuture<'_, std::result::Result<QueryResult, OrmError>> {
        let msg = self.error_message.clone();
        Box::pin(async move { Err(OrmError::Transaction(msg)) })
    }

    fn commit(&mut self) -> BoxFuture<'_, std::result::Result<(), OrmError>> {
        Box::pin(async { Ok(()) })
    }
}

// ── Helper functions ─────────────────────────────────────────────────

pub fn insert_response(iid: &str) -> QueryResult {
    QueryResult::Documents(vec![serde_json::json!({ "iid": iid })])
}

// ── Attribute types ──────────────────────────────────────────────────

define_attribute!(Name, "name", "string");
define_attribute!(Age, "age", "long");
define_attribute!(Position, "position", "string");

// ── Person entity ────────────────────────────────────────────────────

#[derive(Debug)]
pub struct Person {
    pub iid: Option<String>,
    pub name: Name,
    pub age: Age,
}

impl TypeBridgeEntity for Person {
    const TYPE_NAME: &'static str = "person";

    fn owned_attributes() -> &'static [OwnedAttributeInfo] {
        &[
            OwnedAttributeInfo {
                attr_name: "name",
                value_type: ValueType::String,
                annotations: &[Annotation::Key],
            },
            OwnedAttributeInfo {
                attr_name: "age",
                value_type: ValueType::Long,
                annotations: &[],
            },
        ]
    }

    fn iid(&self) -> Option<&str> {
        self.iid.as_deref()
    }

    fn set_iid(&mut self, iid: String) {
        self.iid = Some(iid);
    }

    fn to_attribute_values(&self) -> Vec<(&'static str, AttributeValue)> {
        vec![("name", self.name.to_value()), ("age", self.age.to_value())]
    }

    fn from_document(doc: &serde_json::Map<String, serde_json::Value>) -> Result<Self> {
        let name = doc
            .get("name")
            .and_then(|v| v.as_str())
            .ok_or_else(|| OrmError::Hydration {
                type_name: "person".into(),
                message: "missing name".into(),
            })?;
        let age = doc
            .get("age")
            .and_then(|v| v.as_i64())
            .ok_or_else(|| OrmError::Hydration {
                type_name: "person".into(),
                message: "missing age".into(),
            })?;
        Ok(Person {
            iid: None,
            name: Name(name.to_string()),
            age: Age(age),
        })
    }
}

pub fn make_person(name: &str, age: i64) -> Person {
    Person {
        iid: None,
        name: Name(name.into()),
        age: Age(age),
    }
}

pub fn make_person_with_iid(name: &str, age: i64, iid: &str) -> Person {
    Person {
        iid: Some(iid.to_string()),
        name: Name(name.into()),
        age: Age(age),
    }
}

// ── Employment relation ──────────────────────────────────────────────

#[derive(Debug)]
pub struct Employment {
    pub iid: Option<String>,
    pub employee: RolePlayerRef,
    pub employer: RolePlayerRef,
    pub position: Option<String>,
}

impl TypeBridgeRelation for Employment {
    const TYPE_NAME: &'static str = "employment";

    fn owned_attributes() -> &'static [OwnedAttributeInfo] {
        static ATTRS: [OwnedAttributeInfo; 1] = [OwnedAttributeInfo {
            attr_name: "position",
            value_type: ValueType::String,
            annotations: &[],
        }];
        &ATTRS
    }

    fn role_info() -> &'static [RoleInfo] {
        static ROLES: [RoleInfo; 2] = [
            RoleInfo {
                role_name: "employee",
                player_type_name: "person",
            },
            RoleInfo {
                role_name: "employer",
                player_type_name: "company",
            },
        ];
        &ROLES
    }

    fn iid(&self) -> Option<&str> {
        self.iid.as_deref()
    }

    fn set_iid(&mut self, iid: String) {
        self.iid = Some(iid);
    }

    fn to_attribute_values(&self) -> Vec<(&'static str, AttributeValue)> {
        let mut values = Vec::new();
        if let Some(ref pos) = self.position {
            values.push(("position", AttributeValue::String(pos.clone())));
        }
        values
    }

    fn to_role_player_refs(&self) -> Vec<RolePlayerRef> {
        vec![self.employee.clone(), self.employer.clone()]
    }

    fn from_document(doc: &serde_json::Map<String, serde_json::Value>) -> Result<Self> {
        let position = doc
            .get("position")
            .and_then(|v| v.as_str())
            .map(String::from);
        Ok(Self {
            iid: None,
            employee: RolePlayerRef {
                role: "employee",
                entity_type_name: "person",
                iid: None,
                key: None,
            },
            employer: RolePlayerRef {
                role: "employer",
                entity_type_name: "company",
                iid: None,
                key: None,
            },
            position,
        })
    }
}

pub fn make_employment(
    iid: Option<&str>,
    emp_iid: Option<&str>,
    emp_key: Option<(&'static str, AttributeValue)>,
    er_iid: Option<&str>,
    er_key: Option<(&'static str, AttributeValue)>,
    position: Option<&str>,
) -> Employment {
    Employment {
        iid: iid.map(String::from),
        employee: RolePlayerRef {
            role: "employee",
            entity_type_name: "person",
            iid: emp_iid.map(String::from),
            key: emp_key,
        },
        employer: RolePlayerRef {
            role: "employer",
            entity_type_name: "company",
            iid: er_iid.map(String::from),
            key: er_key,
        },
        position: position.map(String::from),
    }
}

// ── Lifecycle hook helpers ───────────────────────────────────────────

/// Hook that records all calls for verification.
pub struct RecordingHook {
    pub calls: Arc<Mutex<Vec<(String, CrudOperation)>>>,
}

impl RecordingHook {
    #[allow(clippy::type_complexity)]
    pub fn new() -> (Self, Arc<Mutex<Vec<(String, CrudOperation)>>>) {
        let calls = Arc::new(Mutex::new(Vec::new()));
        (
            Self {
                calls: Arc::clone(&calls),
            },
            calls,
        )
    }
}

impl LifecycleHook for RecordingHook {
    fn name(&self) -> &str {
        "recording"
    }

    fn before_operation<'a>(
        &'a self,
        ctx: &'a mut HookContext,
    ) -> BoxFuture<'a, std::result::Result<PreHookResult, HookError>> {
        self.calls
            .lock()
            .unwrap()
            .push((format!("pre:{}", ctx.type_name), ctx.operation));
        Box::pin(async { Ok(PreHookResult::Continue) })
    }

    fn after_operation<'a>(
        &'a self,
        ctx: &'a HookContext,
    ) -> BoxFuture<'a, std::result::Result<(), HookError>> {
        self.calls
            .lock()
            .unwrap()
            .push((format!("post:{}", ctx.type_name), ctx.operation));
        Box::pin(async { Ok(()) })
    }
}

/// Hook that rejects all operations.
pub struct RejectingHook {
    pub reason: String,
}

impl RejectingHook {
    pub fn new(reason: &str) -> Self {
        Self {
            reason: reason.to_string(),
        }
    }
}

impl LifecycleHook for RejectingHook {
    fn name(&self) -> &str {
        "rejecting"
    }

    fn before_operation<'a>(
        &'a self,
        _ctx: &'a mut HookContext,
    ) -> BoxFuture<'a, std::result::Result<PreHookResult, HookError>> {
        let reason = self.reason.clone();
        Box::pin(async move { Ok(PreHookResult::Reject { reason }) })
    }

    fn after_operation<'a>(
        &'a self,
        _ctx: &'a HookContext,
    ) -> BoxFuture<'a, std::result::Result<(), HookError>> {
        Box::pin(async { Ok(()) })
    }
}

/// Hook that only runs for specific operations.
pub struct OperationFilterHook {
    pub allowed_op: CrudOperation,
    pub calls: Arc<Mutex<Vec<CrudOperation>>>,
}

impl OperationFilterHook {
    pub fn new(op: CrudOperation) -> (Self, Arc<Mutex<Vec<CrudOperation>>>) {
        let calls = Arc::new(Mutex::new(Vec::new()));
        (
            Self {
                allowed_op: op,
                calls: Arc::clone(&calls),
            },
            calls,
        )
    }
}

impl LifecycleHook for OperationFilterHook {
    fn name(&self) -> &str {
        "op-filter"
    }

    fn before_operation<'a>(
        &'a self,
        ctx: &'a mut HookContext,
    ) -> BoxFuture<'a, std::result::Result<PreHookResult, HookError>> {
        self.calls.lock().unwrap().push(ctx.operation);
        Box::pin(async { Ok(PreHookResult::Continue) })
    }

    fn after_operation<'a>(
        &'a self,
        _ctx: &'a HookContext,
    ) -> BoxFuture<'a, std::result::Result<(), HookError>> {
        Box::pin(async { Ok(()) })
    }

    fn should_run(&self, ctx: &HookContext) -> bool {
        ctx.operation == self.allowed_op
    }
}

/// Hook whose post-hook fails (errors should be logged, not propagated).
pub struct FailingPostHook;

impl LifecycleHook for FailingPostHook {
    fn name(&self) -> &str {
        "failing-post"
    }

    fn before_operation<'a>(
        &'a self,
        _ctx: &'a mut HookContext,
    ) -> BoxFuture<'a, std::result::Result<PreHookResult, HookError>> {
        Box::pin(async { Ok(PreHookResult::Continue) })
    }

    fn after_operation<'a>(
        &'a self,
        _ctx: &'a HookContext,
    ) -> BoxFuture<'a, std::result::Result<(), HookError>> {
        Box::pin(async {
            Err(HookError::Internal {
                hook_name: "failing-post".to_string(),
                source: "simulated failure".into(),
            })
        })
    }
}