Skip to content

Commit 433d1d9

Browse files
committed
Update dependencies
Signed-off-by: Aaron Erhardt <[email protected]>
1 parent ecd4b33 commit 433d1d9

File tree

5 files changed

+48
-38
lines changed

5 files changed

+48
-38
lines changed

Cargo.toml

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,36 @@
11
[package]
22
name = "async-mongodb-session"
3-
version = "2.0.0"
3+
version = "3.0.0"
44
license = "MIT OR Apache-2.0"
55
repository = "https://github.com/yoshuawuyts/async-mongodb-session"
66
documentation = "https://docs.rs/async-mongodb-session"
77
description = "An async-session implementation for MongoDB"
88
readme = "README.md"
9-
edition = "2018"
9+
edition = "2021"
1010
keywords = ["tide", "web", "async", "session", "mongodb"]
1111
categories = [
1212
"network-programming",
1313
"asynchronous",
14-
"web-programming::http-server"
14+
"web-programming::http-server",
1515
]
1616
authors = [
17-
"Yoshua Wuyts <[email protected]>",
18-
"Irina Shestak <[email protected]>",
19-
"Anton Whalley <[email protected]>",
20-
"Javier Viola <[email protected]>"
17+
"Yoshua Wuyts <[email protected]>",
18+
"Irina Shestak <[email protected]>",
19+
"Anton Whalley <[email protected]>",
20+
"Javier Viola <[email protected]>",
21+
"Aaron Erhardt <[email protected]>",
2122
]
2223

2324
[features]
2425

2526
[dependencies]
26-
mongodb = { version = "1.1.1", default-features = false, features = ["async-std-runtime"] }
27-
async-session = "2.0.0"
27+
async-session = "3"
28+
mongodb = { version = "2.1", default-features = false, features = [
29+
"async-std-runtime",
30+
"bson-chrono-0_4",
31+
] }
2832

2933
[dev-dependencies]
30-
async-std = { version = "1.8.0", features = ["attributes"] }
31-
rand = {version = "0.7.3"}
34+
async-std = { version = "1.10", features = ["attributes"] }
3235
lazy_static = "1"
33-
tide = "0.15"
36+
rand = { version = "0.8" }

examples/tide/Cargo.toml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
[package]
2+
name = "tide"
3+
version = "0.1.0"
4+
edition = "2021"
5+
6+
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
7+
8+
[dependencies]
9+
async-mongodb-session = { path = "../../" }
10+
tide = "0.17.0-beta.1"
11+
async-std = "1.10"
Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
extern crate async_mongodb_session;
2-
extern crate tide;
3-
41
use async_mongodb_session::MongodbSessionStore;
52

63
#[async_std::main]

src/lib.rs

Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -13,21 +13,20 @@
1313
1414
#![forbid(unsafe_code, future_incompatible, rust_2018_idioms)]
1515
#![deny(missing_debug_implementations, nonstandard_style)]
16-
#![warn(missing_docs, missing_doc_code_examples, unreachable_pub)]
16+
#![warn(missing_docs, rustdoc::missing_doc_code_examples, unreachable_pub)]
1717

1818
use async_session::chrono::{Duration, Utc};
1919
use async_session::{async_trait, Result, Session, SessionStore};
20-
use mongodb::bson;
20+
use mongodb::bson::{self, Document};
2121
use mongodb::bson::{doc, Bson};
2222
use mongodb::options::{ReplaceOptions, SelectionCriteria};
2323
use mongodb::Client;
2424

2525
/// A MongoDB session store.
2626
#[derive(Debug, Clone)]
2727
pub struct MongodbSessionStore {
28-
client: mongodb::Client,
29-
db: String,
30-
coll_name: String,
28+
collection: mongodb::Collection<Document>,
29+
database: mongodb::Database,
3130
}
3231

3332
impl MongodbSessionStore {
@@ -40,9 +39,9 @@ impl MongodbSessionStore {
4039
/// .await?;
4140
/// # Ok(()) }) }
4241
/// ```
43-
pub async fn new(uri: &str, db: &str, coll_name: &str) -> mongodb::error::Result<Self> {
42+
pub async fn new(uri: &str, db_name: &str, coll_name: &str) -> mongodb::error::Result<Self> {
4443
let client = Client::with_uri_str(uri).await?;
45-
let middleware = Self::from_client(client, db, coll_name);
44+
let middleware = Self::from_client(client, db_name, coll_name);
4645
middleware.create_expire_index("expireAt", 0).await?;
4746
Ok(middleware)
4847
}
@@ -66,16 +65,17 @@ impl MongodbSessionStore {
6665
/// let store = MongodbSessionStore::from_client(client, "db_name", "collection");
6766
/// # Ok(()) }) }
6867
/// ```
69-
pub fn from_client(client: Client, db: &str, coll_name: &str) -> Self {
68+
pub fn from_client(client: Client, db_name: &str, coll_name: &str) -> Self {
69+
let database = client.database(db_name);
70+
let collection = database.collection(coll_name);
7071
Self {
71-
client,
72-
db: db.to_string(),
73-
coll_name: coll_name.to_string(),
72+
database,
73+
collection,
7474
}
7575
}
7676

7777
/// Initialize the default expiration mechanism, based on the document expiration
78-
/// that mongodb provides https://docs.mongodb.com/manual/tutorial/expire-data/#expire-documents-at-a-specific-clock-time.
78+
/// that mongodb provides <https://docs.mongodb.com/manual/tutorial/expire-data/#expire-documents-at-a-specific-clock-time>.
7979
/// The default ttl applyed to sessions without expiry is 20 minutes.
8080
/// If the `expireAt` date field contains a date in the past, mongodb considers the document expired and will be deleted.
8181
/// Note: mongodb runs the expiration logic every 60 seconds.
@@ -89,7 +89,7 @@ impl MongodbSessionStore {
8989
/// # Ok(()) }) }
9090
/// ```
9191
pub async fn initialize(&self) -> Result {
92-
&self.index_on_expiry_at().await?;
92+
self.index_on_expiry_at().await?;
9393
Ok(())
9494
}
9595

@@ -102,7 +102,7 @@ impl MongodbSessionStore {
102102
expire_after_seconds: u32,
103103
) -> mongodb::error::Result<()> {
104104
let create_index = doc! {
105-
"createIndexes": &self.coll_name,
105+
"createIndexes": self.collection.name(),
106106
"indexes": [
107107
{
108108
"key" : { field_name: 1 },
@@ -111,8 +111,7 @@ impl MongodbSessionStore {
111111
}
112112
]
113113
};
114-
self.client
115-
.database(&self.db)
114+
self.database
116115
.run_command(
117116
create_index,
118117
SelectionCriteria::ReadPreference(mongodb::options::ReadPreference::Primary),
@@ -123,7 +122,7 @@ impl MongodbSessionStore {
123122

124123
/// Create a new index for the `expireAt` property, allowing to expire sessions at a specific clock time.
125124
/// If the `expireAt` date field contains a date in the past, mongodb considers the document expired and will be deleted.
126-
/// https://docs.mongodb.com/manual/tutorial/expire-data/#expire-documents-at-a-specific-clock-time
125+
/// <https://docs.mongodb.com/manual/tutorial/expire-data/#expire-documents-at-a-specific-clock-time>
127126
/// ```rust
128127
/// # fn main() -> async_session::Result { async_std::task::block_on(async {
129128
/// # use async_mongodb_session::MongodbSessionStore;
@@ -142,7 +141,7 @@ impl MongodbSessionStore {
142141
#[async_trait]
143142
impl SessionStore for MongodbSessionStore {
144143
async fn store_session(&self, session: Session) -> Result<Option<String>> {
145-
let coll = self.client.database(&self.db).collection(&self.coll_name);
144+
let coll = &self.collection;
146145

147146
let value = bson::to_bson(&session)?;
148147
let id = session.id();
@@ -162,7 +161,7 @@ impl SessionStore for MongodbSessionStore {
162161

163162
async fn load_session(&self, cookie_value: String) -> Result<Option<Session>> {
164163
let id = Session::id_from_cookie_value(&cookie_value)?;
165-
let coll = self.client.database(&self.db).collection(&self.coll_name);
164+
let coll = &self.collection;
166165
let filter = doc! { "session_id": id };
167166
match coll.find_one(filter, None).await? {
168167
None => Ok(None),
@@ -175,7 +174,7 @@ impl SessionStore for MongodbSessionStore {
175174
// https://docs.mongodb.com/manual/core/index-ttl/#timing-of-the-delete-operation
176175
// This prevents those documents being returned
177176
if let Some(expiry_at) = doc.get("expireAt").and_then(Bson::as_datetime) {
178-
if expiry_at < &Utc::now() {
177+
if expiry_at.to_chrono() < Utc::now() {
179178
return Ok(None);
180179
}
181180
}
@@ -185,14 +184,14 @@ impl SessionStore for MongodbSessionStore {
185184
}
186185

187186
async fn destroy_session(&self, session: Session) -> Result {
188-
let coll = self.client.database(&self.db).collection(&self.coll_name);
187+
let coll = &self.collection;
189188
coll.delete_one(doc! { "session_id": session.id() }, None)
190189
.await?;
191190
Ok(())
192191
}
193192

194193
async fn clear_store(&self) -> Result {
195-
let coll = self.client.database(&self.db).collection(&self.coll_name);
194+
let coll = &self.collection;
196195
coll.drop(None).await?;
197196
self.initialize().await?;
198197
Ok(())

tests/test.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ mod tests {
1717
#[test]
1818
fn test_from_client() -> async_session::Result {
1919
async_std::task::block_on(async {
20-
let client_options = match ClientOptions::parse(&CONNECTION_STRING).await {
20+
let client_options = match ClientOptions::parse(CONNECTION_STRING.as_str()).await {
2121
Ok(c) => c,
2222
Err(e) => panic!("Client Options Failed: {}", e),
2323
};

0 commit comments

Comments
 (0)