Skip to content

Commit

Permalink
dropping test-runner workspace for now... originally I was going to s…
Browse files Browse the repository at this point in the history
…upport CJS, but nah...

Adding simple JSdoc to the main class export.
  • Loading branch information
nathanb committed Nov 21, 2023
1 parent edbaa75 commit e062b00
Show file tree
Hide file tree
Showing 9 changed files with 108 additions and 31 deletions.
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
"author": "Nathan Bridgewater",
"license": "BSD-3-Clause",
"workspaces": [
"package",
"test-runner"
"package"
]
}
13 changes: 13 additions & 0 deletions package/LICENSE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
Copyright (c) 2023, Nathan Bridgewater / Integrated Web Systems, LLC

All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.

2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.

3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS “AS IS” AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
17 changes: 16 additions & 1 deletion package/README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,23 @@
# @iwsio/json-csv-node
# @iwsio/mongodb-express-session

<!-- [![Push to main - @iwsio/json-csv-node](https://github.com/iwsllc/json-csv/actions/workflows/push-node.yml/badge.svg)](https://github.com/iwsllc/json-csv/actions/workflows/push-node.yml) -->

This package provides an updated implementation of the [Express Session Store](https://www.npmjs.com/package/express-session#session-store-implementation) with [MongoDb 6 Driver](https://www.mongodb.com/docs/drivers/node/current/) as an ESM module. This store implements `touch` and `TTL`

# Usage
This module exports a class. The constructor inclues options.

```ts
import { MongoSessionStore } from '@iwsio/mongodb-express-session'

export const store = new MongoSessionStore({
// NOTE: these are default options:
uri: 'mongodb://localhost:27017/express_sessions',
collection: 'sessions',
ttl: 60 * 60 * 24 * 7 * 1000 // 1 week
})

store.on('error', function(error: any) {
console.error(error)
})
```
4 changes: 4 additions & 0 deletions package/src/defaults.mts
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
/**
* @module defaults - default object properties helper. This is my own implementation rather than relying on lodash as a dependency.
*/

/**
* Shallow defaults. This basically assigns in when target is undefined.
*
Expand Down
71 changes: 44 additions & 27 deletions package/src/index.mts
Original file line number Diff line number Diff line change
Expand Up @@ -2,37 +2,54 @@ import type { SessionData } from 'express-session'
import { MongoClient } from 'mongodb'
import { Store } from 'express-session'
import { defaults } from './defaults.mjs'

export type MongoSessionStoreOptions = {
uri: string;
collection: string;

/**
* The TTL (expiration) in milliseconds of the session.
*/
ttl: number | ((data: SessionData) => number);

/**
* The prefix of the session ID key in MongoDB.
*/
prefix: string;
};
import { MongoSessionStoreOptions } from './types/MongoSessionStoreOptions.mjs'
import { SessionDocument } from './types/SessionDocument.mjs'
import { StoreCallback, StoreCallbackMany, StoreCallbackOne, StoreCallbackTotal } from './types/StoreCallback.mjs'

const noop = (_err?: unknown, _data?: any) => {}

const _DEFAULTS: MongoSessionStoreOptions = { uri: 'mongodb://localhost:27017/test', collection: 'sessions', ttl: 86400000, prefix: '' }

type SessionDocument = {
_id: string;
expires: Date;
session: SessionData;
};

type StoreCallback = (err?: any) => void;
type StoreCallbackTotal = (err?: any, total?: number) => void
type StoreCallbackOne = (err?: any, session?: SessionData | null) => void
type StoreCallbackMany = (err?: any, session?: SessionData[]) => void
const _DEFAULTS: MongoSessionStoreOptions = {
uri: 'mongodb://localhost:27017/express-sessions',
collection: 'sessions',
ttl: 86400000,
prefix: ''
}

/**
* Express session store for MongoDB.
*
* Create a new instance and provide to the express-session options as `store` property.
*
* i.e.
* ```
import session from 'express-session'
import { MongoSessionStore } from '@iwsio/mongodb-express-session'
const mongoStore = new MongoSessionStore({
uri: 'mongodb://localhost:27017/express-sessions',
collection: 'sessions',
ttl: 60 * 60 * 24 * 7 * 1000 // 1 week
})
app.use(session({
secret: 'secret cookie key',
resave: false,
store: mongoStore,
saveUninitialized: false,
cookie: { secure: true, sameSite: 'strict' },
}))
* ```
*
* Default options:
* ```
* {
uri: 'mongodb://localhost:27017/express-sessions',
collection: 'sessions',
ttl: 86400000,
prefix: ''
}
* ```
*/
export class MongoSessionStore extends Store {
client: MongoClient
collectionName: string
Expand Down
2 changes: 1 addition & 1 deletion package/src/index.test.mts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ describe('MongoSessionStore', () => {
{ timeout: 1000 }
)
expect(error).not.to.be.ok
expect(constSpy).toBeCalledWith('mongodb://localhost:27017/test')
expect(constSpy).toBeCalledWith('mongodb://localhost:27017/express-sessions')
expect(info).to.equal('Connected to MongoDB')
expect(store.collectionName).to.equal('sessions')
expect(store.prefix).to.equal('')
Expand Down
16 changes: 16 additions & 0 deletions package/src/types/MongoSessionStoreOptions.mts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import type { SessionData } from 'express-session'

export type MongoSessionStoreOptions = {
uri: string;
collection: string;

/**
* The TTL (expiration) in milliseconds of the session.
*/
ttl: number | ((data: SessionData) => number);

/**
* The prefix of the session ID key in MongoDB.
*/
prefix: string;
};
7 changes: 7 additions & 0 deletions package/src/types/SessionDocument.mts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import type { SessionData } from 'express-session'

export type SessionDocument = {
_id: string;
expires: Date;
session: SessionData;
};
6 changes: 6 additions & 0 deletions package/src/types/StoreCallback.mts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { SessionData } from 'express-session'

export type StoreCallback = (err?: any) => void;
export type StoreCallbackTotal = (err?: any, total?: number) => void
export type StoreCallbackOne = (err?: any, session?: SessionData | null) => void
export type StoreCallbackMany = (err?: any, session?: SessionData[]) => void

0 comments on commit e062b00

Please sign in to comment.