express-session-rethinkdb-esm 0.0.3 → 0.0.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -10,6 +10,16 @@ Thank you to everyone writing `express-session` drivers out there for the boiler
10
10
 
11
11
  `npm install express-session-rethinkdb-esm --save`
12
12
 
13
+ ## Constructor Options
14
+
15
+ `client`: **Required** -- RethinkDB import.
16
+
17
+ `conn`: **Required** -- RethinkDB database connection.
18
+
19
+ `table`: RethinkDB table to store sessions in (default: `sessions`).
20
+
21
+ `ttl`: Time in milliseconds to expire sessions (default: two weeks).
22
+
13
23
  ## Usage
14
24
 
15
25
  ```javascript
@@ -30,12 +40,17 @@ app.use( session({
30
40
  })
31
41
  ```
32
42
 
33
- ## Constructor Options
43
+ ### Wait on Readiness
34
44
 
35
- `client`: **Required** -- RethinkDB import.
36
-
37
- `conn`: **Required** -- RethinkDB database connection.
45
+ Using the `ready` property to wait for store initialization. Store will ensure the `table` table is created and has a secondary index for `expires` if not already.
38
46
 
39
- `table`: RethinkDB table to store sessions in (default: `sessions`).
47
+ ```javascript
48
+ // Thennable
49
+ rethinkdbSessionStoreInstance.ready.then( async () => {
50
+ await rethinkdbSessionStoreInstance.vacuum()
51
+ })
40
52
 
41
- `ttl`: Time in milliseconds to expire sessions (default: two weeks).
53
+ // Async/Await
54
+ await rethinkdbSessionStoreInstance.ready
55
+ await rethinkdbSessionStoreInstance.vacuum()
56
+ ```
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "express-session-rethinkdb-esm",
3
- "version": "0.0.3",
3
+ "version": "0.0.5",
4
4
  "license": "MIT",
5
5
  "author": "Robert Sirois",
6
6
  "homepage": "https://github.com/rpsirois/express-session-rethinkdb-esm#readme",
package/rdb-session.js CHANGED
@@ -3,6 +3,8 @@ const defaults = {
3
3
  table: 'sessions',
4
4
  }
5
5
 
6
+ let r
7
+
6
8
  export default cfg => (
7
9
  class RethinkdbSessionStore extends cfg.session.Store {
8
10
  constructor( opt ) {
@@ -10,7 +12,9 @@ export default cfg => (
10
12
  Object.assign(this, defaults, opt)
11
13
  if (! this.client) throw new Error('RethinkdbSessionStore requires a client in setup options.')
12
14
  if (! this.conn) throw new Error('RethinkdbSessionStore requires a connection in setup options.')
13
- this.initDb(this.client, this.conn)
15
+ let _p_then
16
+ this.ready = new Promise( then => _p_then = then )
17
+ this.initDb( this.client, this.conn, _p_then )
14
18
  }
15
19
 
16
20
  getExpiry( cookie ) {
@@ -19,18 +23,33 @@ export default cfg => (
19
23
  : new Date(Date.now() + this.ttl)
20
24
  }
21
25
 
22
- async initDb( r, rc ) {
26
+ async initDb( _r, rc, then ) {
27
+ r = _r
23
28
  let { table } = this
24
- let existing_tables = await r.tableList().run(rc)
25
29
 
26
- if (existing_tables.indexOf( table ) < 0)
27
- await r.tableCreate( table ).run(rc)
28
- this._t = r.table(table)
30
+ // table
31
+ const existing_tables = new Set( await r.tableList().run( rc ) )
32
+
33
+ if ( !existing_tables.has( table ) ) await r.tableCreate( table ).run(rc)
34
+
35
+ const _t = this._t = r.table( table )
36
+
37
+ // index
38
+ const existing_indices = new Set( await _t.indexList().run( rc ) )
39
+
40
+ if ( !existing_indices.has( 'expires' ) ) {
41
+ await _t.indexCreate( 'expires' ).run( rc )
42
+ await _t.indexWait( 'expires' ).run( rc )
43
+ }
44
+
45
+ // ready
46
+ then( _t )
47
+ return _t
29
48
  }
30
49
 
31
50
  async all( cb ) {
32
51
  try {
33
- let result = await this._t.run( this.conn ).toArray()
52
+ let result = await ( await this._t.run( this.conn ) ).toArray()
34
53
  cb?.( null, result )
35
54
  return result
36
55
  } catch (err) {
@@ -127,4 +146,17 @@ export default cfg => (
127
146
  cb(err)
128
147
  }
129
148
  }
149
+
150
+ // custom functions (not part of the express-session Session Store Implementation)
151
+
152
+ // clear table of expired sessions
153
+ async vacuum( cb ) {
154
+ try {
155
+ await this._t.between( r.minval, new Date( Date.now() - this.ttl ), {index: 'expires'} ).delete().run( this.conn )
156
+ cb?.()
157
+ } catch ( err ) {
158
+ if ( !cb ) throw err
159
+ cb( err )
160
+ }
161
+ }
130
162
  })