3个版本
0.1.0-beta.3 | 2023年2月4日 |
---|---|
0.1.0-beta.2 | 2023年2月2日 |
0.1.0-beta.1 | 2023年1月31日 |
#1783 in 数据库接口
在 mintdb-server 中使用
130KB
3K SLoC
一个开源的图数据库
入门指南
创建一个新的Datastore实例
use mintdb::Datastore;
[tokio::main]
async fn main() -> Result<(), mintdb::err::Error> {
let db = Datastore::new().await?;
Ok(())
}
创建
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(())
}
读取
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(())
}
更新
所有操作:如果不存在则创建一个新的文档和表
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(())
}
删除
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": "[email protected]"
});
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?;
}
事务
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?;
}
依赖
~11MB
~295K SLoC