Skip to content

Commit 66a3a00

Browse files
authored
feat(deps)!: upgrade odm layer to MongDB Rust driver v3.0.0 (#30)
1 parent 53035b2 commit 66a3a00

File tree

6 files changed

+49
-56
lines changed

6 files changed

+49
-56
lines changed

Cargo.toml

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "mongodm"
3-
version = "0.9.2"
3+
version = "0.10.0"
44
authors = ["Benoît CORTIER <[email protected]>"]
55
edition = "2021"
66
description = "A thin ODM layer for mongodb"
@@ -12,7 +12,7 @@ readme = "README.md"
1212
include = ["src/**/*", "README.md", "CHANGELOG.md"]
1313

1414
[dependencies]
15-
mongodb = { version = "2", default-features = false }
15+
mongodb = { version = "3", default-features = true }
1616
serde = { version = "1", features = ["derive"] }
1717
futures-core = "0.3"
1818
futures-util = "0.3"
@@ -21,10 +21,4 @@ async-trait = "0.1"
2121
[dev-dependencies]
2222
tokio = "1.14.0"
2323
pretty_assertions = "1.0.0"
24-
chrono = { version = "0.4.19", features = [ "serde" ] }
25-
26-
[features]
27-
default = ["tokio-runtime"]
28-
tokio-runtime = ["mongodb/tokio-runtime"]
29-
async-std-runtime = ["mongodb/async-std-runtime"]
30-
chrono-0_4 = ["mongodb/bson-chrono-0_4"]
24+
chrono = { version = "0.4.38", features = [ "serde" ] }

src/index.rs

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
33
use crate::CollectionConfig;
44
use mongodb::bson::{doc, from_bson, Bson, Document};
5-
use mongodb::options::*;
5+
use mongodb::options::ReadPreference;
6+
use mongodb::options::{RunCommandOptions, SelectionCriteria};
67
use mongodb::Database;
78
use serde::Deserialize;
89
use std::borrow::Cow;
@@ -369,7 +370,11 @@ pub async fn sync_indexes<CollConf: CollectionConfig>(
369370
let mut to_drop = Vec::new();
370371
for (i, index) in indexes.0.clone().into_iter().enumerate() {
371372
let mut text_index_keys = None;
372-
let index_doc = if index.keys.iter().any(|ind| matches!(ind, IndexKey::TextIndex(_))) {
373+
let index_doc = if index
374+
.keys
375+
.iter()
376+
.any(|ind| matches!(ind, IndexKey::TextIndex(_)))
377+
{
373378
let mut doc = index.into_document();
374379

375380
// There can only be 1 text index per collection so when a text index is saved, the keys are automatically changed to this. We keep a copy for the weight comparison.
@@ -390,14 +395,16 @@ pub async fn sync_indexes<CollConf: CollectionConfig>(
390395

391396
// We compare the text index here, the keys become weights of 1 after saving in the DB. Custom weights not supported yet.
392397
if let Some(Bson::Document(mut keys_to_set)) = text_index_keys {
393-
if let Some(Bson::Document(existing_weights)) = existing_index.get("weights") {
398+
if let Some(Bson::Document(existing_weights)) =
399+
existing_index.get("weights")
400+
{
394401
// Changing all text values to the default weight of 1
395402
for keys in keys_to_set.iter_mut() {
396403
match keys.1 {
397404
Bson::String(t) if t == "text" => {
398405
*keys.1 = Bson::Int32(1);
399-
},
400-
_ => ()
406+
}
407+
_ => (),
401408
}
402409
}
403410

@@ -407,7 +414,9 @@ pub async fn sync_indexes<CollConf: CollectionConfig>(
407414
to_drop.push(
408415
index_doc
409416
.get_str("name")
410-
.map_err(|e| std::io::Error::new(std::io::ErrorKind::Other, e))?
417+
.map_err(|e| {
418+
std::io::Error::new(std::io::ErrorKind::Other, e)
419+
})?
411420
.to_owned(),
412421
);
413422
}
@@ -495,11 +504,13 @@ async fn h_run_command(
495504
db: &Database,
496505
command_doc: Document,
497506
) -> Result<Document, mongodb::error::Error> {
507+
let primary_options = RunCommandOptions::builder()
508+
.selection_criteria(SelectionCriteria::ReadPreference(ReadPreference::Primary))
509+
.build();
510+
498511
let ret = db
499-
.run_command(
500-
command_doc,
501-
Some(SelectionCriteria::ReadPreference(ReadPreference::Primary)),
502-
)
512+
.run_command(command_doc)
513+
.with_options(primary_options)
503514
.await?;
504515
if let Ok(err) = from_bson::<mongodb::error::CommandError>(Bson::Document(ret.clone())) {
505516
Err(mongodb::error::Error::from(

src/lib.rs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ pub use mongodb::bson;
108108
pub use mongodb::bson::{bson, doc};
109109

110110
/// Associate a collection configuration.
111-
pub trait Model: serde::ser::Serialize + serde::de::DeserializeOwned + Unpin {
111+
pub trait Model: serde::ser::Serialize + serde::de::DeserializeOwned + Unpin + Send + Sync {
112112
type CollConf: CollectionConfig;
113113
}
114114

@@ -128,8 +128,8 @@ pub trait CollectionConfig {
128128
/// Configure how indexes should be created and synchronized for the associated collection.
129129
///
130130
/// This method has a default implementation returning no index (only special `_id` index will be present).
131-
fn indexes() -> index::Indexes {
132-
index::Indexes::default()
131+
fn indexes() -> Indexes {
132+
Indexes::default()
133133
}
134134
}
135135

@@ -178,13 +178,14 @@ pub mod prelude {
178178
JavaScriptCodeWithScope as BsonJavaScriptCodeWithScope, Regex as BsonRegex,
179179
Serializer as BsonSerializer, Timestamp as BsonTimestamp,
180180
};
181+
181182
#[doc(no_inline)]
182183
pub use crate::mongo::{
183184
error::{
184-
BulkWriteError as MongoBulkWriteError, BulkWriteFailure as MongoBulkWriteFailure,
185-
CommandError as MongoCommandError, Error as MongoError, ErrorKind as MongoErrorKind,
186-
WriteConcernError as MongoWriteConcernError, WriteError as MongoWriteError,
187-
WriteFailure as MongoWriteFailure,
185+
BulkWriteError as MongoBulkWriteError, CommandError as MongoCommandError,
186+
Error as MongoError, ErrorKind as MongoErrorKind,
187+
InsertManyError as MongoInsertManyError, WriteConcernError as MongoWriteConcernError,
188+
WriteError as MongoWriteError, WriteFailure as MongoWriteFailure,
188189
},
189190
options::{
190191
Acknowledgment as MongoAcknowledgment, AggregateOptions as MongoAggregateOptions,

src/repository.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,6 @@ impl<M: Model> Repository<M> {
277277
/// ```
278278
pub async fn bulk_update<V, U>(&self, updates: V) -> Result<BulkUpdateResult>
279279
where
280-
M: Send + Sync,
281280
V: Borrow<Vec<U>> + Send + Sync,
282281
U: Borrow<BulkUpdate> + Send + Sync,
283282
{
@@ -381,7 +380,7 @@ impl<M: Send + Sync> CollectionExt for mongodb::Collection<M> {
381380
if let Some(ref write_concern) = self.write_concern() {
382381
command.insert("writeConcern", to_bson(write_concern)?);
383382
}
384-
let res = db.run_command(command, None).await?;
383+
let res = db.run_command(command).await?;
385384
Ok(from_document(res)?)
386385
}
387386
}

tests/indexes.rs

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -28,17 +28,14 @@ async fn one_sync() {
2828
let db = client.database("rust_mongo_orm_tests");
2929

3030
db.collection::<Document>(OneSyncCollConf::collection_name())
31-
.drop(None)
31+
.drop()
3232
.await
3333
.unwrap();
3434

3535
sync_indexes::<OneSyncCollConf>(&db).await.unwrap();
3636

3737
let ret = db
38-
.run_command(
39-
doc! { "listIndexes": OneSyncCollConf::collection_name() },
40-
None,
41-
)
38+
.run_command(doc! { "listIndexes": OneSyncCollConf::collection_name() })
4239
.await
4340
.unwrap();
4441

@@ -123,17 +120,14 @@ async fn multiple_sync() {
123120
let db = client.database("rust_mongo_orm_tests");
124121

125122
db.collection::<Document>(MultipleSyncCollConf::collection_name())
126-
.drop(None)
123+
.drop()
127124
.await
128125
.unwrap();
129126

130127
sync_indexes::<MultipleSyncCollConf>(&db).await.unwrap();
131128

132129
let ret = db
133-
.run_command(
134-
doc! { "listIndexes": MultipleSyncCollConf::collection_name() },
135-
None,
136-
)
130+
.run_command(doc! { "listIndexes": MultipleSyncCollConf::collection_name() })
137131
.await
138132
.unwrap();
139133

@@ -173,10 +167,7 @@ async fn multiple_sync() {
173167
.unwrap();
174168

175169
let ret = db
176-
.run_command(
177-
doc! { "listIndexes": MultipleNoLastSeenCollConf::collection_name() },
178-
None,
179-
)
170+
.run_command(doc! { "listIndexes": MultipleNoLastSeenCollConf::collection_name() })
180171
.await
181172
.unwrap();
182173

@@ -215,10 +206,7 @@ async fn multiple_sync() {
215206
.unwrap();
216207

217208
let ret = db
218-
.run_command(
219-
doc! { "listIndexes": MultipleNotUniqueCollConf::collection_name() },
220-
None,
221-
)
209+
.run_command(doc! { "listIndexes": MultipleNotUniqueCollConf::collection_name() })
222210
.await
223211
.unwrap();
224212

tests/some_operations.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ async fn insert_delete_find() {
4040
let db = client.database("rust_mongo_orm_tests");
4141

4242
let repository = db.repository::<User>();
43-
repository.drop(None).await.unwrap();
43+
repository.drop().await.unwrap();
4444
sync_indexes::<UserCollConf>(&db).await.unwrap();
4545

4646
let users = vec![
@@ -81,10 +81,10 @@ async fn insert_delete_find() {
8181
},
8282
];
8383

84-
repository.insert_many(users, None).await.unwrap();
84+
repository.insert_many(users).await.unwrap();
8585

8686
let user_dane = repository
87-
.find_one(doc! { f!(name in User): "Dane" }, None)
87+
.find_one(doc! { f!(name in User): "Dane" })
8888
.await
8989
.unwrap()
9090
.unwrap();
@@ -93,19 +93,19 @@ async fn insert_delete_find() {
9393
assert_eq!(user_dane.info, "d");
9494

9595
let found = repository
96-
.find(doc! { f!(age in User): { LesserThan: 40 } }, None)
96+
.find(doc! { f!(age in User): { LesserThan: 40 } })
9797
.await
9898
.unwrap();
9999
let found: Vec<mongodb::error::Result<User>> = found.collect().await;
100100
assert_eq!(found.len(), 4);
101101

102102
repository
103-
.delete_one(doc! { f!(age in User): { LesserThan: 38 } }, None)
103+
.delete_one(doc! { f!(age in User): { LesserThan: 38 } })
104104
.await
105105
.unwrap();
106106

107107
let found = repository
108-
.find(doc! { f!(age in User): { LesserThan: 40 } }, None)
108+
.find(doc! { f!(age in User): { LesserThan: 40 } })
109109
.await
110110
.unwrap();
111111
let found: Vec<mongodb::error::Result<User>> = found.collect().await;
@@ -122,7 +122,7 @@ async fn bulk_updates() {
122122
let db = client.database("rust_mongo_orm_tests");
123123

124124
let repository = db.repository::<User>();
125-
repository.drop(None).await.unwrap();
125+
repository.drop().await.unwrap();
126126
sync_indexes::<UserCollConf>(&db).await.unwrap();
127127

128128
let users = vec![
@@ -163,7 +163,7 @@ async fn bulk_updates() {
163163
},
164164
];
165165

166-
repository.insert_many(users, None).await.unwrap();
166+
repository.insert_many(users).await.unwrap();
167167

168168
let bulk_update_res = repository
169169
.bulk_update(&vec![
@@ -184,15 +184,15 @@ async fn bulk_updates() {
184184
assert_eq!(bulk_update_res.nb_modified, 2);
185185

186186
let user_dane = repository
187-
.find_one(doc! { f!(name in User): "Dane" }, None)
187+
.find_one(doc! { f!(name in User): "Dane" })
188188
.await
189189
.unwrap()
190190
.unwrap();
191191
assert_eq!(user_dane.name, "Dane");
192192
assert_eq!(user_dane.age, 12);
193193

194194
let user_dane = repository
195-
.find_one(doc! { f!(name in User): "David" }, None)
195+
.find_one(doc! { f!(name in User): "David" })
196196
.await
197197
.unwrap()
198198
.unwrap();

0 commit comments

Comments
 (0)