microrm 0.6.3

Lightweight ORM using sqlite as a backend
Documentation
use microrm::prelude::*;

// very simple schema with a bunch of strings and blobs
mod schema {
    use microrm::prelude::*;

    #[derive(Entity, PartialEq, Clone)]
    pub struct KVEntry {
        #[key]
        pub key: String,

        pub value: Vec<u8>,
    }

    #[derive(Schema)]
    pub struct KVSchema {
        pub kvs: microrm::IDMap<KVEntry>,
    }
}

#[global_allocator]
static ALLOCATOR: alloc_tracker::Allocator<std::alloc::System> = alloc_tracker::Allocator::system();

fn main() {
    let (cp, schema): (_, schema::KVSchema) =
        microrm::ConnectionPool::open(":memory:").expect("could not open in-memory database?");

    cp.run_transaction(1, |txn| {
        schema.kvs.insert_ref(
            txn,
            schema::KVEntryRef {
                key: "keyA",
                value: &[],
            },
        )?;

        schema.kvs.insert_ref(
            txn,
            schema::KVEntryRef {
                key: "keyB",
                value: &[1, 2, 3],
            },
        )?;

        schema.kvs.insert_ref(
            txn,
            schema::KVEntryRef {
                key: "keyC",
                value: &[3, 2, 1],
            },
        )?;

        Ok(())
    })
    .expect("could not insert initial data");

    let session = alloc_tracker::Session::new();

    // do a query warmup with both methods, because each query type requires caching a sqlite
    // statement.
    cp.run_transaction(1, |txn| {
        schema.kvs.iter(txn)?.for_each(|_| ());
        schema
            .kvs
            .iter_refs(txn, |_| std::ops::ControlFlow::Continue(()))?;
        Ok(())
    })
    .expect("warmup failed");

    // now iterate and print things out with both iteration methods
    {
        println!("iter():");
        let operation = session.operation("iter()");
        let _span = operation.measure_thread();
        cp.run_transaction(1, |txn| {
            for kv in schema.kvs.iter(txn)? {
                let kv = kv?;
                println!("kv entry: key = {}, values = {:?}", kv.key, kv.value);
            }
            Ok(())
        })
        .expect("could not iterate over data");
    }

    {
        println!("iter_refs():");
        let operation = session.operation("iter_refs()");
        let _span = operation.measure_thread();
        cp.run_transaction(1, |txn| {
            schema.kvs.iter_refs(txn, |kv| {
                println!("kv entry: key = {}, values = {:?}", kv.key, kv.value);
                std::ops::ControlFlow::Continue(())
            })
        })
        .expect("could not iterate over data");
    }

    // should show zero allocations for the iter_refs() version.
    session.print_to_stdout();
}