-
-
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.
Merge pull request #3 from manyuanrong/curd
Basic curd
- Loading branch information
Showing
12 changed files
with
228 additions
and
54 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
export { ClientOptions, MongoClient } from "./ts/client.ts"; | ||
export { Database } from "./ts/database.ts"; | ||
export { init } from "./ts/util.ts"; | ||
export const VERSION = "v0.3.0"; |
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,42 @@ | ||
use crate::*; | ||
use bson::Document; | ||
use mongodb::error::Result; | ||
use serde_json::Value; | ||
use util::maybe_json_to_document; | ||
|
||
#[derive(Deserialize, Debug)] | ||
#[serde(rename_all = "camelCase")] | ||
struct FindArgs { | ||
db_name: String, | ||
collection_name: String, | ||
filter: Option<Value>, | ||
find_one: bool, | ||
} | ||
|
||
pub fn find(command: Command) -> CoreOp { | ||
let fut = async move { | ||
let client = command.get_client(); | ||
let data = command.data; | ||
let args: FindArgs = serde_json::from_slice(data.unwrap().as_ref()).unwrap(); | ||
let db_name = args.db_name; | ||
let collection_name = args.collection_name; | ||
let filter = maybe_json_to_document(args.filter); | ||
let database = client.database(&db_name); | ||
let collection = database.collection(&collection_name); | ||
|
||
if args.find_one { | ||
let doc = collection.find_one(filter, None).unwrap(); | ||
Ok(util::async_result(&command.args, doc)) | ||
} else { | ||
let cursor = collection.find(filter, None).unwrap(); | ||
let docs: Vec<Document> = cursor | ||
.filter_map(|doc: Result<Document>| match doc { | ||
Ok(doc) => Some(doc), | ||
_ => None, | ||
}) | ||
.collect(); | ||
Ok(util::async_result(&command.args, docs)) | ||
} | ||
}; | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,15 @@ | ||
mod connect; | ||
mod delete; | ||
mod find_one; | ||
mod find; | ||
mod insert; | ||
mod list_collection_names; | ||
mod list_database_names; | ||
mod update; | ||
|
||
pub use connect::{connect_with_options, connect_with_uri}; | ||
pub use delete::delete; | ||
pub use find_one::find_one; | ||
pub use find::find; | ||
pub use insert::{insert_many, insert_one}; | ||
pub use list_collection_names::list_collection_names; | ||
pub use list_database_names::list_database_names; | ||
pub use update::update; |
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,50 @@ | ||
use crate::*; | ||
use serde_json::Value; | ||
|
||
#[derive(Deserialize, Debug)] | ||
#[serde(rename_all = "camelCase")] | ||
struct UpdateArgs { | ||
db_name: String, | ||
collection_name: String, | ||
query: Value, | ||
update: Value, | ||
update_one: bool, | ||
} | ||
|
||
#[derive(Serialize, Debug)] | ||
#[serde(rename_all = "camelCase")] | ||
struct UpdateResultArgs { | ||
pub matched_count: i64, | ||
pub modified_count: i64, | ||
pub upserted_id: Option<Value>, | ||
} | ||
|
||
pub fn update(command: Command) -> CoreOp { | ||
let fut = async move { | ||
let client = command.get_client(); | ||
let data = command.data; | ||
let args: UpdateArgs = serde_json::from_slice(data.unwrap().as_ref()).unwrap(); | ||
let db_name = args.db_name; | ||
let collection_name = args.collection_name; | ||
let query_doc = util::json_to_document(args.query).expect("query canot be null"); | ||
let update_doc = util::json_to_document(args.update).expect("update_doc canot be null"); | ||
let database = client.database(&db_name); | ||
let collection = database.collection(&collection_name); | ||
|
||
let result = if args.update_one { | ||
collection.update_one(query_doc, update_doc, None).unwrap() | ||
} else { | ||
collection.update_many(query_doc, update_doc, None).unwrap() | ||
}; | ||
|
||
Ok(util::async_result( | ||
&command.args, | ||
UpdateResultArgs { | ||
matched_count: result.matched_count, | ||
modified_count: result.modified_count, | ||
upserted_id: result.upserted_id.map(|id| id.into()), | ||
}, | ||
)) | ||
}; | ||
CoreOp::Async(fut.boxed()) | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
export interface UpdateResult { | ||
matchedCount: number; | ||
modifiedCount: number; | ||
upsertedId: Object | null; | ||
} |
Oops, something went wrong.