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
1818use async_session:: chrono:: { Duration , Utc } ;
1919use async_session:: { async_trait, Result , Session , SessionStore } ;
20- use mongodb:: bson;
20+ use mongodb:: bson:: { self , Document } ;
2121use mongodb:: bson:: { doc, Bson } ;
2222use mongodb:: options:: { ReplaceOptions , SelectionCriteria } ;
2323use mongodb:: Client ;
2424
2525/// A MongoDB session store.
2626#[ derive( Debug , Clone ) ]
2727pub struct MongodbSessionStore {
28- client : mongodb:: Client ,
29- db : String ,
30- coll_name : String ,
28+ collection : mongodb:: Collection < Document > ,
29+ database : mongodb:: Database ,
3130}
3231
3332impl 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]
143142impl 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 ( ( ) )
0 commit comments