3 releases

0.1.0-beta.3 Feb 4, 2023
0.1.0-beta.2 Feb 2, 2023
0.1.0-beta.1 Jan 31, 2023

#1583 in Database interfaces

Download history 4/week @ 2024-02-19 27/week @ 2024-02-26 56/week @ 2024-04-01

56 downloads per month
Used in mintdb-server

MIT license

130KB
3K SLoC

mintDB Icon

An Open Source Graph Database

Getting Started

Create a new Datastore instance

use mintdb::Datastore;

[tokio::main]
async fn main() -> Result<(), mintdb::err::Error> {
    let db = Datastore::new().await?;
    Ok(())
}

CREATE

use serde_json::{Value, json};
use mintdb::Datastore;

[tokio::main]
async fn main() -> Result<(), mintdb::err::Error> {
    let tb = "db_create";
    let doc = "db_create:1";
    let data = json!({
        "name": "Lucy"
    });
    
    let db = Datastore::new().await?;

    // returns document or error if exists
    db.create_document(tb, doc, data).await?;

    let tb = "tb_create";

    // returns "OK" or error if exists
    db.create_tb(tb).await?;

    let res: Value = db.get_one(tb, doc).await?;
    Ok(())
}

READ

use serde_json::{Value, json};
use mintdb::Datastore;

[tokio::main]
async fn main() -> Result<(), mintdb::err::Error> {
    let tb = "db_read";
    let doc = "db_read:1";

    let db = Datastore::new().await?;

    // returns a document or an Error if not found
    let res: Value = db.get_one(tb, doc).await?;

    // returns an array of all documents in the table
    let res: Value = db.get_many(tb).await?;

    // returns an array of documents matching any key value
    let search = json!({
        "name": "Lucy",
        "state": "FL"
    });
    let res: Value = db.find(tb, search).await?;

    // returns an array of documents matching all key values
    let search = json!({
        "name": "Lucy",
        "state": "FL"
    });
    let res: Value = db.match_all(tb, search).await?;

    // returns an array of documents matching the comparison
    // options for op: "==", "!=", ">=", ">", "<", "<=", "contains" (case sensitive), "icontains" (case insensitve)
    let tb = "car";
    let lhs = "model";
    let op = "icontains";
    let rhs = "amg";
    let res: Value = db.compare(tb, lhs, op, rhs).await?;

    Ok(())
}

UPDATE

All create a new document and table if they do not exist

use serde_json::json;
use mintdb::Datastore;

[tokio::main]
async fn main() -> Result<(), mintdb::err::Error> {
    let tb = "db_update";
    let doc = "db_update:1";
    let data = json!({
        "name": "Lucy"
    });
    
    let db = Datastore::new().await?;

    // returns new document or error
    db.merge(tb, doc, data).await?;
    
    // returns new document or error
    let key = "name";
    let value = json!("Lucille");
    db.put(tb, doc, key, value).await?;

    // pushes a value to a key if it is an array, returns the document or error
    // returns an error if the key exists, but is not an array
    let key = "friends";
    let value = json!("Ricky");
    db.push(tb, doc, key, value).await?;

    Ok(())
}

Delete

use mintdb::Datastore;

[tokio::main]
async fn main() -> Result<(), mintdb::err::Error> {
    let tb = "db_delete";
    let doc = "db_delete:1";
    let data = json!({
        "name": "Lucy",
        "email": "lucy@gmail.com"
    });
    let db = Datastore::new().await?;

    db.create_document(tb, doc, data).await?;

    // returns document or error if document/table does not exist
    db.delete_key(tb, doc, "email").await?;

    // returns document or error if document/table does not exist
    db.delete_document(tb, doc).await?;

}

Transaction

use mintdb::Datastore;

[tokio::main]
async fn main() -> Result<(), mintdb::err::Error> {
    let db = Datastore::new().await?;

    let tb = "tx";
    let doc = "tx:1";
    let data = json!({
        "name": "Lucy",
        "balance": 1000.00 
    });

    db.create_document(tb, doc, data).await?;

    let tb = "tx";
    let doc = "tx:2";
    let data = json!({
        "name": "Ricky",
        "balance": 1000.00 
    });

    db.create_document(tb, doc, data).await?;

    let mut tx = db.transaction().await?;

    tx.begin();
    tx.debit("tx", "tx:2", "balance", 500.00).await?;
    tx.credit("tx", "tx:1", "balance", 500.00).await;
    tx.commit().await?;

}

Dependencies

~11MB
~295K SLoC