From 3fd75139185b94fdcc2a3b2aa44c308e3089aa51 Mon Sep 17 00:00:00 2001 From: Truyen Huynh <59970064+truyenh20@users.noreply.github.com> Date: Mon, 23 Sep 2024 22:51:08 -0400 Subject: [PATCH 1/4] Support sharing connection pool client. This code currently does not allow you to set the external MongoDb client for re-use. A simple modification to this code would help utilize the external connection pool client --- .../src/lib/node-cache-manager-mongodb.ts | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) 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 43cf6569..844250cc 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 @@ -36,7 +36,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) From 34179543d73d05e51cf75eb807490b1933b80c10 Mon Sep 17 00:00:00 2001 From: Tirke Date: Mon, 21 Oct 2024 13:03:29 +0200 Subject: [PATCH 2/4] feat: fix type and add test --- .../src/lib/node-cache-manager-mongodb.spec.ts | 13 +++++++++++++ .../src/lib/node-cache-manager-mongodb.ts | 1 + 2 files changed, 14 insertions(+) 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 From 3315b5e0fe947122998b7869184ea2646ea94d11 Mon Sep 17 00:00:00 2001 From: Tirke Date: Mon, 21 Oct 2024 13:06:26 +0200 Subject: [PATCH 3/4] chore: changeset --- .changeset/shaggy-cycles-compare.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/shaggy-cycles-compare.md diff --git a/.changeset/shaggy-cycles-compare.md b/.changeset/shaggy-cycles-compare.md new file mode 100644 index 00000000..f4102cc5 --- /dev/null +++ b/.changeset/shaggy-cycles-compare.md @@ -0,0 +1,5 @@ +--- +'@tirke/node-cache-manager-mongodb': minor +--- + +Add possibility to pass a Mongo Client to the constructor From 85af6a9cc265c0b4864260529f0865e090284c7e Mon Sep 17 00:00:00 2001 From: Tirke Date: Mon, 21 Oct 2024 13:07:51 +0200 Subject: [PATCH 4/4] fix: lint semi rule --- .eslintrc.json | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/.eslintrc.json b/.eslintrc.json index 41705cef..61e37e68 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -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" } } ]