Skip to content

Commit

Permalink
Merge pull request #694 from truyenh20/patch-1
Browse files Browse the repository at this point in the history
Support sharing connection pool client.
  • Loading branch information
Tirke authored Oct 21, 2024
2 parents 550fef1 + 85af6a9 commit 2530a8e
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 5 deletions.
5 changes: 5 additions & 0 deletions .changeset/shaggy-cycles-compare.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@tirke/node-cache-manager-mongodb': minor
---

Add possibility to pass a Mongo Client to the constructor
6 changes: 2 additions & 4 deletions .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,16 +43,14 @@
"newlines-between": "always"
}
],
"@typescript-eslint/no-extra-semi": "error",
"no-extra-semi": "off"
"no-extra-semi": "error"
}
},
{
"files": ["*.js", "*.jsx"],
"extends": ["plugin:@nx/javascript"],
"rules": {
"@typescript-eslint/no-extra-semi": "error",
"no-extra-semi": "off"
"no-extra-semi": "error"
}
}
]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { caching } from 'cache-manager'
import { MongoClient } from 'mongodb'

import { mongoDbStore, MongoCache } from './node-cache-manager-mongodb'

Expand Down Expand Up @@ -325,3 +326,15 @@ describe('databaseName', () => {
await baseCache.reset()
})
})

describe('reusing Mongo client', () => {
it('should reuse the client', async () => {
const mongoClient = new MongoClient('mongodb://localhost:27017')
const cache = await caching(mongoDbStore, {
client: mongoClient,
})

await cache.set('foo', 'bar')
expect(cache.store.client).toEqual(mongoClient)
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ type Args = {
mongoConfig?: MongoClientOptions
collectionName?: string
databaseName?: string
client?: MongoClient
} & Config


Expand All @@ -36,7 +37,9 @@ class MongoDb implements MongoDbStore {
this.isCacheable = args.isCacheable || ((value: unknown) => value !== undefined && value !== null)
this.collectionName = args.collectionName || 'cache'
this.databaseName = args.databaseName || 'cache'
if (args.url && args.mongoConfig) {
if (args.client) {
this.client = args.client
} else if (args.url && args.mongoConfig) {
this.client = new MongoClient(args.url, args.mongoConfig)
} else if (!args.url && args.mongoConfig) {
this.client = new MongoClient('mongodb://localhost:27017', args.mongoConfig)
Expand Down

0 comments on commit 2530a8e

Please sign in to comment.