Socket
Socket
Sign inDemoInstall

mongoose-keywords

Package Overview
Dependencies
1
Maintainers
1
Versions
7
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    mongoose-keywords

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


Version published
Weekly downloads
959
decreased by-33.82%
Maintainers
1
Install size
1.36 MB
Created
Weekly downloads
 

Readme

Source

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

Keywords

FAQs

Last updated on 04 Apr 2020

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc