Skip to content

Commit 1dffe21

Browse files
authored
Merge pull request #60 from thedodd/57-remove-clones
Update Model trait to use `&db` instead of `db.clone()`.
2 parents 2a9f19d + be77a2a commit 1dffe21

File tree

16 files changed

+903
-1562
lines changed

16 files changed

+903
-1562
lines changed

CHANGELOG.md

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,9 @@ changelog
33

44
## [unreleased]
55

6-
### added
7-
6+
## 0.9.0-alpha.2
87
### changed
9-
10-
### removed
8+
- All Model trait methods have been updated to take a reference to a `mongodb::Database` instance, no more `db.clone()` required. Thanks to @mehmetsefabalik for pointing out that this is now possible with the 1.x version of the driver.
119

1210
## 0.9.0-alpha.1
1311

Cargo.lock

Lines changed: 832 additions & 407 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,17 +55,17 @@ struct User {
5555
async fn main() -> Result<()> {
5656
// Connect & sync indexes.
5757
let db = Client::with_uri_str("mongodb://localhost:27017/").await?.database("mydb");
58-
User::sync(db.clone()).await?;
58+
User::sync(&db).await?;
5959

6060
// Create a user.
6161
let mut me = User{id: None, email: String::from("[email protected]")};
62-
me.save(db.clone(), None).await?;
62+
me.save(&db, None).await?;
6363

6464
// Update user's email address.
65-
me.update(db.clone(), None, doc!{"$set": doc!{"email": "[email protected]"}}, None).await?;
65+
me.update(&db, None, doc!{"$set": doc!{"email": "[email protected]"}}, None).await?;
6666

6767
// Fetch all users.
68-
let mut cursor = User::find(db.clone(), None, None).await?;
68+
let mut cursor = User::find(&db, None, None).await?;
6969
while let Some(user) = cursor.next().await {
7070
println!("{:?}", user);
7171
}

wither/Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,15 @@ license = "Apache-2.0"
99
name = "wither"
1010
readme = "../README.md"
1111
repository = "https://github.com/thedodd/wither"
12-
version = "0.9.0-alpha.1"
12+
version = "0.9.0-alpha.2"
1313
edition = "2018"
1414

1515
[dependencies]
1616
chrono = "0.4"
1717
log = "0.4"
1818
mongodb = { version = "1", default-features=false }
1919
serde = { version = "1", features=["derive"] }
20-
wither_derive = { version = "0.9.0-alpha.1", path = "../wither_derive", default-features = false }
20+
wither_derive = { version = "0.9.0-alpha.2", path = "../wither_derive", default-features = false }
2121
async-trait = "0.1.33"
2222
futures = "0.3.5"
2323
thiserror = "1.0.19"

wither/examples/readme.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,17 @@ struct User {
1919
async fn main() -> Result<()> {
2020
// Connect & sync indexes.
2121
let db = Client::with_uri_str("mongodb://localhost:27017/").await?.database("mydb");
22-
User::sync(db.clone()).await?;
22+
User::sync(&db).await?;
2323

2424
// Create a user.
2525
let mut me = User{id: None, email: String::from("[email protected]")};
26-
me.save(db.clone(), None).await?;
26+
me.save(&db, None).await?;
2727

2828
// Update user's email address.
29-
me.update(db.clone(), None, doc!{"$set": doc!{"email": "[email protected]"}}, None).await?;
29+
me.update(&db, None, doc!{"$set": doc!{"email": "[email protected]"}}, None).await?;
3030

3131
// Fetch all users.
32-
let mut cursor = User::find(db.clone(), None, None).await?;
32+
let mut cursor = User::find(&db, None, None).await?;
3333
while let Some(user) = cursor.next().await {
3434
println!("{:?}", user);
3535
}

wither/src/migration.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ pub trait Migrating: Model {
1212
fn migrations() -> Vec<Box<dyn Migration>>;
1313

1414
/// Execute all migrations for this model.
15-
async fn migrate(db: Database) -> Result<()> {
15+
async fn migrate(db: &Database) -> Result<()> {
1616
let coll = Self::collection(db);
1717
let ns = coll.namespace();
1818
let migrations = Self::migrations();

wither/src/model.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ pub trait Model where Self: Serialize + DeserializeOwned {
7373
///
7474
/// This method uses the model's `selection_criteria`, `read_concern` & `write_concern` when
7575
/// constructing the collection handle.
76-
fn collection(db: Database) -> Collection {
76+
fn collection(db: &Database) -> Collection {
7777
db.collection_with_options(
7878
Self::COLLECTION_NAME,
7979
options::CollectionOptions::builder()
@@ -85,7 +85,7 @@ pub trait Model where Self: Serialize + DeserializeOwned {
8585
}
8686

8787
/// Find all instances of this model matching the given query.
88-
async fn find<F, O>(db: Database, filter: F, options: O) -> Result<ModelCursor<Self>>
88+
async fn find<F, O>(db: &Database, filter: F, options: O) -> Result<ModelCursor<Self>>
8989
where
9090
F: Into<Option<Document>> + Send,
9191
O: Into<Option<options::FindOptions>> + Send,
@@ -97,7 +97,7 @@ pub trait Model where Self: Serialize + DeserializeOwned {
9797
}
9898

9999
/// Find the one model record matching your query, returning a model instance.
100-
async fn find_one<F, O>(db: Database, filter: F, options: O) -> Result<Option<Self>>
100+
async fn find_one<F, O>(db: &Database, filter: F, options: O) -> Result<Option<Self>>
101101
where
102102
F: Into<Option<Document>> + Send,
103103
O: Into<Option<options::FindOneOptions>> + Send,
@@ -110,7 +110,7 @@ pub trait Model where Self: Serialize + DeserializeOwned {
110110
}
111111

112112
/// Finds a single document and deletes it, returning the original.
113-
async fn find_one_and_delete<O>(db: Database, filter: Document, options: O) -> Result<Option<Self>>
113+
async fn find_one_and_delete<O>(db: &Database, filter: Document, options: O) -> Result<Option<Self>>
114114
where O: Into<Option<options::FindOneAndDeleteOptions>> + Send,
115115
{
116116
Ok(Self::collection(db)
@@ -121,7 +121,7 @@ pub trait Model where Self: Serialize + DeserializeOwned {
121121
}
122122

123123
/// Finds a single document and replaces it, returning either the original or replaced document.
124-
async fn find_one_and_replace<O>(db: Database, filter: Document, replacement: Document, options: O) -> Result<Option<Self>>
124+
async fn find_one_and_replace<O>(db: &Database, filter: Document, replacement: Document, options: O) -> Result<Option<Self>>
125125
where O: Into<Option<options::FindOneAndReplaceOptions>> + Send,
126126
{
127127
Ok(Self::collection(db)
@@ -132,7 +132,7 @@ pub trait Model where Self: Serialize + DeserializeOwned {
132132
}
133133

134134
/// Finds a single document and updates it, returning either the original or updated document.
135-
async fn find_one_and_update<U, O>(db: Database, filter: Document, update: U, options: O) -> Result<Option<Self>>
135+
async fn find_one_and_update<U, O>(db: &Database, filter: Document, update: U, options: O) -> Result<Option<Self>>
136136
where
137137
U: Into<options::UpdateModifications> + Send,
138138
O: Into<Option<options::FindOneAndUpdateOptions>> + Send,
@@ -162,7 +162,7 @@ pub trait Model where Self: Serialize + DeserializeOwned {
162162
///
163163
/// **NOTE WELL:** in order to ensure needed behavior of this method, it will force `journaled`
164164
/// write concern.
165-
async fn save(&mut self, db: Database, filter: Option<Document>) -> Result<()> {
165+
async fn save(&mut self, db: &Database, filter: Option<Document>) -> Result<()> {
166166
let coll = Self::collection(db);
167167
let instance_doc = Self::document_from_instance(&self)?;
168168

@@ -216,7 +216,7 @@ pub trait Model where Self: Serialize + DeserializeOwned {
216216
/// concern `journaling` is set to `true`, so that we can receive a complete output document.
217217
///
218218
/// If this model instance was never written to the database, this operation will return an error.
219-
async fn update(self, db: Database, filter: Option<Document>, update: Document, opts: Option<options::FindOneAndUpdateOptions>) -> Result<Self> {
219+
async fn update(self, db: &Database, filter: Option<Document>, update: Document, opts: Option<options::FindOneAndUpdateOptions>) -> Result<Self> {
220220
// Extract model's ID & use as filter for this operation.
221221
let id = self.id().ok_or_else(|| WitherError::ModelIdRequiredForOperation)?;
222222

@@ -266,7 +266,7 @@ pub trait Model where Self: Serialize + DeserializeOwned {
266266
/// Delete this model instance by ID.
267267
///
268268
/// Wraps the driver's `Collection.delete_one` method.
269-
async fn delete(&self, db: Database) -> Result<DeleteResult> {
269+
async fn delete(&self, db: &Database) -> Result<DeleteResult> {
270270
// Return an error if the instance was never saved.
271271
let id = self.id().ok_or_else(|| WitherError::ModelIdRequiredForOperation)?;
272272
Ok(Self::collection(db).delete_one(doc!{"_id": id}, None).await?)
@@ -304,7 +304,7 @@ pub trait Model where Self: Serialize + DeserializeOwned {
304304
/// This routine will destroy any indexes found on this model's collection which are not
305305
/// defined in the response from `Self.indexes()`.
306306
#[deprecated(since="0.9.0", note="Index management is currently missing in the underlying driver, so this method no longer does anything. We are hoping to re-enable this in a future release.")]
307-
async fn sync(_db: Database) -> Result<()> {
307+
async fn sync(_db: &Database) -> Result<()> {
308308
// NOTE: blocked by https://jira.mongodb.org/projects/RUST/issues/RUST-166
309309
Ok(())
310310
}

wither/src/sync/cursor.rs

Lines changed: 0 additions & 32 deletions
This file was deleted.

wither/src/sync/migration.rs

Lines changed: 0 additions & 98 deletions
This file was deleted.

wither/src/sync/mod.rs

Lines changed: 0 additions & 11 deletions
This file was deleted.

0 commit comments

Comments
 (0)