Skip to content

Commit

Permalink
feat: mongodb custom collection name
Browse files Browse the repository at this point in the history
  • Loading branch information
Tirke committed Jan 30, 2024
1 parent 091ea4d commit 406c142
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 1 deletion.
18 changes: 18 additions & 0 deletions .changeset/six-bugs-push.md
Original file line number Diff line number Diff line change
@@ -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: '<password>', username: '<user>' } },
})
```
14 changes: 14 additions & 0 deletions packages/node-cache-manager-mongodb/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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: '<password>', username: '<user>' } },
})
```
Original file line number Diff line number Diff line change
Expand Up @@ -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()
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -41,7 +45,7 @@ class MongoDb implements MongoDbStore {
}

async getColl() {
const coll = this.db.collection<CacheDoc>('cache')
const coll = this.db.collection<CacheDoc>(this.collectionName)

if (this.initIndexes) {
await coll.createIndex({ key: 1 }, { unique: true })
Expand Down

0 comments on commit 406c142

Please sign in to comment.