shared-framework 0.0.17

Reusable building blocks for HTTP services — Hyper routing, SeaORM data layer, validation, OpenAPI docs, jobs, queues, cache.
Documentation
//! Data persistence layer built on SeaORM 2.
//!
//! Provides entity identity and auditing traits, a query builder with cursor
//! pagination, a generic repository for CRUD and paginated reads, explicit
//! relation hydration, key-value caches, database seeding, connection setup,
//! and shared result types.
//!
//! Key types: [`base::BaseEntity`] and [`base::BaseAuditableEntity`] for models,
//! [`query::QueryData`] and [`query::RepositoryOptions`] for reads,
//! [`repository::PersistentRepository`] for database access,
//! [`traverse`] for loading relations, [`cache`] for caching,
//! [`seed`] for idempotent seeders, and [`types_extra`] for result wrappers.
//!
//! Use [`repository::PersistentRepository`] as the entry point for most
//! database work; reach for the submodules directly for setup or advanced use.
//!
//!

pub mod base;
pub mod cache;
pub mod connectors;
pub mod query;
pub mod repository;
pub mod seed;
pub mod types_extra;

pub use base::{BaseAuditableEntity, BaseEntity};
pub use query::{DeleteQueryData, NoUser, PageResult, QueryData, RepositoryOptions};
pub use repository::PersistentRepository;
pub use seed::{DatabaseSeeder, DatabaseSeederRunner, entity as seeder_entity};
pub use types_extra::{
    ChangeResultModel, EntityProjection, PaginatedResult as PaginatedResultAlias, Position,
};

/// A macro to define an enum that can be used as a SeaORM ActiveEnum with string values. This is done this way precisely because SeaORM does
/// not support enums with string values out of the box, and this macro provides a convenient way to define such enums with the necessary traits
/// and methods for working with them in a SeaORM context.
#[macro_export]
macro_rules! active_enum {
    ($name:ident, $pg_name:literal, $( $(#[$meta:meta])* $variant:ident => $value:literal),+ $(,)?) => {
        #[derive(Copy, Clone, Debug, PartialEq, Eq, schemars::JsonSchema, sea_orm::entity::prelude::EnumIter, sea_orm::entity::prelude::DeriveActiveEnum, serde::Serialize, serde::Deserialize)]
        #[sea_orm(rs_type = "String", db_type = "Enum", enum_name = $pg_name, rename_all= "SCREAMING_SNAKE_CASE")]    
        #[serde(rename_all = "SCREAMING_SNAKE_CASE")]
		#[doc = concat!("An enum representing a database type that is best represented as an enumeration of string values which is named `", stringify!($name), "`.")]
		pub enum $name {
            $(
                $(#[$meta])*
				$variant,
            )+
        }

        impl $name {
			#[doc = concat!("Returns a vector of all variants of the `", stringify!($name), "` enum.")]
            pub fn variants() -> Vec<Self> {
				vec![
                    $(Self::$variant),+
                ]
            }

			#[doc = concat!("Returns the string value associated with the `", stringify!($name), "` enum variant.")]
            pub fn value(&self) -> String {
                match self {
                    $(Self::$variant => $value.to_string()),+
                }
            }

			#[doc = concat!("Returns an `Option<", stringify!($name), ">` corresponding to the provided string value. If the value does not match any variant, `None` is returned.")]
            pub fn from_value(value: String) -> Option<Self> {
                Self::variants().into_iter().find(|variant| variant.value() == value)
            }
        }
    };
}