Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support sharing connection pool client. #694

Merged
merged 4 commits into from
Oct 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/shaggy-cycles-compare.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@tirke/node-cache-manager-mongodb': minor
---

Add possibility to pass a Mongo Client to the constructor
6 changes: 2 additions & 4 deletions .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { caching } from 'cache-manager'
import { MongoClient } from 'mongodb'

import { mongoDbStore, MongoCache } from './node-cache-manager-mongodb'

Expand Down Expand Up @@ -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)
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ type Args = {
mongoConfig?: MongoClientOptions
collectionName?: string
databaseName?: string
client?: MongoClient
} & Config


Expand All @@ -36,7 +37,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)
Expand Down