[−][src]Crate tinydb
NOTE: This project is not affiliated with the Python TinyDB, accidental naming error from when this project was started. See renaming for updates
TinyDB or tinydb
is a small-footprint, superfast database designed to be
used in-memory and easily dumped/retrieved from a file when it's time to save
✨
This database aims to provide an easy frontend to an efficiant in-memory database (that can also be dumped to a file). It purposefully disallows duplicate items to be sorted due to constraints with hash tables.
Example 🚀
A simple example of adding a structure then querying for it:
use serde::{Serialize, Deserialize}; use tinydb::Database; #[derive(Debug, Eq, PartialEq, Hash, Serialize, Deserialize, Clone)] struct ExampleStruct { my_age: i32 } fn main() { let my_struct = ExampleStruct { my_age: 329 }; let mut my_db = Database::new("query_test", None, false); my_db.add_item(my_struct.clone()); let results = my_db.query_item(|s: &ExampleStruct| &s.my_age, 329); assert_eq!(results.unwrap(), &my_struct); }
Installation
Simply add the following to your Cargo.toml
file:
[dependencies]
tinydb = "1"
Implementation notes
- This database does not save 2 duplicated items, either ignoring or raising an error depending on end-user preference.
- 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.
Operation | Implamentation |
---|---|
Create database | Database::new |
Create database from file | Database::from |
Load database or create if non-existant | Database::auto_from |
Query for item | Database::query_item |
Contains specific item | Database::contains |
Update/replace item | Database::update_item |
Delete item | Database::remove_item |
Dump database | Database::dump_db |
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. |