thedodd / wither

An ODM for MongoDB built on the official MongoDB Rust driver.
https://docs.rs/wither
Other
324 stars 40 forks source link

Expecting one working cargo project for example. #55

Closed amber-m-o-r-e closed 4 years ago

amber-m-o-r-e commented 4 years ago

Compiled Readme.md code against following toml dependencies and getting buch of errors.

Code

// First, we add import statements for the crates that we need.
// In Rust 2018, `extern crate` declarations will no longer be needed.
#[macro_use]
extern crate mongodb;
extern crate serde;
#[macro_use(Serialize, Deserialize)]
extern crate serde_derive;
extern crate wither;
#[macro_use(Model)]
extern crate wither_derive;

// Next we bring a few types into scope for our example.
use mongodb::{
    Client, ThreadedClient,
    db::{Database, ThreadedDatabase},
    coll::options::IndexModel,
    oid::ObjectId,
};
use wither::prelude::*;

// Now we define our model. Simple as deriving a few traits.
#[derive(Model, Serialize, Deserialize)]
struct User {
    /// The ID of the model.
    #[serde(rename="_id", skip_serializing_if="Option::is_none")]
    pub id: Option<ObjectId>,

    /// This field has a unique index on it.
    #[model(index(index="dsc", unique="true"))]
    pub email: String,
}

fn main() {
    // Create a user.
    let db = mongodb::Client::with_uri("mongodb://localhost:27017/").unwrap().db("mydb");
    let mut me = User{id: None, email: "my.email@example.com".to_string()};
    me.save(db.clone(), None);

    // Update user's email address.
    me.update(db.clone(), None, doc!{"$set": doc!{"email": "new.email@example.com"}}, None).unwrap();

    // Fetch all users.
    let all_users = User::find(db.clone(), None, None).unwrap();
}

toml dependencies

[dependencies]
futures = "0.3.5"
serde = "1.0.114"
serde_derive = "1.0.114"
wither = "0.8.0"
wither_derive = "0.8.0"
[dependencies.mongodb]
version = "1.0.0"
default-features = false
features = ["sync"]

Errors

error[E0432]: unresolved imports `mongodb::ThreadedClient`, `mongodb::db::ThreadedDatabase`, `mongodb::coll::options::IndexModel`, `mongodb::oid`
  --> src/main.rs:14:13
   |
14 |     Client, ThreadedClient,
   |             ^^^^^^^^^^^^^^ no `ThreadedClient` in the root
15 |     db::{Database, ThreadedDatabase},
   |                    ^^^^^^^^^^^^^^^^ no `ThreadedDatabase` in `db`
16 |     coll::options::IndexModel,
   |     ^^^^^^^^^^^^^^^^^^^^^^^^^ no `IndexModel` in `coll::options`
17 |     oid::ObjectId,
   |     ^^^ help: a similar path exists: `wither::mongodb::oid`

error[E0432]: unresolved import `mongodb`
  --> src/main.rs:22:10
   |
22 | #[derive(Model, Serialize, Deserialize)]
   |          ^^^^^ no `IndexModel` in `coll::options`
   |
   = note: this error originates in a derive macro (in Nightly builds, run with -Z macro-backtrace for more info)

error: cannot find macro `doc` in this scope
  --> src/main.rs:22:10
   |
22 | #[derive(Model, Serialize, Deserialize)]
   |          ^^^^^
   |
   = note: this error originates in a derive macro (in Nightly builds, run with -Z macro-backtrace for more info)

error: cannot find macro `doc` in this scope
  --> src/main.rs:40:33
   |
40 |     me.update(db.clone(), None, doc!{"$set": doc!{"email": "new.email@example.com"}}, None).unwrap();
   |                                 ^^^

error[E0433]: failed to resolve: could not find `oid` in `mongodb`
  --> src/main.rs:22:10
   |
22 | #[derive(Model, Serialize, Deserialize)]
   |          ^^^^^ could not find `oid` in `mongodb`
   |
   = note: this error originates in a derive macro (in Nightly builds, run with -Z macro-backtrace for more info)

error[E0603]: struct `Client` is private
   --> src/main.rs:14:5
    |
14  |     Client, ThreadedClient,
    |     ^^^^^^ private struct
    |
note: the struct `Client` is defined here
   --> /home/bob/.cargo/registry/src/github.com-1ecc6299db9ec823/mongodb-1.0.0/src/lib.rs:141:9
    |
141 |         client::Client,
    |         ^^^^^^^^^^^^^^

error[E0603]: module `db` is private
   --> src/main.rs:15:5
    |
15  |     db::{Database, ThreadedDatabase},
    |     ^^ private module
    |
note: the module `db` is defined here
   --> /home/bob/.cargo/registry/src/github.com-1ecc6299db9ec823/mongodb-1.0.0/src/lib.rs:111:5
    |
111 |     mod db;
    |     ^^^^^^^

error[E0603]: module `coll` is private
   --> src/main.rs:16:5
    |
16  |     coll::options::IndexModel,
    |     ^^^^ private module
    |
note: the module `coll` is defined here
   --> /home/bob/.cargo/registry/src/github.com-1ecc6299db9ec823/mongodb-1.0.0/src/lib.rs:107:5
    |
107 |     mod coll;
    |     ^^^^^^^^^

error[E0603]: module `coll` is private
   --> src/main.rs:22:10
    |
22  | #[derive(Model, Serialize, Deserialize)]
    |          ^^^^^ private module
    |
note: the module `coll` is defined here
   --> /home/bob/.cargo/registry/src/github.com-1ecc6299db9ec823/mongodb-1.0.0/src/lib.rs:107:5
    |
107 |     mod coll;
    |     ^^^^^^^^^

error[E0603]: struct `Client` is private
   --> src/main.rs:35:23
    |
35  |     let db = mongodb::Client::with_uri("mongodb://localhost:27017/").unwrap().db("mydb");
    |                       ^^^^^^ private struct
    |
note: the struct `Client` is defined here
   --> /home/bob/.cargo/registry/src/github.com-1ecc6299db9ec823/mongodb-1.0.0/src/lib.rs:141:9
    |
141 |         client::Client,
    |         ^^^^^^^^^^^^^^

warning: unused `#[macro_use]` import
 --> src/main.rs:3:1
  |
3 | #[macro_use]
  | ^^^^^^^^^^^^
  |
  = note: `#[warn(unused_imports)]` on by default

error[E0599]: no function or associated item named `with_uri` found for struct `mongodb::client::Client` in the current scope
  --> src/main.rs:35:31
   |
35 |     let db = mongodb::Client::with_uri("mongodb://localhost:27017/").unwrap().db("mydb");
   |                               ^^^^^^^^ function or associated item not found in `mongodb::client::Client`

error: aborting due to 11 previous errors; 1 warning emitted

Some errors have detailed explanations: E0432, E0433, E0599, E0603.
For more information about an error, try `rustc --explain E0432`.
error: could not compile `wither_eg`.

To learn more, run the command again with --verbose.
thedodd commented 4 years ago

Hey @amber-m-o-r-e looks like you are using an old version of Wither with the new version of the mongo driver. You need to use the latest alpha release of wither (0.9.0-alpha.1).

If you still have issues, check out #53

Keep me posted. Happy to help.

rumanHuq commented 4 years ago

https://github.com/thedodd/wither/issues/53#issuecomment-653091948

thedodd commented 4 years ago

@rumanbsl just making sure you saw my response over there: https://github.com/thedodd/wither/issues/53#issuecomment-653182243 :)

thedodd commented 4 years ago

@amber-m-o-r-e any update on this front? Things working out for you? If so, do you mind closing this issue? I don't want to close it myself if you still need assistance.

amber-m-o-r-e commented 4 years ago

It works well with one warning but I think since we are using alpha build in future it will be resolve, reference code-

use futures::stream::StreamExt;
use serde::{Serialize, Deserialize};
use wither::{prelude::*, Result};
use wither::bson::{doc, oid::ObjectId};
use wither::mongodb::Client;

// Define a model. Simple as deriving a few traits.
#[derive(Debug, Model, Serialize, Deserialize)]
#[model(index(keys=r#"doc!{"email": 1}"#, options=r#"doc!{"unique": true}"#))]
struct User {
    /// The ID of the model.
    #[serde(rename="_id", skip_serializing_if="Option::is_none")]
    pub id: Option<ObjectId>,
    /// The user's email address.
    pub email: String,
}

#[tokio::main]
async fn main() -> Result<()> {
    // Connect & sync indexes.
    let db = Client::with_uri_str("mongodb://localhost:27017/").await?.database("mydb");
    User::sync(db.clone()).await?;

    // Create a user.
    let mut me = User{id: None, email: String::from("my.email@example.com")};
    me.save(db.clone(), None).await?;

    // Update user's email address.
    me.update(db.clone(), None, doc!{"$set": doc!{"email": "new.email@example.com"}}, None).await?;

    // Fetch all users.
    let mut cursor = User::find(db.clone(), None, None).await?;
    while let Some(user) = cursor.next().await {
        println!("{:?}", user);
    }
    Ok(())
}

toml-

[dependencies]
futures = "0.3.5"
serde = "1.0.114"
serde_derive = "1.0.114"
wither = { version = "0.9.0-alpha.1", default-features = false, features = ["async-std-runtime"] }
tokio = { version = "0.2.21", features = ["full"] }