mongoose-recent
A mongoose plugin for handling recently accessed data to be stored on a document
var mongoose = ;var recentPlugin = ; mongoose; var CatSchema = name: String ;var Cat = mongoose; var kitty = name: 'Fang' ; kitty; // You can also add hairballs with promiseskitty;
(see a working example of this in examples/)
Usage
mongoose-recent adds a new collection to your document, along with instance and static methods for adding recent items. This collection is kept in a date-sorted order (newest at the top), and by default does not allow duplicates and has a maximum size of 10 entries. If you re-add an entry that already exists in the collection, it will be bubbled back up to the top.
This plugin is intended to be used whenever you find yourself with a collection sorted by date. For example:
- Recent items that a customer has viewed
- Recent videos that a user has played
- Recently clicked buttons
To get started with a minimal plugin, which will add a recentViews collection and an addRecentView(...) function, simply add it to any Schema:
var mongoose = ;var recentPlugin = ; var UserSchema = ...;UserSchema;
Adding to the collection
You can add to the recent items collection either with an instance method on the document, or a static method on the model. You can also use callbacks, which follow the standard mongoose/node callback structure (err, object), or use all the power of bluebird promises.
With Callbacks:
User; var user = ;user;
With Promises:
User; user = ;user;
Options
name
Type: String
Default: view
In many cases, this is the only option you'll need to set. This should usually be a singular noun and will be used to set the following three things:
- The collection, which will be named recent{options.name}s
- The instance method, which will be named addRecent{options.name}
- The property in the collection, which will be named {options.name}
Schema;Model = mongoose;doc = ; console; // [], with the schema {item: ObjectId, date: Date}console; // [Function]
If you'd like more control over how everything is named, see collectionPath, addFunctionName, and dateFieldName.
numToKeep:
Type: Number
Default: 10
This controls the number of items to keep in the collection.
schemaType
Type: Object
Default: DefaultObjectId
By default, this is just a mongo object Id, but can be another id type (for example, String or ShortId), or a complex object.
Schema
compareFunc
Type: Function
Default: ===
By default, mongoose-recent uses === to compare when checking for duplicates. If you have an object as the schema type, this is usually not the desired behavior. Instead, you should define your own custome comparison function. The comparison function is passed the object being added as the first argument, and the object to compare against as the second argument.
var { return o1miceCaught === o2miceCaught;}; Schema;// ...
collectionPath
Type: String
Default: recent{options.name}s
You can use this for fine-grained control over how the collection is added to your schema.
Schema;Model = mongoose;doc = ; console; // undefinedconsole; // []console; // [Function]
addFunctionName
Type: String
Default: addRecent{options.name}
You can use this for fine-grained control over how the addRecent instance method is added to your schema.
Schema;Model = mongoose;doc = ; console; // []console; // undefinedconsole; // [Function]
dateFieldName
Type: String
Default: date
You can use this for fine-grained control over how the date field is named.
Schema
allowDuplicates
Type: boolean
Default: false
By default, duplicates are not allowed. If you re-add an item that's already in the collection, it will be given a new date and moved to the top of the collection. If you want to track all recent instances, rather than just the most recent for an item, set this to true. When true, every call to addRecent will add a new entry to the collection.
Versions
- 1.0.3: Relax mongoose peer dependency further
- 1.0.2: Relax mongoose peer dependency
- 1.0.1: Fix issue when calling methods
- 1.0.0: Initial Release