Expand description
microrm is a simple object relational manager (ORM) for sqlite.
Unlike many fancier ORM systems, microrm is designed to be lightweight, both in terms of runtime overhead and developer LoC. By necessity, it sacrifices flexibility towards these goals, and so can be thought of as more opinionated than, say, SeaORM or Diesel. Major limitations of microrm are:
- lack of database migration support
- limited vocabulary for describing object-to-object relations
There are three externally-facing components in microrm:
- Object modelling (via the
Datum
andEntity
traits) - Database querying (via
Queryable
,RelationInterface
andInsertable
traits) - Command-line interface generation via the
clap
crate (seecli::CLIObject
andcli::ClapInterface
; requires the optional crate featureclap
)
microrm pushes the Rust type system somewhat to provide better ergonomics, so the MSRV is
currently 1.75. Don’t be scared off by the web of traits in the schema
module — you should
never need to interact with any of them!
§Examples
For the simplest kind of database schema, a key-value store, one possible microrm implementation of it might look like the following:
use microrm::prelude::*;
#[derive(Entity)]
struct KVEntry {
#[key]
key: String,
value: String,
}
#[derive(Database)]
struct KVDB {
kvs: microrm::IDMap<KVEntry>,
}
let db = KVDB::open_path(":memory:")?;
db.kvs.insert(KVEntry {
key: "example_key".to_string(),
value: "example_value".to_string()
})?;
// can get with a String reference
assert_eq!(
db.kvs.keyed(&String::from("example_key")).get()?.map(|v| v.value.clone()),
Some("example_value".to_string()));
// thanks to the QueryEquivalent trait, we can also just use a plain &str
assert_eq!(
db.kvs.keyed("example_key").get()?.map(|v| v.value.clone()),
Some("example_value".to_string()));
// obviously, if we get another KV entry with a missing key, it doesn't come back...
assert_eq!(db.kvs.keyed("another_example_key").get()?.is_some(), false);
// note that the above all return an Option<Stored<T>>. when using filters on arbitrary
// columns, a Vec<Stored<T>> is returned:
assert_eq!(
db
.kvs
// note that the column constant uses CamelCase
.with(KVEntry::Value, "example_value")
.get()?
.into_iter()
.map(|v| v.wrapped().value).collect::<Vec<_>>(),
vec!["example_value".to_string()]);
Re-exports§
pub use schema::relation::Relation;
pub use schema::relation::RelationDomain;
pub use schema::relation::RelationMap;
pub use schema::relation::RelationRange;
pub use schema::IDMap;
pub use schema::Serialized;
pub use schema::Stored;
Modules§
- This module provides autogeneration of clap commands to manipulate entities that are stored in a microrm database.
- SQLite database interaction functions.
- Module prelude with commonly-used traits that shouldn’t have overlap with other crates.
- Schema specification types.
Enums§
- microrm error type, returned from most microrm methods.
Type Aliases§
- Shorthand alias.