microrm 0.6.3

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

mod common;

struct AuthorBookRelation;
impl microrm::Relation for AuthorBookRelation {
    type Domain = Author;
    type Range = Book;

    const NAME: &'static str = "AuthorBook";
    const INJECTIVE: bool = true;
}

#[derive(Entity)]
struct Author {
    #[key]
    name: String,

    works: microrm::RelationDomain<AuthorBookRelation>,
}

#[derive(Entity)]
struct Book {
    #[key]
    title: String,

    creator: microrm::RelationRange<AuthorBookRelation>,
}

#[derive(Schema)]
struct ABDB {
    authors: microrm::IDMap<Author>,
    books: microrm::IDMap<Book>,
}

#[test]
fn schema_creation() {
    let (_pool, _db): (_, ABDB) = common::open_test_db!();
}

#[test]
fn single_connection() {
    let (pool, db): (_, ABDB) = common::open_test_db!();
    let mut txn = pool.start().unwrap();

    let a1 = db
        .authors
        .insert_and_return(
            &mut txn,
            Author {
                name: "Homer".to_string(),
                works: Default::default(),
            },
        )
        .expect("couldn't insert author");

    let a2 = db
        .authors
        .insert_and_return(
            &mut txn,
            Author {
                name: "Virgil".to_string(),
                works: Default::default(),
            },
        )
        .expect("couldn't insert author");

    let b1_id = db
        .books
        .insert(
            &mut txn,
            Book {
                title: "Odyssey".to_string(),
                creator: Default::default(),
            },
        )
        .expect("couldn't insert book");
    let b2_id = db
        .books
        .insert(
            &mut txn,
            Book {
                title: "Aeneid".to_string(),
                creator: Default::default(),
            },
        )
        .expect("couldn't insert book");

    a1.works
        .connect_to(&mut txn, b1_id)
        .expect("couldn't connect a1 and b1");
    a2.works
        .connect_to(&mut txn, b2_id)
        .expect("couldn't connect a2 and b2");

    // we can't claim that Homer wrote the Aeneid because it's an injective relationship and
    // in this model, only one Author can claim the Book as their work
    match a1.works.connect_to(&mut txn, b2_id) {
        Err(microrm::Error::ConstraintViolation(_)) => {
            // all good, this is what we expected
        },
        Err(e) => {
            panic!("Unexpected error while testing injective connection: {e}");
        },
        Ok(_) => {
            panic!("Unexpected success while testing injective connection");
        },
    }
}