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
/// This macro will implement [`LoadFrom`][] for Diesel models. /// /// [`LoadFrom`]: trait.LoadFrom.html /// /// # Example usage /// /// ``` /// #[macro_use] /// extern crate diesel; /// /// use diesel::pg::PgConnection; /// use diesel::prelude::*; /// use juniper_eager_loading::impl_load_from_for_diesel; /// # /// # fn main() {} /// /// table! { /// users (id) { /// id -> Integer, /// } /// } /// /// table! { /// companies (id) { /// id -> Integer, /// } /// } /// /// table! { /// employments (id) { /// id -> Integer, /// user_id -> Integer, /// company_id -> Integer, /// } /// } /// /// #[derive(Queryable)] /// struct User { /// id: i32, /// } /// /// #[derive(Queryable)] /// struct Company { /// id: i32, /// } /// /// #[derive(Queryable)] /// struct Employment { /// id: i32, /// user_id: i32, /// company_id: i32, /// } /// /// impl_load_from_for_diesel! { /// ( /// error = diesel::result::Error, /// connection = PgConnection, /// ) => { /// i32 -> (users, User), /// i32 -> (companies, Company), /// i32 -> (employments, Employment), /// /// User.id -> (employments.user_id, Employment), /// Company.id -> (employments.company_id, Employment), /// /// Employment.company_id -> (companies.id, Company), /// Employment.user_id -> (users.id, User), /// } /// } /// ``` /// /// # Syntax /// /// First you specify your error and connection type with /// /// ```text /// ( /// error = diesel::result::Error, /// connection = PgConnection, /// ) => { /// // ... /// } /// ``` /// /// Then you define each model type you want to implement [`LoadFrom`] for and which columns and /// tables to use. There are two possible syntaxes for different purposes. /// /// ```text /// i32 -> (users, User), /// ``` /// /// The first syntax implements `LoadFrom<i32> for User`, meaning from a `Vec<i32>` we can load a /// `Vec<User>`. It just takes the id type, the table, and the model struct. /// /// ```text /// User.id -> (employments.user_id, Employment), /// ``` /// /// This syntax is required when using [`HasMany`][] and [`HasManyThrough`][]. In this case it /// implements `LoadFrom<User> for Employment`, meaning from a `Vec<User>` we can get /// `Vec<Employment>`. It does this by loading the users, mapping the list to the user ids, /// then finding the employments with those ids. /// /// [`HasMany`]: trait.HasMany.html /// [`HasManyThrough`]: trait.HasManyThrough.html /// /// # What gets generated /// /// The two syntaxes generates code like this: /// /// ``` /// # #[macro_use] /// # extern crate diesel; /// # use diesel::pg::PgConnection; /// # use diesel::prelude::*; /// # use juniper_eager_loading::impl_load_from_for_diesel; /// # fn main() {} /// # table! { /// # users (id) { /// # id -> Integer, /// # } /// # } /// # table! { /// # companies (id) { /// # id -> Integer, /// # } /// # } /// # table! { /// # employments (id) { /// # id -> Integer, /// # user_id -> Integer, /// # company_id -> Integer, /// # } /// # } /// # #[derive(Queryable)] /// # struct User { /// # id: i32, /// # } /// # #[derive(Queryable)] /// # struct Company { /// # id: i32, /// # } /// # #[derive(Queryable)] /// # struct Employment { /// # id: i32, /// # user_id: i32, /// # company_id: i32, /// # } /// // i32 -> (users, User), /// impl juniper_eager_loading::LoadFrom<i32> for User { /// type Error = diesel::result::Error; /// type Connection = PgConnection; /// /// fn load(ids: &[i32], db: &Self::Connection) -> Result<Vec<Self>, Self::Error> { /// use diesel::pg::expression::dsl::any; /// /// users::table /// .filter(users::id.eq(any(ids))) /// .load::<User>(db) /// .map_err(From::from) /// } /// } /// /// // User.id -> (employments.user_id, Employment), /// impl juniper_eager_loading::LoadFrom<User> for Employment { /// type Error = diesel::result::Error; /// type Connection = PgConnection; /// /// fn load(froms: &[User], db: &Self::Connection) -> Result<Vec<Self>, Self::Error> { /// use diesel::pg::expression::dsl::any; /// /// let from_ids = froms.iter().map(|other| other.id).collect::<Vec<_>>(); /// employments::table /// .filter(employments::user_id.eq(any(from_ids))) /// .load(db) /// .map_err(From::from) /// } /// } /// ``` #[macro_export] macro_rules! impl_load_from_for_diesel { ( ( error = $error:path, connection = $connection:path, ) => { $($inner:tt)* } ) => { $crate::__impl_load_from_for_diesel_inner! { error = $error, connection = $connection, $( $inner )* } } } #[doc(hidden)] #[macro_export] macro_rules! __impl_load_from_for_diesel_inner { ( error = $error:path, connection = $connection:path, ) => {}; ( error = $error:path, connection = $connection:path, $id_ty:ident -> ($table:ident, $ty:ident), $( $rest:tt )* ) => { impl juniper_eager_loading::LoadFrom<$id_ty> for $ty { type Error = $error; type Connection = $connection; fn load( ids: &[$id_ty], db: &Self::Connection, ) -> Result<Vec<Self>, Self::Error> { use diesel::pg::expression::dsl::any; $table::table .filter($table::id.eq(any(ids))) .load::<$ty>(db) .map_err(From::from) } } $crate::__impl_load_from_for_diesel_inner! { error = $error, connection = $connection, $($rest)* } }; ( error = $error:path, connection = $connection:path, $join_ty:ident . $join_from:ident -> ($table:ident . $join_to:ident, $ty:ident), $( $rest:tt )* ) => { impl juniper_eager_loading::LoadFrom<$join_ty> for $ty { type Error = $error; type Connection = $connection; fn load( froms: &[$join_ty], db: &Self::Connection, ) -> Result<Vec<Self>, Self::Error> { use diesel::pg::expression::dsl::any; let from_ids = froms.iter().map(|other| other.$join_from).collect::<Vec<_>>(); $table::table .filter($table::$join_to.eq(any(from_ids))) .load(db) .map_err(From::from) } } $crate::__impl_load_from_for_diesel_inner! { error = $error, connection = $connection, $($rest)* } }; }