MacroDB
I like using databases to store things. Specifically, using SQLite is often nice, even for small, local applications. However, there are cases where you just want to store some relational data in-memory and have some indexes for it to look up values quickly.
This is where MacroDB comes it. It is a Rust macro that lets you define a database-like schema, and generates code for you to insert and delete rows. The code it generate fully handles updating all indices upon insertion, deletion and mutation of values.
It supports:
- Tables
- Primary keys
- Unique indexes
- Indexes
- Constraints
It is also generic over the underlying data structures you use to store primary
keys and indices. You can use HashMap or BTreeMap-backed data structures
for each.
Examples
Imagine you want to create a table to represent users. Every user has an ID, an email address and a name. The ID and the email should be unique per user. Users also have tags, and it should be able to look up all users with a given tag.
Conceptually, this means we need two tables: a table of users, and a table of user-tag associations. We have to define structs for each:
#![allow(unused)] fn main() { struct User { id: u64, email: String, name: String, } enum Tag { Admin, Supervisor, Employee, Guest, } struct UserTag { user: u64, tag: Tag, } }
Todo