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
//! # Oxidizer //! A simple orm based on [tokio-postgres](https://crates.io/crates/tokio-postgres) and [refinery](https://crates.io/crates/refinery) //! ```ignore //! #[async_trait] //! pub trait Entity: Sized { //! async fn save(&mut self, db: &DB) -> DBResult<bool>; //! async fn delete(&mut self, db: &DB) -> DBResult<bool>; //! //! fn from_row(row: &Row) -> Self; //! fn create_migration() -> DBResult<Migration>; //! fn get_table_name() -> String; //! //! async fn find(db: &DB, query: &str, params: &'_ [&'_ (dyn ToSql + Sync)]) -> DBResult<Vec<Self>>; //! async fn first(db: &DB, query: &str, params: &'_ [&'_ (dyn ToSql + Sync)]) -> DBResult<Option<Self>>; //! } //! ``` //! ``` //! use oxidizer::*; //! use chrono::{DateTime, Utc}; //! //! #[derive(Entity)] //! #[derive(Default)] //! pub struct MyEntity { //! #[primary_key] //! id: i32, //! //! name: String, //! //! #[indexed] //! integer: i32, //! integer64: i64, //! //! float: f32, //! double: f64, //! //! boolean: bool, //! //! datetime: Option<DateTime<Utc>>, //! } //! //! #[tokio::test] //! async fn test_my_entity() { //! let uri = "postgres://postgres:alkje2lkaj2e@db/postgres"; //! let max_open = 50; // mobc //! let ca_file: Option<&str> = None; //! let db = DB::connect(&uri, max_open, ca_file).await.unwrap(); //! //! db.migrate_tables(&[MyEntity::create_migration().unwrap()]).await.unwrap(); //! //! let mut entity = MyEntity::default(); //! let creating = entity.save(&db).await.unwrap(); //! assert_eq!(creating, true); //! } //! //! ``` //! //! //! ## Attributes //! //! Derive attributes can be used to create indexes, change the default table name and //! create reverse relation accessors //! //! ### #[primary_key] //! Required //! Field attribute used to mark the field as the primary key, this will make the field autoincrement //! //! ``` //! use oxidizer::*; //! #[derive(Entity)] //! struct Entity { //! #[primary_key] //! id: i32 //! } //! ``` //! //! ### #[indexed] //! Make the specified field indexed in the db //! //! ``` //! use oxidizer::*; //! #[derive(Entity)] //! struct Entity { //! #[primary_key] //! id: i32, //! #[indexed] //! name: String, //! } //! ``` //! //! ### #[relation] //! See [Relations](#Relations) //! //! ### #[has_many] //! See [Relations](#Relations) //! //! ### #[entity] //! General settings for the entity struct //! //! #### table_name: String; //! Allows one to change the table name of the entity //! //! ``` //! use oxidizer::*; //! #[derive(Entity)] //! #[entity(table_name="custom_table_name")] //! struct Entity { //! #[primary_key] //! id: i32 //! } //! ``` //! //! ### #[index] //! Creates a custom index/constraint on one or more column //! //! ``` //! use oxidizer::*; //! #[derive(Default, Entity)] //! #[index(name="myindex", columns="name, email", unique)] //! struct MyEntity { //! #[primary_key] //! id: i32, //! //! name: String, //! email: String, //! } //! ``` //! //! ### #[field_ignore] //! Ignores the specified field. The field type must implement the `Default` trait. //! //! ``` //! use oxidizer::*; //! #[derive(Default, Entity)] //! struct MyEntity { //! #[primary_key] //! id: i32, //! //! name: String, //! #[field_ignore] //! email: String, //! } //! ``` //! //! ### #[custom_type] //! The custom type attribute lets you override the default type provided by oxidizer. //! //! ``` //! use oxidizer::*; //! pub enum MyEnum { //! Item1, //! Item2, //! } //! //! impl std::convert::From<&MyEnum> for i32 { //! fn from(v: &MyEnum) -> Self { //! match v { //! MyEnum::Item1 => 0, //! MyEnum::Item2 => 1, //! } //! } //! } //! //! impl std::convert::From<i32> for MyEnum { //! fn from(v: i32) -> Self { //! match v { //! 0 => MyEnum::Item1, //! 1 => MyEnum::Item2, //! _ => unimplemented!(), //! } //! } //! } //! //! #[derive(Entity)] //! pub struct TestCustomType { //! #[primary_key] //! id: i32, //! //! #[custom_type(ty = "i32")] //! my_enum: MyEnum, //! } //! ``` //! The custom type requires you to explicity implement the related `From` functions to convert between the actual type and the overriden type //! //! //! ## Relations //! //! ### #[relation] //! Relations can be created using the `relation` attribute as in the example: //! ``` //! use oxidizer::*; //! #[derive(Entity)] //! struct Entity { //! #[primary_key] //! id: i32, //! } //! //! #[derive(Entity)] //! struct TestRelation { //! #[primary_key] //! id: i32, //! device_id: String, //! //! #[relation(model="Entity", key="id")] //! entity_id: i32, //! } //! ``` //! //! This will implement for `TestRelation` the following generated trait: //! ```ignore //! #[oxidizer::async_trait] //! pub trait __AccessorTestRelationToEntity { //! async fn get_test_entity(&self, db: &oxidizer::db::DB) -> oxidizer::db::DBResult<Entity>; //! async fn set_test_entity(&mut self, db: &oxidizer::db::DB, v: &Entity) -> oxidizer::db::DBResult<()>; //! } //! ``` //! //! #[has_many] //! 1-to-many or many-to-many relations can be achieved using the `has_many` attribute //! //! ### basic (1-to-many) //! //! ``` //! use oxidizer::*; //! //! #[derive(Entity)] //! #[derive(Default)] //! #[has_many(model="TargetEntity", field="entity_id")] //! pub struct Entity { //! #[primary_key] //! id: i32, //! name: String //! } //! //! #[derive(Default, Entity)] //! pub struct TargetEntity { //! #[primary_key] //! id: i32, //! #[relation(model="Entity", key="id")] //! entity_id: i32 //! } //! ``` //! This will create helper functions to access all the `TargetEntity` that Entity has. //! This is what the generated trait and implementation looks like (implementaion is also generated). //! //! ```ignore //! #[oxidizer::async_trait] //! pub trait __AccessorHasManyTargetEntityToEntity { //! async fn get_all_test_entity(&self, db: &oxidizer::db::DB) -> oxidizer::db::DBResult<Vec<Entity>>; //! } //! ``` //! //! ### With a through table (many-to-many) //! ``` //! use oxidizer::*; //! //! #[derive(Entity)] //! #[derive(Default)] //! pub struct Entity { //! #[primary_key] //! id: i32, //! name: String //! } //! //! #[derive(Default, Entity)] //! #[has_many(model="Entity", field="entity_id", through="TestManyToMany")] //! pub struct TargetEntity { //! #[primary_key] //! id: i32, //! } //! //! #[derive(Default, Entity)] //! pub struct TestManyToMany { //! #[primary_key] //! id: i32, //! //! #[relation(model="TargetEntity", key="id")] //! target_id: i32, //! //! #[relation(model="Entity", key="id")] //! entity_id: i32, //! } //! ``` //! This will create helper functions to access the related entities. This is what the generated trait looks like (implementaion is also generated): //! ```ignore //! #[oxidizer::async_trait] //! pub trait __AccessorHasManyTargetEntityToEntity { //! async fn get_all_test_entity(&self, db: &oxidizer::db::DB) -> oxidizer::db::DBResult<Vec<TestManyToMany>>; //! } //! ``` //! //! pub mod db; pub use db::*; pub mod entity; pub use entity::*; pub mod migration; /// Re-export of [async_trait::async_trait](https://crates.io/crates/async-trait) pub use async_trait::async_trait; pub use tokio_postgres; pub use tokio_postgres::types as db_types; pub use barrel::types; pub use oxidizer_entity_macro::*; #[cfg(test)] mod tests_macro; #[cfg(test)] mod migrations;