Crates.io | geekorm-derive |
lib.rs | geekorm-derive |
version | |
source | src |
created_at | 2024-04-10 21:36:33.240613 |
updated_at | 2024-11-19 20:38:44.790389 |
description | GeekORM Derive Macros Library |
homepage | |
repository | https://github.com/42ByteLabs/geekorm |
max_upload_size | |
id | 1204093 |
Cargo.toml error: | TOML parse error at line 19, column 1 | 19 | autolib = false | ^^^^^^^ unknown field `autolib`, expected one of `name`, `version`, `edition`, `authors`, `description`, `readme`, `license`, `repository`, `homepage`, `documentation`, `build`, `resolver`, `links`, `default-run`, `default_dash_run`, `rust-version`, `rust_dash_version`, `rust_version`, `license-file`, `license_dash_file`, `license_file`, `licenseFile`, `license_capital_file`, `forced-target`, `forced_dash_target`, `autobins`, `autotests`, `autoexamples`, `autobenches`, `publish`, `metadata`, `keywords`, `categories`, `exclude`, `include` |
size | 0 |
The geekorm_derive
crate is for all pre-processing derive macros used by geekorm
at build time.
By default, the following methods are generated for the struct:
create()
: Create Queryselect()
: Select Queryall()
: Select all rows in a tableinsert()
: Insert Queryupdate()
: Update Querycount()
: Count the number of rowsThese are all defined by the geekorm_core::QueryBuilderTrait
trait.
use geekorm::prelude::*;
#[derive(Table, Default, serde::Serialize, serde::Deserialize)]
struct Users {
id: PrimaryKeyInteger,
name: String,
age: i32,
occupation: String,
}
// Create a new table query
let create = Users::query_create().build()
.expect("Failed to build CREATE TABLE query");
// Select data from the table
let select = Users::query_select()
.where_eq("name", "geekmasher")
.build()
.expect("Failed to build SELECT query");
// Create a default User
let mut user = Users::default();
// Insert data
let insert = Users::query_insert(&user);
// Update query
user.name = String::from("42ByteLabs");
let update = Users::query_update(&user);
When the new
feature is enabled, the following methods are generated for the struct:
PrimaryKey<T>
fields are not generatedOption<T>
fields are not generateduse geekorm::prelude::*;
#[derive(Table, Default, serde::Serialize, serde::Deserialize)]
struct Users {
id: PrimaryKeyInteger,
name: String,
age: i32,
occupation: String,
country: Option<String>,
}
let user = Users::new(
String::from("geekmasher"),
42,
String::from("Software Developer")
);
When the helpers
feature is enabled, the following helper methods are generated for the struct:
Note: This is a very experimental feature and might change in the future.
use geekorm::prelude::*;
#[derive(Table, Default, serde::Serialize, serde::Deserialize)]
struct Users {
id: PrimaryKeyInteger,
name: String,
age: i32,
occupation: String,
}
// Select by column helper function
let user = Users::query_select_by_name("geekmasher");
# assert_eq!(user.query, String::from("SELECT id, name, age, occupation FROM Users WHERE name = ?;"));
let user = Users::query_select_by_age(42);
# assert_eq!(user.query, String::from("SELECT id, name, age, occupation FROM Users WHERE age = ?;"));
let user = Users::query_select_by_occupation("Software Developer");
# assert_eq!(user.query, String::from("SELECT id, name, age, occupation FROM Users WHERE occupation = ?;"));
When using the rand
feature, you can automatically generate random strings and use
# #[cfg(feature = "rand")]
# {
use geekorm::prelude::*;
#[derive(Table, Default, serde::Serialize, serde::Deserialize)]
pub struct Users {
id: PrimaryKeyInteger,
name: String,
#[geekorm(rand, rand_length = 42, rand_prefix = "token")]
token: String
}
let mut user = Users::new(String::from("geekmasher"));
println!("{}", user.token);
# assert_eq!(user.token.len(), 48);
# let old_token = user.token.clone();
user.regenerate_token();
# assert_ne!(old_token, user.token);
# }
rand
attributes:
rand
: Sets the String field as a randomly generated valuerand_length
: Sets the length of the randomly generated string
32
rand_prefix
: Sets a prefix to the randomly generated string
When using the hash
feature, you can automatically hash passwords to make sure they are stored securely.
# #[cfg(feature = "hash-sha512")]
# {
use geekorm::prelude::*;
#[derive(Table, Default, serde::Serialize, serde::Deserialize)]
pub struct Users {
id: PrimaryKeyInteger,
username: String,
#[geekorm(hash)]
# // This config below is not the most secure algorithm, always use default ;)
# #[geekorm(hash_algorithm = "Sha512")]
password: String,
}
# fn main() -> Result<(), geekorm::Error> {
let mut user = Users::new(String::from("geekmasher"), String::from("password"));
# assert_eq!(user.password.len(), 95);
// Update password
user.hash_password("newpassword");
// Verify password
if user.check_password("newpassword")? {
println!("Password is correct");
} else {
println!("Password is incorrect");
}
# Ok(())
# }
# }
hash
attributes:
hash
or password
: Sets the String field as a hashable valuehash_algorithm
: Set the algorithm to use
Pbkdf2