Skip to content

Commit

Permalink
Merge pull request #64 from 42ByteLabs/update-docs
Browse files Browse the repository at this point in the history
docs: Update docs
  • Loading branch information
GeekMasher committed May 15, 2024
2 parents 4ec3837 + 44369f2 commit 69d03fc
Show file tree
Hide file tree
Showing 10 changed files with 217 additions and 143 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ resolver = "2"
members = [".", "geekorm-core", "geekorm-derive", "geekorm-cli"]

[workspace.package]
version = "0.2.7"
version = "0.2.8"
license = "MIT"
edition = "2021"
rust-version = "1.74"
Expand Down
10 changes: 10 additions & 0 deletions geekorm-core/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# GeekORM Core

GeekORM is a simple Object Relation Mapper for empowering your Rust development.

## Backends

Currently there is the following backends supported by GeekORM:

- [`libsql`](https://github.com/tursodatabase/libsql)

2 changes: 2 additions & 0 deletions geekorm-core/src/backends/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//! Backend Module for GeekORM

use std::collections::HashMap;

use crate::{Query, QueryBuilderTrait, TableBuilder, Value};
Expand Down
2 changes: 2 additions & 0 deletions geekorm-core/src/builder/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//! Query builder module

/// Column builder module
pub mod columns;
/// Column types module
Expand Down
2 changes: 1 addition & 1 deletion geekorm-core/src/builder/table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::{Columns, QueryBuilder, ToSqlite, Values};
use serde::{Deserialize, Serialize};
use std::fmt::Display;

/// The Table struct for creating tables
/// The Table struct for defining a table
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct Table {
/// Name of the table
Expand Down
2 changes: 2 additions & 0 deletions geekorm-core/src/error.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//! Error Module for GeekORM

/// Error type for the crate
#[derive(Debug, thiserror::Error, Clone)]
pub enum Error {
Expand Down
17 changes: 9 additions & 8 deletions geekorm-core/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
//! GeekORM is a simple ORM for SQLite databases.
#![forbid(unsafe_code)]
#![allow(dead_code)]
#![forbid(unsafe_code)]
#![deny(missing_docs)]
#![doc = include_str!("../README.md")]
#![doc(
html_logo_url = "https://raw.githubusercontent.com/42ByteLabs/geekorm/main/assets/geekorm.png"
)]

/// Backend module
pub mod backends;
/// Builder module
pub mod builder;
/// Error module
pub mod error;
/// Query module
pub mod queries;
/// Utils module
pub mod utils;

#[cfg(feature = "libsql")]
Expand All @@ -27,7 +25,10 @@ pub use crate::builder::table::Table;
pub use crate::builder::values::{Value, Values};
pub use crate::queries::{Query, QueryBuilder};

/// Trait for creating tables
/// Trait for basic creation of tables
///
/// This trait is used to define the table structure for the database.
/// It is used to define the table name and the columns in the table.
pub trait TableBuilder
where
Self: Sized,
Expand Down
10 changes: 10 additions & 0 deletions geekorm-core/src/utils/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
//! The Utility module
//!
//! This is where all the utility functions are defined for the GeekORM crate.
//! There are several utility functions that are used in the crate.
//!
//! - Cryptography
//! - `generate_random_string` - Generate a random string
//! - `generate_hash` - Generate a hash
//!

/// Cryptography module
pub mod crypto;

Expand Down
176 changes: 176 additions & 0 deletions geekorm-derive/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,176 @@
# 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.

```rust
use geekorm::{GeekTable, PrimaryKeyInteger};
use geekorm::prelude::*;

#[derive(GeekTable, Default)]
struct Users {
id: PrimaryKeyInteger,
name: String,
age: i32,
occupation: String,
}

// Create a new table query
let create = Users::create().build()
.expect("Failed to build CREATE TABLE query");

// Select data from the table
let select = Users::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::insert(&user);

// Update query
user.name = String::from("42ByteLabs");
let update = Users::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

```rust
use geekorm::{GeekTable, PrimaryKeyInteger};
use geekorm::prelude::*;

#[derive(GeekTable)]
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.

```rust
use geekorm::{GeekTable, PrimaryKeyInteger};
use geekorm::prelude::*;

#[derive(GeekTable)]
struct Users {
id: PrimaryKeyInteger,
name: String,
age: i32,
occupation: String,
}

// Select by column helper function
let user = Users::select_by_name("geekmasher");
# assert_eq!(user.query, String::from("SELECT id, name, age, occupation FROM Users WHERE name = ?;"));
let user = Users::select_by_age(42);
# assert_eq!(user.query, String::from("SELECT id, name, age, occupation FROM Users WHERE age = ?;"));
let user = Users::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

```rust
use geekorm::prelude::*;
use geekorm::{GeekTable, PrimaryKeyInteger};

#[derive(GeekTable, Debug)]
pub struct Users {
id: PrimaryKeyInteger,
name: String,
#[geekorm(rand, rand_length = 42, rand_prefix = "token")]
token: String
}

let user = Users::new(String::from("geekmasher"));
println!("{}", user.token);
# assert_eq!(user.token.len(), 48);
```

**`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.

```rust
use geekorm::prelude::*;
use geekorm::{GeekTable, PrimaryKeyInteger};

#[derive(GeekTable, Debug)]
pub struct Users {
id: PrimaryKeyInteger,
username: String,

#[geekorm(hash, hash_algorithm = "Pbkdf2")]
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`

0 comments on commit 69d03fc

Please sign in to comment.