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
//! [![CI](https://github.com/polymny/ergol/workflows/build/badge.svg?branch=master&event=push)](https://github.com/polymny/ergol/actions?query=workflow%3Abuild) //! //! This crate provides the `#[ergol]` macro. It allows to persist the data in a //! database. For example, you just have to write //! //! ```rust //! use ergol::prelude::*; //! //! #[ergol] //! pub struct User { //! #[id] pub id: i32, //! #[unique] pub username: String, //! pub password: String, //! pub age: Option<i32>, //! } //! ``` //! //! and the `#[ergol]` macro will generate most of the code you will need. You'll //! then be able to run code like the following: //! //! ```rust //! # use ergol::prelude::*; //! # #[ergol] //! # pub struct User { //! # #[id] pub id: i32, //! # #[unique] pub username: String, //! # pub password: String, //! # pub age: Option<i32>, //! # } //! # use ergol::tokio; //! # #[tokio::main] //! # async fn main() -> Result<(), ergol::tokio_postgres::Error> { //! # let (client, connection) = ergol::tokio_postgres::connect( //! # "host=localhost user=orm password=orm dbname=orm", //! # ergol::tokio_postgres::NoTls, //! # ) //! # .await?; //! # tokio::spawn(async move { //! # if let Err(e) = connection.await { //! # eprintln!("connection error: {}", e); //! # } //! # }); //! // Drop the user table if it exists //! User::drop_table().execute(&client).await.ok(); //! //! // Create the user table //! User::create_table().execute(&client).await?; //! //! // Create a user and save it into the database //! let mut user: User = User::create("thomas", "pa$$w0rd", Some(28)).save(&client).await?; //! //! // Change some of its fields //! *user.age.as_mut().unwrap() += 1; //! //! // Update the user in the database //! user.save(&client).await?; //! //! // Fetch a user by its username thanks to the unique attribute //! let user: Option<User> = User::get_by_username("thomas", &client).await?; //! //! // Select all users //! let users: Vec<User> = User::select().execute(&client).await?; //! # Ok(()) //! # } //! ``` //! //! # Many-to-one and one-to-one relationships //! //! Let's say you want a user to be able to have projects. You can use the //! `#[many_to_one]` attribute in order to do so. Just add: //! //! ```rust //! # use ergol::prelude::*; //! # #[ergol] //! # pub struct User { //! # #[id] pub id: i32, //! # #[unique] pub username: String, //! # pub password: String, //! # pub age: Option<i32>, //! # } //! #[ergol] //! pub struct Project { //! #[id] pub id: i32, //! pub name: String, //! #[many_to_one(projects)] pub owner: User, //! } //! ``` //! //! Once you have defined this struct, many more functions become available: //! //! ```rust //! # use ergol::prelude::*; //! # #[ergol] //! # pub struct User { //! # #[id] pub id: i32, //! # #[unique] pub username: String, //! # pub password: String, //! # pub age: Option<i32>, //! # } //! # #[ergol] //! # pub struct Project { //! # #[id] pub id: i32, //! # pub name: String, //! # #[many_to_one(projects)] pub owner: User, //! # } //! # use ergol::tokio; //! # #[tokio::main] //! # async fn main() -> Result<(), ergol::tokio_postgres::Error> { //! # let (client, connection) = ergol::tokio_postgres::connect( //! # "host=localhost user=orm password=orm dbname=orm", //! # ergol::tokio_postgres::NoTls, //! # ) //! # .await?; //! # tokio::spawn(async move { //! # if let Err(e) = connection.await { //! # eprintln!("connection error: {}", e); //! # } //! # }); //! // Drop the user table if it exists //! Project::drop_table().execute(&client).await.ok(); //! User::drop_table().execute(&client).await.ok(); //! //! // Create the user table //! User::create_table().execute(&client).await?; //! Project::create_table().execute(&client).await?; //! //! // Create two users and save them into the database //! let thomas: User = User::create("thomas", "pa$$w0rd", 28).save(&client).await?; //! User::create("nicolas", "pa$$w0rd", 28).save(&client).await?; //! //! // Create some projects for the user //! let project: Project = Project::create("My first project", &thomas).save(&client).await?; //! Project::create("My second project", &thomas).save(&client).await?; //! //! // You can easily find all projects from the user //! let projects: Vec<Project> = thomas.projects(&client).await?; //! //! // You can also find the owner of a project //! let owner: User = projects[0].owner(&client).await?; //! # Ok(()) //! # } //! ``` //! //! You can similarly have one-to-one relationship between a user and a project by //! using the `#[one_to_one]` attribute: //! //! ```rust //! # use ergol::prelude::*; //! # #[ergol] //! # pub struct User { //! # #[id] pub id: i32, //! # #[unique] pub username: String, //! # pub password: String, //! # } //! #[ergol] //! pub struct Project { //! #[id] pub id: i32, //! pub name: String, //! #[one_to_one(project)] pub owner: User, //! } //! ``` //! //! This will add the `UNIQUE` attribute in the database and make the `project` //! method only return an option: //! //! ```rust //! # use ergol::prelude::*; //! # #[ergol] //! # pub struct User { //! # #[id] pub id: i32, //! # #[unique] pub username: String, //! # pub password: String, //! # pub age: Option<i32>, //! # } //! # #[ergol] //! # pub struct Project { //! # #[id] pub id: i32, //! # pub name: String, //! # #[one_to_one(project)] pub owner: User, //! # } //! # use ergol::tokio; //! # #[tokio::main] //! # async fn main() -> Result<(), ergol::tokio_postgres::Error> { //! # let (client, connection) = ergol::tokio_postgres::connect( //! # "host=localhost user=orm password=orm dbname=orm", //! # ergol::tokio_postgres::NoTls, //! # ) //! # .await?; //! # tokio::spawn(async move { //! # if let Err(e) = connection.await { //! # eprintln!("connection error: {}", e); //! # } //! # }); //! # Project::drop_table().execute(&client).await.ok(); //! # User::drop_table().execute(&client).await.ok(); //! # User::create_table().execute(&client).await?; //! # Project::create_table().execute(&client).await?; //! # let thomas: User = User::create("thomas", "pa$$w0rd", 28).save(&client).await?; //! // You can easily find a user's project //! let project: Option<Project> = thomas.project(&client).await?; //! # Ok(()) //! # } //! ``` //! //! Note that that way, a project has exactly one owner, but a user can have no //! project. //! //! # Many-to-many relationships //! //! This macro also supports many-to-many relationships. In order to do so, you //! need to use the `#[many_to_many]` attribute: //! //! ```rust //! # use ergol::prelude::*; //! # #[ergol] //! # pub struct User { //! # #[id] pub id: i32, //! # #[unique] pub username: String, //! # pub password: String, //! # } //! #[ergol] //! pub struct Project { //! #[id] pub id: i32, //! pub name: String, //! #[many_to_many(visible_projects)] pub authorized_users: User, //! } //! ``` //! //! The same way, you will have plenty of functions that you will be able to use to //! manage your objects: //! //! ```rust //! # use ergol::prelude::*; //! # #[ergol] //! # pub struct User { //! # #[id] pub id: i32, //! # #[unique] pub username: String, //! # pub password: String, //! # pub age: i32, //! # } //! # #[ergol] //! # pub struct Project { //! # #[id] pub id: i32, //! # pub name: String, //! # #[many_to_many(visible_projects)] pub authorized_users: User, //! # } //! # #[tokio::main] //! # async fn main() -> Result<(), ergol::tokio_postgres::Error> { //! # let (client, connection) = ergol::tokio_postgres::connect( //! # "host=localhost user=orm password=orm dbname=orm", //! # ergol::tokio_postgres::NoTls, //! # ) //! # .await?; //! # tokio::spawn(async move { //! # if let Err(e) = connection.await { //! # eprintln!("connection error: {}", e); //! # } //! # }); //! # Project::drop_table().execute(&client).await.ok(); //! # User::drop_table().execute(&client).await.ok(); //! # User::create_table().execute(&client).await?; //! # Project::create_table().execute(&client).await?; //! # User::create("thomas", "pa$$w0rd", 28).save(&client).await?; //! # User::create("nicolas", "pa$$w0rd", 28).save(&client).await?; //! // Find some users in the database //! let thomas = User::get_by_username("thomas", &client).await?.unwrap(); //! let nicolas = User::get_by_username("nicolas", &client).await?.unwrap(); //! //! // Create a project //! let first_project = Project::create("My first project").save(&client).await?; //! //! // Thomas can access this project //! first_project.add_authorized_user(&thomas, &client).await?; //! //! // The other way round //! nicolas.add_visible_project(&first_project, &client).await?; //! //! // The second project can only be used by thomas //! let second_project = Project::create("My second project").save(&client).await?; //! thomas.add_visible_project(&second_project, &client).await?; //! //! // The third project can only be used by nicolas. //! let third_project = Project::create("My third project").save(&client).await?; //! third_project.add_authorized_user(&nicolas, &client).await?; //! //! // You can easily retrieve all projects available for a certain user //! let projects: Vec<Project> = thomas.visible_projects(&client).await?; //! //! // And you can easily retrieve all users that have access to a certain project //! let users: Vec<User> = first_project.authorized_users(&client).await?; //! //! // You can easily remove a user from a project //! let _: bool = first_project.remove_authorized_user(&thomas, &client).await?; //! //! // Or vice-versa //! let _: bool = nicolas.remove_visible_project(&first_project, &client).await?; //! //! // The remove functions return true if they successfully removed something. //! # Ok(()) //! # } //! ``` //! //! # Limitations //! //! For the moment, we still have plenty of limitations: //! //! - this crate only works with tokio-postgres //! - there is no support for migrations //! - the names of the structs you use in `#[ergol]` must be used previously, e.g. //! ```rust,ignore //! mod user { //! use ergol::prelude::*; //! //! #[ergol] //! pub struct User { //! #[id] pub id: i32, //! } //! } //! //! use ergol::prelude::*; //! #[ergol] //! pub struct Project { //! #[id] pub id: i32, //! #[many_to_one(projects)] pub owner: user::User, // this will not work //! } //! ``` //! //! ```rust //! mod user { //! use ergol::prelude::*; //! #[ergol] //! pub struct User { //! #[id] pub id: i32, //! } //! } //! use user::User; //! //! use ergol::prelude::*; //! #[ergol] //! pub struct Project { //! #[id] pub id: i32, //! #[many_to_one(projects)] pub owner: User, // this will work //! } //! ``` pub mod pg; pub mod query; pub mod relation; use crate::query::{CreateTable, DropTable, Select}; /// Any type that should be transformed into a table should implement this trait. /// /// You should not implement this trait yourself, and use the #[ergol] macro to implement this /// trait for your structs. #[async_trait::async_trait] pub trait ToTable: Send + std::fmt::Debug { /// Converts a row of a table into an object. fn from_row(row: tokio_postgres::Row) -> Self; /// Returns the name of the table corresponding to Self. fn table_name() -> &'static str; /// Returns the name of the primary key of the table corresponding to Self. fn id_name() -> &'static str; /// Returns the id of self. fn id(&self) -> i32; /// Returns the query that creates the table. fn create_table() -> CreateTable; /// Returns the query that drops the table. fn drop_table() -> DropTable; /// Returns a select query. fn select() -> Select<Self>; } pub use async_trait; pub use bytes; pub use tokio; pub use tokio_postgres; pub use ergol_proc_macro::ergol; /// Any enum that has no field on any variant can derive `PgEnum` in order to be usable in a /// `#[ergol]` struct. /// /// # Note: /// Any enum needs to derive Debug in order to derive PgEnum, since deriving Debug is /// required in order to implement ToSql. /// /// ``` /// # use ergol::prelude::*; /// #[ergol] /// pub struct Struct { /// #[id] pub id: i32, /// pub ok: IsOk, /// } /// /// #[derive(PgEnum, Debug)] /// pub enum IsOk { /// IAmOk, /// IAmNotOk, /// } /// ``` pub use ergol_proc_macro::PgEnum; /// The prelude contains the macros and usefull traits. pub mod prelude { pub use crate::ergol; pub use crate::pg::Pg; pub use crate::query::Query; pub use crate::PgEnum; pub use crate::ToTable; }