#sqlite #sql #orm

macro geekorm-derive

GeekORM Derive Macros Library

52 releases (10 breaking)

Uses new Rust 2024

0.11.4 Mar 16, 2025
0.10.2 Mar 4, 2025
0.8.3 Dec 20, 2024
0.7.2 Nov 26, 2024
0.5.2 Jul 19, 2024

#296 in #orm

Download history 15/week @ 2024-12-25 113/week @ 2025-01-01 206/week @ 2025-01-08 244/week @ 2025-01-15 23/week @ 2025-01-22 10/week @ 2025-01-29 30/week @ 2025-02-05 246/week @ 2025-02-12 77/week @ 2025-02-19 113/week @ 2025-02-26 186/week @ 2025-03-05 577/week @ 2025-03-12 33/week @ 2025-03-19 28/week @ 2025-03-26 11/week @ 2025-04-02 66/week @ 2025-04-09

168 downloads per month
Used in 5 crates (via geekorm)

MIT license

330KB
7.5K SLoC

GeekORM Derive

The geekorm_derive crate is for all pre-processing derive macros used by geekorm at build time.

Default Features

Generate Query Methods

By default, the following methods are generated for the struct:

  • create(): Create Query
  • select(): Select Query
  • all(): Select all rows in a table
  • insert(): Insert Query
  • update(): Update Query
  • count(): Count the number of rows

These 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);

Feature - Automatic New Struct Function

When the new feature is enabled, the following methods are generated for the struct:

  • PrimaryKey<T> fields are not generated
  • Option<T> fields are not generated
use 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")
);

Feature - Generated Helper Methods

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 = ?;"));

Feature - Generate Random Data for Column

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 value
  • rand_length: Sets the length of the randomly generated string
    • Default: 32
  • rand_prefix: Sets a prefix to the randomly generated string
    • Default: None

Feature - Generate Hashs for storing passwords

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 value
  • hash_algorithm: Set the algorithm to use
    • Default: Pbkdf2

Dependencies

~2–12MB
~140K SLoC