mongodb_command_cli/utils/
mongodb_command.rs

1use crate::errors::Result;
2use mongodb::bson::{doc, Array, Bson};
3
4pub mod user {
5    pub struct User {
6        pub user: String,
7        pub pwd: String,
8        pub roles: Vec<Role>,
9    }
10
11    pub struct Role {
12        pub role: String,
13        pub db: Option<String>,
14    }
15}
16
17pub async fn create_user(db: mongodb::Database, new_user: user::User) -> Result<()> {
18    let mut roles = Array::new();
19    for role in new_user.roles {
20        if role.db.is_some() {
21            roles.push(Bson::Document(doc! {"role":role.role,"db":role.db}));
22        } else {
23            roles.push(Bson::Document(doc! {"role":role.role}));
24        }
25    }
26    let command = doc! {
27        "createUser": new_user.user,
28        "pwd": new_user.pwd,
29        "roles": roles,
30    };
31    let r = db.run_command(command, None).await?;
32    println!("{}", r);
33
34    Ok(())
35}
36
37pub mod role {
38
39    pub struct Role {
40        pub role: String,
41    }
42}
43
44pub fn create_role(_db: mongodb::Database, _new_role: role::Role) -> Result<()> {
45    todo!()
46}
47
48#[cfg(test)]
49mod tests {
50
51    use serde::Deserialize;
52
53    use super::create_user;
54    use crate::utils::ser::{read_json, str_to_bson};
55
56    #[tokio::test]
57    async fn test_create_user() {
58        let client = mongodb::Client::with_uri_str("mongodb://example.com")
59            .await
60            .unwrap();
61        let db = client.database("admin");
62        let new_user = super::user::User {
63            user: "zhouyu".to_owned(),
64            pwd: "123456".to_owned(),
65            roles: vec![super::user::Role {
66                role: "readWrite".to_string(),
67                db: Some("project".to_string()),
68            }],
69        };
70
71        let r = create_user(db, new_user).await;
72
73        if r.is_err() {
74            let e = r.err().unwrap();
75            println!("ERROR {}", e);
76        }
77    }
78
79    #[tokio::test]
80    async fn test_read_json() {
81        let v = read_json("commands.json").unwrap();
82        for ele in v.as_array().unwrap() {
83            let ss = ele.to_string();
84            let command = str_to_bson(&ss).unwrap();
85            println!("{}\n", command);
86        }
87    }
88
89    #[tokio::test]
90    async fn test_run_command() {
91        let client = mongodb::Client::with_uri_str(
92            "mongodb://admin:a0QgTFipMt9TbV6bNYX8@192.168.50.5:27017",
93        )
94        .await
95        .unwrap();
96        let db = client.database("admin");
97
98        let v = read_json("commands.json").unwrap();
99        for ele in v.as_array().unwrap() {
100            let ss = ele.to_string();
101            let command = str_to_bson(&ss).unwrap();
102            println!("{}\n", command);
103
104            let r = db.run_command(command, None).await;
105
106            if r.is_err() {
107                let e = r.err().unwrap();
108                println!("ERROR {}", e);
109            }
110        }
111    }
112
113    #[derive(Debug, Deserialize)]
114    struct Order {
115        _name: String,
116    }
117
118    #[tokio::test]
119    async fn test_query() {
120        use mongodb::bson::doc;
121
122        let uri = "mongodb://hello:123456@192.168.50.5:27017";
123        let client = mongodb::Client::with_uri_str(uri).await.unwrap();
124        let db = client.database("hello");
125
126        let r = db
127            .collection::<Order>("world")
128            .find_one(doc! {}, None)
129            .await;
130        if r.is_err() {
131            let e = r.err().unwrap();
132            println!("ERROR {}", e);
133        } else {
134            println!("RESULT: {:?}", r.unwrap().unwrap())
135        }
136    }
137}