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-cacheNo compatibility data collected yet for this library.
Verified import paths — ran on the pinned version, not inferred.
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.
Re-import data from the database on application startup using the import() method.
Wire up Mongoose change streams or use post-save/post-remove hooks to call cache.add() and cache.delete() accordingly.
Ensure your Node.js version supports stable sort (v12+), or implement a custom sort with stable characteristics.
Use Mongoose hydrate if you need a full document: CarModel.hydrate(cachedObj)
Run 'npm install mharj-mongoose-cache' --save
Use import { ModelCache } from 'mharj-mongoose-cache'