Crates.io | mock-store |
lib.rs | mock-store |
version | 0.2.0 |
source | src |
created_at | 2023-04-06 00:23:08.056523 |
updated_at | 2023-04-15 22:36:50.385307 |
description | Simple Rust in-memory mock-store for testing and prototyping (with modql support). |
homepage | https://github.com/jeremychone/rust-mock-store |
repository | https://github.com/jeremychone/rust-mock-store |
max_upload_size | |
id | 831585 |
size | 41,758 |
Rust mock-store is a simple Rust in-memory mock-store for testing and prototyping (with modql implementation).
Do not use this in production code.
mock-store uses modql for filtering capability. It's also a great way to experiment with modql.
Example | Library Scope | Limitations
See examples/readme.rs for the full working source.
// -- Store is Send + Sync (backed by Arc/Mutex).
let store = Store::new();
// -- Insert the objects.
store.insert(Ticket {
id: 1,
title: "Ticket AAA".to_string(),
})?;
store.insert(Ticket {
id: 1,
title: "Ticket BBB".to_string(),
})?;
// -- List all tickets (no filter).
let all_tickets = store.list::<Ticket>(None)?;
// [Ticket { id: 1, title: "Ticket AAA" }, Ticket { id: 1, title: "Ticket BBB" }]
println!("{:<20}: {all_tickets:?}", "all tickets");
// -- List with filter (using modql: https://github.com/jeremychone/rust-modql)
let filter: FilterGroup = vec![("title", OpValString::Contains("AA".to_string())).into()].into();
let double_a_tickets = store.list::<Ticket>(filter)?;
// [Ticket { id: 1, title: "Ticket AAA" }]
println!("{:<20}: {double_a_tickets:?}", "double_a_tickets");
// -- Update with filter.
let filter: FilterGroup = vec![("title", OpValString::Contains("BB".to_string())).into()].into();
let count = store.update::<Ticket, _>(filter, |mut ticket| {
ticket.title = "TICKET BB - UPDATE".to_string();
ticket
})?;
// 1
println!("{:<20}: {count:?}", "tickets updated");
// -- List all tickets again.
let all_tickets = store.list::<Ticket>(None)?;
// [Ticket { id: 1, title: "Ticket AAA" }, Ticket { id: 1, title: "TICKET BB - UPDATE" }]
println!("{:<20}: {all_tickets:?}", "all tickets");
// -- Delete is: store.delete::<Ticket>(filter)?;