1
0
mirror of https://github.com/actix/examples synced 2024-11-27 16:02:57 +01:00

refactor: update mongodb to 3

This commit is contained in:
Rob Ede 2024-07-07 02:03:51 +01:00
parent 0bdd5479ff
commit dca1d50430
No known key found for this signature in database
GPG Key ID: 97C636207D3EF933
6 changed files with 100 additions and 91 deletions

172
Cargo.lock generated
View File

@ -239,7 +239,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e01ed3140b2f8d422c68afa1ed2e85d996ea619c988ac834d255db32138655cb" checksum = "e01ed3140b2f8d422c68afa1ed2e85d996ea619c988ac834d255db32138655cb"
dependencies = [ dependencies = [
"quote", "quote",
"syn 2.0.48", "syn 2.0.69",
] ]
[[package]] [[package]]
@ -277,7 +277,7 @@ dependencies = [
"parse-size", "parse-size",
"proc-macro2", "proc-macro2",
"quote", "quote",
"syn 2.0.48", "syn 2.0.69",
] ]
[[package]] [[package]]
@ -489,7 +489,7 @@ dependencies = [
"actix-router", "actix-router",
"proc-macro2", "proc-macro2",
"quote", "quote",
"syn 2.0.48", "syn 2.0.69",
] ]
[[package]] [[package]]
@ -538,7 +538,7 @@ checksum = "9aa0b287c8de4a76b691f29dbb5451e8dd5b79d777eaf87350c9b0cbfdb5e968"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
"quote", "quote",
"syn 2.0.48", "syn 2.0.69",
] ]
[[package]] [[package]]
@ -562,7 +562,7 @@ checksum = "7c7db3d5a9718568e4cf4a537cfd7070e6e6ff7481510d0237fb529ac850f6d3"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
"quote", "quote",
"syn 2.0.48", "syn 2.0.69",
] ]
[[package]] [[package]]
@ -902,7 +902,7 @@ dependencies = [
"proc-macro2", "proc-macro2",
"quote", "quote",
"serde", "serde",
"syn 2.0.48", "syn 2.0.69",
] ]
[[package]] [[package]]
@ -1019,7 +1019,7 @@ dependencies = [
"proc-macro2", "proc-macro2",
"quote", "quote",
"strum", "strum",
"syn 2.0.48", "syn 2.0.69",
"thiserror", "thiserror",
] ]
@ -1066,7 +1066,7 @@ checksum = "16e62a023e7c117e27523144c5d2459f4397fcc3cab0085af8e2224f643a0193"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
"quote", "quote",
"syn 2.0.48", "syn 2.0.69",
] ]
[[package]] [[package]]
@ -1077,7 +1077,7 @@ checksum = "a507401cad91ec6a857ed5513a2073c82a9b9048762b885bb98655b306964681"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
"quote", "quote",
"syn 2.0.48", "syn 2.0.69",
] ]
[[package]] [[package]]
@ -1104,7 +1104,7 @@ dependencies = [
"derive_utils 0.14.1", "derive_utils 0.14.1",
"proc-macro2", "proc-macro2",
"quote", "quote",
"syn 2.0.48", "syn 2.0.69",
] ]
[[package]] [[package]]
@ -1748,7 +1748,7 @@ dependencies = [
"regex", "regex",
"rustc-hash", "rustc-hash",
"shlex", "shlex",
"syn 2.0.48", "syn 2.0.69",
"which", "which",
] ]
@ -1867,9 +1867,9 @@ dependencies = [
[[package]] [[package]]
name = "bson" name = "bson"
version = "2.9.0" version = "2.11.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ce21468c1c9c154a85696bb25c20582511438edb6ad67f846ba1378ffdd80222" checksum = "d8a88e82b9106923b5c4d6edfca9e7db958d4e98a478ec115022e81b9b38e2c8"
dependencies = [ dependencies = [
"ahash 0.8.11", "ahash 0.8.11",
"base64 0.13.1", "base64 0.13.1",
@ -2131,7 +2131,7 @@ dependencies = [
"heck 0.5.0", "heck 0.5.0",
"proc-macro2", "proc-macro2",
"quote", "quote",
"syn 2.0.48", "syn 2.0.69",
] ]
[[package]] [[package]]
@ -2228,7 +2228,7 @@ dependencies = [
"darling 0.20.3", "darling 0.20.3",
"proc-macro2", "proc-macro2",
"quote", "quote",
"syn 2.0.48", "syn 2.0.69",
] ]
[[package]] [[package]]
@ -2571,7 +2571,7 @@ dependencies = [
"proc-macro2", "proc-macro2",
"quote", "quote",
"strsim 0.10.0", "strsim 0.10.0",
"syn 2.0.48", "syn 2.0.69",
] ]
[[package]] [[package]]
@ -2593,7 +2593,7 @@ checksum = "836a9bbc7ad63342d6d6e7b815ccab164bc77a2d95d84bc3117a8c0d5c98e2d5"
dependencies = [ dependencies = [
"darling_core 0.20.3", "darling_core 0.20.3",
"quote", "quote",
"syn 2.0.48", "syn 2.0.69",
] ]
[[package]] [[package]]
@ -2615,6 +2615,28 @@ version = "2.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c2e66c9d817f1720209181c316d28635c050fa304f9c79e47a520882661b7308" checksum = "c2e66c9d817f1720209181c316d28635c050fa304f9c79e47a520882661b7308"
[[package]]
name = "db-diesel"
version = "1.0.0"
dependencies = [
"actix-web",
"diesel",
"dotenvy",
"env_logger",
"log",
"serde",
"uuid",
]
[[package]]
name = "db-mongo"
version = "1.0.0"
dependencies = [
"actix-web",
"mongodb",
"serde",
]
[[package]] [[package]]
name = "db-postgres" name = "db-postgres"
version = "1.0.0" version = "1.0.0"
@ -2718,7 +2740,7 @@ checksum = "5fe87ce4529967e0ba1dcf8450bab64d97dfd5010a6256187ffe2e43e6f0e049"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
"quote", "quote",
"syn 2.0.48", "syn 2.0.69",
] ]
[[package]] [[package]]
@ -2762,7 +2784,7 @@ checksum = "9abcad25e9720609ccb3dcdb795d845e37d8ce34183330a9f48b03a1a71c8e21"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
"quote", "quote",
"syn 2.0.48", "syn 2.0.69",
] ]
[[package]] [[package]]
@ -2773,7 +2795,7 @@ checksum = "61bb5a1014ce6dfc2a378578509abe775a5aa06bff584a547555d9efdb81b926"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
"quote", "quote",
"syn 2.0.48", "syn 2.0.69",
] ]
[[package]] [[package]]
@ -2800,19 +2822,6 @@ dependencies = [
"uuid", "uuid",
] ]
[[package]]
name = "diesel-example"
version = "1.0.0"
dependencies = [
"actix-web",
"diesel",
"dotenvy",
"env_logger",
"log",
"serde",
"uuid",
]
[[package]] [[package]]
name = "diesel_derives" name = "diesel_derives"
version = "2.2.0" version = "2.2.0"
@ -2823,7 +2832,7 @@ dependencies = [
"dsl_auto_type", "dsl_auto_type",
"proc-macro2", "proc-macro2",
"quote", "quote",
"syn 2.0.48", "syn 2.0.69",
] ]
[[package]] [[package]]
@ -2832,7 +2841,7 @@ version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "209c735641a413bc68c4923a9d6ad4bcb3ca306b794edaa7eb0b3228a99ffb25" checksum = "209c735641a413bc68c4923a9d6ad4bcb3ca306b794edaa7eb0b3228a99ffb25"
dependencies = [ dependencies = [
"syn 2.0.48", "syn 2.0.69",
] ]
[[package]] [[package]]
@ -2855,7 +2864,7 @@ checksum = "487585f4d0c6655fe74905e2504d8ad6908e4db67f744eb140876906c2f3175d"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
"quote", "quote",
"syn 2.0.48", "syn 2.0.69",
] ]
[[package]] [[package]]
@ -2884,7 +2893,7 @@ dependencies = [
"heck 0.5.0", "heck 0.5.0",
"proc-macro2", "proc-macro2",
"quote", "quote",
"syn 2.0.48", "syn 2.0.69",
] ]
[[package]] [[package]]
@ -3213,7 +3222,7 @@ dependencies = [
"once_cell", "once_cell",
"proc-macro2", "proc-macro2",
"quote", "quote",
"syn 2.0.48", "syn 2.0.69",
"unic-langid", "unic-langid",
] ]
@ -3315,7 +3324,7 @@ checksum = "b0fa992f1656e1707946bbba340ad244f0814009ef8c0118eb7b658395f19a2e"
dependencies = [ dependencies = [
"frunk_proc_macro_helpers", "frunk_proc_macro_helpers",
"quote", "quote",
"syn 2.0.48", "syn 2.0.69",
] ]
[[package]] [[package]]
@ -3327,7 +3336,7 @@ dependencies = [
"frunk_core", "frunk_core",
"proc-macro2", "proc-macro2",
"quote", "quote",
"syn 2.0.48", "syn 2.0.69",
] ]
[[package]] [[package]]
@ -3339,7 +3348,7 @@ dependencies = [
"frunk_core", "frunk_core",
"frunk_proc_macro_helpers", "frunk_proc_macro_helpers",
"quote", "quote",
"syn 2.0.48", "syn 2.0.69",
] ]
[[package]] [[package]]
@ -3469,7 +3478,7 @@ checksum = "53b153fd91e4b0147f4aced87be237c98248656bb01050b96bf3ee89220a8ddb"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
"quote", "quote",
"syn 2.0.48", "syn 2.0.69",
] ]
[[package]] [[package]]
@ -4275,7 +4284,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5305557fa27b460072ae15ce07617e999f5879f14d376c8449f0bfb9f9d8e91e" checksum = "5305557fa27b460072ae15ce07617e999f5879f14d376c8449f0bfb9f9d8e91e"
dependencies = [ dependencies = [
"derive_utils 0.13.2", "derive_utils 0.13.2",
"syn 2.0.48", "syn 2.0.69",
] ]
[[package]] [[package]]
@ -4472,7 +4481,7 @@ checksum = "760dbe46660494d469023d661e8d268f413b2cb68c999975dcc237407096a693"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
"quote", "quote",
"syn 2.0.48", "syn 2.0.69",
"url", "url",
] ]
@ -5003,18 +5012,9 @@ checksum = "c9be0862c1b3f26a88803c4a49de6889c10e608b3ee9344e6ef5b45fb37ad3d1"
[[package]] [[package]]
name = "mongodb" name = "mongodb"
version = "1.0.0" version = "3.0.0"
dependencies = [
"actix-web",
"mongodb 2.8.2",
"serde",
]
[[package]]
name = "mongodb"
version = "2.8.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ef206acb1b72389b49bc9985efe7eb1f8a9bb18e5680d262fac26c07f44025f1" checksum = "bb4af699cbb12a221e978134999b16cbf030d964c3a7cd6c4af2584734190e58"
dependencies = [ dependencies = [
"async-trait", "async-trait",
"base64 0.13.1", "base64 0.13.1",
@ -5029,8 +5029,9 @@ dependencies = [
"futures-util", "futures-util",
"hex", "hex",
"hmac", "hmac",
"lazy_static",
"md-5", "md-5",
"mongodb-internal-macros",
"once_cell",
"pbkdf2", "pbkdf2",
"percent-encoding", "percent-encoding",
"rand", "rand",
@ -5042,7 +5043,7 @@ dependencies = [
"serde_with", "serde_with",
"sha-1", "sha-1",
"sha2", "sha2",
"socket2 0.4.10", "socket2 0.5.5",
"stringprep", "stringprep",
"strsim 0.10.0", "strsim 0.10.0",
"take_mut", "take_mut",
@ -5057,6 +5058,17 @@ dependencies = [
"webpki-roots 0.25.4", "webpki-roots 0.25.4",
] ]
[[package]]
name = "mongodb-internal-macros"
version = "3.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1159486b5e1993138c385369c3c46f5608cb0b1c53734e503538366f3c4cedcc"
dependencies = [
"proc-macro2",
"quote",
"syn 2.0.69",
]
[[package]] [[package]]
name = "multer" name = "multer"
version = "2.1.0" version = "2.1.0"
@ -5165,7 +5177,7 @@ dependencies = [
"proc-macro-error", "proc-macro-error",
"proc-macro2", "proc-macro2",
"quote", "quote",
"syn 2.0.48", "syn 2.0.69",
"termcolor", "termcolor",
"thiserror", "thiserror",
] ]
@ -5449,7 +5461,7 @@ checksum = "a948666b637a0f465e8564c73e89d4dde00d72d4d473cc972f390fc3dcee7d9c"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
"quote", "quote",
"syn 2.0.48", "syn 2.0.69",
] ]
[[package]] [[package]]
@ -5726,7 +5738,7 @@ dependencies = [
"pest_meta", "pest_meta",
"proc-macro2", "proc-macro2",
"quote", "quote",
"syn 2.0.48", "syn 2.0.69",
] ]
[[package]] [[package]]
@ -5833,7 +5845,7 @@ checksum = "2f38a4412a78282e09a2cf38d195ea5420d15ba0602cb375210efbc877243965"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
"quote", "quote",
"syn 2.0.48", "syn 2.0.69",
] ]
[[package]] [[package]]
@ -5982,7 +5994,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8d3928fb5db768cb86f891ff014f0144589297e3c6a1aba6ed7cecfdace270c7" checksum = "8d3928fb5db768cb86f891ff014f0144589297e3c6a1aba6ed7cecfdace270c7"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
"syn 2.0.48", "syn 2.0.69",
] ]
[[package]] [[package]]
@ -6045,9 +6057,9 @@ checksum = "dc375e1527247fe1a97d8b7156678dfe7c1af2fc075c9a4db3690ecd2a148068"
[[package]] [[package]]
name = "proc-macro2" name = "proc-macro2"
version = "1.0.76" version = "1.0.86"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "95fc56cda0b5c3325f5fbbd7ff9fda9e02bb00bb3dac51252d2f1bfa1cb8cc8c" checksum = "5e719e8df665df0d1c8fbfd238015744736151d4445ec0836b8e628aae103b77"
dependencies = [ dependencies = [
"unicode-ident", "unicode-ident",
] ]
@ -6072,7 +6084,7 @@ dependencies = [
"itertools 0.12.1", "itertools 0.12.1",
"proc-macro2", "proc-macro2",
"quote", "quote",
"syn 2.0.48", "syn 2.0.69",
] ]
[[package]] [[package]]
@ -6549,7 +6561,7 @@ checksum = "853977598f084a492323fe2f7896b4100a86284ee8473612de60021ea341310f"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
"quote", "quote",
"syn 2.0.48", "syn 2.0.69",
] ]
[[package]] [[package]]
@ -6886,7 +6898,7 @@ dependencies = [
"proc-macro2", "proc-macro2",
"quote", "quote",
"serde", "serde",
"syn 2.0.48", "syn 2.0.69",
"toml 0.8.8", "toml 0.8.8",
] ]
@ -7078,7 +7090,7 @@ checksum = "500cbc0ebeb6f46627f50f3f5811ccf6bf00643be300b4c3eabc0ef55dc5b5ba"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
"quote", "quote",
"syn 2.0.48", "syn 2.0.69",
] ]
[[package]] [[package]]
@ -7731,7 +7743,7 @@ dependencies = [
"proc-macro2", "proc-macro2",
"quote", "quote",
"rustversion", "rustversion",
"syn 2.0.48", "syn 2.0.69",
] ]
[[package]] [[package]]
@ -7763,9 +7775,9 @@ dependencies = [
[[package]] [[package]]
name = "syn" name = "syn"
version = "2.0.48" version = "2.0.69"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0f3531638e407dfc0814761abb7c00a5b54992b849452a0646b7f65c9f770f3f" checksum = "201fcda3845c23e8212cd466bfebf0bd20694490fc0356ae8e428e0824a915a6"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
"quote", "quote",
@ -7976,7 +7988,7 @@ checksum = "c61f3ba182994efc43764a46c018c347bc492c79f024e705f46567b418f6d4f7"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
"quote", "quote",
"syn 2.0.48", "syn 2.0.69",
] ]
[[package]] [[package]]
@ -8107,7 +8119,7 @@ checksum = "8d9ef545650e79f30233c0003bcc2504d7efac6dad25fca40744de773fe2049c"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
"quote", "quote",
"syn 2.0.48", "syn 2.0.69",
] ]
[[package]] [[package]]
@ -8180,7 +8192,7 @@ checksum = "5f5ae998a069d4b5aba8ee9dad856af7d520c3699e6159b185c2acd48155d39a"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
"quote", "quote",
"syn 2.0.48", "syn 2.0.69",
] ]
[[package]] [[package]]
@ -8496,7 +8508,7 @@ checksum = "34704c8d6ebcbc939824180af020566b01a7c01f80641264eba0999f6c2b6be7"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
"quote", "quote",
"syn 2.0.48", "syn 2.0.69",
] ]
[[package]] [[package]]
@ -8999,7 +9011,7 @@ dependencies = [
"proc-macro-error", "proc-macro-error",
"proc-macro2", "proc-macro2",
"quote", "quote",
"syn 2.0.48", "syn 2.0.69",
] ]
[[package]] [[package]]
@ -9080,7 +9092,7 @@ dependencies = [
"once_cell", "once_cell",
"proc-macro2", "proc-macro2",
"quote", "quote",
"syn 2.0.48", "syn 2.0.69",
"wasm-bindgen-shared", "wasm-bindgen-shared",
] ]
@ -9114,7 +9126,7 @@ checksum = "c5353b8dab669f5e10f5bd76df26a9360c748f054f862ff5f3f8aae0c7fb3907"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
"quote", "quote",
"syn 2.0.48", "syn 2.0.69",
"wasm-bindgen-backend", "wasm-bindgen-backend",
"wasm-bindgen-shared", "wasm-bindgen-shared",
] ]
@ -9767,7 +9779,7 @@ checksum = "b3c129550b3e6de3fd0ba67ba5c81818f9805e58b8d7fee80a3a59d2c9fc601a"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
"quote", "quote",
"syn 2.0.48", "syn 2.0.69",
] ]
[[package]] [[package]]
@ -9787,7 +9799,7 @@ checksum = "ce36e65b0d2999d2aafac989fb249189a141aee1f53c612c1f37d72631959f69"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
"quote", "quote",
"syn 2.0.48", "syn 2.0.69",
] ]
[[package]] [[package]]

View File

@ -1,5 +1,5 @@
[package] [package]
name = "diesel-example" name = "db-diesel"
version = "1.0.0" version = "1.0.0"
edition = "2021" edition = "2021"

View File

@ -1,9 +1,9 @@
[package] [package]
name = "mongodb" name = "db-mongo"
version = "1.0.0" version = "1.0.0"
edition = "2021" edition = "2021"
[dependencies] [dependencies]
actix-web.workspace = true actix-web.workspace = true
mongodb = "2" mongodb = "3"
serde.workspace = true serde.workspace = true

View File

@ -1,6 +1,6 @@
# MongoDB # MongoDB
Simple example of MongoDB usage with Actix Web. For more information on the MongoDB Rust driver, visit the [documentation](https://docs.rs/mongodb/2.0.0/mongodb/index.html) and [source code](https://github.com/mongodb/mongo-rust-driver). Simple example of MongoDB usage with Actix Web. For more information on the MongoDB Rust driver, visit the [documentation](https://docs.rs/mongodb/3) and [source code](https://github.com/mongodb/mongo-rust-driver).
## Usage ## Usage

View File

@ -15,7 +15,7 @@ const COLL_NAME: &str = "users";
#[post("/add_user")] #[post("/add_user")]
async fn add_user(client: web::Data<Client>, form: web::Form<User>) -> HttpResponse { async fn add_user(client: web::Data<Client>, form: web::Form<User>) -> HttpResponse {
let collection = client.database(DB_NAME).collection(COLL_NAME); let collection = client.database(DB_NAME).collection(COLL_NAME);
let result = collection.insert_one(form.into_inner(), None).await; let result = collection.insert_one(form.into_inner()).await;
match result { match result {
Ok(_) => HttpResponse::Ok().body("user added"), Ok(_) => HttpResponse::Ok().body("user added"),
Err(err) => HttpResponse::InternalServerError().body(err.to_string()), Err(err) => HttpResponse::InternalServerError().body(err.to_string()),
@ -27,10 +27,7 @@ async fn add_user(client: web::Data<Client>, form: web::Form<User>) -> HttpRespo
async fn get_user(client: web::Data<Client>, username: web::Path<String>) -> HttpResponse { async fn get_user(client: web::Data<Client>, username: web::Path<String>) -> HttpResponse {
let username = username.into_inner(); let username = username.into_inner();
let collection: Collection<User> = client.database(DB_NAME).collection(COLL_NAME); let collection: Collection<User> = client.database(DB_NAME).collection(COLL_NAME);
match collection match collection.find_one(doc! { "username": &username }).await {
.find_one(doc! { "username": &username }, None)
.await
{
Ok(Some(user)) => HttpResponse::Ok().json(user), Ok(Some(user)) => HttpResponse::Ok().json(user),
Ok(None) => { Ok(None) => {
HttpResponse::NotFound().body(format!("No user found with username {username}")) HttpResponse::NotFound().body(format!("No user found with username {username}"))
@ -49,7 +46,7 @@ async fn create_username_index(client: &Client) {
client client
.database(DB_NAME) .database(DB_NAME)
.collection::<User>(COLL_NAME) .collection::<User>(COLL_NAME)
.create_index(model, None) .create_index(model)
.await .await
.expect("creating an index should succeed"); .expect("creating an index should succeed");
} }

View File

@ -16,7 +16,7 @@ async fn test() {
client client
.database(DB_NAME) .database(DB_NAME)
.collection::<User>(COLL_NAME) .collection::<User>(COLL_NAME)
.drop(None) .drop()
.await .await
.expect("drop collection should succeed"); .expect("drop collection should succeed");