luxid-orm 0.1.2

The Lucid data layer for the Luxid web framework
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
//! Eloquent-flavoured model operations over SeaORM entities.

use luxid_core::error::{Error, Result};
use sea_orm::{
    ActiveModelBehavior, ActiveModelTrait, ColumnTrait, EntityTrait, FromQueryResult,
    IntoActiveModel, Order, PaginatorTrait, PrimaryKeyTrait, QueryFilter, QueryOrder, QuerySelect,
    Select, Value,
};
use serde::Serialize;

use crate::db;
use crate::hooks::Hooks;
use crate::relations::Relations;
use crate::with_connection;
use luxid_core::middleware::BoxFuture;

/// The primary-key value type of an entity.
pub type PrimaryKeyOf<E> = <<E as EntityTrait>::PrimaryKey as PrimaryKeyTrait>::ValueType;

pub(crate) fn database_error(err: sea_orm::DbErr) -> Error {
    Error::internal(format!("database error: {err}"))
}

/// A column a query can filter or order by.
///
/// Two kinds implement this. The entity's own `Column` enum carries
/// `Value = sea_orm::Value`, so it accepts anything — the escape hatch.
/// `#[derive(Model)]` also generates a zero-sized type per field whose `Value`
/// is that field's actual Rust type, so `where_eq(User::team_id, "abc")` fails
/// to compile where the enum form would only fail at runtime.
pub trait ColumnRef<E: EntityTrait> {
    /// What this column may be compared against.
    type Value;

    fn column(&self) -> E::Column;
}

/// A page of results. Serializes in the shape Laravel's paginator produces, so
/// existing clients need no translation.
#[derive(Debug, Clone, PartialEq, Serialize)]
pub struct Paginated<T> {
    pub data: Vec<T>,
    pub page: u64,
    pub per_page: u64,
    pub total: u64,
    pub last_page: u64,
}

impl<T> Paginated<T> {
    pub fn is_empty(&self) -> bool {
        self.data.is_empty()
    }

    pub fn len(&self) -> usize {
        self.data.len()
    }

    pub fn has_more(&self) -> bool {
        self.page < self.last_page
    }
}

/// A query under construction.
/// An eager-load step, captured when `.with(..)` is called so the rest of the
/// query API needs no `Relatable` bound.
type Loader<M> = Box<dyn for<'a> Fn(&'a mut Vec<M>) -> BoxFuture<'a, Result<()>> + Send + Sync>;

pub struct Query<E: EntityTrait> {
    select: Select<E>,
    loaders: Vec<Loader<E::Model>>,
}

impl<E> Query<E>
where
    E: EntityTrait,
    E::Model: FromQueryResult + Send + Sync,
{
    pub fn new(select: Select<E>) -> Self {
        Self {
            select,
            loaders: Vec::new(),
        }
    }

    /// Escape hatch: reach the underlying SeaORM `Select` for anything Lucid
    /// does not express.
    pub fn into_inner(self) -> Select<E> {
        self.select
    }

    #[must_use]
    pub fn where_eq<C, V>(mut self, column: C, value: V) -> Self
    where
        C: ColumnRef<E>,
        V: Into<C::Value>,
        C::Value: Into<Value>,
    {
        let target = column.column();
        self.select = self.select.filter(target.eq(value.into().into()));
        self
    }

    #[must_use]
    pub fn where_ne<C, V>(mut self, column: C, value: V) -> Self
    where
        C: ColumnRef<E>,
        V: Into<C::Value>,
        C::Value: Into<Value>,
    {
        let target = column.column();
        self.select = self.select.filter(target.ne(value.into().into()));
        self
    }

    #[must_use]
    pub fn where_gt<C, V>(mut self, column: C, value: V) -> Self
    where
        C: ColumnRef<E>,
        V: Into<C::Value>,
        C::Value: Into<Value>,
    {
        let target = column.column();
        self.select = self.select.filter(target.gt(value.into().into()));
        self
    }

    #[must_use]
    pub fn where_lt<C, V>(mut self, column: C, value: V) -> Self
    where
        C: ColumnRef<E>,
        V: Into<C::Value>,
        C::Value: Into<Value>,
    {
        let target = column.column();
        self.select = self.select.filter(target.lt(value.into().into()));
        self
    }

    #[must_use]
    pub fn where_in<C, V>(mut self, column: C, values: impl IntoIterator<Item = V>) -> Self
    where
        C: ColumnRef<E>,
        V: Into<C::Value>,
        C::Value: Into<Value>,
    {
        let target = column.column();
        let values: Vec<Value> = values.into_iter().map(|v| v.into().into()).collect();

        self.select = self.select.filter(target.is_in(values));
        self
    }

    #[must_use]
    pub fn where_like<C>(mut self, column: C, pattern: impl Into<String>) -> Self
    where
        C: ColumnRef<E>,
    {
        let target = column.column();
        self.select = self.select.filter(target.like(pattern.into()));
        self
    }

    #[must_use]
    pub fn where_null<C: ColumnRef<E>>(mut self, column: C) -> Self {
        let target = column.column();
        self.select = self.select.filter(target.is_null());
        self
    }

    #[must_use]
    pub fn where_not_null<C: ColumnRef<E>>(mut self, column: C) -> Self {
        let target = column.column();
        self.select = self.select.filter(target.is_not_null());
        self
    }

    #[must_use]
    pub fn order_by_asc<C: ColumnRef<E>>(mut self, column: C) -> Self {
        let target = column.column();
        self.select = self.select.order_by(target, Order::Asc);
        self
    }

    #[must_use]
    pub fn order_by_desc<C: ColumnRef<E>>(mut self, column: C) -> Self {
        let target = column.column();
        self.select = self.select.order_by(target, Order::Desc);
        self
    }

    #[must_use]
    pub fn limit(mut self, limit: u64) -> Self {
        self.select = self.select.limit(limit);
        self
    }

    #[must_use]
    pub fn offset(mut self, offset: u64) -> Self {
        self.select = self.select.offset(offset);
        self
    }

    pub async fn all(self) -> Result<Vec<E::Model>> {
        let Self { select, loaders } = self;
        let handle = db::current()?;

        let mut rows = with_connection!(handle, |conn| select
            .all(conn)
            .await
            .map_err(database_error))?;

        load_all(&loaders, &mut rows).await?;
        Ok(rows)
    }

    pub async fn first(self) -> Result<Option<E::Model>> {
        let Self { select, loaders } = self;
        let handle = db::current()?;

        let found = with_connection!(handle, |conn| select
            .one(conn)
            .await
            .map_err(database_error))?;

        let Some(row) = found else {
            return Ok(None);
        };

        // Relations load in batches, so a single row goes through the same
        // path as a page of them.
        let mut rows = vec![row];
        load_all(&loaders, &mut rows).await?;

        Ok(rows.pop())
    }

    /// The first row, or a 404 naming the model.
    pub async fn first_or_fail(self) -> Result<E::Model>
    where
        E::Model: Lucid<Entity = E>,
    {
        self.first()
            .await?
            .ok_or_else(|| Error::not_found(<E::Model as Lucid>::MODEL, "?"))
    }

    pub async fn count(self) -> Result<u64> {
        let handle = db::current()?;
        let select = self.select;

        with_connection!(handle, |conn| select
            .count(conn)
            .await
            .map_err(database_error))
    }

    pub async fn exists(self) -> Result<bool> {
        Ok(self.count().await? > 0)
    }

    /// A page of results. `page` is 1-based, matching Laravel and every URL
    /// users will ever type.
    pub async fn paginate(self, page: u64, per_page: u64) -> Result<Paginated<E::Model>> {
        let per_page = per_page.max(1);
        let page = page.max(1);

        let handle = db::current()?;

        let Self { select, loaders } = self;

        let (mut data, counts) = with_connection!(handle, |conn| {
            let paginator = select.paginate(conn, per_page);

            let counts = paginator
                .num_items_and_pages()
                .await
                .map_err(database_error)?;
            let data = paginator
                .fetch_page(page - 1)
                .await
                .map_err(database_error)?;

            Ok::<_, Error>((data, counts))
        })?;

        load_all(&loaders, &mut data).await?;

        Ok(Paginated {
            data,
            page,
            per_page,
            total: counts.number_of_items,
            last_page: counts.number_of_pages.max(1),
        })
    }
}

impl<E> Query<E>
where
    E: EntityTrait,
    E::Model: Relatable + FromQueryResult + Send + Sync,
{
    /// Eager-load a relation declared by `#[luxid::model]`.
    ///
    /// Loading happens in one batched query per relation after the parents are
    /// fetched, which is what makes this an N+1 fix rather than a rename of
    /// one.
    #[must_use]
    pub fn with(mut self, name: impl Into<String>) -> Self {
        let name = name.into();

        self.loaders.push(Box::new(move |rows| {
            E::Model::load_relation(name.clone(), rows)
        }));
        self
    }
}

async fn load_all<M>(loaders: &[Loader<M>], rows: &mut Vec<M>) -> Result<()> {
    if rows.is_empty() {
        return Ok(());
    }

    for loader in loaders {
        loader(rows).await?;
    }
    Ok(())
}

/// A model that declares relations. Implemented by `#[luxid::model]`.
pub trait Relatable: Lucid {
    fn relations(&self) -> &Relations;

    fn relations_mut(&mut self) -> &mut Relations;

    /// Load `name` onto every parent, in one batched query.
    fn load_relation(name: String, parents: &mut Vec<Self>) -> BoxFuture<'_, Result<()>>;
}

/// Model operations, implemented by `#[derive(Model)]`.
pub trait Lucid: FromQueryResult + Send + Sync + Sized + 'static {
    type Entity: EntityTrait<Model = Self>;

    /// Name used in 404s and diagnostics.
    const MODEL: &'static str;

    fn query() -> Query<Self::Entity> {
        Query::new(Self::Entity::find())
    }

    fn find(id: PrimaryKeyOf<Self::Entity>) -> impl Future<Output = Result<Option<Self>>> + Send {
        async move {
            let handle = db::current()?;
            let select = Self::Entity::find_by_id(id);

            with_connection!(handle, |conn| select
                .one(conn)
                .await
                .map_err(database_error))
        }
    }

    /// The row, or a 404 carrying the model name and id — the line that keeps
    /// controller actions free of error handling.
    fn find_or_fail(id: PrimaryKeyOf<Self::Entity>) -> impl Future<Output = Result<Self>> + Send
    where
        PrimaryKeyOf<Self::Entity>: Clone + std::fmt::Display,
    {
        async move {
            let requested = id.clone();

            Self::find(id)
                .await?
                .ok_or_else(|| Error::not_found(Self::MODEL, requested))
        }
    }

    fn all() -> impl Future<Output = Result<Vec<Self>>> + Send {
        async move { Self::query().all().await }
    }

    fn count_all() -> impl Future<Output = Result<u64>> + Send {
        async move { Self::query().count().await }
    }
}

/// Insert an active model and return the stored row, running the model's
/// create hooks around the write.
pub async fn insert<A>(active: A) -> Result<<A::Entity as EntityTrait>::Model>
where
    A: ActiveModelTrait + ActiveModelBehavior + Send,
    <A::Entity as EntityTrait>::Model: IntoActiveModel<A> + Hooks<Active = A>,
{
    type Model<A> = <<A as ActiveModelTrait>::Entity as EntityTrait>::Model;

    let mut active = active;

    Model::<A>::before_save(&mut active).await?;
    Model::<A>::before_create(&mut active).await?;

    let handle = db::current()?;
    let stored = with_connection!(handle, |conn| active
        .insert(conn)
        .await
        .map_err(database_error))?;

    Model::<A>::after_create(&stored).await?;
    Model::<A>::after_save(&stored).await?;

    Ok(stored)
}

/// Persist changes to an active model and return the stored row, running the
/// model's update hooks around the write.
pub async fn update<A>(active: A) -> Result<<A::Entity as EntityTrait>::Model>
where
    A: ActiveModelTrait + ActiveModelBehavior + Send,
    <A::Entity as EntityTrait>::Model: IntoActiveModel<A> + Hooks<Active = A>,
{
    type Model<A> = <<A as ActiveModelTrait>::Entity as EntityTrait>::Model;

    let mut active = active;

    Model::<A>::before_save(&mut active).await?;
    Model::<A>::before_update(&mut active).await?;

    let handle = db::current()?;
    let stored = with_connection!(handle, |conn| active
        .update(conn)
        .await
        .map_err(database_error))?;

    Model::<A>::after_update(&stored).await?;
    Model::<A>::after_save(&stored).await?;

    Ok(stored)
}

/// Insert without running hooks.
///
/// Named for what it costs you. Reach for it in seeders and fixtures where
/// hooks would be wrong, never in application code.
pub async fn insert_without_hooks<A>(active: A) -> Result<<A::Entity as EntityTrait>::Model>
where
    A: ActiveModelTrait + ActiveModelBehavior + Send,
    <A::Entity as EntityTrait>::Model: IntoActiveModel<A>,
{
    let handle = db::current()?;
    with_connection!(handle, |conn| active
        .insert(conn)
        .await
        .map_err(database_error))
}

/// Delete a row by primary key. Returns whether anything was removed.
pub async fn delete_by_id<E: EntityTrait>(id: PrimaryKeyOf<E>) -> Result<bool> {
    let handle = db::current()?;

    with_connection!(handle, |conn| {
        E::delete_by_id(id)
            .exec(conn)
            .await
            .map(|outcome| outcome.rows_affected > 0)
            .map_err(database_error)
    })
}