{"id":18523603,"url":"https://github.com/mongoosejs/mongoose-keywordize","last_synced_at":"2025-04-09T11:31:42.558Z","repository":{"id":2423674,"uuid":"3392440","full_name":"mongoosejs/mongoose-keywordize","owner":"mongoosejs","description":"Derives keywords from document properties","archived":false,"fork":false,"pushed_at":"2012-11-27T17:49:41.000Z","size":138,"stargazers_count":21,"open_issues_count":1,"forks_count":5,"subscribers_count":0,"default_branch":"master","last_synced_at":"2025-03-24T05:13:45.655Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"http://aheckmann.github.com/mongoose-keywordize","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/mongoosejs.png","metadata":{"files":{"readme":"README.md","changelog":"History.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2012-02-08T23:14:23.000Z","updated_at":"2023-03-28T05:50:01.000Z","dependencies_parsed_at":"2022-08-17T22:35:23.835Z","dependency_job_id":null,"html_url":"https://github.com/mongoosejs/mongoose-keywordize","commit_stats":null,"previous_names":["aheckmann/mongoose-keywordize"],"tags_count":7,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mongoosejs%2Fmongoose-keywordize","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mongoosejs%2Fmongoose-keywordize/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mongoosejs%2Fmongoose-keywordize/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mongoosejs%2Fmongoose-keywordize/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/mongoosejs","download_url":"https://codeload.github.com/mongoosejs/mongoose-keywordize/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248031508,"owners_count":21036418,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-06T17:36:28.177Z","updated_at":"2025-04-09T11:31:40.042Z","avatar_url":"https://github.com/mongoosejs.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"#Mongoose-keywordize Plugin\n\nProvides keyword derivation for [Mongoose](http://mongoosejs.com) documents.\n\n[![Build Status](https://secure.travis-ci.org/aheckmann/mongoose-keywordize.png)](http://travis-ci.org/aheckmann/mongoose-keywordize)\n\nOptions:\n\n  - `fields`: an array of paths you want watched and converted into keywords\n  - `fn`: optional function to execute when keywordize() runs; if a value is returned it is included in the keywords array\n  - `pre`: optional function to run against each value returned from each `field` before it's parsed and added to the keywords array\n  - `keywordField`: the name of the field in which keywords will be stored; defaults to `keywords`\n  - `upper`: true to retain letter casing. default is false (all keywords are lowercased)\n\nExample:\n\n```js\nvar schema = new Schema({ name: String, title: String });\nschema.plugin(keywordize, { fields: 'name title'.split(' ') });\n```\n\nThis 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.\n\nExample:\n\n```js\nvar Person = mongoose.model('Person', schema);\nvar me = new Person({ name: 'aaron' });\nme.keywordize();\nconsole.log(me.keywords) // ['aaron']\n```\n\nThe `keywordize` method is always called upon saving each document, auto-updating to the latest keywords.\n\n```js\nme.title = 'Mr';\nme.save(function (err) {\n  console.log(me.keywords) // ['aaron', 'Mr']\n})\n```\n\n###index\n\nKeywordize, by default, does not define an index on the \"keywords\" key.\nIf you want to define an index you should use the \"index\" option:\n\n```js\nvar opts = {}\nopts.index = true\n```\n\n###pre\n\nTo have the opportunity to pre-process field values as they're retreived by the `keywordize` plugin before they are processed, pass an optional `pre` function. This function, when provided, will be run against each value returned from each `field` before it's parsed and added to the keywords array. The function is passed the `value` and field name.\n\n```js\nvar opts = {};\nopts.fields = ['description', 'title']\nopts.pre = function (value, field) {\n  // remove html entities from each keyword picked from description\n  if ('description' == field) {\n    return value.replace(/\u0026#?[a-z0-9]{2,8};/ig, ' ');\n  } else {\n    return value;\n  }\n}\nvar schema = new Schema({ description: String, title: String });\nschema.plugin(keywordize, opts);\n\nvar Person = mongoose.model('Person', schema);\nvar me = new Person({ name: 'aaron' });\nme.description = 'Tall\u0026nbsp;\u0026amp;\u0026nbsp;Awkward';\nme.keywordize();\nconsole.log(me.keywords) // ['aaron', 'tall', 'awkward']\n```\n\n###fn\n\nOne may also pass an optional function to run custom logic within the call to `keywordize`. The optional function will be executed within the context of the document, meaning we have access to the documents properties through the `this` keyword to perform any custom logic necessary.\n\n```js\nvar opts = {};\nopts.fields = ['name', 'title']\nopts.fn = function custom () {\n  if ('Mister' === this.title) {\n    return 'Mr';\n  }\n}\nvar schema = new Schema({ name: String, title: String });\nschema.plugin(keywordize, opts);\n\nvar Person = mongoose.model('Person', schema);\nvar me = new Person({ name: 'aaron' });\nme.title = 'Mister';\nme.keywordize();\nconsole.log(me.keywords) // ['aaron', 'Mister', 'Mr']\n```\n\n_Either a an `Array` or single string may be returned from the function and will be pushed onto the keywords array._\n\n###upper\n\nBy default mongoose-keywordize lowercases the keywords. To preserve casing pass the `upper: true` option.\n\n## Mongoose Version\n`\u003e= 2.x`\n\n[LICENSE](https://github.com/aheckmann/mongoose-keywordize/blob/master/LICENSE)\n\n\n\n\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmongoosejs%2Fmongoose-keywordize","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmongoosejs%2Fmongoose-keywordize","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmongoosejs%2Fmongoose-keywordize/lists"}