this-rs 0.0.9

Framework for building complex multi-entity REST and GraphQL APIs with many relationships
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
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
//! ScyllaDB storage backend using the scylla-rust CQL driver.
//!
//! Provides `ScyllaDataService<T>` and `ScyllaLinkService` implementations
//! backed by a ScyllaDB/Cassandra cluster via `scylla::Session`.
//!
//! # Feature flag
//!
//! This module is gated behind the `scylladb` feature flag:
//! ```toml
//! [dependencies]
//! this-rs = { version = "0.0.7", features = ["scylladb"] }
//! ```
//!
//! # Storage model
//!
//! Entities are stored in an `entities` table with `(entity_type, id)` as
//! composite primary key. A `entity_data` column holds the full JSON string for
//! reliable deserialization. All scalar fields are also stored as individual
//! columns for CQL-level querying.
//!
//! Links are stored in a single `links` table with `id` as primary key.
//! Secondary indexes on `source_id` and `target_id` enable efficient
//! `find_by_source` and `find_by_target` queries.

use crate::core::field::FieldValue;
use crate::core::link::LinkEntity;
use crate::core::{Data, DataService, LinkService};
use anyhow::{Result, anyhow};
use async_trait::async_trait;
use scylla::client::session::Session;
use serde::Serialize;
use serde::de::DeserializeOwned;
use std::sync::Arc;
use uuid::Uuid;

// ---------------------------------------------------------------------------
// Schema management
// ---------------------------------------------------------------------------

/// Default keyspace name used when none is specified.
const DEFAULT_KEYSPACE: &str = "this_rs";

/// Create the keyspace and tables if they don't exist.
///
/// This is idempotent — safe to call on every startup.
pub async fn ensure_schema(session: &Session, keyspace: &str) -> Result<()> {
    // Create keyspace with SimpleStrategy (suitable for dev/single-node).
    // Production deployments should pre-create the keyspace with NetworkTopologyStrategy.
    let create_ks = format!(
        "CREATE KEYSPACE IF NOT EXISTS {} WITH replication = \
         {{'class': 'SimpleStrategy', 'replication_factor': 1}}",
        keyspace
    );
    session
        .query_unpaged(create_ks, ())
        .await
        .map_err(|e| anyhow!("Failed to create keyspace: {}", e))?;

    // Entities table: partition by entity_type, cluster by id
    let create_entities = format!(
        "CREATE TABLE IF NOT EXISTS {}.entities (\
            entity_type TEXT, \
            id TEXT, \
            name TEXT, \
            status TEXT, \
            entity_data TEXT, \
            created_at TEXT, \
            updated_at TEXT, \
            PRIMARY KEY ((entity_type), id)\
        )",
        keyspace
    );
    session
        .query_unpaged(create_entities, ())
        .await
        .map_err(|e| anyhow!("Failed to create entities table: {}", e))?;

    // Links table: id as primary key
    let create_links = format!(
        "CREATE TABLE IF NOT EXISTS {}.links (\
            id TEXT PRIMARY KEY, \
            entity_type TEXT, \
            source_id TEXT, \
            target_id TEXT, \
            link_type TEXT, \
            source_type TEXT, \
            target_type TEXT, \
            status TEXT, \
            entity_data TEXT, \
            created_at TEXT, \
            updated_at TEXT\
        )",
        keyspace
    );
    session
        .query_unpaged(create_links, ())
        .await
        .map_err(|e| anyhow!("Failed to create links table: {}", e))?;

    // Secondary indexes for efficient link queries
    let idx_source = format!(
        "CREATE INDEX IF NOT EXISTS ON {}.links (source_id)",
        keyspace
    );
    let idx_target = format!(
        "CREATE INDEX IF NOT EXISTS ON {}.links (target_id)",
        keyspace
    );
    let idx_name = format!("CREATE INDEX IF NOT EXISTS ON {}.entities (name)", keyspace);

    for idx in [&idx_source, &idx_target, &idx_name] {
        session
            .query_unpaged(idx.clone(), ())
            .await
            .map_err(|e| anyhow!("Failed to create index: {}", e))?;
    }

    Ok(())
}

// ---------------------------------------------------------------------------
// ScyllaDataService<T>
// ---------------------------------------------------------------------------

/// Generic data storage service backed by ScyllaDB.
///
/// Each entity type is stored in the `entities` table, partitioned by
/// `entity_type` with `id` as clustering key.
///
/// # Example
///
/// ```rust,ignore
/// use scylla::SessionBuilder;
/// use this::storage::ScyllaDataService;
///
/// let session = SessionBuilder::new().known_node("127.0.0.1:9042").build().await?;
/// let service = ScyllaDataService::<MyEntity>::new(session, "my_keyspace");
/// ```
#[derive(Clone)]
pub struct ScyllaDataService<T> {
    session: Arc<Session>,
    keyspace: String,
    _marker: std::marker::PhantomData<T>,
}

impl<T> ScyllaDataService<T> {
    pub fn new(session: Arc<Session>, keyspace: impl Into<String>) -> Self {
        Self {
            session,
            keyspace: keyspace.into(),
            _marker: std::marker::PhantomData,
        }
    }

    /// Create with the default keyspace name.
    pub fn with_default_keyspace(session: Arc<Session>) -> Self {
        Self::new(session, DEFAULT_KEYSPACE)
    }

    pub fn session(&self) -> &Session {
        &self.session
    }
}

impl<T: Data + Serialize + DeserializeOwned> ScyllaDataService<T> {
    fn entity_type_name() -> &'static str {
        T::resource_name_singular()
    }
}

#[async_trait]
impl<T: Data + Serialize + DeserializeOwned> DataService<T> for ScyllaDataService<T> {
    async fn create(&self, entity: T) -> Result<T> {
        let json_str = serde_json::to_string(&entity)
            .map_err(|e| anyhow!("Failed to serialize entity: {}", e))?;
        let json_val: serde_json::Value = serde_json::to_value(&entity)
            .map_err(|e| anyhow!("Failed to serialize entity: {}", e))?;

        let id = entity.id().to_string();
        let name = json_val
            .get("name")
            .and_then(|v| v.as_str())
            .unwrap_or("")
            .to_string();
        let status = json_val
            .get("status")
            .and_then(|v| v.as_str())
            .unwrap_or("")
            .to_string();
        let created_at = json_val
            .get("created_at")
            .and_then(|v| v.as_str())
            .unwrap_or("")
            .to_string();
        let updated_at = json_val
            .get("updated_at")
            .and_then(|v| v.as_str())
            .unwrap_or("")
            .to_string();
        let entity_type = Self::entity_type_name().to_string();

        let cql = format!(
            "INSERT INTO {}.entities (entity_type, id, name, status, entity_data, created_at, updated_at) \
             VALUES (?, ?, ?, ?, ?, ?, ?)",
            self.keyspace
        );

        self.session
            .query_unpaged(
                cql,
                (
                    &entity_type,
                    &id,
                    &name,
                    &status,
                    &json_str,
                    &created_at,
                    &updated_at,
                ),
            )
            .await
            .map_err(|e| anyhow!("Failed to create entity: {}", e))?;

        Ok(entity)
    }

    async fn get(&self, id: &Uuid) -> Result<Option<T>> {
        let cql = format!(
            "SELECT entity_data FROM {}.entities WHERE entity_type = ? AND id = ?",
            self.keyspace
        );

        let result = self
            .session
            .query_unpaged(cql, (Self::entity_type_name(), id.to_string().as_str()))
            .await
            .map_err(|e| anyhow!("Failed to get entity: {}", e))?;

        let rows_result = result
            .into_rows_result()
            .map_err(|e| anyhow!("Failed to parse result: {}", e))?;

        let rows: Vec<(String,)> = rows_result
            .rows()
            .map_err(|e| anyhow!("Failed to deserialize rows: {}", e))?
            .collect::<std::result::Result<Vec<_>, _>>()
            .map_err(|e| anyhow!("Failed to collect rows: {}", e))?;

        match rows.first() {
            Some((data,)) => {
                let entity: T = serde_json::from_str(data)
                    .map_err(|e| anyhow!("Failed to deserialize entity: {}", e))?;
                Ok(Some(entity))
            }
            None => Ok(None),
        }
    }

    async fn list(&self) -> Result<Vec<T>> {
        let cql = format!(
            "SELECT entity_data FROM {}.entities WHERE entity_type = ?",
            self.keyspace
        );

        let result = self
            .session
            .query_unpaged(cql, (Self::entity_type_name(),))
            .await
            .map_err(|e| anyhow!("Failed to list entities: {}", e))?;

        let rows_result = result
            .into_rows_result()
            .map_err(|e| anyhow!("Failed to parse result: {}", e))?;

        let rows: Vec<(String,)> = rows_result
            .rows()
            .map_err(|e| anyhow!("Failed to deserialize rows: {}", e))?
            .collect::<std::result::Result<Vec<_>, _>>()
            .map_err(|e| anyhow!("Failed to collect rows: {}", e))?;

        let mut entities = Vec::new();
        for (data,) in &rows {
            let entity: T = serde_json::from_str(data)
                .map_err(|e| anyhow!("Failed to deserialize entity: {}", e))?;
            entities.push(entity);
        }

        // Sort by created_at DESC (CQL doesn't support ORDER BY on non-clustering columns)
        entities.sort_by_key(|b| std::cmp::Reverse(b.created_at()));

        Ok(entities)
    }

    async fn update(&self, id: &Uuid, entity: T) -> Result<T> {
        // Verify entity exists first
        let existing = self.get(id).await?;
        if existing.is_none() {
            return Err(anyhow!("Entity not found: {}", id));
        }

        let json_str = serde_json::to_string(&entity)
            .map_err(|e| anyhow!("Failed to serialize entity: {}", e))?;
        let json_val: serde_json::Value = serde_json::to_value(&entity)
            .map_err(|e| anyhow!("Failed to serialize entity: {}", e))?;

        let name = json_val
            .get("name")
            .and_then(|v| v.as_str())
            .unwrap_or("")
            .to_string();
        let status = json_val
            .get("status")
            .and_then(|v| v.as_str())
            .unwrap_or("")
            .to_string();
        let updated_at = json_val
            .get("updated_at")
            .and_then(|v| v.as_str())
            .unwrap_or("")
            .to_string();

        let cql = format!(
            "UPDATE {}.entities SET name = ?, status = ?, entity_data = ?, updated_at = ? \
             WHERE entity_type = ? AND id = ?",
            self.keyspace
        );

        self.session
            .query_unpaged(
                cql,
                (
                    &name,
                    &status,
                    &json_str,
                    &updated_at,
                    Self::entity_type_name(),
                    id.to_string().as_str(),
                ),
            )
            .await
            .map_err(|e| anyhow!("Failed to update entity: {}", e))?;

        Ok(entity)
    }

    async fn delete(&self, id: &Uuid) -> Result<()> {
        let cql = format!(
            "DELETE FROM {}.entities WHERE entity_type = ? AND id = ?",
            self.keyspace
        );

        self.session
            .query_unpaged(cql, (Self::entity_type_name(), id.to_string().as_str()))
            .await
            .map_err(|e| anyhow!("Failed to delete entity: {}", e))?;

        Ok(())
    }

    async fn search(&self, field: &str, value: &str) -> Result<Vec<T>> {
        // For "name" field, we can use the secondary index directly
        if field == "name" {
            let cql = format!(
                "SELECT entity_data FROM {}.entities WHERE entity_type = ? AND name = ? ALLOW FILTERING",
                self.keyspace
            );

            let result = self
                .session
                .query_unpaged(cql, (Self::entity_type_name(), value))
                .await
                .map_err(|e| anyhow!("Failed to search entities: {}", e))?;

            let rows_result = result
                .into_rows_result()
                .map_err(|e| anyhow!("Failed to parse result: {}", e))?;

            let rows: Vec<(String,)> = rows_result
                .rows()
                .map_err(|e| anyhow!("Failed to deserialize rows: {}", e))?
                .collect::<std::result::Result<Vec<_>, _>>()
                .map_err(|e| anyhow!("Failed to collect rows: {}", e))?;

            let mut entities = Vec::new();
            for (data,) in &rows {
                let entity: T = serde_json::from_str(data)
                    .map_err(|e| anyhow!("Failed to deserialize entity: {}", e))?;
                entities.push(entity);
            }
            return Ok(entities);
        }

        // For other fields, load all entities and filter client-side via entity_data JSON.
        // ScyllaDB doesn't support arbitrary field queries efficiently.
        let all = self.list().await?;
        let results = all
            .into_iter()
            .filter(|entity| {
                entity.field_value(field).is_some_and(|fv| match &fv {
                    FieldValue::String(s) => s == value,
                    FieldValue::Integer(i) => i.to_string() == value,
                    FieldValue::Float(f) => f.to_string() == value,
                    FieldValue::Boolean(b) => b.to_string() == value,
                    FieldValue::Uuid(u) => u.to_string() == value,
                    FieldValue::DateTime(dt) => dt.to_rfc3339() == value,
                    FieldValue::Null => false,
                })
            })
            .collect();

        Ok(results)
    }
}

// ---------------------------------------------------------------------------
// ScyllaLinkService
// ---------------------------------------------------------------------------

/// Link storage service backed by ScyllaDB.
///
/// Links are stored in a single `links` table with secondary indexes on
/// `source_id` and `target_id` for efficient directional queries.
///
/// # Example
///
/// ```rust,ignore
/// use scylla::SessionBuilder;
/// use this::storage::ScyllaLinkService;
///
/// let session = SessionBuilder::new().known_node("127.0.0.1:9042").build().await?;
/// let service = ScyllaLinkService::new(session, "my_keyspace");
/// ```
#[derive(Clone)]
pub struct ScyllaLinkService {
    session: Arc<Session>,
    keyspace: String,
}

impl ScyllaLinkService {
    pub fn new(session: Arc<Session>, keyspace: impl Into<String>) -> Self {
        Self {
            session,
            keyspace: keyspace.into(),
        }
    }

    /// Create with the default keyspace name.
    pub fn with_default_keyspace(session: Arc<Session>) -> Self {
        Self::new(session, DEFAULT_KEYSPACE)
    }

    pub fn session(&self) -> &Session {
        &self.session
    }

    /// Parse a link from its entity_data JSON string.
    fn parse_link(data: &str) -> Result<LinkEntity> {
        serde_json::from_str(data).map_err(|e| anyhow!("Failed to deserialize link: {}", e))
    }

    /// Collect links from a query result.
    async fn collect_links(
        &self,
        cql: String,
        values: impl scylla::serialize::row::SerializeRow,
    ) -> Result<Vec<LinkEntity>> {
        let result = self
            .session
            .query_unpaged(cql, values)
            .await
            .map_err(|e| anyhow!("Failed to query links: {}", e))?;

        let rows_result = result
            .into_rows_result()
            .map_err(|e| anyhow!("Failed to parse result: {}", e))?;

        let rows: Vec<(String,)> = rows_result
            .rows()
            .map_err(|e| anyhow!("Failed to deserialize rows: {}", e))?
            .collect::<std::result::Result<Vec<_>, _>>()
            .map_err(|e| anyhow!("Failed to collect rows: {}", e))?;

        let mut links = Vec::new();
        for (data,) in &rows {
            links.push(Self::parse_link(data)?);
        }

        // Sort by created_at DESC
        links.sort_by_key(|b| std::cmp::Reverse(b.created_at));

        Ok(links)
    }
}

#[async_trait]
impl LinkService for ScyllaLinkService {
    async fn create(&self, link: LinkEntity) -> Result<LinkEntity> {
        let json_str =
            serde_json::to_string(&link).map_err(|e| anyhow!("Failed to serialize link: {}", e))?;

        let cql = format!(
            "INSERT INTO {}.links (id, entity_type, source_id, target_id, link_type, \
             source_type, target_type, status, entity_data, created_at, updated_at) \
             VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
            self.keyspace
        );

        let source_type = link.entity_type.clone();
        let target_type = "".to_string(); // LinkEntity doesn't store source/target types separately

        self.session
            .query_unpaged(
                cql,
                (
                    link.id.to_string().as_str(),
                    &link.entity_type,
                    link.source_id.to_string().as_str(),
                    link.target_id.to_string().as_str(),
                    &link.link_type,
                    &source_type,
                    &target_type,
                    &link.status,
                    &json_str,
                    link.created_at.to_rfc3339().as_str(),
                    link.updated_at.to_rfc3339().as_str(),
                ),
            )
            .await
            .map_err(|e| anyhow!("Failed to create link: {}", e))?;

        Ok(link)
    }

    async fn get(&self, id: &Uuid) -> Result<Option<LinkEntity>> {
        let cql = format!(
            "SELECT entity_data FROM {}.links WHERE id = ?",
            self.keyspace
        );

        let result = self
            .session
            .query_unpaged(cql, (id.to_string().as_str(),))
            .await
            .map_err(|e| anyhow!("Failed to get link: {}", e))?;

        let rows_result = result
            .into_rows_result()
            .map_err(|e| anyhow!("Failed to parse result: {}", e))?;

        let rows: Vec<(String,)> = rows_result
            .rows()
            .map_err(|e| anyhow!("Failed to deserialize rows: {}", e))?
            .collect::<std::result::Result<Vec<_>, _>>()
            .map_err(|e| anyhow!("Failed to collect rows: {}", e))?;

        match rows.first() {
            Some((data,)) => Ok(Some(Self::parse_link(data)?)),
            None => Ok(None),
        }
    }

    async fn list(&self) -> Result<Vec<LinkEntity>> {
        let cql = format!("SELECT entity_data FROM {}.links", self.keyspace);
        self.collect_links(cql, ()).await
    }

    async fn find_by_source(
        &self,
        source_id: &Uuid,
        link_type: Option<&str>,
        _target_type: Option<&str>,
    ) -> Result<Vec<LinkEntity>> {
        let cql = format!(
            "SELECT entity_data FROM {}.links WHERE source_id = ?",
            self.keyspace
        );

        let mut links = self
            .collect_links(cql, (source_id.to_string().as_str(),))
            .await?;

        // Apply optional filters client-side
        if let Some(lt) = link_type {
            links.retain(|l| l.link_type == lt);
        }

        Ok(links)
    }

    async fn find_by_target(
        &self,
        target_id: &Uuid,
        link_type: Option<&str>,
        _source_type: Option<&str>,
    ) -> Result<Vec<LinkEntity>> {
        let cql = format!(
            "SELECT entity_data FROM {}.links WHERE target_id = ?",
            self.keyspace
        );

        let mut links = self
            .collect_links(cql, (target_id.to_string().as_str(),))
            .await?;

        if let Some(lt) = link_type {
            links.retain(|l| l.link_type == lt);
        }

        Ok(links)
    }

    async fn update(&self, id: &Uuid, link: LinkEntity) -> Result<LinkEntity> {
        // Verify link exists
        let existing = self.get(id).await?;
        if existing.is_none() {
            return Err(anyhow!("Link not found: {}", id));
        }

        let json_str =
            serde_json::to_string(&link).map_err(|e| anyhow!("Failed to serialize link: {}", e))?;

        let cql = format!(
            "UPDATE {}.links SET entity_type = ?, source_id = ?, target_id = ?, \
             link_type = ?, status = ?, entity_data = ?, updated_at = ? WHERE id = ?",
            self.keyspace
        );

        self.session
            .query_unpaged(
                cql,
                (
                    &link.entity_type,
                    link.source_id.to_string().as_str(),
                    link.target_id.to_string().as_str(),
                    &link.link_type,
                    &link.status,
                    &json_str,
                    link.updated_at.to_rfc3339().as_str(),
                    id.to_string().as_str(),
                ),
            )
            .await
            .map_err(|e| anyhow!("Failed to update link: {}", e))?;

        Ok(link)
    }

    async fn delete(&self, id: &Uuid) -> Result<()> {
        let cql = format!("DELETE FROM {}.links WHERE id = ?", self.keyspace);

        self.session
            .query_unpaged(cql, (id.to_string().as_str(),))
            .await
            .map_err(|e| anyhow!("Failed to delete link: {}", e))?;

        Ok(())
    }

    async fn delete_by_entity(&self, entity_id: &Uuid) -> Result<()> {
        let eid = entity_id.to_string();

        // Find all links where entity is source
        let source_cql = format!("SELECT id FROM {}.links WHERE source_id = ?", self.keyspace);
        let source_result = self
            .session
            .query_unpaged(source_cql, (eid.as_str(),))
            .await
            .map_err(|e| anyhow!("Failed to find source links: {}", e))?;

        // Find all links where entity is target
        let target_cql = format!("SELECT id FROM {}.links WHERE target_id = ?", self.keyspace);
        let target_result = self
            .session
            .query_unpaged(target_cql, (eid.as_str(),))
            .await
            .map_err(|e| anyhow!("Failed to find target links: {}", e))?;

        // Collect all link IDs to delete
        let mut ids_to_delete = Vec::new();

        let source_rows = source_result
            .into_rows_result()
            .map_err(|e| anyhow!("Failed to parse result: {}", e))?;
        let rows: Vec<(String,)> = source_rows
            .rows()
            .map_err(|e| anyhow!("Failed to deserialize: {}", e))?
            .collect::<std::result::Result<Vec<_>, _>>()
            .map_err(|e| anyhow!("Failed to collect: {}", e))?;
        for (id,) in &rows {
            ids_to_delete.push(id.clone());
        }

        let target_rows = target_result
            .into_rows_result()
            .map_err(|e| anyhow!("Failed to parse result: {}", e))?;
        let rows: Vec<(String,)> = target_rows
            .rows()
            .map_err(|e| anyhow!("Failed to deserialize: {}", e))?
            .collect::<std::result::Result<Vec<_>, _>>()
            .map_err(|e| anyhow!("Failed to collect: {}", e))?;
        for (id,) in &rows {
            if !ids_to_delete.contains(id) {
                ids_to_delete.push(id.clone());
            }
        }

        // Delete each link
        let delete_cql = format!("DELETE FROM {}.links WHERE id = ?", self.keyspace);
        for link_id in &ids_to_delete {
            self.session
                .query_unpaged(delete_cql.clone(), (link_id.as_str(),))
                .await
                .map_err(|e| anyhow!("Failed to delete link {}: {}", link_id, e))?;
        }

        Ok(())
    }
}

#[cfg(test)]
#[cfg(feature = "scylladb")]
#[allow(dead_code)]
mod tests {
    use super::*;
    use crate::core::link::LinkEntity;
    use serde_json::json;
    use uuid::Uuid;

    // A lightweight entity for testing field_value / search-filter logic.
    crate::impl_data_entity!(TestWidget, "test_widget", ["name"], {
        weight: f64,
    });

    // -----------------------------------------------------------------------
    // parse_link helpers
    // -----------------------------------------------------------------------

    fn make_link(metadata: Option<serde_json::Value>) -> LinkEntity {
        LinkEntity::new("owns", Uuid::new_v4(), Uuid::new_v4(), metadata)
    }

    #[test]
    fn parse_link_valid_json() {
        let link = make_link(None);
        let json_str = serde_json::to_string(&link).expect("serialize");
        let parsed = ScyllaLinkService::parse_link(&json_str).expect("parse_link should succeed");

        assert_eq!(parsed.id, link.id);
        assert_eq!(parsed.link_type, "owns");
        assert_eq!(parsed.source_id, link.source_id);
        assert_eq!(parsed.target_id, link.target_id);
        assert_eq!(parsed.status, link.status);
    }

    #[test]
    fn parse_link_invalid_json() {
        let result = ScyllaLinkService::parse_link("not json");
        assert!(result.is_err());
        let msg = result.unwrap_err().to_string();
        assert!(
            msg.contains("Failed to deserialize link"),
            "unexpected error message: {msg}"
        );
    }

    #[test]
    fn parse_link_empty_object() {
        let result = ScyllaLinkService::parse_link("{}");
        assert!(
            result.is_err(),
            "empty JSON object should fail due to missing required fields"
        );
    }

    #[test]
    fn parse_link_with_metadata() {
        let meta = json!({"key": "val", "nested": {"a": 1}});
        let link = make_link(Some(meta.clone()));
        let json_str = serde_json::to_string(&link).expect("serialize");
        let parsed = ScyllaLinkService::parse_link(&json_str).expect("parse_link should succeed");

        assert_eq!(parsed.metadata, Some(meta));
    }

    #[test]
    fn parse_link_with_null_metadata() {
        let link = make_link(None);
        let json_str = serde_json::to_string(&link).expect("serialize");
        let parsed = ScyllaLinkService::parse_link(&json_str).expect("parse_link should succeed");

        assert_eq!(parsed.metadata, None);
        // Verify the rest of the entity survived the roundtrip.
        assert_eq!(parsed.id, link.id);
        assert_eq!(parsed.source_id, link.source_id);
        assert_eq!(parsed.target_id, link.target_id);
    }

    // -----------------------------------------------------------------------
    // Search field-value matching (mirrors the client-side filter in search())
    // -----------------------------------------------------------------------

    #[test]
    fn search_field_value_string_matching() {
        let widget = TestWidget::new("sprocket".into(), "active".into(), 3.5);

        let fv = widget.field_value("name").expect("name field should exist");
        assert_eq!(fv, FieldValue::String("sprocket".to_string()));

        // Simulate the filter predicate from ScyllaDataService::search
        let matches = match &fv {
            FieldValue::String(s) => s == "sprocket",
            _ => false,
        };
        assert!(matches, "FieldValue::String should match the search value");
    }

    #[test]
    fn search_field_value_integer_matching() {
        // FieldValue::Integer comparison uses to_string() in the search filter.
        let fv = FieldValue::Integer(42);
        let matches = match &fv {
            FieldValue::Integer(i) => i.to_string() == "42",
            _ => false,
        };
        assert!(
            matches,
            "FieldValue::Integer(42).to_string() should equal \"42\""
        );

        // Negative case
        let no_match = match &fv {
            FieldValue::Integer(i) => i.to_string() == "99",
            _ => false,
        };
        assert!(!no_match, "FieldValue::Integer(42) should not match \"99\"");
    }

    // -----------------------------------------------------------------------
    // Entity JSON serialization roundtrip
    // -----------------------------------------------------------------------

    #[test]
    fn entity_json_serialization_roundtrip() {
        let widget = TestWidget::new("gear".into(), "active".into(), 7.25);
        let json_str = serde_json::to_string(&widget).expect("serialize should succeed");
        let restored: TestWidget =
            serde_json::from_str(&json_str).expect("deserialize should succeed");

        assert_eq!(restored.id, widget.id);
        assert_eq!(restored.name, "gear");
        assert_eq!(restored.status, "active");
        assert_eq!(restored.entity_type, "test_widget");
        assert!((restored.weight - 7.25).abs() < f64::EPSILON);
    }
}