mongoose-keywords
Advanced tools
Comparing version 0.2.0 to 0.3.0
@@ -6,3 +6,2 @@ 'use strict'; | ||
}); | ||
exports.default = keywordsPlugin; | ||
@@ -21,23 +20,26 @@ var _lodash = require('lodash'); | ||
function normalize(value) { | ||
var normalize = function normalize(value) { | ||
return _lodash2.default.kebabCase(value).replace(/\-/g, ' '); | ||
} | ||
}; | ||
function keywordsPlugin(schema) { | ||
var options = arguments.length <= 1 || arguments[1] === undefined ? {} : arguments[1]; | ||
var keywordsPlugin = function keywordsPlugin(schema) { | ||
var _ref = arguments.length <= 1 || arguments[1] === undefined ? {} : arguments[1]; | ||
var paths = options.paths && options.paths.map(function (p) { | ||
var paths = _ref.paths; | ||
var _ref$field = _ref.field; | ||
var field = _ref$field === undefined ? 'keywords' : _ref$field; | ||
var _ref$transform = _ref.transform; | ||
var transform = _ref$transform === undefined ? normalize : _ref$transform; | ||
paths = paths && paths.map(function (p) { | ||
return schema.path(p); | ||
}); | ||
var field = options.field || 'keywords'; | ||
if (paths && paths.length) { | ||
schema.add(_defineProperty({}, field, { | ||
type: [String], | ||
index: true | ||
})); | ||
} else { | ||
return; | ||
} | ||
if (!paths || !paths.length) return; | ||
schema.add(_defineProperty({}, field, { | ||
type: [String], | ||
index: true | ||
})); | ||
paths.forEach(function (path) { | ||
@@ -48,2 +50,5 @@ schema.path(path.path).set(function (value) { | ||
var oldValue = this[path.path]; | ||
if (value === oldValue) return value; | ||
var parsePath = function parsePath(path, value) { | ||
@@ -58,9 +63,7 @@ if (path instanceof _mongoose.SchemaTypes.ObjectId) { | ||
} else { | ||
oldValue && _this[field].pull(normalize(oldValue)); | ||
_this[field].addToSet(normalize(value)); | ||
oldValue && _this[field].pull(transform(oldValue)); | ||
_this[field].addToSet(transform(value)); | ||
} | ||
}; | ||
if (value === oldValue) return value; | ||
if (path instanceof _mongoose.SchemaTypes.Array) { | ||
@@ -77,2 +80,4 @@ value.forEach(function (val) { | ||
}); | ||
} | ||
}; | ||
exports.default = keywordsPlugin; |
{ | ||
"name": "mongoose-keywords", | ||
"version": "0.2.0", | ||
"version": "0.3.0", | ||
"description": "Mongoose plugin that recursively generates keywords for documents based on its fields", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
@@ -62,3 +62,3 @@ # mongoose-keywords | ||
ArtistSchema.plugin(require('mongoose-keywords'), { | ||
paths: ['name'], | ||
paths: ['name'], | ||
field: 'terms' | ||
@@ -74,2 +74,24 @@ }); | ||
### Custom `transform` option | ||
By default, `mongoose-keywords` normalizes the value, but you can provide your own transform function. | ||
```js | ||
var mongoose = require('mongoose'); | ||
var ArtistSchema = new mongoose.Schema({ | ||
name: String | ||
}); | ||
ArtistSchema.plugin(require('mongoose-keywords'), { | ||
paths: ['name'], | ||
transform: function (value) { | ||
return value + '!!!'; | ||
} | ||
}); | ||
var Artist = mongoose.model('Artist', ArtistSchema); | ||
var artist = new Artist({name: "L'arc~en~Ciel"}); | ||
console.log(artist.keywords); // ["L'arc~en~Ciel!!!"] | ||
``` | ||
### Nested models | ||
@@ -76,0 +98,0 @@ ```js |
9708
56
143