cast_core/lib.rs
1//! Cast — Eloquent-shaped ORM core on top of sqlx + sea-query.
2
3pub mod column;
4pub mod error;
5pub mod migration;
6pub mod model;
7pub mod paginator;
8pub mod pool;
9pub mod query;
10pub mod relation;
11pub mod schema;
12
13pub use column::{Column, ColumnRef};
14pub use error::{Error, Result};
15pub use migration::{Migration, MigrationRunner};
16pub use model::{Model, Loaded};
17pub use migration::MigrationStatus;
18pub use paginator::Paginator;
19pub use pool::{connect, Connection, ConnectionManager, Driver, Pool};
20pub use query::QueryBuilder;
21pub use relation::{BelongsTo, HasMany, HasOne, RelationDef, RelationKind};
22pub use schema::{ColumnDef, Schema, Table};
23
24pub use chrono;
25pub use sea_query;
26pub use sea_query_binder;
27pub use sqlx;
28pub use uuid;
29
30/// Define chainable Eloquent-style local scopes on a model's query builder.
31///
32/// Mirrors Laravel's `scopeActive($query)` / `scopePublished($query)` pattern.
33/// Generates a user-named trait and an `impl` for `QueryBuilder<Model>` so
34/// scopes chain directly: `User::query().active().verified().get(pool)`.
35///
36/// ## Usage
37///
38/// ```ignore
39/// use anvilforge::cast::scopes;
40///
41/// scopes!(UserScopes for User {
42/// fn active(q) -> q.where_eq(User::columns().active(), true);
43/// fn verified(q) -> q.where_not_null(User::columns().email_verified_at());
44/// fn older_than(q, days: i32) -> q.where_lt(User::columns().created_at(), cutoff(days));
45/// });
46///
47/// // In handler code:
48/// use crate::app::Models::UserScopes;
49/// let active_verified = User::query().active().verified().get(pool).await?;
50/// ```
51#[macro_export]
52macro_rules! scopes {
53 (
54 $trait_name:ident for $model:ty {
55 $(
56 fn $scope:ident ( $q:ident $(, $arg:ident : $arg_ty:ty )* )
57 -> $body:expr
58 );* $(;)?
59 }
60 ) => {
61 pub trait $trait_name {
62 $(
63 fn $scope(self $(, $arg: $arg_ty)*) -> Self;
64 )*
65 }
66
67 impl $trait_name for $crate::QueryBuilder<$model> {
68 $(
69 fn $scope(self $(, $arg: $arg_ty)*) -> Self {
70 let $q = self;
71 $body
72 }
73 )*
74 }
75 };
76}