Struct mintdb::kvs::store::Datastore

source ·
pub struct Datastore {
    pub collections: Arc<RwLock<Collection>>,
}

Fields§

§collections: Arc<RwLock<Collection>>

Implementations§

source§

impl Datastore

source

pub async fn sign_in(
    &self,
    username: &str,
    password: &str
) -> Result<TokenResponse>

Signs a user in, returns JWT response or error

Examples
use mintdb::Datastore;
use serde_json::json;
 
#[tokio::main]
async fn main() ->  Result<(), mintdb::Error> {
    let db = Datastore::new().await?;
    let username = "lucy@gmail.com";
    let password = "abc123";
    match db.sign_up(username, password).await {
        Ok(val) => {
            println!("User Created");
        }
        Err(e) => {
            println!("User Exists");
        }
    };
 
    db.sign_in(username, password).await?;

    Ok(())
}
source

pub async fn sign_up(&self, username: &str, pwd: &str) -> Result<TokenResponse>

Signs a user up, returns JWT response or error if user exists

Examples
use mintdb::Datastore;
use serde_json::json;
 
#[tokio::main]
async fn main() ->  Result<(), mintdb::Error> {
    let db = Datastore::new().await?;
    let username = "lucy@gmail.com";
    let password = "abc123";
    match db.sign_up(username, password).await {
        Ok(val) => {
            println!("User Created");
        }
        Err(e) => {
            println!("User Exists");
        }
    };
 
    db.sign_in(username, password).await?;

    Ok(())
}
source

pub async fn sign_out(&self, jwt: &str) -> Result<()>

Signs a user out, returns () or error

Examples
use mintdb::Datastore;
use serde_json::json;
 
#[tokio::main]
async fn main() ->  Result<(), mintdb::Error> {
    let db = Datastore::new().await?;
    let username = "lucy@gmail.com";
    let password = "abc123";
    match db.sign_up(username, password).await {
        Ok(val) => {
            println!("User Created");
        }
        Err(e) => {
            println!("User Exists");
        }
    };
 
    let jwt = db.sign_in(username, password).await?;
 
    db.sign_out(&jwt.token).await?;

    Ok(())
}
source

pub async fn get_user(&self, uid: &str) -> Result<Value>

gets one user, returns user document or error if doesn’t exist

Examples
use mintdb::Datastore;
use serde_json::json;
 
#[tokio::main]
async fn main() ->  Result<(), mintdb::Error> {
    let db = Datastore::new().await?;
    let username = "lucy@gmail.com";
    let password = "abc123";
    match db.sign_up(username, password).await {
        Ok(val) => {
            println!("User Created");
        }
        Err(e) => {
            println!("User Exists");
        }
    };
 
    db.get_user(username).await?;

    Ok(())
}
source

pub async fn list_users(&self) -> Result<Value>

gets all users, returns user document or error if doesn’t exist

Examples
use mintdb::Datastore;
use serde_json::json;
 
#[tokio::main]
async fn main() ->  Result<(), mintdb::Error> {
    let db = Datastore::new().await?;
    let username = "lucy@gmail.com";
    let password = "abc123";
    match db.sign_up(username, password).await {
        Ok(val) => {
            println!("User Created");
        }
        Err(e) => {
            println!("User Exists");
        }
    };
 
    db.list_users().await?;

    Ok(())
}
source§

impl Datastore

source

pub async fn create_api_token(&self, username: &str) -> Result<Value>

source

pub async fn delete_api_token(&self, token: &str) -> Result<Value>

source

pub async fn validate_api_token(&self, token: &str) -> Result<Value>

source§

impl Datastore

source

pub async fn get_one(&self, tb: &str, doc: &str) -> Result<Value>

Returns a document or error if not found

Examples
use mintdb::Datastore;
use serde_json::json;
 
#[tokio::main]
async fn main() ->  Result<(), mintdb::Error> {
    let db = Datastore::new().await?;
    let tb = "person";
    let doc = "person:1";
 
    db.merge(tb, doc, json!({"name": "Lucy"})).await?;
 
    let res = db.get_one(tb, doc).await?;
    Ok(())
}
source

pub async fn get_many(&self, tb: &str) -> Result<Value>

Returns all documents in a table or error if not found

Examples
use mintdb::Datastore;
use serde_json::json;
 
#[tokio::main]
async fn main() ->  Result<(), mintdb::Error> {
    let db = Datastore::new().await?;
    let tb = "person";
    let doc = "person:1";
 
    db.merge(tb, doc, json!({"name": "Lucy"})).await?;
 
    let res = db.get_many(tb).await?.as_array().unwrap();
    Ok(())
}
source

pub async fn find(&self, tb: &str, data: Value) -> Result<Value>

Returns all documents matching any key value pair

Examples
use mintdb::Datastore;
use serde_json::json;
 
#[tokio::main]
async fn main() ->  Result<(), mintdb::Error> {
    let db = Datastore::new().await?;
    let tb = "person";
    let doc = "person:1";
 
    db.merge(tb, doc, json!({"name": "Lucy"})).await?;
 
    let tb = "person";
    let doc = "person:2";
 
    db.merge(tb, doc, json!({"name": "Ricky", "state": "FL"})).await?;
 
 
    let data = json!({"name":"Lucy", "state": "FL"});
    let res = db.find(tb, data).await?.as_array().unwrap();

    Ok(())
}
source

pub async fn match_all(&self, tb: &str, data: Value) -> Result<Value>

Returns all documents matching all key value pairs

Examples
use mintdb::Datastore;
use serde_json::json;
 
#[tokio::main]
async fn main() ->  Result<(), mintdb::Error> {
    let db = Datastore::new().await?;
    let tb = "person";
    let doc = "person:1";
 
    db.merge(tb, doc, json!({"name": "Lucy"})).await?;
 
    let tb = "person";
    let doc = "person:2";
 
    db.merge(tb, doc, json!({"name": "Ricky", "state": "FL"})).await?;
 
 
    let data = json!({"name":"Lucy", "state": "FL"});
    let res = db.match_all(tb, data).await?.as_array().unwrap();
     
    Ok(())
}
source

pub async fn compare(
    &self,
    tb: &str,
    lhs: &str,
    op: &str,
    rhs: &Value
) -> Result<Value>

Returns all documents matching comparison Comparison operators: “==”, “!=”, “>=”, “>”, “<=”, “<”, “contains” (case sensitive), “icontains” (case insensitive)

Examples
use mintdb::Datastore;
use serde_json::json;
 
#[tokio::main]
async fn main() ->  Result<(), mintdb::Error> {
    let db = Datastore::new().await?;
    let tb = "car";
    let doc = "car:1";
 
    db.merge(tb, doc, json!({"make": "Mercedes-Benz", "year": 2024})).await?;
 
    let tb = "car";
    let doc = "car:2";
 
    db.merge(tb, doc, json!({"make": "Suzuki", "year": 1995})).await?;
 
 
    let lhs = "make";
    let op = "icontains";
    let rhs = json!("benz");
    let res = db.compare(tb, lhs, op, &rhs).await?.as_array().unwrap();
 
    let lhs = "year";
    let op = ">=";
    let rhs = json!(2024);
    let res = db.compare(tb, lhs, op, &rhs).await?.as_array().unwrap();
     
    Ok(())
}
source§

impl Datastore

source

pub async fn create_document(
    &self,
    tb: &str,
    doc: &str,
    data: Value
) -> Result<Value>

Creates a document, returns new document or error if document exists

Examples
use mintdb::Datastore;
use serde_json::json;
 
#[tokio::main]
async fn main() ->  Result<(), mintdb::Error> {
    let db = Datastore::new().await?;
    let tb = "person";
    let doc = "person:1";
 
    match db.create_document(tb, doc, json!({"name": "Lucy"})).await {
        Ok(res) => {
            println!("New document created");
        }
        Err(e) => {
            println!("Document already exists");
        }
    };

    Ok(())
}
source

pub async fn create_table(&self, tb: &str) -> Result<Value>

Creates a table, returns string confirmation or error if table exists

Examples
use mintdb::Datastore;
use serde_json::json;
 
#[tokio::main]
async fn main() ->  Result<(), mintdb::Error> {
    let db = Datastore::new().await?;
    let tb = "person";
 
    match db.create_table(tb).await {
        Ok(res) => {
            println!("New document created");
        }
        Err(e) => {
            println!("Document already exists");
        }
    };

    Ok(())
}
source

pub async fn merge(&self, tb: &str, doc: &str, data: Value) -> Result<Value>

Updates or creates a document, returns updated document or error if document exists

Examples
use mintdb::Datastore;
use serde_json::json;
 
#[tokio::main]
async fn main() ->  Result<(), mintdb::Error> {
    let db = Datastore::new().await?;
    let tb = "person";
    let doc = "person:1";
 
    db.merge(tb, doc, json!({"email": "lucy@gmail.com"})).await?;

    Ok(())
}
source

pub async fn insert(
    &self,
    tb: &str,
    doc: &str,
    key: &str,
    value: Value
) -> Result<Value>

source

pub async fn push(
    &self,
    tb: &str,
    doc: &str,
    key: &str,
    value: Value
) -> Result<Value>

Updates or creates a document and pushes the value to an array in the key, returns updated document or error if document contains the key and it is not an array.

Examples
use mintdb::Datastore;
use serde_json::json;
 
#[tokio::main]
async fn main() ->  Result<(), mintdb::Error> {
    let db = Datastore::new().await?;
    let tb = "person";
    let doc = "person:1";
    let key = "friends";
    let value = json!("person:2");
 
    db.push(tb, doc, key, value).await?;

    Ok(())
}
source

pub async fn put(
    &self,
    tb: &str,
    doc: &str,
    key: &str,
    value: Value
) -> Result<Value>

Updates or creates a document and inserts the value into the key, returns updated document or error.

Examples
use mintdb::Datastore;
use serde_json::json;
 
#[tokio::main]
async fn main() ->  Result<(), mintdb::Error> {
    let db = Datastore::new().await?;
    let tb = "person";
    let doc = "person:1";
    let key = "city";
    let value = json!("Miami");
 
    db.put(tb, doc, key, value).await?;

    Ok(())
}
source

pub async fn put_document(&self, tb: &str, doc: &str, data: Value) -> Result<Value>

Inserts a document into a table, overwriting the previous document if it exists, returns updated document or error if document contains the key and it is not an array.

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

    Ok(())
}
source

pub async fn delete_key(&self, tb: &str, doc: &str, key: &str) -> Result<Value>

Deletes a key from a document in a table, returns updated document or error if document, table, or key do not exist.

Examples
use mintdb::Datastore;
use serde_json::json;
 
#[tokio::main]
async fn main() ->  Result<(), mintdb::Error> {
    let db = Datastore::new().await?;
    let tb = "person";
    let doc = "person:1";
    let key = "email";
    db.put(tb, doc, key, json!("lucy@gmail.com")).await?;
 
    db.delete_key(tb, doc, key).await?;

    Ok(())
}
source

pub async fn delete_document(&self, tb: &str, doc: &str) -> Result<Value>

Deletes a document from a table, returns deleted document or error if document or table do not exist.

Examples
use mintdb::Datastore;
use serde_json::json;
 
#[tokio::main]
async fn main() ->  Result<(), mintdb::Error> {
    let db = Datastore::new().await?;
    let tb = "person";
    let doc = "person:1";
    db.merge(tb, doc, json!({"name": "Lucy"})).await?;
 
    db.delete_document(tb, doc).await?;

    Ok(())
}
source

pub async fn delete_key_from_table(&self, tb: &str, key: &str) -> Result<Value>

Deletes a key from all documents in a table, returns string confirmation or error if the table does not exist.

Examples
use mintdb::Datastore;
use serde_json::json;
 
#[tokio::main]
async fn main() ->  Result<(), mintdb::Error> {
    let db = Datastore::new().await?;
    let tb = "person";
    let key = "email"
    db.delete_key_from_table(tb, key).await?;

    Ok(())
}
source

pub async fn delete_table(&self, tb: &str) -> Result<Value>

Deletes a table, returns string confirmation or error if the table does not exist.

Examples
use mintdb::Datastore;
use serde_json::json;
 
#[tokio::main]
async fn main() ->  Result<(), mintdb::Error> {
    let db = Datastore::new().await?;
    let tb = "person";
    let key = "email"
    db.delete_key_from_table(tb, key).await?;

    Ok(())
}
source§

impl Datastore

source

pub async fn execute(&self, sql: &SQL) -> Result<Value>

executes a SQL statement

Examples
use mintdb::Datastore;
use serde_json::json;
use mintdb::db::exe::SQL;
 
#[tokio::main]
async fn main() ->  Result<(), mintdb::Error> {
    let db = Datastore::new().await?;
    let tb = "car";
    let doc = "car:1";
    let data = json!({"make": "Mercedes-Benz"});
    let sql = SQL{
        stmt: "MERGE".into(),
        tb: tb.into(),
        doc: doc.into(),
        data: data,
        topic: "".into(),
        user_id: Some(1),
        message: "".into(),    
    };
    db.execute(&sql).await?;
 
    let sql = SQL{
        stmt: "SELECT".into(),
        tb: "car".into(),
        doc: "*".into(),
        data: json!({}),
        topic: "".into(),
        user_id: Some(1),
        message: "".into(),    
    };
    let res = db.execute(&sql).await?;
    Ok(())
}
source§

impl Datastore

source

pub async fn transaction(&self) -> Result<Transaction<'_>>

source§

impl Datastore

source

pub async fn bfs<'a>(
    &self,
    tb: &str,
    doc: &str,
    rel: &str,
    target_doc: &str,
    visited: &'a mut Vec<Value>
) -> Result<Option<Value>>

source§

impl Datastore

source

pub fn dfs<'a>(
    &self,
    tb: &str,
    doc: &str,
    rel: &str,
    target_doc: &str,
    visited: &'a mut Vec<Value>
) -> Result<Option<Value>>

source§

impl Datastore

source

pub async fn add_node(
    &self,
    tb: &str,
    doc: &str,
    ref_tb: &str,
    ref_doc: &str,
    rel: &str
) -> Result<()>

source§

impl Datastore

source

pub async fn new() -> Result<Self>

Creates a new Datastore instance

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

impl Datastore

source

pub async fn add_read_rule(&self, tb: &str, rule: Rule) -> Result<Value>

source

pub async fn get_read_rules(&self, tb: &str) -> Result<Value>

source

pub async fn remove_read_rule(&self, tb: &str, rule: Rule) -> Result<Value>

source

pub async fn add_write_rule(&self, tb: &str, rule: Rule) -> Result<Value>

source

pub async fn get_write_rules(&self, tb: &str) -> Result<Value>

source

pub async fn remove_write_rule(&self, tb: &str, rule: Rule) -> Result<Value>

source

pub async fn add_delete_rule(&self, tb: &str, rule: Rule) -> Result<Value>

source

pub async fn get_delete_rules(&self, tb: &str) -> Result<Value>

source

pub async fn remove_delete_rule(&self, tb: &str, rule: Rule) -> Result<Value>

source§

impl Datastore

source

pub async fn add_schema_auth(&self, tb: &str, schema: &Value) -> Result<Value>

source

pub async fn delete_schema_auth(&self, tb: &str) -> Result<()>

Trait Implementations§

source§

impl Clone for Datastore

source§

fn clone(&self) -> Datastore

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl Debug for Datastore

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl Default for Datastore

source§

fn default() -> Datastore

Returns the “default value” for a type. Read more
source§

impl<'de> Deserialize<'de> for Datastore

source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
    __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
source§

impl Serialize for Datastore

source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>where
    __S: Serializer,

Serialize this value into the given Serde serializer. Read more

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for Twhere
    T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere
    T: ?Sized,

const: unstable · source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere
    T: ?Sized,

const: unstable · source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

const: unstable · source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for Twhere
    U: From<T>,

const: unstable · source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> ToOwned for Twhere
    T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T, U> TryFrom<U> for Twhere
    U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
const: unstable · source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere
    U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
const: unstable · source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
source§

impl<T> DeserializeOwned for Twhere
    T: for<'de> Deserialize<'de>,