Registry / database / cache-manager-mongoose

cache-manager-mongoose

JSON →
library1.0.1jsnpmunverified

Mongoose store for node-cache-manager that uses an existing Mongoose connection to cache data in MongoDB. Version 1.0.1 is stable, with infrequent releases. Key differentiators: allows custom Mongoose models, supports TTL-based expiration via MongoDB TTL index, and lets you reuse existing connections instead of creating a new one. Compared to other MongoDB cache stores, it integrates tightly with Mongoose schemas.

npm install cache-manager-mongoose
INSTALL
IMPORT
SIG · CACHE-MANAGER-MONG
C
cache-manager-mongoose
databasejavascriptv1.0.1
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

default
const mongooseStore = require('cache-manager-mongoose');
import mongooseStore from 'cache-manager-mongoose';
Package is CJS-only; no default ESM export. Use require (or use createRequire in ESM).
cacheManager
const cacheManager = require('cache-manager');
Used to create a cache instance with mongooseStore as the store.
mongoose
const mongoose = require('mongoose');
Required to pass to cache-manager options when using a Mongoose connection.

Shows basic usage: connect Mongoose, create a cache manager with mongoose store, set/get/delete a value.

const mongoose = require('mongoose'); const cacheManager = require('cache-manager'); const mongooseStore = require('cache-manager-mongoose'); mongoose.connect('mongodb://localhost/test'); const cache = cacheManager.caching({ store: mongooseStore, mongoose: mongoose, ttl: 600 }); async function run() { await cache.set('myKey', 'myValue', { ttl: 300 }); const value = await cache.get('myKey'); console.log('Cached value:', value); await cache.del('myKey'); } run().catch(console.error);
Debug
Known issues
gotchaMust provide 'mongoose' option (mongoose instance) unless providing a custom 'model'
fix
Pass mongoose: require('mongoose') or your existing connection instance to the caching options.
affects: >=0.0.0
gotchaWhen using mongoose.createConnection(), you must explicitly pass the connection in options as 'connection'
fix
Pass connection: yourConnectionObject in addition to mongoose: mongoose to the caching options.
affects: >=0.0.0
deprecatedTTL of 0 used to mean forever but behavior may change in future versions
fix
Use a very large TTL or set 'ttl' option to Infinity/null if supported by cache-manager.
affects: >=1.0.0
Errors
Common errors & fixes
TypeError: Cannot read properties of undefined (reading 'model')
Mongoose instance not provided or incorrect options object
fix
Ensure options passed to cacheManager.caching include mongoose: require('mongoose')
MongooseError: Model 'MongooseCache' already exists
Multiple cache instances with same modelName or default name conflict
fix
Provide a unique modelName option to each cache instance, e.g., modelName: 'MyCache'
Error: store must be an object with methods get, set, del, etc.
Store not passed correctly or require('cache-manager-mongoose') returns undefined
fix
Check package is installed and required as const mongooseStore = require('cache-manager-mongoose'); (not destructured)
Upgrade
Version history
1.0.1latest on npm
Audit
Dependencies
cache-managerrequiredpeer dependency; provides caching API that this store implements
Agent activity
40 hits · last 30 days
node
36
OpenAI (training)
1
Resources
cache-manager-mongoose — npm install cache-manager-mongoose · libregistry