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
//! # Entity Relations
//!
//! Crash ORM provides a convenient API for OneToOne and OneToMany/ManyToOne relations.
//!
//! Right now you have to manually construct cross-reference tables (ManyToMany relations).
//!
//! ## OneToOne
//!
//! Declaring a OneToOne is quite simple:
//!
//! ```rust
//! use crash_orm::prelude::*;
//!
//! #[derive(Entity, Debug, Schema)]
//! pub struct TestItem1 {
//! pub id: u32,
//! pub name1: Option<String>,
//! pub active: bool,
//! pub other: Option<OneToOne<TestItem2, u32>>, // nullable
//! }
//!
//! #[derive(Entity, Debug, Schema)]
//! pub struct TestItem2 {
//! pub id: u32,
//! pub name1: Option<String>,
//! pub active: bool,
//! }
//! ```
//!
//! This describes the owning site of the relation, this is where the id of TestItem2 is stored.
//!
//! It will generate a function with the following signature for TestItem1:
//!
//! ```no_build
//! async fn get_other(&self, connection: &impl crash_orm::DatabaseConnection) -> crash_orm::Result<Option<TestItem2>>;
//! ```
//!
//! This function allows you to retrieve the linked entity.
//!
//! **NOTE**: You can still create your own impl block for TestItem1, the function above will be implemented via trait.
//!
//! You might want to be able to retrieve TestItem1 from an instance of TestItem2.
//! If you need this function, you can add the following field to TestItem2:
//!
//! ```no_build
//! #[mapped_by("other")]
//! pub test_item_1: OneToOneRef<TestItem1>,
//! ```
//!
//! This will generate a similar function like above with the name get_test_item_1.
//!
//! **NOTE**: mapped_by must contain the field name of the field which it corresponds to.
//! In this case, we called the field of TestItem1 "other", so we can pass it here.
//! This is **MANDATORY**.
//!
//! ### Full OneToOne Example
//! ```rust
//! use crash_orm::derive::{Entity, Schema};
//! use crash_orm::prelude::{OneToOne, OneToOneRef};
//!
//! #[derive(Entity, Debug, Schema)]
//! pub struct TestItem1 {
//! pub id: u32,
//! pub name1: Option<String>,
//! pub active: bool,
//! pub other: Option<OneToOne<TestItem2, u32>>,
//! }
//!
//! #[derive(Entity, Debug, Schema)]
//! pub struct TestItem2 {
//! pub id: u32,
//! pub name1: Option<String>,
//! pub active: bool,
//! #[mapped_by("other")]
//! pub test_item_1: OneToOneRef<TestItem1, u32>,
//! }
//! ```
//!
//! ## OneToMany/ManyToOne
//!
//! For this type of relation, you must declare, once again, the owning site.
//! In this kind of relation the ManyToOne is the owning site.
//! So let's start declaring a simple ManyToOne relation:
//!
//! ```rust
//! use crash_orm::prelude::*;
//!
//! #[derive(Entity, Debug, Schema)]
//! pub struct TestItem1 {
//! pub id: u32,
//! pub name1: Option<String>,
//! pub active: bool,
//! pub other: Option<ManyToOne<TestItem2, u32>>,
//! }
//!
//! #[derive(Entity, Debug, Schema)]
//! pub struct TestItem2 {
//! pub id: u32,
//! pub name1: Option<String>,
//! pub active: bool,
//! }
//! ```
//!
//! Once again, it will generate a function with the following signature for TestItem1:
//!
//! ```no_build
//! async fn get_other(&self, connection: &impl crash_orm::DatabaseConnection) -> crash_orm::Result<Option<TestItem2>>;
//! ```
//!
//! If you need to retrieve all TestItem1 from a TestItem2, you need to add the following field:
//!
//! ```no_build
//! #[mapped_by("other")]
//! pub test_items_1: OneToMany<TestItem1>,
//! ```
//!
//! This will generate a slightly different function:
//!
//! ```no_build
//! async fn get_test_items_1(&self, connection: &impl crash_orm::DatabaseConnection) -> crash_orm::Result<Vec<TestItem1>>;
//! ```
//!
//! **NOTE**: mapped_by must contain the field name of the field which it corresponds to.
//! In this case, we called the field of TestItem1 "other", so we can pass it here.
//! This is **MANDATORY**.
//!
//! ### Full OneToMany/ManyToOne Example
//! ```rust
//! use crash_orm::derive::{Entity, Schema};
//! use crash_orm::prelude::{ManyToOne, OneToMany};
//!
//! #[derive(Entity, Debug, Schema)]
//! pub struct TestItem1 {
//! pub id: u32,
//! pub name1: Option<String>,
//! pub active: bool,
//! pub other: Option<ManyToOne<TestItem2, u32>>,
//! }
//!
//! #[derive(Entity, Debug, Schema)]
//! pub struct TestItem2 {
//! pub id: u32,
//! pub name1: Option<String>,
//! pub active: bool,
//! #[mapped_by("other")]
//! pub test_items_1: OneToMany<TestItem1, u32>,
//! }
//! ```
pub use *;
pub use *;