From 0c60ec4b1c1a4b792b2f67156e897e84c4a369f9 Mon Sep 17 00:00:00 2001 From: eraden Date: Sun, 8 Oct 2023 07:20:11 +0200 Subject: [PATCH] Insert defaul with 1 query --- crates/oswilno-parking-space/src/lib.rs | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/crates/oswilno-parking-space/src/lib.rs b/crates/oswilno-parking-space/src/lib.rs index a0d3c19..a6e6e68 100644 --- a/crates/oswilno-parking-space/src/lib.rs +++ b/crates/oswilno-parking-space/src/lib.rs @@ -296,7 +296,8 @@ async fn create( } async fn ensure_locations(db: Arc) { - for (name, number, stage) in [ + use oswilno_contract::parking_space_locations::{ActiveModel,Entity}; + let records: Vec<_> = [ ("Wilno", 1, "1a"), ("Wilno", 1, "1b"), ("Wilno", 1, "2AF1"), @@ -318,17 +319,15 @@ async fn ensure_locations(db: Arc) { // 5 ("Wilno", 5, "1"), ("Wilno", 5, "2"), - ] { - let _model = oswilno_contract::parking_space_locations::ActiveModel { + ].into_iter().map(|(name, number, stage)| + oswilno_contract::parking_space_locations::ActiveModel { name: Set(name.to_string()), number: Set(number), stage: Set(stage.to_string()), ..Default::default() } - .save(&*db) - .await - .ok(); - } + ).collect(); + Entity::insert_many(records).exec(&*db).await.ok(); } #[get("/edit/{id}")]