From 262638976a7674d23c0942213012771b4263e543 Mon Sep 17 00:00:00 2001 From: Tim Hoffmann Date: Tue, 13 Feb 2024 16:25:27 +0100 Subject: [PATCH 1/4] feat: mongodb custom database name --- .../lib/node-cache-manager-mongodb.spec.ts | 30 +++++++++++++++++++ .../src/lib/node-cache-manager-mongodb.ts | 5 +++- 2 files changed, 34 insertions(+), 1 deletion(-) 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 c4afcad5..0ddcb80c 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 @@ -295,3 +295,33 @@ describe('collectionName', () => { await baseCache.reset() }) }) + +describe('databaseName', () => { + it('should use a custom database name', async () => { + const cacheWithCustomDatabaseName = await caching(mongoDbStore, { + databaseName: 'custom-database', + url: 'mongodb://localhost:27017', + ttl: 60, + mongoConfig: {}, + }) + + await cacheWithCustomDatabaseName.set('foo', 'bar') + + expect(cacheWithCustomDatabaseName.store.db.databaseName).toEqual('custom-database') + + await cacheWithCustomDatabaseName.reset() + }) + + it('should use cache as the default collection name', async () => { + const baseCache = await caching(mongoDbStore, { + url: 'mongodb://localhost:27017', + ttl: 60, + mongoConfig: {}, + }) + + await baseCache.set('foo', 'bar') + expect(baseCache.store.db.databaseName).toEqual('cache') + + await baseCache.reset() + }) +}) 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 f9b37644..43cf6569 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 @@ -18,6 +18,7 @@ type Args = { url?: string mongoConfig?: MongoClientOptions collectionName?: string + databaseName?: string } & Config @@ -26,6 +27,7 @@ class MongoDb implements MongoDbStore { readonly internalTtl: number | undefined readonly isCacheable: (value: unknown) => boolean readonly collectionName: string + readonly databaseName: string readonly db: Db private initIndexes = true @@ -33,6 +35,7 @@ class MongoDb implements MongoDbStore { this.internalTtl = args.ttl 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) { this.client = new MongoClient(args.url, args.mongoConfig) } else if (!args.url && args.mongoConfig) { @@ -41,7 +44,7 @@ class MongoDb implements MongoDbStore { throw new Error('MongoDB connection URL and mongoConfig are required') } - this.db = this.client.db('cache') + this.db = this.client.db(this.databaseName) } async getColl() { From d55b179741a889ca2d3881b2ff3e6aa7c91a633a Mon Sep 17 00:00:00 2001 From: Tim Hoffmann Date: Tue, 13 Feb 2024 16:28:24 +0100 Subject: [PATCH 2/4] docs: update README --- packages/node-cache-manager-mongodb/README.md | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/packages/node-cache-manager-mongodb/README.md b/packages/node-cache-manager-mongodb/README.md index 66a4977a..d1f5e3ea 100644 --- a/packages/node-cache-manager-mongodb/README.md +++ b/packages/node-cache-manager-mongodb/README.md @@ -64,3 +64,17 @@ const mongoCache = await caching(mongoDbStore, { mongoConfig: { auth: { password: '', username: '' } }, }) ``` + +### Custom database name + +```typescript +import { caching } from 'cache-manager' + +import { mongoDbStore } from './node-cache-manager-mongodb' + +const mongoCache = await caching(mongoDbStore, { + url: 'mongodb://localhost:27017', + databaseName: 'custom-database-name', + mongoConfig: { auth: { password: '', username: '' } }, +}) +``` From 2e82e4dcfdee3b57d5e47b98f70068bc6bfa5803 Mon Sep 17 00:00:00 2001 From: Tim Hoffmann Date: Tue, 13 Feb 2024 16:40:09 +0100 Subject: [PATCH 3/4] test: fix test description --- .../src/lib/node-cache-manager-mongodb.spec.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 0ddcb80c..3522824d 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 @@ -312,7 +312,7 @@ describe('databaseName', () => { await cacheWithCustomDatabaseName.reset() }) - it('should use cache as the default collection name', async () => { + it('should use cache as the default database name', async () => { const baseCache = await caching(mongoDbStore, { url: 'mongodb://localhost:27017', ttl: 60, From 8d95c84afd16ea64facaa84a40bdd7913a2b1418 Mon Sep 17 00:00:00 2001 From: Tim Hoffmann Date: Tue, 13 Feb 2024 21:33:35 +0100 Subject: [PATCH 4/4] chore: add changeset --- .changeset/spotty-crabs-hug.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/spotty-crabs-hug.md diff --git a/.changeset/spotty-crabs-hug.md b/.changeset/spotty-crabs-hug.md new file mode 100644 index 00000000..18467ed6 --- /dev/null +++ b/.changeset/spotty-crabs-hug.md @@ -0,0 +1,5 @@ +--- +"@tirke/node-cache-manager-mongodb": minor +--- + +add option to configure a custom database name