Skip to content

Commit

Permalink
Merge pull request #526 from tim-hoffmann/main
Browse files Browse the repository at this point in the history
mongodb: Add custom database name
  • Loading branch information
Tirke authored Feb 14, 2024
2 parents 7a6b501 + 8d95c84 commit 7981c84
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/spotty-crabs-hug.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@tirke/node-cache-manager-mongodb": minor
---

add option to configure a custom database name
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 @@ -64,3 +64,17 @@ const mongoCache = await caching(mongoDbStore, {
mongoConfig: { auth: { password: '<password>', username: '<user>' } },
})
```

### 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: '<password>', username: '<user>' } },
})
```
Original file line number Diff line number Diff line change
Expand Up @@ -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 database 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()
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ type Args = {
url?: string
mongoConfig?: MongoClientOptions
collectionName?: string
databaseName?: string
} & Config


Expand All @@ -26,13 +27,15 @@ 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

constructor(args: Args) {
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) {
Expand All @@ -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() {
Expand Down

0 comments on commit 7981c84

Please sign in to comment.