lazybe 0.2.1

Handy CRUD boilerplate macros and utils for Rust backend
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
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
use axum::http::{Method, StatusCode};
use axum::{Json, Router};
use serde::de::DeserializeOwned;
use serde::{Deserialize, Serialize};
use sqlx::{Database, Pool};
use uuid::Uuid;

use crate::Entity;
use crate::db::DbOps;
use crate::filter::Filter;
use crate::page::{Page, PaginationInput};
use crate::sort::Sort;

/// A subset of properties outlined in
/// <https://www.rfc-editor.org/rfc/rfc9457#name-members-of-a-problem-detail>
#[derive(Debug, Clone, Serialize, Deserialize)]
#[cfg_attr(feature = "openapi", derive(utoipa::ToSchema))]
pub struct ErrorResponse {
    pub title: String,
    pub detail: Option<String>,
    pub instance: Option<String>,
}

impl ErrorResponse {
    pub fn new(title: &str) -> Self {
        Self {
            title: title.to_string(),
            detail: None,
            instance: None,
        }
    }

    pub fn with_detail(self, detail: &str) -> Self {
        Self {
            detail: Some(detail.to_string()),
            ..self
        }
    }

    pub fn with_instance(self, id: Uuid) -> Self {
        Self {
            instance: Some(id.to_string()),
            ..self
        }
    }
}

/// Defines how a collection API [`ListRouter`] behave
///
/// By default, the [`Entity`](crate::macros::Entity) macro generates a simple list collection API.
/// The list collection API returns a simple list containing all records and does not support
/// filtering, sorting or pagination.
///
/// # Example
///
/// You can also manually implement the [`EntityCollectionApi`], as different applications have
/// their own conventions, pagination methods, or may require custom syntax to support
/// sorting, filtering, and pagination logic.
///
/// ```
/// # use lazybe::filter::Filter;
/// # use lazybe::macros::Entity;
/// # use lazybe::page::{Page, PaginationInput};
/// # use lazybe::router::EntityCollectionApi;
/// # use lazybe::sort::Sort;
/// # use serde::{Deserialize, Serialize};
/// #[derive(Serialize, Entity)]
/// #[lazybe(table = "todo", endpoint = "/todos", collection_api = "manual")]
/// pub struct Todo {
///     #[lazybe(primary_key)]
///     pub id: i32,
///     pub title: String,
///     pub description: Option<String>,
///     pub is_completed: bool,
/// }
///
/// #[derive(Deserialize)]
/// pub struct TodoQuery {
///     page: Option<u32>,
///     completed: Option<bool>,
/// }
///
/// impl EntityCollectionApi for Todo {
///     type Resp = Vec<Todo>; // This could be a pagination result containing paging metadata
///     type Query = TodoQuery;
///
///     fn page_response(page: Page<Self>) -> Self::Resp {
///         page.data
///     }
///
///     fn page_input(input: &Self::Query) -> Option<PaginationInput> {
///         Some(PaginationInput {
///             // API uses 1-index, lazybe uses 0-index
///             page: input.page.map(|i| i.max(1)).unwrap_or(1) - 1,
///             limit: 100,
///         })
///     }
///
///     fn filter_input(input: &Self::Query) -> Filter<Self> {
///         // Apply filter only if provided explicitly in the query parameter
///         match input.completed {
///             Some(completed) => Filter::all([TodoFilter::is_completed().eq(completed)]),
///             None => Filter::empty(),
///         }
///     }
///
///     fn sort_input(_input: &Self::Query) -> Sort<Self> {
///         Sort::new([TodoSort::id().asc()])
///     }
/// }
/// ```
pub trait EntityCollectionApi: Sized {
    /// A collection response. Possibly a list or paginated data with some metadata.
    type Resp: Serialize;

    /// A shape of query parameter on a collection endpoint.
    /// These params are used to define pagination, filtering, and sorting.
    type Query: DeserializeOwned;

    /// Defines how a page should be translated into a collection response.
    fn page_response(page: Page<Self>) -> Self::Resp;

    /// Construct a pagination input from query parameters.
    /// Return `None` to list all records.
    fn page_input(input: &Self::Query) -> Option<PaginationInput>;

    /// Construct collection filter from query parameters
    fn filter_input(input: &Self::Query) -> Filter<Self>;

    /// Construct collection sorting from query parameters
    fn sort_input(input: &Self::Query) -> Sort<Self>;
}

/// A validation logic that gets called before and after database modifications.
///
/// When the validation returns error, the endpoint returns `400 BadRequest`
/// and the database transaction is rolled back.
///
/// # Example
///
/// ```
/// use chrono::{DateTime, Utc};
/// use lazybe::macros::Entity;
/// use lazybe::router::{ErrorResponse, ValidationHook};
/// use serde::{Deserialize, Serialize};
///
/// #[derive(Debug, PartialEq, Eq, Serialize, Deserialize, Entity)]
/// #[lazybe(table = "todo", endpoint = "/todos", validation = "manual")]
/// pub struct Todo {
///     #[lazybe(primary_key)]
///     pub id: i32,
///     pub title: String,
///     pub description: Option<String>,
///     pub is_completed: bool,
///     #[lazybe(created_at)]
///     pub created_at: DateTime<Utc>,
///     #[lazybe(updated_at)]
///     pub updated_at: DateTime<Utc>,
/// }
///
/// impl ValidationHook for Todo {
///     fn before_create(input: &Self::Create) -> Result<(), ErrorResponse> {
///         if input.title.len() > 100 {
///             Err(ErrorResponse {
///                 title: "Invalid todo title".to_string(),
///                 detail: Some("Title cannot be longer than 100 characters".to_string()),
///                 instance: None,
///             })?
///         }
///         Ok(())
///     }
/// }
/// ```
pub trait ValidationHook: Entity {
    #[allow(unused_variables)]
    fn before_create(input: &Self::Create) -> Result<(), ErrorResponse> {
        Ok(())
    }

    #[allow(unused_variables)]
    fn after_create(entity: &Self) -> Result<(), ErrorResponse> {
        Ok(())
    }

    #[allow(unused_variables)]
    fn before_update(pk: &Self::Pk, _input: &Self::Update) -> Result<(), ErrorResponse> {
        Ok(())
    }

    #[allow(unused_variables)]
    fn after_update(entity: &Self) -> Result<(), ErrorResponse> {
        Ok(())
    }
}

/// Describes a URL path for an [`Entity`]
pub trait Routable {
    /// A URL path for an entity with its ID (e.g. `/books/{id}`)
    fn entity_path() -> &'static str;
    /// A URL path for a collection of entity (e.g. `/books`)
    fn entity_collection_path() -> &'static str;
}

pub trait GetRouter<S, Db> {
    fn get_endpoint() -> Router<S>;
}

pub trait ListRouter<S, Db> {
    fn list_endpoint() -> Router<S>;
}

pub trait CreateRouter<S, Db> {
    fn create_endpoint() -> Router<S>;
}

pub trait UpdateRouter<S, Db> {
    fn update_endpoint() -> Router<S>;
    fn replace_endpoint() -> Router<S>;
}

pub trait DeleteRouter<S, Db> {
    fn delete_endpoint() -> Router<S>;
}

/// Describes how to extract a configuration from a shared [`axum`] state.
pub trait RouteConfig {
    type Ctx: DbOps<Self::Db>;
    type Db: Database;

    fn db_ctx(&self) -> (Self::Ctx, Pool<Self::Db>);
}

trait ResultExt<T> {
    fn map_err_500<U: Entity>(
        self,
        method: &Method,
        url: &str,
        msg: &str,
        id: Option<&<U as Entity>::Pk>,
    ) -> Result<T, (StatusCode, Json<ErrorResponse>)>;
}

impl<T, E: std::error::Error> ResultExt<T> for Result<T, E> {
    fn map_err_500<U: Entity>(
        self,
        method: &Method,
        url: &str,
        msg: &str,
        id: Option<&<U as Entity>::Pk>,
    ) -> Result<T, (StatusCode, Json<ErrorResponse>)> {
        self.map_err(|e| {
            let instance = Uuid::new_v4();
            let entity = U::entity_name();
            tracing::error!(?instance, ?method, ?url, ?entity, ?id, "{}: {}", msg, e);
            (
                StatusCode::INTERNAL_SERVER_ERROR,
                Json(
                    ErrorResponse::new("Internal server error")
                        .with_detail("Unknown error occurred, please check the logs for more details.")
                        .with_instance(instance),
                ),
            )
        })
    }
}

#[cfg(feature = "sqlite")]
#[doc(cfg(feature = "sqlite"))]
pub mod sqlite {
    super::macros::axum_route_impl_imports!();
    super::macros::axum_route_impl!(sqlx::Sqlite, crate::db::sqlite::SqliteDbCtx);
}

#[cfg(feature = "postgres")]
#[doc(cfg(feature = "postgres"))]
pub mod postgres {
    super::macros::axum_route_impl_imports!();
    super::macros::axum_route_impl!(sqlx::Postgres, crate::db::postgres::PostgresDbCtx);
}

mod macros {
    macro_rules! axum_route_impl_imports {
        () => {
            use axum::extract::{Path, Query, State};
            use axum::http::{Method, StatusCode};
            use axum::routing::{delete, get, patch, post, put};
            use axum::{Json, Router};
            use serde::Serialize;
            use serde::de::DeserializeOwned;

            use super::{
                CreateRouter, DeleteRouter, EntityCollectionApi, ErrorResponse, GetRouter, ListRouter, ResultExt,
                Routable, RouteConfig, UpdateRouter, ValidationHook,
            };
            use crate::Entity;
            use crate::db::DbOps;
            use crate::entity::ops::{CreateEntity, DeleteEntity, GetEntity, ListEntity, UpdateEntity};
        };
    }

    macro_rules! axum_route_impl {
        ($db_ty:ty, $ctx_ty:ty) => {
            type DbImpl = $db_ty;
            type CtxImpl = $ctx_ty;

            impl<T, S> GetRouter<S, DbImpl> for T
            where
                T: GetEntity<DbImpl> + Routable + Serialize + Send + 'static,
                S: RouteConfig<Ctx = CtxImpl, Db = DbImpl> + Clone + Send + Sync + 'static,
                <T as Entity>::Pk: DeserializeOwned + Send,
            {
                fn get_endpoint() -> Router<S> {
                    let route = <T as Routable>::entity_path();
                    Router::new().route(route, get(get_endpoint_impl::<T, S>))
                }
            }

            async fn get_endpoint_impl<T, S>(
                Path(id): Path<<T as Entity>::Pk>,
                State(state): State<S>,
            ) -> Result<Json<T>, (StatusCode, Json<ErrorResponse>)>
            where
                T: GetEntity<DbImpl> + Routable + Serialize + Send + 'static,
                S: RouteConfig<Ctx = CtxImpl, Db = DbImpl> + Clone + Send + 'static,
                <T as Entity>::Pk: DeserializeOwned + Send,
            {
                let (ctx, pool) = state.db_ctx();
                let method = Method::GET;
                let url = <T as Routable>::entity_path();
                let mut tx = pool.begin().await.map_err_500::<T>(
                    &method,
                    url,
                    "Failed to acquire a database transaction",
                    None,
                )?;
                let result = ctx
                    .get::<T>(&mut tx, id.clone())
                    .await
                    .map_err_500::<T>(&method, url, "Failed to get an entity from database", Some(&id))?
                    .ok_or((
                        StatusCode::NOT_FOUND,
                        Json(
                            ErrorResponse::new("Not found")
                                .with_detail(&format!("An entity with id {:?} was not found.", id)),
                        ),
                    ))?;
                tx.commit()
                    .await
                    .map_err_500::<T>(&method, url, "Failed to commit a transaction", None)?;
                Ok(Json(result))
            }

            impl<T, S> ListRouter<S, DbImpl> for T
            where
                T: ListEntity<DbImpl> + EntityCollectionApi + Routable + Serialize + Send + 'static,
                S: RouteConfig<Ctx = CtxImpl, Db = DbImpl> + Clone + Send + Sync + 'static,
                <T as EntityCollectionApi>::Query: Send,
            {
                fn list_endpoint() -> Router<S> {
                    let route = <T as Routable>::entity_collection_path();
                    Router::new().route(route, get(list_endpoint_impl::<T, S>))
                }
            }

            async fn list_endpoint_impl<T, S>(
                State(state): State<S>,
                Query(query): Query<<T as EntityCollectionApi>::Query>,
            ) -> Result<Json<<T as EntityCollectionApi>::Resp>, (StatusCode, Json<ErrorResponse>)>
            where
                T: ListEntity<DbImpl> + EntityCollectionApi + Routable + Serialize + Send + 'static,
                S: RouteConfig<Ctx = CtxImpl, Db = DbImpl> + Clone + Send + 'static,
                <T as EntityCollectionApi>::Query: Send,
            {
                let (ctx, pool) = state.db_ctx();
                let method = Method::GET;
                let url = <T as Routable>::entity_collection_path();
                let mut tx = pool.begin().await.map_err_500::<T>(
                    &method,
                    url,
                    "Failed to acquire a database transaction",
                    None,
                )?;
                let page_input = <T as EntityCollectionApi>::page_input(&query);
                let filter_input = <T as EntityCollectionApi>::filter_input(&query);
                let sort_input = <T as EntityCollectionApi>::sort_input(&query);
                let result = ctx
                    .list::<T>(&mut tx, filter_input, sort_input, page_input)
                    .await
                    .map_err_500::<T>(&Method::GET, url, "Failed to list entities from database", None)?;
                tx.commit()
                    .await
                    .map_err_500::<T>(&method, url, "Failed to commit a transaction", None)?;
                let page_resp = <T as EntityCollectionApi>::page_response(result);
                Ok(Json(page_resp))
            }

            impl<T, S> CreateRouter<S, DbImpl> for T
            where
                T: CreateEntity<DbImpl> + ValidationHook + Routable + Serialize + Send + 'static,
                S: RouteConfig<Ctx = CtxImpl, Db = DbImpl> + Clone + Send + Sync + 'static,
                <T as Entity>::Create: DeserializeOwned + Send,
            {
                fn create_endpoint() -> Router<S> {
                    let route = <T as Routable>::entity_collection_path();
                    Router::new().route(route, post(create_endpoint_impl::<T, S>))
                }
            }

            async fn create_endpoint_impl<T, S>(
                State(state): State<S>,
                Json(input): Json<<T as Entity>::Create>,
            ) -> Result<(StatusCode, Json<T>), (StatusCode, Json<ErrorResponse>)>
            where
                T: CreateEntity<DbImpl> + ValidationHook + Routable + Serialize + Send + 'static,
                S: RouteConfig<Ctx = CtxImpl, Db = DbImpl> + Clone + Send + Sync + 'static,
                <T as Entity>::Create: DeserializeOwned + Send,
            {
                let (ctx, pool) = state.db_ctx();
                let method = Method::POST;
                let url = <T as Routable>::entity_collection_path();
                let mut tx = pool.begin().await.map_err_500::<T>(
                    &method,
                    url,
                    "Failed to acquire a database transaction",
                    None,
                )?;

                <T as ValidationHook>::before_create(&input).map_err(|e| (StatusCode::BAD_REQUEST, Json(e)))?;
                let result = ctx.create::<T>(&mut tx, input).await.map_err_500::<T>(
                    &method,
                    url,
                    "Failed to create an entity in database",
                    None,
                )?;
                <T as ValidationHook>::after_create(&result).map_err(|e| (StatusCode::BAD_REQUEST, Json(e)))?;

                tx.commit()
                    .await
                    .map_err_500::<T>(&method, url, "Failed to commit a transaction", None)?;
                Ok((StatusCode::CREATED, Json(result)))
            }

            impl<T, S> DeleteRouter<S, DbImpl> for T
            where
                T: DeleteEntity<DbImpl> + Routable + Serialize + Send + 'static,
                S: RouteConfig<Ctx = CtxImpl, Db = DbImpl> + Clone + Send + Sync + 'static,
                <T as Entity>::Pk: DeserializeOwned + Send,
            {
                fn delete_endpoint() -> Router<S> {
                    let route = <T as Routable>::entity_path();
                    Router::new().route(route, delete(delete_endpoint_impl::<T, S>))
                }
            }

            async fn delete_endpoint_impl<T, S>(
                Path(id): Path<<T as Entity>::Pk>,
                State(state): State<S>,
            ) -> Result<Json<()>, (StatusCode, Json<ErrorResponse>)>
            where
                T: DeleteEntity<DbImpl> + Routable + Serialize + Send + 'static,
                S: RouteConfig<Ctx = CtxImpl, Db = DbImpl> + Clone + Send + 'static,
                <T as Entity>::Pk: DeserializeOwned + Send,
            {
                let (ctx, pool) = state.db_ctx();
                let method = Method::DELETE;
                let url = <T as Routable>::entity_path();
                let mut tx = pool.begin().await.map_err_500::<T>(
                    &method,
                    url,
                    "Failed to acquire a database transaction",
                    None,
                )?;
                ctx.delete::<T>(&mut tx, id.clone()).await.map_err_500::<T>(
                    &method,
                    url,
                    "Failed to delete an entity from database",
                    Some(&id),
                )?;
                tx.commit()
                    .await
                    .map_err_500::<T>(&method, url, "Failed to commit a transaction", None)?;
                Ok(Json(()))
            }

            impl<T, S> UpdateRouter<S, DbImpl> for T
            where
                T: UpdateEntity<DbImpl> + ValidationHook + Routable + Serialize + Send + 'static,
                S: RouteConfig<Ctx = CtxImpl, Db = DbImpl> + Clone + Send + Sync + 'static,
                <T as Entity>::Pk: DeserializeOwned + Send,
                <T as Entity>::Update: DeserializeOwned + Send,
                <T as Entity>::Replace: DeserializeOwned + Send,
            {
                fn replace_endpoint() -> Router<S> {
                    let route = <T as Routable>::entity_path();
                    Router::new().route(route, put(replace_endpoint_impl::<T, S>))
                }

                fn update_endpoint() -> Router<S> {
                    let route = <T as Routable>::entity_path();
                    Router::new().route(route, patch(update_endpoint_impl::<T, S>))
                }
            }

            async fn update_endpoint_impl<T, S>(
                Path(id): Path<<T as Entity>::Pk>,
                State(state): State<S>,
                Json(input): Json<<T as Entity>::Update>,
            ) -> Result<Json<T>, (StatusCode, Json<ErrorResponse>)>
            where
                T: UpdateEntity<DbImpl> + ValidationHook + Routable + Serialize + Send + 'static,
                S: RouteConfig<Ctx = CtxImpl, Db = DbImpl> + Clone + Send + 'static,
                <T as Entity>::Pk: DeserializeOwned + Send,
                <T as Entity>::Update: DeserializeOwned + Send,
            {
                update_endpoint_logic(Method::PATCH, id, state, input).await
            }

            async fn replace_endpoint_impl<T, S>(
                Path(id): Path<<T as Entity>::Pk>,
                State(state): State<S>,
                Json(input): Json<<T as Entity>::Replace>,
            ) -> Result<Json<T>, (StatusCode, Json<ErrorResponse>)>
            where
                T: UpdateEntity<DbImpl> + ValidationHook + Routable + Serialize + Send + 'static,
                S: RouteConfig<Ctx = CtxImpl, Db = DbImpl> + Clone + Send + 'static,
                <T as Entity>::Pk: DeserializeOwned + Send,
                <T as Entity>::Replace: DeserializeOwned,
                <T as Entity>::Update: Send,
            {
                update_endpoint_logic(Method::PUT, id, state, input.into()).await
            }

            async fn update_endpoint_logic<T, S>(
                method: Method,
                id: <T as Entity>::Pk,
                state: S,
                input: <T as Entity>::Update,
            ) -> Result<Json<T>, (StatusCode, Json<ErrorResponse>)>
            where
                T: UpdateEntity<DbImpl> + ValidationHook + Routable + Serialize + Send + 'static,
                S: RouteConfig<Ctx = CtxImpl, Db = DbImpl> + Clone + Send + 'static,
                <T as Entity>::Pk: Send,
                <T as Entity>::Update: Send,
            {
                let (ctx, pool) = state.db_ctx();
                let url = <T as Routable>::entity_path();
                let mut tx =
                    pool.begin()
                        .await
                        .map_err_500::<T>(&method, url, "Failed to acquire a transaction", Some(&id))?;

                <T as ValidationHook>::before_update(&id, &input).map_err(|e| (StatusCode::BAD_REQUEST, Json(e)))?;
                let result = ctx
                    .update::<T>(&mut tx, id.clone(), input)
                    .await
                    .map_err_500::<T>(&method, url, "Failed to update an entity in database", Some(&id))?
                    .ok_or((
                        StatusCode::NOT_FOUND,
                        Json(
                            ErrorResponse::new("Not found")
                                .with_detail(&format!("An entity with id {:?} was not found.", id)),
                        ),
                    ))?;
                <T as ValidationHook>::after_update(&result).map_err(|e| (StatusCode::BAD_REQUEST, Json(e)))?;

                tx.commit()
                    .await
                    .map_err_500::<T>(&method, url, "Failed to commit a transaction", Some(&id))?;
                Ok(Json(result))
            }
        };
    }

    pub(super) use {axum_route_impl, axum_route_impl_imports};
}