[][src]Macro juniper_eager_loading::impl_load_from_for_diesel_mysql

macro_rules! impl_load_from_for_diesel_mysql {
    ( $($token:tt)* ) => { ... };
}

This macro will implement LoadFrom for Diesel models using the MySQL backend.

For more details see impl_load_from_for_diesel_pg.

Example usage

#[macro_use]
extern crate diesel;

use diesel::mysql::MysqlConnection;
use diesel::prelude::*;
use juniper_eager_loading::impl_load_from_for_diesel_mysql;

table! {
    users (id) {
        id -> Integer,
    }
}

table! {
    companies (id) {
        id -> Integer,
    }
}

table! {
    employments (id) {
        id -> Integer,
        user_id -> Integer,
        company_id -> Integer,
    }
}

#[derive(Queryable)]
struct User {
    id: i32,
}

#[derive(Queryable)]
struct Company {
    id: i32,
}

#[derive(Queryable)]
struct Employment {
    id: i32,
    user_id: i32,
    company_id: i32,
}

struct Context {
    db: MysqlConnection,
}

impl Context {
    // The macro assumes this method exists
    fn db(&self) -> &MysqlConnection {
        &self.db
    }
}

impl_load_from_for_diesel_mysql! {
    (
        error = diesel::result::Error,
        context = Context,
    ) => {
        i32 -> (users, User),
        i32 -> (companies, Company),
        i32 -> (employments, Employment),

        User.id -> (employments.user_id, Employment),
        Company.id -> (employments.company_id, Employment),

        Employment.company_id -> (companies.id, Company),
        Employment.user_id -> (users.id, User),
    }
}