From 2b275e8ed15a2577b5eeea21fb6c863aec86a1e2 Mon Sep 17 00:00:00 2001 From: Alex Ted Date: Tue, 21 Jan 2025 20:41:27 +0300 Subject: [PATCH] chore: updated from main --- Cargo.lock | 78 +++++++++++++++++++++++++++ databases/diesel-async/src/actions.rs | 34 ++++++------ databases/diesel-async/src/main.rs | 28 +++++----- databases/diesel-async/src/models.rs | 4 +- 4 files changed, 109 insertions(+), 35 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index ced37c0f..6986b512 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1692,6 +1692,18 @@ dependencies = [ "log", ] +[[package]] +name = "bb8" +version = "0.8.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d89aabfae550a5c44b43ab941844ffcd2e993cb6900b342debf59e9ea74acdb8" +dependencies = [ + "async-trait", + "futures-util", + "parking_lot", + "tokio", +] + [[package]] name = "bigdecimal" version = "0.3.1" @@ -2584,6 +2596,18 @@ dependencies = [ "uuid", ] +[[package]] +name = "db-diesel-async" +version = "1.0.0" +dependencies = [ + "actix-web", + "diesel", + "diesel-async", + "dotenvy", + "serde", + "uuid", +] + [[package]] name = "db-mongo" version = "1.0.0" @@ -2811,6 +2835,21 @@ dependencies = [ "uuid", ] +[[package]] +name = "diesel-async" +version = "0.5.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "51a307ac00f7c23f526a04a77761a0519b9f0eb2838ebf5b905a58580095bdcb" +dependencies = [ + "async-trait", + "bb8", + "diesel", + "futures-util", + "scoped-futures", + "tokio", + "tokio-postgres", +] + [[package]] name = "diesel_derives" version = "2.2.3" @@ -4340,6 +4379,17 @@ dependencies = [ "libc", ] +[[package]] +name = "inotify" +version = "0.9.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f8069d3ec154eb856955c1c0fbffefbf5f3c40a104ec912d4797314c1801abff" +dependencies = [ + "bitflags 1.3.2", + "inotify-sys", + "libc", +] + [[package]] name = "inotify" version = "0.11.0" @@ -5439,6 +5489,25 @@ version = "0.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "38bf9645c8b145698bb0b18a4637dcacbc421ea49bef2317e4fd8065a387cf21" +[[package]] +name = "notify" +version = "6.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6205bd8bb1e454ad2e27422015fb5e4f2bcc7e08fa8f27058670d208324a4d2d" +dependencies = [ + "bitflags 2.6.0", + "crossbeam-channel", + "filetime", + "fsevent-sys", + "inotify 0.9.6", + "kqueue", + "libc", + "log", + "mio 0.8.11", + "walkdir", + "windows-sys 0.48.0", +] + [[package]] name = "notify" version = "6.1.1" @@ -7090,6 +7159,15 @@ dependencies = [ "parking_lot", ] +[[package]] +name = "scoped-futures" +version = "0.1.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1b24aae2d0636530f359e9d5ef0c04669d11c5e756699b27a6a6d845d8329091" +dependencies = [ + "pin-project-lite", +] + [[package]] name = "scopeguard" version = "1.2.0" diff --git a/databases/diesel-async/src/actions.rs b/databases/diesel-async/src/actions.rs index 303d6284..17ecb31d 100644 --- a/databases/diesel-async/src/actions.rs +++ b/databases/diesel-async/src/actions.rs @@ -27,27 +27,27 @@ pub async fn find_item_by_uid( Ok(item) } - /// Run query using Diesel to insert a new database row and return the result. +/// Run query using Diesel to insert a new database row and return the result. pub async fn insert_new_item( conn: &mut AsyncPgConnection, nm: &str, // prevent collision with `name` column imported inside the function ) -> Result { - // It is common when using Diesel with Actix Web to import schema-related - // modules inside a function's scope (rather than the normal module's scope) - // to prevent import collisions and namespace pollution. - use crate::schema::items::dsl::*; - - let new_item = models::Item { - id: Uuid::new_v7(Timestamp::now(NoContext)), - name: nm.to_owned(), + // It is common when using Diesel with Actix Web to import schema-related + // modules inside a function's scope (rather than the normal module's scope) + // to prevent import collisions and namespace pollution. + use crate::schema::items::dsl::*; + + let new_item = models::Item { + id: Uuid::new_v7(Timestamp::now(NoContext)), + name: nm.to_owned(), }; - + let item = diesel::insert_into(items) - .values(&new_item) - .returning(models::Item::as_returning()) - .get_result(conn) - .await - .expect("Error inserting person"); - + .values(&new_item) + .returning(models::Item::as_returning()) + .get_result(conn) + .await + .expect("Error inserting person"); + Ok(item) -} \ No newline at end of file +} diff --git a/databases/diesel-async/src/main.rs b/databases/diesel-async/src/main.rs index cd69324b..84d36691 100644 --- a/databases/diesel-async/src/main.rs +++ b/databases/diesel-async/src/main.rs @@ -1,19 +1,16 @@ #[macro_use] extern crate diesel; -use std::env::VarError; - use actix_web::{error, get, post, web, App, HttpResponse, HttpServer, Responder}; -use diesel_async::pooled_connection::{bb8::Pool, AsyncDieselConnectionManager, PoolError}; +use diesel_async::pooled_connection::{bb8::Pool, AsyncDieselConnectionManager}; use diesel_async::AsyncPgConnection; use dotenvy::dotenv; use std::{env, io}; -use thiserror::Error as ThisError; use uuid::Uuid; -pub mod actions; -pub mod models; -pub mod schema; +mod actions; +mod models; +mod schema; type DbPool = Pool; @@ -35,9 +32,9 @@ async fn get_item( .expect("Couldn't get db connection from the pool"); let item = actions::find_item_by_uid(&mut conn, item_uid) - .await - // map diesel query errors to a 500 error response - .map_err(error::ErrorInternalServerError)?; + .await + // map diesel query errors to a 500 error response + .map_err(error::ErrorInternalServerError)?; Ok(match item { // item was found; return 200 response with JSON formatted item object @@ -58,16 +55,15 @@ async fn add_item( pool: web::Data, form: web::Json, ) -> actix_web::Result { - let mut conn = pool .get() .await .expect("Couldn't get db connection from the pool"); let item = actions::insert_new_item(&mut conn, &form.name) - .await - // map diesel query errors to a 500 error response - .map_err(error::ErrorInternalServerError)?; + .await + // map diesel query errors to a 500 error response + .map_err(error::ErrorInternalServerError)?; // item was added successfully; return 201 response with new item info Ok(HttpResponse::Created().json(item)) @@ -76,7 +72,7 @@ async fn add_item( #[actix_web::main] async fn main() -> io::Result<()> { dotenv().ok(); - + let db_url = env::var("DATABASE_URL").expect("Env var `DATABASE_URL` not set"); let mgr = AsyncDieselConnectionManager::::new(db_url); @@ -88,7 +84,7 @@ async fn main() -> io::Result<()> { .service(add_item) .service(get_item) }) - .bind(("127.0.0.1", 5000))? + .bind(("127.0.0.1", 8080))? .run() .await } diff --git a/databases/diesel-async/src/models.rs b/databases/diesel-async/src/models.rs index e8cc61bf..6d08565f 100644 --- a/databases/diesel-async/src/models.rs +++ b/databases/diesel-async/src/models.rs @@ -1,6 +1,6 @@ +use super::schema::items; use serde::{Deserialize, Serialize}; use uuid::Uuid; -use super::schema::items; /// Item details. #[derive(Debug, Clone, Serialize, Deserialize, Queryable, Selectable, Insertable)] @@ -20,6 +20,6 @@ impl NewItem { /// Constructs new item details from name. #[cfg(test)] // only needed in tests pub fn new(name: impl Into) -> Self { - Self { name: name.into(), ..Default::default() } + Self { name: name.into() } } }