0.2.0 • Published 11 years ago

mongoose-keywordize v0.2.0

Weekly downloads
36
License
-
Repository
github
Last release
11 years ago

#Mongoose-keywordize Plugin

Provides keyword derivation for Mongoose documents.

Build Status

Options:

  • fields: an array of paths you want watched and converted into keywords
  • fn: a custom function to execute when keywordize() runs

Example:

var schema = new Schema({ name: String, title: String });
schema.plugin(keywordize, { fields: 'name title'.split(' ') });

This will introduce a new keywordize() document method which detects if any of the passed fields have been modified and updates the new keywords property appropriately.

Example:

var Person = mongoose.model('Person', schema);
var me = new Person({ name: 'aaron' });
me.keywordize();
console.log(me.keywords) // ['aaron']

The keywordize method is always called upon saving each document, auto-updating to the latest keywords.

me.title = 'Mr';
me.save(function (err) {
  console.log(me.keywords) // ['aaron', 'Mr']
})

One may also pass an optional function to run custom logic within the call to keywordize.

var opts = {};
opts.fields = ['name', 'title']
opts.fn = function custom () {
  if ('Mister' === this.title) {
    return 'Mr';
  }
}
var schema = new Schema({ name: String, title: String });
schema.plugin(keywordize, opts);

var Person = mongoose.model('Person', schema);
var me = new Person({ name: 'aaron' });
me.title = 'Mister';
me.keywordize();
console.log(me.keywords) // ['aaron', 'Mister', 'Mr']

The optional function will be executed within the context of the document meaning we have access to the documents properties through the this keyword.

Either a an Array or single string may be returned from the function and will be pushed onto the keywords array.

Mongoose Version

>= 2.x

LICENCE

0.2.0

11 years ago

0.1.1

12 years ago

0.1.0

12 years ago

0.0.4

12 years ago

0.0.3

12 years ago

0.0.2

12 years ago

0.0.1

12 years ago