Giter Site home page Giter Site logo

mongoose-keywords's Introduction

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

npm install --save mongoose-keywords

Usage

Single path

var mongoose = require('mongoose');

var ArtistSchema = new mongoose.Schema({
  name: String
});

ArtistSchema.plugin(require('mongoose-keywords'), {paths: ['name']});

var Artist = mongoose.model('Artist', ArtistSchema);

var artist = new Artist({name: "L'arc~en~Ciel"});
console.log(artist.keywords); // ['larc en ciel']

Multiple path

var ArtistSchema = new mongoose.Schema({
  name: String,
  genre: String
});

ArtistSchema.plugin(require('mongoose-keywords'), {paths: ['name', 'genre']});

var Artist = mongoose.model('Artist', ArtistSchema);

var artist = new Artist({name: "L'arc~en~Ciel", genre: 'Jrock'});
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.

var ArtistSchema = new mongoose.Schema({
  name: String,
  keywords: {
    type: [String],
    unique: true // new custom option
  }
});

ArtistSchema.plugin(require('mongoose-keywords'), {paths: ['name']});

Custom keywords field

var ArtistSchema = new mongoose.Schema({
  name: String
});

ArtistSchema.plugin(require('mongoose-keywords'), {
  paths: ['name'],
  field: 'terms'
});

var Artist = mongoose.model('Artist', ArtistSchema);

var artist = new Artist({name: "L'arc~en~Ciel"});
console.log(artist.keywords); // undefined
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.

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

var mongoose = require('mongoose');
var mongooseKeywords = require('mongoose-keywords');

var GenreSchema = new mongoose.Schema({
  title: String
});
GenreSchema.plugin(mongooseKeywords, {paths: ['title']});

var ArtistSchema = new mongoose.Schema({
  name: String,
  genre: {
    type: mongoose.Schema.ObjectId,
    ref: 'Genre'
  }
});
ArtistSchema.plugin(mongooseKeywords, {paths: ['name', 'genre']});

var Genre = mongoose.model('Genre', GenreSchema);
var genre = new Genre({title: 'Jrock'});
console.log(genre.keywords); // ['jrock']

var Artist = mongoose.model('Artist', ArtistSchema);
var artist = new Artist({name: "L'arc~en~Ciel", genre: genre});
console.log(artist.keywords); // ['larc en ciel', 'jrock']

License

MIT © Diego Haz

mongoose-keywords's People

Contributors

diegohaz avatar greenkeeper[bot] avatar

Watchers

James Cloos avatar Ali avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.