Skip to content

Commit cec637f

Browse files
committed
Switch examples/todo to use single_model_file generation
1 parent b979408 commit cec637f

File tree

4 files changed

+30
-6
lines changed

4 files changed

+30
-6
lines changed

examples/todo/src/dsync.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ fn main() {
1212
models_dir.as_path(),
1313
GenerationConfig {
1414
connection_type: "diesel::pg::PgConnection".into(),
15-
default_table_options: TableOptions::default().disable_serde(),
15+
default_table_options: TableOptions::default().disable_serde().single_model_file(),
1616
model_path: "crate::models::".into(),
1717
schema_path: "crate::schema::".into(),
1818
once_common_structs: true,

examples/todo/src/models/common.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
11
/* @generated and managed by dsync */
2+
/// Result of a `.paginate` function
23
#[derive(Debug, )]
34
pub struct PaginationResult<T> {
5+
/// Resulting items that are from the current page
46
pub items: Vec<T>,
7+
/// The count of total items there are
58
pub total_items: i64,
6-
/// 0-based index
9+
/// Current page, 0-based index
710
pub page: i64,
11+
/// Size of a page
812
pub page_size: i64,
13+
/// Number of total possible pages, given the `page_size` and `total_items`
914
pub num_pages: i64,
1015
}
1116

examples/todo/src/models/todo/generated.rs renamed to examples/todo/src/models/todo.rs

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,42 +5,61 @@ use crate::schema::*;
55
use diesel::QueryResult;
66
use crate::models::common::*;
77

8-
#[derive(Debug, Clone, Queryable, Selectable)]
8+
/// Struct representing a row in table `todo`
9+
#[derive(Debug, Clone, Queryable, Selectable, QueryableByName)]
910
#[diesel(table_name=todo, primary_key(id))]
1011
pub struct Todo {
12+
/// Field representing column `id`
1113
pub id: i32,
14+
/// Field representing column `text`
1215
pub text: String,
16+
/// Field representing column `completed`
1317
pub completed: bool,
18+
/// Field representing column `created_at`
1419
pub created_at: chrono::DateTime<chrono::Utc>,
20+
/// Field representing column `updated_at`
1521
pub updated_at: chrono::DateTime<chrono::Utc>,
1622
}
1723

24+
/// Create Struct for a row in table `todo` for [`Todo`]
1825
#[derive(Debug, Clone, Insertable)]
1926
#[diesel(table_name=todo)]
2027
pub struct CreateTodo {
28+
/// Field representing column `id`
2129
pub id: i32,
30+
/// Field representing column `text`
2231
pub text: String,
32+
/// Field representing column `completed`
2333
pub completed: bool,
34+
/// Field representing column `created_at`
2435
pub created_at: chrono::DateTime<chrono::Utc>,
36+
/// Field representing column `updated_at`
2537
pub updated_at: chrono::DateTime<chrono::Utc>,
2638
}
2739

28-
#[derive(Debug, Clone, AsChangeset, Default)]
40+
/// Update Struct for a row in table `todo` for [`Todo`]
41+
#[derive(Debug, Clone, AsChangeset, PartialEq, Default)]
2942
#[diesel(table_name=todo)]
3043
pub struct UpdateTodo {
44+
/// Field representing column `text`
3145
pub text: Option<String>,
46+
/// Field representing column `completed`
3247
pub completed: Option<bool>,
48+
/// Field representing column `created_at`
3349
pub created_at: Option<chrono::DateTime<chrono::Utc>>,
50+
/// Field representing column `updated_at`
3451
pub updated_at: Option<chrono::DateTime<chrono::Utc>>,
3552
}
3653

3754
impl Todo {
55+
/// Insert a new row into `todo` with a given [`CreateTodo`]
3856
pub fn create(db: &mut ConnectionType, item: &CreateTodo) -> QueryResult<Self> {
3957
use crate::schema::todo::dsl::*;
4058

4159
insert_into(todo).values(item).get_result::<Self>(db)
4260
}
4361

62+
/// Get a row from `todo`, identified by the primary key
4463
pub fn read(db: &mut ConnectionType, param_id: i32) -> QueryResult<Self> {
4564
use crate::schema::todo::dsl::*;
4665

@@ -65,12 +84,14 @@ impl Todo {
6584
})
6685
}
6786

87+
/// Update a row in `todo`, identified by the primary key with [`UpdateTodo`]
6888
pub fn update(db: &mut ConnectionType, param_id: i32, item: &UpdateTodo) -> QueryResult<Self> {
6989
use crate::schema::todo::dsl::*;
7090

7191
diesel::update(todo.filter(id.eq(param_id))).set(item).get_result(db)
7292
}
7393

94+
/// Delete a row in `todo`, identified by the primary key
7495
pub fn delete(db: &mut ConnectionType, param_id: i32) -> QueryResult<usize> {
7596
use crate::schema::todo::dsl::*;
7697

examples/todo/src/models/todo/mod.rs

Lines changed: 0 additions & 2 deletions
This file was deleted.

0 commit comments

Comments
 (0)