diff --git a/examples/todo/src/dsync.rs b/examples/todo/src/dsync.rs index 271046e6..83e2625b 100644 --- a/examples/todo/src/dsync.rs +++ b/examples/todo/src/dsync.rs @@ -12,7 +12,7 @@ fn main() { models_dir.as_path(), GenerationConfig { connection_type: "diesel::pg::PgConnection".into(), - default_table_options: TableOptions::default().disable_serde(), + default_table_options: TableOptions::default().disable_serde().single_model_file(), model_path: "crate::models::".into(), schema_path: "crate::schema::".into(), once_common_structs: true, diff --git a/examples/todo/src/models/common.rs b/examples/todo/src/models/common.rs index edf3fda2..d8db5d1d 100644 --- a/examples/todo/src/models/common.rs +++ b/examples/todo/src/models/common.rs @@ -1,11 +1,16 @@ /* @generated and managed by dsync */ +/// Result of a `.paginate` function #[derive(Debug, )] pub struct PaginationResult { + /// Resulting items that are from the current page pub items: Vec, + /// The count of total items there are pub total_items: i64, - /// 0-based index + /// Current page, 0-based index pub page: i64, + /// Size of a page pub page_size: i64, + /// Number of total possible pages, given the `page_size` and `total_items` pub num_pages: i64, } diff --git a/examples/todo/src/models/todo/generated.rs b/examples/todo/src/models/todo.rs similarity index 67% rename from examples/todo/src/models/todo/generated.rs rename to examples/todo/src/models/todo.rs index bef667df..2077106e 100644 --- a/examples/todo/src/models/todo/generated.rs +++ b/examples/todo/src/models/todo.rs @@ -5,42 +5,61 @@ use crate::schema::*; use diesel::QueryResult; use crate::models::common::*; -#[derive(Debug, Clone, Queryable, Selectable)] +/// Struct representing a row in table `todo` +#[derive(Debug, Clone, Queryable, Selectable, QueryableByName)] #[diesel(table_name=todo, primary_key(id))] pub struct Todo { + /// Field representing column `id` pub id: i32, + /// Field representing column `text` pub text: String, + /// Field representing column `completed` pub completed: bool, + /// Field representing column `created_at` pub created_at: chrono::DateTime, + /// Field representing column `updated_at` pub updated_at: chrono::DateTime, } +/// Create Struct for a row in table `todo` for [`Todo`] #[derive(Debug, Clone, Insertable)] #[diesel(table_name=todo)] pub struct CreateTodo { + /// Field representing column `id` pub id: i32, + /// Field representing column `text` pub text: String, + /// Field representing column `completed` pub completed: bool, + /// Field representing column `created_at` pub created_at: chrono::DateTime, + /// Field representing column `updated_at` pub updated_at: chrono::DateTime, } -#[derive(Debug, Clone, AsChangeset, Default)] +/// Update Struct for a row in table `todo` for [`Todo`] +#[derive(Debug, Clone, AsChangeset, PartialEq, Default)] #[diesel(table_name=todo)] pub struct UpdateTodo { + /// Field representing column `text` pub text: Option, + /// Field representing column `completed` pub completed: Option, + /// Field representing column `created_at` pub created_at: Option>, + /// Field representing column `updated_at` pub updated_at: Option>, } impl Todo { + /// Insert a new row into `todo` with a given [`CreateTodo`] pub fn create(db: &mut ConnectionType, item: &CreateTodo) -> QueryResult { use crate::schema::todo::dsl::*; insert_into(todo).values(item).get_result::(db) } + /// Get a row from `todo`, identified by the primary key pub fn read(db: &mut ConnectionType, param_id: i32) -> QueryResult { use crate::schema::todo::dsl::*; @@ -65,12 +84,14 @@ impl Todo { }) } + /// Update a row in `todo`, identified by the primary key with [`UpdateTodo`] pub fn update(db: &mut ConnectionType, param_id: i32, item: &UpdateTodo) -> QueryResult { use crate::schema::todo::dsl::*; diesel::update(todo.filter(id.eq(param_id))).set(item).get_result(db) } + /// Delete a row in `todo`, identified by the primary key pub fn delete(db: &mut ConnectionType, param_id: i32) -> QueryResult { use crate::schema::todo::dsl::*; diff --git a/examples/todo/src/models/todo/mod.rs b/examples/todo/src/models/todo/mod.rs deleted file mode 100644 index 136e9a24..00000000 --- a/examples/todo/src/models/todo/mod.rs +++ /dev/null @@ -1,2 +0,0 @@ -pub mod generated; -pub use generated::*;