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, Collection } ;
21- use mongodb:: bson:: { doc, Bson , Document } ;
20+ use mongodb:: bson:: { self , doc, Bson , Document } ;
2221use mongodb:: options:: { ReplaceOptions , SelectionCriteria } ;
2322use mongodb:: Client ;
2423
2524/// A MongoDB session store.
2625#[ derive( Debug , Clone ) ]
2726pub struct MongodbSessionStore {
28- client : mongodb:: Client ,
29- db : String ,
30- coll_name : String ,
27+ collection : mongodb:: Collection < Document > ,
28+ database : mongodb:: Database ,
3129}
3230
3331impl MongodbSessionStore {
@@ -40,9 +38,9 @@ impl MongodbSessionStore {
4038 /// .await?;
4139 /// # Ok(()) }) }
4240 /// ```
43- pub async fn new ( uri : & str , db : & str , coll_name : & str ) -> mongodb:: error:: Result < Self > {
41+ pub async fn new ( uri : & str , db_name : & str , coll_name : & str ) -> mongodb:: error:: Result < Self > {
4442 let client = Client :: with_uri_str ( uri) . await ?;
45- let middleware = Self :: from_client ( client, db , coll_name) ;
43+ let middleware = Self :: from_client ( client, db_name , coll_name) ;
4644 middleware. create_expire_index ( "expireAt" , 0 ) . await ?;
4745 Ok ( middleware)
4846 }
@@ -66,16 +64,17 @@ impl MongodbSessionStore {
6664 /// let store = MongodbSessionStore::from_client(client, "db_name", "collection");
6765 /// # Ok(()) }) }
6866 /// ```
69- pub fn from_client ( client : Client , db : & str , coll_name : & str ) -> Self {
67+ pub fn from_client ( client : Client , db_name : & str , coll_name : & str ) -> Self {
68+ let database = client. database ( db_name) ;
69+ let collection = database. collection ( coll_name) ;
7070 Self {
71- client,
72- db : db. to_string ( ) ,
73- coll_name : coll_name. to_string ( ) ,
71+ database,
72+ collection,
7473 }
7574 }
7675
7776 /// 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.
77+ /// that mongodb provides < https://docs.mongodb.com/manual/tutorial/expire-data/#expire-documents-at-a-specific-clock-time> .
7978 /// The default ttl applyed to sessions without expiry is 20 minutes.
8079 /// If the `expireAt` date field contains a date in the past, mongodb considers the document expired and will be deleted.
8180 /// Note: mongodb runs the expiration logic every 60 seconds.
@@ -89,8 +88,7 @@ impl MongodbSessionStore {
8988 /// # Ok(()) }) }
9089 /// ```
9190 pub async fn initialize ( & self ) -> Result {
92- let _ = & self . index_on_expiry_at ( ) . await ?;
93- Ok ( ( ) )
91+ self . index_on_expiry_at ( ) . await
9492 }
9593
9694 /// private associated function
@@ -102,7 +100,7 @@ impl MongodbSessionStore {
102100 expire_after_seconds : u32 ,
103101 ) -> mongodb:: error:: Result < ( ) > {
104102 let create_index = doc ! {
105- "createIndexes" : & self . coll_name ,
103+ "createIndexes" : self . collection . name ( ) ,
106104 "indexes" : [
107105 {
108106 "key" : { field_name: 1 } ,
@@ -111,8 +109,7 @@ impl MongodbSessionStore {
111109 }
112110 ]
113111 } ;
114- self . client
115- . database ( & self . db )
112+ self . database
116113 . run_command (
117114 create_index,
118115 SelectionCriteria :: ReadPreference ( mongodb:: options:: ReadPreference :: Primary ) ,
@@ -123,7 +120,7 @@ impl MongodbSessionStore {
123120
124121 /// Create a new index for the `expireAt` property, allowing to expire sessions at a specific clock time.
125122 /// 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
123+ /// < https://docs.mongodb.com/manual/tutorial/expire-data/#expire-documents-at-a-specific-clock-time>
127124 /// ```rust
128125 /// # fn main() -> async_session::Result { async_std::task::block_on(async {
129126 /// # use async_mongodb_session::MongodbSessionStore;
@@ -142,14 +139,13 @@ impl MongodbSessionStore {
142139#[ async_trait]
143140impl SessionStore for MongodbSessionStore {
144141 async fn store_session ( & self , session : Session ) -> Result < Option < String > > {
145- let coll = self . client . database ( & self . db ) . collection ( & self . coll_name ) ;
142+ let coll = & self . collection ;
146143
147144 let value = bson:: to_bson ( & session) ?;
148145 let id = session. id ( ) ;
149146 let query = doc ! { "session_id" : id } ;
150147 let expire_at = match session. expiry ( ) {
151148 None => Utc :: now ( ) + Duration :: from_std ( std:: time:: Duration :: from_secs ( 1200 ) ) . unwrap ( ) ,
152-
153149 Some ( expiry) => * { expiry } ,
154150 } ;
155151 let replacement = doc ! { "session_id" : id, "session" : value, "expireAt" : expire_at, "created" : Utc :: now( ) } ;
@@ -162,7 +158,7 @@ impl SessionStore for MongodbSessionStore {
162158
163159 async fn load_session ( & self , cookie_value : String ) -> Result < Option < Session > > {
164160 let id = Session :: id_from_cookie_value ( & cookie_value) ?;
165- let coll: Collection < Document > = self . client . database ( & self . db ) . collection ( & self . coll_name ) ;
161+ let coll = & self . collection ;
166162 let filter = doc ! { "session_id" : id } ;
167163 match coll. find_one ( filter, None ) . await ? {
168164 None => Ok ( None ) ,
@@ -175,7 +171,7 @@ impl SessionStore for MongodbSessionStore {
175171 // https://docs.mongodb.com/manual/core/index-ttl/#timing-of-the-delete-operation
176172 // This prevents those documents being returned
177173 if let Some ( expiry_at) = doc. get ( "expireAt" ) . and_then ( Bson :: as_datetime) {
178- if expiry_at < & Utc :: now ( ) . into ( ) {
174+ if expiry_at. to_chrono ( ) < Utc :: now ( ) {
179175 return Ok ( None ) ;
180176 }
181177 }
@@ -185,14 +181,14 @@ impl SessionStore for MongodbSessionStore {
185181 }
186182
187183 async fn destroy_session ( & self , session : Session ) -> Result {
188- let coll: Collection < Document > = self . client . database ( & self . db ) . collection ( & self . coll_name ) ;
184+ let coll = & self . collection ;
189185 coll. delete_one ( doc ! { "session_id" : session. id( ) } , None )
190186 . await ?;
191187 Ok ( ( ) )
192188 }
193189
194190 async fn clear_store ( & self ) -> Result {
195- let coll: Collection < Document > = self . client . database ( & self . db ) . collection ( & self . coll_name ) ;
191+ let coll = & self . collection ;
196192 coll. drop ( None ) . await ?;
197193 self . initialize ( ) . await ?;
198194 Ok ( ( ) )
0 commit comments