Giter VIP home page Giter VIP logo

rust-mock-store's Introduction

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

Example

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)?;

Library Scope

  • This is not intended for production use.
  • Primarily for writing tests or proofs of concept without a real data store.
  • Prioritizes ergonomics and convenience over performance.

Current Limitations

  • Capability: For now, supports only one level down matches and numbers, booleans, strings.
  • Performance: The store is per type, but the object store contains the serialized serde_json Value.
  • Performance: Because objects are stored as Value, during an update, objects are deserialized and re-serialized.

rust-mock-store's People

Contributors

jeremychone avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.