Registry / database / ts-cache-mongoose

ts-cache-mongoose

JSON →
library2.2.1jsnpmunverified

v2.2.1 — Mongoose query and aggregate caching plugin supporting in-memory and Redis backends. Integrates via a .cache() method on Mongoose Query and Aggregate instances, with configurable TTL by string or number (seconds). Peer dependencies: mongoose (6.6-9.x), bson, and optional @nestjs/common. Tested against mongoose 6.12.2, 7.6.4, 8.23.0, and 9.4.1. Key differentiator: minimal API surface, ESM + CJS, TypeScript-first, with built-in bounded in-memory cache option.

npm install ts-cache-mongoose
INSTALL
IMPORT
SIG · TS-CACHE-MONGOOSE
T
ts-cache-mongoose
databasejavascriptv2.2.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 (cache)
import cache from 'ts-cache-mongoose'
const cache = require('ts-cache-mongoose')
Package defaults to ESM. CJS require() works only in CJS projects; use import for TypeScript.
init
cache.init(mongoose, { defaultTTL: '60 seconds', engine: 'memory' })
cache.init(mongoose, { ttl: 60, engine: 'memory' })
The config field is 'defaultTTL', not 'ttl'. TTL can be a string (e.g., '10 seconds') or number (seconds).
.cache() method
User.find({}).cache('10 seconds').exec()
User.find({}).cache({ ttl: '10 seconds' }).exec()
cache() takes a TTL string or number directly, not an options object.

Initialize the plugin with in-memory caching, run a cached query and aggregate.

import mongoose from 'mongoose'; import cache from 'ts-cache-mongoose'; // Initialize with in-memory engine cache.init(mongoose, { defaultTTL: '60 seconds', engine: 'memory', }); await mongoose.connect('mongodb://localhost:27017/test'); // Define a model const User = mongoose.model('User', new mongoose.Schema({ name: String, role: String })); // Cache a query const users = await User.find({ role: 'user' }).cache('10 seconds').exec(); console.log('Cached users:', users); // Cache an aggregate const counts = await User.aggregate([ { $group: { _id: '$role', count: { $sum: 1 } } } ]).cache('1 minute').exec(); console.log('Cached aggregate:', counts);
Debug
Known issues
gotchacache() does not accept an options object — pass TTL as a string or number directly
fix
Use .cache('30 seconds') instead of .cache({ ttl: '30 seconds' })
affects: >=2.0.0
deprecatedIn-memory cache is unbounded by default; may cause memory leaks under high cardinality queries
fix
Upgrade to >=2.2.0 and configure bounded cache via engineOptions.maxKeys
affects: <2.2.0
gotchaPeer dependency mongoose version must be >=6.6.0 <10; using an unsupported version may break
fix
Verify mongoose version with 'npm ls mongoose' and install compatible version
affects: >=2.0.0
breakingDrop of Node.js <20 support in v2.0.0
fix
Upgrade Node.js to >=20
affects: >=2.0.0
Errors
Common errors & fixes
Error: Cannot find module 'ts-cache-mongoose'
Missing installed package or incorrect import path
fix
Run 'npm install ts-cache-mongoose mongoose' and ensure import path is correct
TypeError: cache.init is not a function
Using CJS require() when package is ESM-only in certain bundler setups
fix
Use 'import cache from 'ts-cache-mongoose'' or enable ESM in your project
MongooseError: Operation `users.find()` buffering timed out after 10000ms
Mongoose not connected before running cached query
fix
Ensure 'await mongoose.connect(...)' completes before executing queries
Upgrade
Version history
2.2.1latest on npm
Audit
Dependencies
mongooserequiredpeer dependency — version 6.6.0 to 9.x required
bsonrequiredpeer dependency — version 4.7.2 to 7.x required
@nestjs/commonoptionalpeer dependency for NestJS integration — version 9.0.0 to 11.x
Agent activity
21 hits · last 30 days
node
18
Meta
2
OpenAI (training)
1
Resources
ts-cache-mongoose — npm install ts-cache-mongoose · libregistry