Registry / database / mharj-mongoose-cache

mharj-mongoose-cache

JSON →
library0.9.3jsnpmunverified

An in-memory cache layer for Mongoose models that reduces database queries by maintaining a local copy of documents. Version 0.9.3, actively developed. Supports importing cached data from Mongoose queries, retrieving documents by ID or with list operations, and populating arrays of IDs. Can be synchronized via change streams, model hooks, or explicit add/delete calls. Emits events for cache updates, suitable for pushing data to presentation layers like websockets. Requires Mongoose >= 6.0.0 and @avanio/logger-like >= 0.0.1 as peer dependencies. Compared to mongoose-lean-cache or query-level caching, this provides a model-level cache with a simple API and change stream integration.

npm install mharj-mongoose-cache
INSTALL
IMPORT
SIG · MHARJ-MONGOOSE-CAC
M
mharj-mongoose-cache
databasejavascriptv0.9.3
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.

ModelCache
import { ModelCache } from 'mharj-mongoose-cache'
const ModelCache = require('mharj-mongoose-cache')
The package is ESM-only and ships TypeScript types. CommonJS require() will not work.
ModelCache
import { ModelCache } from 'mharj-mongoose-cache'
import ModelCache from 'mharj-mongoose-cache'
ModelCache is a named export, not a default export.

Creates a ModelCache for a 'Car' model, imports all documents, and demonstrates basic operations: get by ID, list all, iterate in chunks, and listen for updates.

import { ModelCache } from 'mharj-mongoose-cache'; import mongoose from 'mongoose'; // Define your Mongoose schema and model interface ICar { _id: string; name: string; } const carSchema = new mongoose.Schema<ICar>({ name: String }); const CarModel = mongoose.model<ICar>('Car', carSchema); // Create cache instance with logger const CarCache = new ModelCache<ICar>('Car', { logger: console }); // Import initial data from database const cars = await CarModel.find(); CarCache.import(cars); // Retrieve a document by ID declare const carId: string; const car = CarCache.get(carId); // List all documents const allCars = CarCache.list(); // Use iterator to process in chunks const chunkSession = CarCache.getChunkSession(100, { sort: (a, b) => a.name.localeCompare(b.name) }); const iter = chunkSession.getIterator(); for (const chunk of iter) { console.log(`Processing chunk of ${chunk.chunk.length} cars`); } // Listen for cache updates CarCache.on('updated', () => { console.log('Car cache updated'); });
Debug
Known issues
gotchaThe cache is in-memory and not persisted. Restarting the application will lose the cache until re-imported.
fix
Re-import data from the database on application startup using the import() method.
affects: >=0.0.0
gotchaModelCache instances are not automatically synchronized with database changes unless you manually set up change streams or hooks.
fix
Wire up Mongoose change streams or use post-save/post-remove hooks to call cache.add() and cache.delete() accordingly.
affects: >=0.0.0
gotchaThe getChunkSession method with a sort function requires stable sort, as sort is done by Array.prototype.sort which is stable in Node.js >= 12 but may behave differently in older engines.
fix
Ensure your Node.js version supports stable sort (v12+), or implement a custom sort with stable characteristics.
affects: >=0.0.0
gotchaThe cache does not handle Mongoose Document methods. Cached objects are plain JavaScript objects, not full Mongoose documents.
fix
Use Mongoose hydrate if you need a full document: CarModel.hydrate(cachedObj)
affects: >=0.0.0
Errors
Common errors & fixes
Cannot find module 'mharj-mongoose-cache'
Package not installed, or installed as dev dependency instead of production dependency.
fix
Run 'npm install mharj-mongoose-cache' --save
Uncaught TypeError: ModelCache is not a constructor
Using default import instead of named import.
fix
Use import { ModelCache } from 'mharj-mongoose-cache'
Upgrade
Version history
0.9.3latest on npm
Audit
Dependencies
mongooserequiredPeer dependency: Mongoose >= 6.0.0 is required for the cache to work with Mongoose models and change streams.
@avanio/logger-likeoptionalPeer dependency: Logger interface for logging cache operations.
Agent activity
7 hits · last 30 days
node
6
Resources
mharj-mongoose-cache — npm install mharj-mongoose-cache · libregistry