Expand description
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 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 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.
Modules§
Structs§
- EasyDB
- The main type for dealing with easydb.