#io #api-bindings #error #client #iterface

bin+lib easydb

An iterface for working with easydb.io in rust

2 unstable releases

0.2.0 Nov 23, 2019
0.1.0 Nov 22, 2019

#874 in #error

MIT/Apache

20KB
324 lines

easydb-rust

An interface for working with easydb.io in rust.

Docs

Crates.io page

Repository

License

Licensed under either of

at your option.

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.


lib.rs:

An interface for working with easydb.io in rust.

Quick start

use easydb::EasyDB;

// Create an EasyDB struct to interact with.
// Gets information from `./easydb.toml`.
let edb: EasyDB = EasyDB::new()?;
#

// Store some data
   edb.put("hello", "world")?;
   edb.put("goodbye", "earth")?;
#

// Get a single item
let stored_hello: String = edb.get("hello")?;
assert_eq!(&stored_hello, "world");

// Update an item
edb.put("goodbye", "dirt")?;
assert_eq!(&edb.get("goodbye")?, "dirt");

// Get a HashMap of all database entries
   let resp: HashMap<String, String> = edb.list()?;
   assert_eq!(&resp["hello"], "world");
   assert_eq!(&resp["goodbye"], "dirt");

// Delete items
   edb.delete("hello")?;
let deleted_item: String = edb.get("hello")?;
assert_eq!(&deleted_item, "");

Commands

The easiest way to create an [EasyDB][EasyDB] is to call EasyDB::new(). This generates the struct using data in ./easydb.toml, which should include the following information:

UUID = "aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee"
Token = "ffffffff-0000-1111-2222-333333333333"
URL = "https://app.easydb.io/database/"

The URL field is optional and will default to https://app.easydb.io/database/.

If your toml is not at the default location, you can just parse it from a string in toml format. For example:

let edb: EasyDB = "UUID = \"aaaa...\"\nToken = \"ffff...\"".parse().unwrap();

If you have individual items, you can initalize an [EasyDB][EasyDB] with from_uuid_token:

let edb = EasyDB::from_uuid_token("aaaa...".to_string(), "ffff...".to_string(), None);

Using EasyDB

The four methods get, put, delete, and list correspond to the four available APIs in easydb.io. get and delete take one argument: a key. put takes two arguments: a key and a value. list takes no arguments. Example usage can be seen in the quick start section at the top of this page.

The above methods deal with String values and will fail if any value is not a JSON string. If you would like to use JSON, there are get_json, put_json, and list_json (delete is the same). These deal with values that are of the Json type, which is a re-export of the Value type from serde_json.

In addition, there is the clear method for easily clearing the entire database, which, for example, is useful when initializing the database. This just calls delete on every item, but if easydb.io implements a clear function in the future, this will call it.

Errors

All network errors as reported by the reqwest crate are returned in Results. Other errors are documented on their respective methods.

Due to the unknown nature of the database, there may be unexpected results when reading data just after writing data. Expect that read values will be either up-to-date or old values.

Dependencies

~23MB
~502K SLoC