diff --git a/packages/node-cache-manager-mongodb/src/lib/node-cache-manager-mongodb.spec.ts b/packages/node-cache-manager-mongodb/src/lib/node-cache-manager-mongodb.spec.ts index 3522824d..38031e99 100644 --- a/packages/node-cache-manager-mongodb/src/lib/node-cache-manager-mongodb.spec.ts +++ b/packages/node-cache-manager-mongodb/src/lib/node-cache-manager-mongodb.spec.ts @@ -1,4 +1,5 @@ import { caching } from 'cache-manager' +import { MongoClient } from 'mongodb' import { mongoDbStore, MongoCache } from './node-cache-manager-mongodb' @@ -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) + }) +}) diff --git a/packages/node-cache-manager-mongodb/src/lib/node-cache-manager-mongodb.ts b/packages/node-cache-manager-mongodb/src/lib/node-cache-manager-mongodb.ts index 844250cc..20d4d36e 100644 --- a/packages/node-cache-manager-mongodb/src/lib/node-cache-manager-mongodb.ts +++ b/packages/node-cache-manager-mongodb/src/lib/node-cache-manager-mongodb.ts @@ -19,6 +19,7 @@ type Args = { mongoConfig?: MongoClientOptions collectionName?: string databaseName?: string + client?: MongoClient } & Config