Skip to content

Commit

Permalink
feat: add client option when create store
Browse files Browse the repository at this point in the history
  • Loading branch information
mingchuno committed Mar 8, 2021
1 parent 70a77c0 commit 96538c8
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 6 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Added

- Add `client` option for non-promise client

## [4.2.2] - 2021-03-02

### Fixed
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,7 @@ One of the following options should be provided. If more than one option are pro
|:------:|------|-----------|
|1|`mongoUrl`|A [connection string](https://docs.mongodb.com/manual/reference/connection-string/) for creating a new MongoClient connection. If database name is not present in the connection string, database name should be provided using `dbName` option. |
|2|`clientPromise`|A Promise that is resolved with MongoClient connection. If the connection was established without database name being present in the connection string, database name should be provided using `dbName` option.|
|3|`client`|An existing MongoClient connection. If the connection was established without database name being present in the connection string, database name should be provided using `dbName` option.|

### More options

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "connect-mongo",
"version": "4.2.2",
"version": "4.3.0",
"description": "MongoDB session store for Express and Connect",
"main": "build/main/index.js",
"typings": "build/main/index.d.ts",
Expand Down
26 changes: 26 additions & 0 deletions src/lib/MongoStore.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import test from 'ava'
import { SessionData } from 'express-session'
import { MongoClient } from 'mongodb'

import MongoStore from './MongoStore'
import {
createStoreHelper,
makeData,
Expand All @@ -23,6 +25,30 @@ test.afterEach.always(async () => {
await storePromise.close()
})

test.serial('create store w/o provide required options', (t) => {
t.throws(() => MongoStore.create({}), {
message: /Cannot init client/,
})
})

test.serial('create store with clientPromise', (t) => {
const clientP = MongoClient.connect('mongodb://root:example@127.0.0.1:27017')
const store = MongoStore.create({ clientPromise: clientP })
t.not(store, null)
t.not(store, undefined)
store.close()
})

test.serial('create store with client', async (t) => {
const client = await MongoClient.connect(
'mongodb://root:example@127.0.0.1:27017'
)
const store = MongoStore.create({ client: client })
t.not(store, null)
t.not(store, undefined)
store.close()
})

test.serial('length should be 0', async (t) => {
;({ store, storePromise } = createStoreHelper())
const length = await storePromise.length()
Expand Down
13 changes: 8 additions & 5 deletions src/lib/MongoStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export type CryptoOptions = {
export type ConnectMongoOptions = {
mongoUrl?: string
clientPromise?: Promise<MongoClient>
client?: MongoClient
collectionName?: string
mongoOptions?: MongoClientOptions
dbName?: string
Expand All @@ -47,6 +48,7 @@ type ConcretCryptoOptions = Required<CryptoOptions>
type ConcretConnectMongoOptions = {
mongoUrl?: string
clientPromise?: Promise<MongoClient>
client?: MongoClient
collectionName: string
mongoOptions: MongoClientOptions
dbName?: string
Expand All @@ -65,8 +67,6 @@ type ConcretConnectMongoOptions = {
crypto: ConcretCryptoOptions
}

// type ErrorOrNull = Error | null

type InternalSessionType = {
_id: string
session: any
Expand Down Expand Up @@ -190,12 +190,15 @@ export default class MongoStore extends session.Store {
_clientP = MongoClient.connect(options.mongoUrl, options.mongoOptions)
} else if (options.clientPromise) {
_clientP = options.clientPromise
} else if (options.client) {
_clientP = Promise.resolve(options.client)
} else {
throw new Error('Cannot init client')
throw new Error('Cannot init client. Please provide correct options')
}
this.clientP = _clientP!
assert(!!_clientP, 'Client is null|undefined')
this.clientP = _clientP
this.options = options
this.collectionP = _clientP!
this.collectionP = _clientP
.then((con) => con.db(options.dbName))
.then((db) => db.collection(options.collectionName))
.then((collection) => {
Expand Down

0 comments on commit 96538c8

Please sign in to comment.