pub struct Datastore {
pub collections: Arc<RwLock<Collection>>,
}
Fields§
§collections: Arc<RwLock<Collection>>
Implementations§
source§impl Datastore
impl Datastore
sourcepub async fn sign_in(
&self,
username: &str,
password: &str
) -> Result<TokenResponse>
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(())
}
sourcepub async fn sign_up(&self, username: &str, pwd: &str) -> Result<TokenResponse>
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(())
}
sourcepub async fn sign_out(&self, jwt: &str) -> Result<()>
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(())
}
sourcepub async fn get_user(&self, uid: &str) -> Result<Value>
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(())
}
sourcepub async fn list_users(&self) -> Result<Value>
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
impl Datastore
pub async fn create_api_token(&self, username: &str) -> Result<Value>
pub async fn delete_api_token(&self, token: &str) -> Result<Value>
pub async fn validate_api_token(&self, token: &str) -> Result<Value>
source§impl Datastore
impl Datastore
sourcepub async fn get_one(&self, tb: &str, doc: &str) -> Result<Value>
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(())
}
sourcepub async fn get_many(&self, tb: &str) -> Result<Value>
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(())
}
sourcepub async fn find(&self, tb: &str, data: Value) -> Result<Value>
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(())
}
sourcepub async fn match_all(&self, tb: &str, data: Value) -> Result<Value>
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(())
}
sourcepub async fn compare(
&self,
tb: &str,
lhs: &str,
op: &str,
rhs: &Value
) -> Result<Value>
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
impl Datastore
sourcepub async fn create_document(
&self,
tb: &str,
doc: &str,
data: Value
) -> Result<Value>
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(())
}
sourcepub async fn create_table(&self, tb: &str) -> Result<Value>
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(())
}
sourcepub async fn merge(&self, tb: &str, doc: &str, data: Value) -> Result<Value>
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(())
}
pub async fn insert(
&self,
tb: &str,
doc: &str,
key: &str,
value: Value
) -> Result<Value>
sourcepub async fn push(
&self,
tb: &str,
doc: &str,
key: &str,
value: Value
) -> Result<Value>
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(())
}
sourcepub async fn put(
&self,
tb: &str,
doc: &str,
key: &str,
value: Value
) -> Result<Value>
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(())
}
sourcepub async fn put_document(&self, tb: &str, doc: &str, data: Value) -> Result<Value>
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(())
}
sourcepub async fn delete_key(&self, tb: &str, doc: &str, key: &str) -> Result<Value>
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(())
}
sourcepub async fn delete_document(&self, tb: &str, doc: &str) -> Result<Value>
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(())
}
sourcepub async fn delete_key_from_table(&self, tb: &str, key: &str) -> Result<Value>
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(())
}
sourcepub async fn delete_table(&self, tb: &str) -> Result<Value>
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
impl Datastore
sourcepub async fn execute(&self, sql: &SQL) -> Result<Value>
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(())
}