-
-
Notifications
You must be signed in to change notification settings - Fork 95
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
715ca63
commit ff72aa4
Showing
12 changed files
with
152 additions
and
65 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
use crate::*; | ||
use bson::Document; | ||
use serde_json::Value; | ||
|
||
#[derive(Deserialize, Debug)] | ||
#[serde(rename_all = "camelCase")] | ||
struct InsertManyArgs { | ||
db_name: String, | ||
collection_name: String, | ||
docs: Vec<Value>, | ||
} | ||
|
||
#[derive(Deserialize, Debug)] | ||
#[serde(rename_all = "camelCase")] | ||
struct InsertOneArgs { | ||
db_name: String, | ||
collection_name: String, | ||
doc: Value, | ||
} | ||
|
||
pub fn insert_one(command: Command) -> CoreOp { | ||
let fut = async move { | ||
let client = command.get_client(); | ||
let data = command.data; | ||
let args: InsertOneArgs = serde_json::from_slice(data.unwrap().as_ref()).unwrap(); | ||
let db_name = args.db_name; | ||
let collection_name = args.collection_name; | ||
let doc = util::json_to_document(args.doc).expect("doc canot be null"); | ||
let database = client.database(&db_name); | ||
let collection = database.collection(&collection_name); | ||
|
||
let insert_result = collection.insert_one(doc, None).unwrap(); | ||
Ok(util::async_result(&command.args, insert_result.inserted_id)) | ||
}; | ||
CoreOp::Async(fut.boxed()) | ||
} | ||
|
||
pub fn insert_many(command: Command) -> CoreOp { | ||
let fut = async move { | ||
let client = command.get_client(); | ||
let data = command.data; | ||
let args: InsertManyArgs = serde_json::from_slice(data.unwrap().as_ref()).unwrap(); | ||
let db_name = args.db_name; | ||
let collection_name = args.collection_name; | ||
let docs: Vec<Document> = util::jsons_to_documents(args.docs); | ||
|
||
let database = client.database(&db_name); | ||
let collection = database.collection(&collection_name); | ||
|
||
let insert_result = collection.insert_many(docs, None).unwrap(); | ||
let ids: Vec<bson::Bson> = insert_result | ||
.inserted_ids | ||
.iter() | ||
.map(|(_, id)| id.to_owned()) | ||
.collect(); | ||
|
||
Ok(util::async_result(&command.args, ids)) | ||
}; | ||
CoreOp::Async(fut.boxed()) | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,13 @@ | ||
mod connect; | ||
mod delete; | ||
mod find_one; | ||
mod insert_one; | ||
mod insert; | ||
mod list_collection_names; | ||
mod list_database_names; | ||
|
||
pub use connect::{connect_with_options, connect_with_uri}; | ||
pub use delete::delete; | ||
pub use find_one::find_one; | ||
pub use insert_one::insert_one; | ||
pub use insert::{insert_many, insert_one}; | ||
pub use list_collection_names::list_collection_names; | ||
pub use list_database_names::list_database_names; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters