项目作者: diegohaz

项目描述 :
Mongoose plugin that generates a keywords path combining other paths values
高级语言: JavaScript
项目地址: git://github.com/diegohaz/mongoose-keywords.git
创建时间: 2016-05-03T03:16:42Z
项目社区:https://github.com/diegohaz/mongoose-keywords

开源协议:Other

下载


mongoose-keywords

JS Standard Style
NPM version
Build Status
Coveralls Status
Dependency Status
Downloads

Mongoose plugin that recursively generates keywords for documents based on its fields

Install

  1. npm install --save mongoose-keywords

Usage

Single path

  1. var mongoose = require('mongoose');
  2. var ArtistSchema = new mongoose.Schema({
  3. name: String
  4. });
  5. ArtistSchema.plugin(require('mongoose-keywords'), {paths: ['name']});
  6. var Artist = mongoose.model('Artist', ArtistSchema);
  7. var artist = new Artist({name: "L'arc~en~Ciel"});
  8. console.log(artist.keywords); // ['larc en ciel']

Multiple path

  1. var ArtistSchema = new mongoose.Schema({
  2. name: String,
  3. genre: String
  4. });
  5. ArtistSchema.plugin(require('mongoose-keywords'), {paths: ['name', 'genre']});
  6. var Artist = mongoose.model('Artist', ArtistSchema);
  7. var artist = new Artist({name: "L'arc~en~Ciel", genre: 'Jrock'});
  8. console.log(artist.keywords); // ['larc en ciel', 'jrock']

Custom keywords path options

You can still define a keywords path on your schema with predefined options.

  1. var ArtistSchema = new mongoose.Schema({
  2. name: String,
  3. keywords: {
  4. type: [String],
  5. unique: true // new custom option
  6. }
  7. });
  8. ArtistSchema.plugin(require('mongoose-keywords'), {paths: ['name']});

Custom keywords field

  1. var ArtistSchema = new mongoose.Schema({
  2. name: String
  3. });
  4. ArtistSchema.plugin(require('mongoose-keywords'), {
  5. paths: ['name'],
  6. field: 'terms'
  7. });
  8. var Artist = mongoose.model('Artist', ArtistSchema);
  9. var artist = new Artist({name: "L'arc~en~Ciel"});
  10. console.log(artist.keywords); // undefined
  11. console.log(artist.terms); // ['larc en ciel']

Custom transform option

By default, mongoose-keywords normalizes the value, but you can provide your own transform function.

  1. var mongoose = require('mongoose');
  2. var ArtistSchema = new mongoose.Schema({
  3. name: String
  4. });
  5. ArtistSchema.plugin(require('mongoose-keywords'), {
  6. paths: ['name'],
  7. transform: function (value) {
  8. return value + '!!!';
  9. }
  10. });
  11. var Artist = mongoose.model('Artist', ArtistSchema);
  12. var artist = new Artist({name: "L'arc~en~Ciel"});
  13. console.log(artist.keywords); // ["L'arc~en~Ciel!!!"]

Nested models

  1. var mongoose = require('mongoose');
  2. var mongooseKeywords = require('mongoose-keywords');
  3. var GenreSchema = new mongoose.Schema({
  4. title: String
  5. });
  6. GenreSchema.plugin(mongooseKeywords, {paths: ['title']});
  7. var ArtistSchema = new mongoose.Schema({
  8. name: String,
  9. genre: {
  10. type: mongoose.Schema.ObjectId,
  11. ref: 'Genre'
  12. }
  13. });
  14. ArtistSchema.plugin(mongooseKeywords, {paths: ['name', 'genre']});
  15. var Genre = mongoose.model('Genre', GenreSchema);
  16. var genre = new Genre({title: 'Jrock'});
  17. console.log(genre.keywords); // ['jrock']
  18. var Artist = mongoose.model('Artist', ArtistSchema);
  19. var artist = new Artist({name: "L'arc~en~Ciel", genre: genre});
  20. console.log(artist.keywords); // ['larc en ciel', 'jrock']

License

MIT © Diego Haz