[][src]Crate dino

Dino is a lightweight database for rust! It makes writing databases with types more easy. Normally if you use a File Storage database then you will have to parse the types by yourself

Dino uses json ie. You will have all of the basic types like bool, usize, str, etc... and the special one is Tree Dino is special about trees. Because some of the databases allow you to open trees but you cannot open sub trees there. You can P easily open a sub tree. Here are some of the examples that show how to use trees and how to use the database without sub trees ;)

Basic Database

Note: In this example we are not making a sub tree as everything goes into the main tree. All of the sub trees also go in the main tree. We can directly insert values into the main tree too

// Create the database instance
let mut db = Database::new("./basic.dino");

// Load and create the database if does not exist
db.load();

// Insert values in the db in the format of key, value
db.insert("key-1", "value-1");
db.insert("key-2", "value-2");

Sub Trees

Note: Here we are making a sub tree! Now in this case making a sub tree is essential as there is a id key with some values

 
// Create the database instance
let mut db = Database::new("./sub_trees.dino");
 
// Load and create the database if does not exist
db.load();
 
// Create a new sub Tree in the main Tree of the db
let mut data_tree = Tree::new();
 
// Insert the key and value in the sub tree
data_tree.insert("a", "b");
 
// Insert the [data_tree] under the main tree
db.insert_tree("id", data_tree);

Querying the Database

// Create the database instance
let mut db = Database::new("./basic.dino");
 
// Load and create the database if does not exist
db.load();

// Insert values in the db in the format of key, value
db.insert("key-1", "value-1");
 
// Print the value of `key-1`
println!("The value of key: id is {}", db.find("key-1").unwrap());
 
match db.find("not_exists") {
    Ok(_value) => {
        println!("This is unfortunate :(")
    }

    Err(error) => {
        println!("Everting works! Here is the error for reference: {}", error)
    }
}
 

Basic Operations

// Create the database instance
let mut db = Database::new("./basic.dino");
 
// Load and create the database if does not exist
db.load();

// Insert values in the db in the format of key, value
db.insert("id", "value-1");
 
// Remove a key in the database with its value
db.remove("id");

// Now here it wont print that it exists as it does not we removed it ^^^^^
if db.contains_key("id") {
    println!("The key `id` exists!")
};

println!("The length of items in the database is: {}", db.len());

Using it with rocket.rs

// Simple rocket route
#[get("/<id>")]
// Here we add the arg `db: State<dino::Database>`
// To get the db state that we passed before!
fn hello(db: State<dino::Database>, id: String) -> String {
    // Now in this rocket route we take a id param
    // We will extract the param to str
    // Then check if it exists
    match db.find(id.as_str()) {
        // If it exists it will return Ok(value)
        Ok(value) => {
            // Then we can return the value!
            return value.to_string();
        }

        // If it does not exists it gives a error
        Err(error) => {
            // So return the error!
            // You might want to handle the error too!
            return error;
        }
   }
}

fn main() {
    // Create the database instance
    let mut db = dino::Database::new("rocket.dino");

    // Load and create the database if does not exist
    db.load();

    // Insert a key with a dummy value for now!
    db.insert("key", "value!");

    // Ignite the rocket and mount the routes
    rocket::ignite()
        .mount("/", routes![hello])
        // Important part here!
        // Here we pass the db state to rocket
        // So we can access it when we go to any route.
        .manage(db)
        .launch();
}

Structs

Database

The main struct of Dino. The Database struct is responsible for creating the storage instance that will store this database's documents, managing the database tables as well as providing access to the default table.

Tree

The struct that allows you to create sub trees in the main tree in the database Sub trees do not auto insert in the main tree of the database You can do that by doing

Value

This struct is returned when you find something in the database. Value also impls fmt::Display