From 6bd1d72313c5fc66eb978e995ee205c313be66b7 Mon Sep 17 00:00:00 2001 From: theMackabu Date: Mon, 18 Dec 2023 11:50:49 -0800 Subject: [PATCH] revise mongo functions --- src/main.rs | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/src/main.rs b/src/main.rs index 3a5ea74..d50554e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -21,7 +21,7 @@ use rhai_url::UrlPackage; use mongodb::{ bson::{doc, Document}, - results::{CollectionSpecification, DeleteResult, InsertManyResult, InsertOneResult, UpdateResult}, + results::{CollectionSpecification, DeleteResult, InsertOneResult, UpdateResult}, sync::{Client as MongoClient, Collection, Cursor, Database}, }; @@ -368,7 +368,7 @@ mod mongo_db { match Arc::into_inner(cursor) { Some(cursor) => match cursor.collect() { Ok(items) => to_dynamic::(items), - Err(err) => to_dynamic::(err.to_string()), + Err(err) => Err(err.to_string().into()), }, None => to_dynamic::(vec![]), } @@ -397,15 +397,15 @@ mod mongo_db { pub fn insert_one(collection: Collection, map: Dynamic) -> Result> { match collection.insert_one(map.into_map(), None) { Ok(res) => to_dynamic::(res), - Err(err) => to_dynamic::(err.to_string()), + Err(err) => Err(err.to_string().into()), } } #[rhai_fn(global, return_raw, name = "insert")] - pub fn insert_many(collection: Collection, map: Array) -> Result> { + pub fn insert_many(collection: Collection, map: Array) -> Result> { match collection.insert_many(map.into_vec(), None) { - Ok(res) => to_dynamic::(res), - Err(err) => to_dynamic::(err.to_string()), + Ok(res) => Ok(res.inserted_ids.into_iter().map(|(_, value)| to_dynamic(value).unwrap()).collect::()), + Err(err) => Err(err.to_string().into()), } } @@ -413,7 +413,7 @@ mod mongo_db { pub fn delete(collection: Collection, map: Dynamic) -> Result> { match collection.delete_one(map.into_doc(), None) { Ok(res) => to_dynamic::(res), - Err(err) => to_dynamic::(err.to_string()), + Err(err) => Err(err.to_string().into()), } } @@ -421,7 +421,7 @@ mod mongo_db { pub fn delete_many(collection: Collection, map: Dynamic) -> Result> { match collection.delete_many(map.into_doc(), None) { Ok(res) => to_dynamic::(res), - Err(err) => to_dynamic::(err.to_string()), + Err(err) => Err(err.to_string().into()), } } @@ -430,7 +430,7 @@ mod mongo_db { let replacement: MongoDynamic = replacement.into(); match collection.replace_one(query.into_doc(), replacement, None) { Ok(res) => to_dynamic::(res), - Err(err) => to_dynamic::(err.to_string()), + Err(err) => Err(err.to_string().into()), } } } -- GitLab