Skip to content

Commit

Permalink
fix: createIndex should have correct async dependency setup
Browse files Browse the repository at this point in the history
  • Loading branch information
mingchuno committed Aug 17, 2021
1 parent ccd716a commit 788f603
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 13 deletions.
4 changes: 3 additions & 1 deletion src/lib/MongoStore.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,12 @@ test.serial('create store w/o provide required options', (t) => {
})
})

test.serial('create store with clientPromise', (t) => {
test.serial('create store with clientPromise', async (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)
await store.collectionP
store.close()
})

Expand All @@ -46,6 +47,7 @@ test.serial('create store with client', async (t) => {
const store = MongoStore.create({ client: client })
t.not(store, null)
t.not(store, undefined)
await store.collectionP
store.close()
})

Expand Down
24 changes: 12 additions & 12 deletions src/lib/MongoStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -198,13 +198,13 @@ export default class MongoStore extends session.Store {
assert(!!_clientP, 'Client is null|undefined')
this.clientP = _clientP
this.options = options
this.collectionP = _clientP
.then((con) => con.db(options.dbName))
.then((db) => db.collection(options.collectionName))
.then((collection) => {
this.setAutoRemove(collection)
return collection
})
this.collectionP = _clientP.then(async (con) => {
const collection = con
.db(options.dbName)
.collection(options.collectionName)
await this.setAutoRemove(collection)
return collection
})
if (options.crypto.secret) {
this.crypto = require('kruptein')(options.crypto)
}
Expand All @@ -214,7 +214,7 @@ export default class MongoStore extends session.Store {
return new MongoStore(options)
}

private setAutoRemove(collection: Collection) {
private setAutoRemove(collection: Collection): Promise<unknown> {
const removeQuery = () => ({
expires: {
$lt: new Date(),
Expand All @@ -223,14 +223,14 @@ export default class MongoStore extends session.Store {
switch (this.options.autoRemove) {
case 'native':
debug('Creating MongoDB TTL index')
collection.createIndex(
return collection.createIndex(
{ expires: 1 },
{
background: true,
expireAfterSeconds: 0,
writeConcern: this.options.writeOperationOptions,
}
)
break
case 'interval':
debug('create Timer to remove expired sessions')
this.timer = setInterval(
Expand All @@ -244,10 +244,10 @@ export default class MongoStore extends session.Store {
this.options.autoRemoveInterval * 1000 * 60
)
this.timer.unref()
break
return Promise.resolve()
case 'disabled':
default:
break
return Promise.resolve()
}
}

Expand Down

0 comments on commit 788f603

Please sign in to comment.