From 729d09b2ec0750906c6d869ffdcb01ea0475d42f Mon Sep 17 00:00:00 2001 From: shray sharma Date: Sun, 17 Nov 2024 16:46:56 +0100 Subject: [PATCH 1/2] upd aliases --- examples/sqlite/relations/src/main.rs | 24 ++++++++++-------------- 1 file changed, 10 insertions(+), 14 deletions(-) diff --git a/examples/sqlite/relations/src/main.rs b/examples/sqlite/relations/src/main.rs index 658b5bab1cb6..fb3933bb536a 100644 --- a/examples/sqlite/relations/src/main.rs +++ b/examples/sqlite/relations/src/main.rs @@ -10,6 +10,8 @@ pub mod schema; use crate::model::*; use crate::schema::*; +type DbResult = Result>; + fn establish_connection() -> SqliteConnection { dotenv().ok(); @@ -18,10 +20,7 @@ fn establish_connection() -> SqliteConnection { .unwrap_or_else(|_| panic!("Error connecting to {database_url}")) } -fn new_author( - conn: &mut SqliteConnection, - name: &str, -) -> Result> { +fn new_author(conn: &mut SqliteConnection, name: &str) -> DbResult { let author = diesel::insert_into(authors::table) .values(authors::name.eq(name)) .returning(Author::as_returning()) @@ -29,10 +28,7 @@ fn new_author( Ok(author) } -fn new_book( - conn: &mut SqliteConnection, - title: &str, -) -> Result> { +fn new_book(conn: &mut SqliteConnection, title: &str) -> DbResult { let book = diesel::insert_into(books::table) .values(books::title.eq(title)) .returning(Book::as_returning()) @@ -44,7 +40,7 @@ fn new_books_author( conn: &mut SqliteConnection, book_id: i32, author_id: i32, -) -> Result> { +) -> DbResult { let book_author = diesel::insert_into(books_authors::table) .values(( books_authors::book_id.eq(book_id), @@ -60,7 +56,7 @@ fn new_page( page_number: i32, content: &str, book_id: i32, -) -> Result> { +) -> DbResult { let page = diesel::insert_into(pages::table) .values(( pages::page_number.eq(page_number), @@ -72,7 +68,7 @@ fn new_page( Ok(page) } -fn joins(conn: &mut SqliteConnection) -> Result<(), Box> { +fn joins(conn: &mut SqliteConnection) -> DbResult<()> { let page_with_book = pages::table .inner_join(books::table) .filter(books::title.eq("Momo")) @@ -90,7 +86,7 @@ fn joins(conn: &mut SqliteConnection) -> Result<(), Box Ok(()) } -fn one_to_n_relations(conn: &mut SqliteConnection) -> Result<(), Box> { +fn one_to_n_relations(conn: &mut SqliteConnection) -> DbResult<()> { let momo = books::table .filter(books::title.eq("Momo")) .select(Book::as_select()) @@ -123,7 +119,7 @@ fn one_to_n_relations(conn: &mut SqliteConnection) -> Result<(), Box Result<(), Box> { +fn m_to_n_relations(conn: &mut SqliteConnection) -> DbResult<()> { let astrid_lindgren = authors::table .filter(authors::name.eq("Astrid Lindgren")) .select(Author::as_select()) @@ -178,7 +174,7 @@ fn main() -> Result<(), Box> { Ok(()) } -fn setup_data(conn: &mut SqliteConnection) -> Result<(), Box> { +fn setup_data(conn: &mut SqliteConnection) -> DbResult<()> { // create a book let momo = new_book(conn, "Momo")?; From 9e671f09392e978cf675954135ebfb1e325f0bb0 Mon Sep 17 00:00:00 2001 From: shray sharma Date: Sun, 17 Nov 2024 16:48:15 +0100 Subject: [PATCH 2/2] upd aliases --- examples/postgres/relations/src/main.rs | 22 ++++++++++------------ 1 file changed, 10 insertions(+), 12 deletions(-) diff --git a/examples/postgres/relations/src/main.rs b/examples/postgres/relations/src/main.rs index 906ddce52796..f15e1fe6a80b 100644 --- a/examples/postgres/relations/src/main.rs +++ b/examples/postgres/relations/src/main.rs @@ -10,6 +10,8 @@ pub mod schema; use crate::model::*; use crate::schema::*; +type DbResult = Result>; + fn establish_connection() -> PgConnection { dotenv().ok(); @@ -18,7 +20,7 @@ fn establish_connection() -> PgConnection { .unwrap_or_else(|_| panic!("Error connecting to {database_url}")) } -fn new_author(conn: &mut PgConnection, name: &str) -> Result> { +fn new_author(conn: &mut PgConnection, name: &str) -> DbResult { let author = diesel::insert_into(authors::table) .values(authors::name.eq(name)) .returning(Author::as_returning()) @@ -26,7 +28,7 @@ fn new_author(conn: &mut PgConnection, name: &str) -> Result Result> { +fn new_book(conn: &mut PgConnection, title: &str) -> DbResult { let book = diesel::insert_into(books::table) .values(books::title.eq(title)) .returning(Book::as_returning()) @@ -34,11 +36,7 @@ fn new_book(conn: &mut PgConnection, title: &str) -> Result Result> { +fn new_books_author(conn: &mut PgConnection, book_id: i32, author_id: i32) -> DbResult { let book_author = diesel::insert_into(books_authors::table) .values(( books_authors::book_id.eq(book_id), @@ -54,7 +52,7 @@ fn new_page( page_number: i32, content: &str, book_id: i32, -) -> Result> { +) -> DbResult { let page = diesel::insert_into(pages::table) .values(( pages::page_number.eq(page_number), @@ -66,7 +64,7 @@ fn new_page( Ok(page) } -fn joins(conn: &mut PgConnection) -> Result<(), Box> { +fn joins(conn: &mut PgConnection) -> DbResult<()> { let page_with_book = pages::table .inner_join(books::table) .filter(books::title.eq("Momo")) @@ -84,7 +82,7 @@ fn joins(conn: &mut PgConnection) -> Result<(), Box> { Ok(()) } -fn one_to_n_relations(conn: &mut PgConnection) -> Result<(), Box> { +fn one_to_n_relations(conn: &mut PgConnection) -> DbResult<()> { let momo = books::table .filter(books::title.eq("Momo")) .select(Book::as_select()) @@ -117,7 +115,7 @@ fn one_to_n_relations(conn: &mut PgConnection) -> Result<(), Box Result<(), Box> { +fn m_to_n_relations(conn: &mut PgConnection) -> DbResult<()> { let astrid_lindgren = authors::table .filter(authors::name.eq("Astrid Lindgren")) .select(Author::as_select()) @@ -172,7 +170,7 @@ fn main() -> Result<(), Box> { Ok(()) } -fn setup_data(conn: &mut PgConnection) -> Result<(), Box> { +fn setup_data(conn: &mut PgConnection) -> DbResult<()> { // create a book let momo = new_book(conn, "Momo")?;