[][src]Crate tinydb

About

A small-footprint database implamentation, originally designed for the Zeno code editor. This database does not accept duplicates and will not save a second identical item.

Under the surface, tinydb uses a HashSet-based table that works in a similar fashion to SQL-like/Grid based databases.

Disclaimer

This project is not intended to be used inside of any critical systems due to the nature of dumping/recovery. If you are using this crate as a temporary and in-memory only database, it should preform at a reasonable speed (as it uses HashSet underneath).

Essential operations

Some commonly-used operations for the Database structure.

OperationImplamentation
Create databaseDatabase::new
Create database from fileDatabase::from
Query for itemDatabase::query_item
Contains specific itemDatabase::contains
Update/replace itemDatabase::update_item
Delete itemDatabase::remove_item
Get all itemsDatabase::read_db
Dump databaseDatabase::dump_db

Examples

Adding an item then deleting the same item by finding it in the database:

use tinydb::Database;

/// A structure to test the capabilities of TinyDB. This requires [Hash],
/// [Eq], [Serialize] and [Deserialize] (final two being for file operations).
#[derive(Hash, Eq)]
struct ExampleStruct {
    age: i32,
    other_int: i8
}

fn main() {
    let mut db = Database::new(
        String::from("test-db"),
        None,
        false
    ); // Creates the database.

    db.add_item(
        ExampleStruct { age: 595, other_int: 34 }
    ); // Adds an item to the database.

    let got_item = db.query_item(
        |s: ExampleStruct| s.age,
        595
    ).unwrap(); // Returns the item with [ExampleStruct::age] of 595.

    db.remove_item(got_item).unwrap(); // Will remove the item found from the database
}

Modules

error

Contains various items related to errors inside of TinyDB.

Structs

Database

The primary database structure, allowing storage of a generic type with dumping/saving options avalible.