Skip to content

Commit

Permalink
Switch examples/todo to use single_model_file generation
Browse files Browse the repository at this point in the history
  • Loading branch information
Wulf committed Jan 2, 2024
1 parent b979408 commit cec637f
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 6 deletions.
2 changes: 1 addition & 1 deletion examples/todo/src/dsync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
7 changes: 6 additions & 1 deletion examples/todo/src/models/common.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
/* @generated and managed by dsync */
/// Result of a `.paginate` function
#[derive(Debug, )]
pub struct PaginationResult<T> {
/// Resulting items that are from the current page
pub items: Vec<T>,
/// 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,
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<chrono::Utc>,
/// Field representing column `updated_at`
pub updated_at: chrono::DateTime<chrono::Utc>,
}

/// 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<chrono::Utc>,
/// Field representing column `updated_at`
pub updated_at: chrono::DateTime<chrono::Utc>,
}

#[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<String>,
/// Field representing column `completed`
pub completed: Option<bool>,
/// Field representing column `created_at`
pub created_at: Option<chrono::DateTime<chrono::Utc>>,
/// Field representing column `updated_at`
pub updated_at: Option<chrono::DateTime<chrono::Utc>>,
}

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

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

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

Expand All @@ -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<Self> {
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<usize> {
use crate::schema::todo::dsl::*;

Expand Down
2 changes: 0 additions & 2 deletions examples/todo/src/models/todo/mod.rs

This file was deleted.

0 comments on commit cec637f

Please sign in to comment.