New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Added MongoDB example #2535
base: main
Are you sure you want to change the base?
Added MongoDB example #2535
Conversation
examples/mongodb/src/main.rs
Outdated
// defining Member type | ||
#[derive(Debug, Deserialize, Serialize)] | ||
struct Member { | ||
_id: u32, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why the _
?
_id: u32, | |
id: u32, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe this is because MongoDB uses the _id
field as primary key.
Maybe it could be #[serde(rename = "_id")] id: u32
to adhere to Rust's convention?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed in 3cd9ded.
examples/mongodb/src/main.rs
Outdated
Json(input): Json<Member>, | ||
) -> impl IntoResponse { | ||
let result = db.insert_one(input, None).await.unwrap(); | ||
println!("{:?}", result); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure we need all this print. Same goes for the ones below.
println!("{:?}", result); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed in 3cd9ded.
examples/mongodb/src/main.rs
Outdated
async fn create_member( | ||
State(db): State<Collection<Member>>, | ||
Json(input): Json<Member>, | ||
) -> impl IntoResponse { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We generally don't recommend returning impl IntoResponse
, see https://docs.rs/axum/latest/axum/response/index.html#regarding-impl-intoresponse.
So I think instead we should write the full type.
Would also be good to not unwrap
inside the handler and instead return a Result
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed in 3cd9ded.
Hello! ^^
Motivation
Recently, I've been trying to create an API service in
Rust
to communicate with MongoDB usingaxum
. Adding an example would be nice for future developers.Solution
examples/mongodb
.