From 406c1424c37916131fa4dcf1cb355a0e3b5b360b Mon Sep 17 00:00:00 2001 From: Tirke Date: Tue, 30 Jan 2024 22:05:22 +0100 Subject: [PATCH] feat: mongodb custom collection name --- .changeset/six-bugs-push.md | 18 +++++++++++ packages/node-cache-manager-mongodb/README.md | 14 ++++++++ .../lib/node-cache-manager-mongodb.spec.ts | 32 +++++++++++++++++++ .../src/lib/node-cache-manager-mongodb.ts | 6 +++- 4 files changed, 69 insertions(+), 1 deletion(-) create mode 100644 .changeset/six-bugs-push.md diff --git a/.changeset/six-bugs-push.md b/.changeset/six-bugs-push.md new file mode 100644 index 00000000..2d54d525 --- /dev/null +++ b/.changeset/six-bugs-push.md @@ -0,0 +1,18 @@ +--- +"@tirke/node-cache-manager-mongodb": minor +--- + +Can now specify a custom collection name. +Default collection name will remain cache if unspecified. + +```typescript +import { caching } from 'cache-manager' + +import { mongoDbStore } from './node-cache-manager-mongodb' + +const mongoCache = await caching(mongoDbStore, { + url: 'mongodb://localhost:27017', + collectionName: 'custom-collection-name', + mongoConfig: { auth: { password: '', username: '' } }, +}) +``` diff --git a/packages/node-cache-manager-mongodb/README.md b/packages/node-cache-manager-mongodb/README.md index e409f285..66a4977a 100644 --- a/packages/node-cache-manager-mongodb/README.md +++ b/packages/node-cache-manager-mongodb/README.md @@ -50,3 +50,17 @@ await mongoCache.set('foo', 'bar', { ttl: 5 }) const result = await mongoCache.get('foo') await mongoCache.del('foo') ``` + +### Custom collection name + +```typescript +import { caching } from 'cache-manager' + +import { mongoDbStore } from './node-cache-manager-mongodb' + +const mongoCache = await caching(mongoDbStore, { + url: 'mongodb://localhost:27017', + collectionName: 'custom-collection-name', + mongoConfig: { auth: { password: '', username: '' } }, +}) +``` 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 91ebb9e7..c4afcad5 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 @@ -263,3 +263,35 @@ describe('wrap function', () => { expect(await mongoCache.wrap(`user`, () => getUser(userId + 1))).toEqual({ id: userId }) }) }) + +describe('collectionName', () => { + it('should use a custom collection name', async () => { + const cacheWithCustomCollName = await caching(mongoDbStore, { + collectionName: 'custom-collection', + url: 'mongodb://localhost:27017', + ttl: 60, + mongoConfig: {}, + }) + + await cacheWithCustomCollName.set('foo', 'bar') + const collections = await cacheWithCustomCollName.store.db.listCollections().toArray() + + expect(collections[0].name).toEqual('custom-collection') + + await cacheWithCustomCollName.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') + const collections = await baseCache.store.db.listCollections().toArray() + expect(collections[0].name).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 2b857d39..f9b37644 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 @@ -17,18 +17,22 @@ interface MongoDbStore extends Store { type Args = { url?: string mongoConfig?: MongoClientOptions + collectionName?: string } & Config + class MongoDb implements MongoDbStore { public readonly client: MongoClient readonly internalTtl: number | undefined readonly isCacheable: (value: unknown) => boolean + readonly collectionName: string readonly db: Db private initIndexes = true constructor(args: Args) { this.internalTtl = args.ttl this.isCacheable = args.isCacheable || ((value: unknown) => value !== undefined && value !== null) + this.collectionName = args.collectionName || 'cache' if (args.url && args.mongoConfig) { this.client = new MongoClient(args.url, args.mongoConfig) } else if (!args.url && args.mongoConfig) { @@ -41,7 +45,7 @@ class MongoDb implements MongoDbStore { } async getColl() { - const coll = this.db.collection('cache') + const coll = this.db.collection(this.collectionName) if (this.initIndexes) { await coll.createIndex({ key: 1 }, { unique: true })