Crates.io | geekorm |
lib.rs | geekorm |
version | |
source | src |
created_at | 2024-04-10 21:37:17.348307 |
updated_at | 2024-12-08 14:32:35.52126 |
description | A simple and easy to use ORM for SQLite databases |
homepage | |
repository | https://github.com/42ByteLabs/geekorm |
max_upload_size | |
id | 1204096 |
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 |
GeekORM is a simple Object Relation Mapper for empowering your Rust development.
Table
Data
Select
, Create
, Update
, and Insert
queriesforeign_key
: Set the foreign key for a joinrand
: Generate random strings (set lenght, set prefix, set enviroment)hash
or password
: Generate secure Hashes of passwords (set algorithm)You can install the library from crates.io:
cargo add geekorm
cargo install --git https://github.com/42ByteLabs/geekorm
Once you have installed geekorm
, you can start using the derive macros like the following:
use anyhow::Result;
use geekorm::prelude::*;
#[derive(Table, Debug, Default, serde::Serialize, serde::Deserialize)]
struct Users {
#[geekorm(primary_key, auto_increment)]
id: PrimaryKeyInteger,
/// Unique username field
#[geekorm(unique)]
username: String,
/// Password field with automatic hashing
#[geekorm(hash)]
password: String,
/// User Type Enum (defaults to `User`)
#[geekorm(new = "UserType::User")]
user_type: UserType,
/// Optional postcode field (nullable in the database)
postcode: Option<String>,
/// Randomly generated session token
#[geekorm(rand = "42", rand_prefix = "session_")]
session: String,
/// Created and Updated timestamps
#[geekorm(new = "chrono::Utc::now()")]
created_at: chrono::DateTime<chrono::Utc>,
#[geekorm(new = "chrono::Utc::now()", on_update = "chrono::Utc::now()")]
updated_at: chrono::DateTime<chrono::Utc>,
}
#[derive(Data, Debug, Default, Clone)]
enum UserType {
Admin,
Modirator,
#[default]
User,
}
#[tokio::main]
async fn main() -> Result<()> {
// Setup the database and connection
let db = libsql::Builder::new_local(":memory:").build().await
.expect("Failed to create database");
let connection = db.connect()
.expect("Failed to connect to database");
// Create the table in the database
Users::create_table(&connection).await?;
// Use the generated `new` function to create a new User
// using the default values set in the struct.
let mut user = Users::new("GeekMasher", "ThisIsNotMyPassword");
// Saving the new User in the database
user.save(&connection).await?;
// Print the Primary Key value set by the database (auto_increment)
println!("User ID: {:?}", user.id);
// Updating the Users postcode (optional field)
user.postcode = Some("SW1A 1AA".to_string());
user.update(&connection).await?;
// Fetch the Admin Users
let admin_users = Users::fetch_by_user_type(&connection, UserType::Admin).await?;
println!("Admin Users: {:?}", admin_users);
// Counts the number of Users in the database
let total_users = Users::total(&connection).await?;
// Enums are used to help columns with a limited set of values
// and GeekORM will handle the conversion for you.
user.user_type = UserType::Admin;
// or you can use the `.from()` or `.into()` functions
user.user_type = UserType::from("Admin");
user.user_type = "Admin".into();
// GeekORM offers a number of helper functions to make your life easier.
// Search unique fields or search tagged fields
let search = Users::search(&connection, "GeekMasher").await?;
// Automatically hashing passwords for you.
user.hash_password("ThisIsStillNotMyPassword")?;
// Automatically generate random strings for you.
user.regenerate_session();
// Go back to basics and build your own queries dynamically using
// the QueryBuilder built into GeekORM
let query = Users::query_select()
.where_eq("username", "GeekMasher")
.order_by("id", geekorm::QueryOrder::Desc)
.limit(1)
.build()?;
// Execute the query and return the results
let users = Users::query(&connection, query).await?;
println!("Users: {:?}", users);
Ok(())
}
There are a number of opt-in features supported by GeekORM.
Features can be added either using cargo add geekorm -F all
or added them directly in your Cargo.toml
file.
all
: Enable all the major stable featuresnew
: Generate Table::new(...)
functionshelpers
: Generate a number of helper functions
Table::select_by_primary_key()
Table::select_by_{field}()
rand
: Support Generating random stringshash
: Support Generating password hasheslibsql
: Add LibSQL backend supportrusqlite
: Add Rusqlite backend supportMathew Payne 💻 👀 |
Cale 🎨 |
Please create GitHub Issues if there are bugs or feature requests.
This project uses Semantic Versioning (v2) and with major releases, breaking changes will occur.
This project is licensed under the terms of the MIT open source license. Please refer to MIT for the full terms.