Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions examples/kitchen-sink/examples/api_pagination.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,8 @@ pub struct Book {
#[derive(Debug, Clone, Serialize, Deserialize, ToSchema)]
pub struct PaginatedBook {
pub page: u32,
pub page_size: u64,
pub count: u64,
pub page_size: u32,
pub count: u32,
pub data: Vec<Book>,
}

Expand Down
2 changes: 1 addition & 1 deletion flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -145,8 +145,8 @@
watchexec
which
# rust
cargo-edit
cargo-expand
cargo-outdated
rust
])
++ (builtins.attrValues scripts);
Expand Down
44 changes: 16 additions & 28 deletions lazybe-macros/src/entity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,22 +172,15 @@ fn expand_struct(input: &DeriveInput, data_struct: &DataStruct) -> syn::Result<T
match &data_struct.fields {
Fields::Named(fields_named) => {
let entity_meta = EntityMeta::try_parse(input, fields_named)?;
let entity_types_impl = entity_types_impl(&entity_meta);
let entity_row_impl = entity_row_impl(&entity_meta);
let entity_entity_trait_impl = entity_entity_trait_impl(&entity_meta);
let entity_query_trait_impl = entity_query_trait_impl(&entity_meta);
let entity_route_trait_impl = entity_route_trait_impl(&entity_meta);
let entity_collection_api_trait_impl = entity_collection_api_trait_impl(&entity_meta);
let entity_validation_hook_trait_impl = entity_validation_hook_trait_impl(&entity_meta);
Ok(quote! {
#entity_types_impl
#entity_row_impl
#entity_entity_trait_impl
#entity_query_trait_impl
#entity_route_trait_impl
#entity_collection_api_trait_impl
#entity_validation_hook_trait_impl
})
let mut ts = TokenStream::new();
ts.extend(entity_types_impl(&entity_meta));
ts.extend(entity_row_impl(&entity_meta));
ts.extend(entity_entity_trait_impl(&entity_meta));
ts.extend(entity_query_trait_impl(&entity_meta));
ts.extend(entity_route_trait_impl(&entity_meta));
ts.extend(entity_collection_api_trait_impl(&entity_meta));
ts.extend(entity_validation_hook_trait_impl(&entity_meta));
Ok(ts)
}
Fields::Unnamed(_) => Err(syn::Error::new_spanned(
&input.ident,
Expand Down Expand Up @@ -401,18 +394,13 @@ fn entity_entity_trait_impl(entity_meta: &EntityMeta) -> TokenStream {
}

fn entity_query_trait_impl(entity_meta: &EntityMeta) -> TokenStream {
let get_query_trait_impl = get_query_trait_impl(entity_meta);
let list_query_trait_impl = list_query_trait_impl(entity_meta);
let create_query_trait_impl = create_query_trait_impl(entity_meta);
let update_query_trait_impl = update_query_trait_impl(entity_meta);
let delete_query_trait_impl = delete_query_trait_impl(entity_meta);
quote! {
#get_query_trait_impl
#list_query_trait_impl
#create_query_trait_impl
#update_query_trait_impl
#delete_query_trait_impl
}
let mut ts = TokenStream::new();
ts.extend(get_query_trait_impl(entity_meta));
ts.extend(list_query_trait_impl(entity_meta));
ts.extend(create_query_trait_impl(entity_meta));
ts.extend(update_query_trait_impl(entity_meta));
ts.extend(delete_query_trait_impl(entity_meta));
ts
}

fn create_query_trait_impl(entity_meta: &EntityMeta) -> TokenStream {
Expand Down
6 changes: 3 additions & 3 deletions lazybe-macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@ pub fn derive_newtype(input: TokenStream) -> TokenStream {
///
/// # Field attributes
/// - `primary_key` - Specify the field to be used as primary key.
/// - `generate_with = "..."` - A function use for generating an ID. If omitted, ID should be generate by the database.
/// - `created_at` - Speicify the field for created_at timestamp. The time is stamped once a record is created.
/// - `updated_at` - Speicify the field for updated_at timestamp. The time is stamped once a record is updated.
/// - `generate_with = "..."` - A function use for generating an ID. If omitted, ID should be generated by the database.
/// - `created_at` - Specify the field for created_at timestamp. The time is stamped once a record is created.
/// - `updated_at` - Specify the field for updated_at timestamp. The time is stamped once a record is updated.
/// - `json` - The field should be encoded as JSON column.
pub fn derive_entity(input: TokenStream) -> TokenStream {
let input = parse_macro_input!(input as DeriveInput);
Expand Down
2 changes: 1 addition & 1 deletion lazybe/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "lazybe"
description = "Derive boring CRUD API boilerplate for Rust backend"
description = "Derive boring CRUD boilerplate for Rust backend"
version.workspace = true
edition.workspace = true
authors.workspace = true
Expand Down
4 changes: 2 additions & 2 deletions lazybe/src/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ pub trait DbCtx<Db> {
fn query_builder(&self) -> Self::Qb;
}

/// Integraion with sqlite database
/// Integration with sqlite database
#[cfg(feature = "sqlite")]
#[doc(cfg(feature = "sqlite"))]
pub mod sqlite {
Expand All @@ -34,7 +34,7 @@ pub mod sqlite {
}
}

/// Integraion with postgres database
/// Integration with postgres database
#[cfg(feature = "postgres")]
#[doc(cfg(feature = "postgres"))]
pub mod postgres {
Expand Down
5 changes: 3 additions & 2 deletions lazybe/src/entity/ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,8 +153,9 @@ where

let count_result: CountResult = sqlx::query_as(&count_query).fetch_one(tx.deref_mut()).await?;

let total_records: u32 = count_result.count.try_into().expect("record count does not fit in u32");
let mut page: u32 = 0;
let mut page_size: u64 = count_result.count.unsigned_abs();
let mut page_size: u32 = total_records;
if let Some(p) = pagination {
page = p.page;
page_size = p.limit.into();
Expand All @@ -163,7 +164,7 @@ where
let result = Page {
page,
page_size,
total_records: count_result.count.unsigned_abs(),
total_records,
data: data_result.into_iter().map(|i| i.into()).collect(),
};
Ok(result)
Expand Down
2 changes: 1 addition & 1 deletion lazybe/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
//! Much of this work tends to be boilerplate, with little domain logic or custom code.
//!
//! If you are already using:
//! - [`axum`] for HTTP interface
//! - [`axum`](::axum) for HTTP interface
//! - [`sqlx`] for database interactions
//! - [`utoipa`] for OpenAPI docuementation
//!
Expand Down
4 changes: 2 additions & 2 deletions lazybe/src/page.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ impl PaginationInput {
pub struct Page<T> {
/// 0-index page number (0 is the first page)
pub page: u32,
pub page_size: u64,
pub total_records: u64,
pub page_size: u32,
pub total_records: u32,
pub data: Vec<T>,
}