#json-file #object #github #write #flushes #com-maxogden-toiletdb

toiletdb

Flushes an object to a JSON file. Rust implementation of https://github.com/maxogden/toiletdb

3 releases

0.1.2 Apr 12, 2020
0.1.1 Apr 12, 2020
0.1.0 Apr 11, 2020

#164 in #json-file

MIT license

7KB
68 lines

toiletdb 🚽🦀

Flushes an object to a JSON file. Rust implementation of https://github.com/maxogden/toiletdb

A Rust key/value store backed by a JSON file.

Usage

use toiletdb::Toiletdb;

  // pass the name of the json file to use
fn example() -> Result<(), std::io::Error> {
  let mut db = Toiletdb::new("data.json")?;

  // write some key/value pairs to data.json
  db.write("test", 123)?;
  db.write("name", "toiletdb")?;
  db.write("rust", true)?;

  // get the entire data.json contents
  let data: String = db.read()?;

  // read a value from a key
  if let Some(v) = db.read_key("test") {
     assert_eq!(v, 123);
  }

  // delete a key/value pair
  db.delete("test")?;

  // reset state and delete data.json
  db.flush()?;
  Ok(())
}

API

db.write(key, value)

sets key to val inside the JSON file

db.read()

read the entire JSON file to a String

db.read_key(key)

get the value of a key

db.delete(key)

deletes key and it's value from the JSON file

db.flush()

resets state and deletes the JSON file

Installation

With cargo-edit:

cargo add toiletdb

Or manually add toiletdb to Cargo.toml

toiletdb = "0.1.0"

See Also

License

MIT

Dependencies

~2–11MB
~134K SLoC