Macro repository

Source
macro_rules! repository {
    {
        $( #[$meta:meta] )*
        $vis:vis $ident:ident;
    } => { ... };
    {
        $( #[$meta:meta] )*
        $vis:vis $ident:ident<$model:ty>;
    } => { ... };
    {
        $( #[$meta:meta] )*
        $vis:vis $ident:ident<$model:ty>;

        $($tokens:tt)*
    } => { ... };
    {
        !inner
        $( #[$meta:meta] )*
        $vis:vis $ident:ident<$model:ty>;

        $($tokens:tt)*
    } => { ... };
    {
        !zst
        $( #[$meta:meta] )*
        $vis:vis $ident:ident;
    } => { ... };
    {
        !zst
        $( #[$meta:meta] )*
        $vis:vis $ident:ident<$model:ty>;
    } => { ... };
    {
        !zst
        $( #[$meta:meta] )*
        $vis:vis $ident:ident<$model:ty>;

        $($tokens:tt)*
    } => { ... };
    {
        !zst !inner
        $( #[$meta:meta] )*
        $vis:vis $ident:ident<$model:ty>;

        $($tokens:tt)*
    } => { ... };
}
Expand description

Creates a new database repository, either just creates a basic new type and statics to interact with the main database pool.

If a database model is provided it will also try to implement the crate::traits::Repository trait.

For non ZST repositories it will implement Deref, Borrow, and AsRef to get the inner pool.

§Examples

use sqlx_utils::repository;
use sqlx_utils::traits::Model;


struct Person {
    id: String,
    name: String
}

impl Model for Person {
    type Id = String;

    fn get_id(&self) -> Option<Self::Id> {
        Some(self.id.clone())
    }
}

repository!{
    PersonRepository<Person>;
}

§Zero Sized Type Repository

It is possible to make a repository that is zero sized by never storing the reference to the database pool, this will add a slight cost however, whenever we want to use the pool() method we now need to access the DB_POOL static via the get_db_pool(). This cost however is tiny and in most cases not an issue as it will be overshadowed by the actual database request.

§Example of a ZST Repository




repository!{
    !zst
    PersonRepository<Person>; // The generated type `PersonRepository` will now have size of 0
}