Skip to main content

toasty_driver_integration_suite/scenarios/
user_comment_article.rs

1use crate::prelude::*;
2
3scenario! {
4    #[derive(Debug, toasty::Model)]
5    struct User {
6        #[key]
7        #[auto]
8        id: uuid::Uuid,
9
10        name: String,
11
12        #[has_many]
13        comments: toasty::Deferred<Vec<Comment>>,
14
15        // User → comments → article
16        #[has_many(via = comments.article)]
17        commented_articles: toasty::Deferred<Vec<Article>>,
18
19        // User → comments → article → title (scalar terminal)
20        #[has_many(via = comments.article.title)]
21        commented_article_titles: toasty::Deferred<Vec<String>>,
22
23        // User → comments → body: a 2-step scalar terminal. The terminal field
24        // sits directly on the first relation's target, so the relation chain is
25        // a single step — the minimal scalar-via walk.
26        #[has_many(via = comments.body)]
27        comment_bodies: toasty::Deferred<Vec<String>>,
28    }
29
30    #[derive(Debug, toasty::Model)]
31    struct Article {
32        #[key]
33        #[auto]
34        id: uuid::Uuid,
35
36        title: String,
37
38        #[has_many]
39        comments: toasty::Deferred<Vec<Comment>>,
40    }
41
42    #[derive(Debug, toasty::Model)]
43    struct Comment {
44        #[key]
45        #[auto]
46        id: uuid::Uuid,
47
48        body: String,
49
50        #[index]
51        user_id: uuid::Uuid,
52
53        #[belongs_to(key = user_id, references = id)]
54        user: toasty::Deferred<User>,
55
56        #[index]
57        article_id: uuid::Uuid,
58
59        #[belongs_to(key = article_id, references = id)]
60        article: toasty::Deferred<Article>,
61    }
62
63    async fn setup(test: &mut Test) -> toasty::Db {
64        test.setup_db(models!(User, Article, Comment)).await
65    }
66}