{"id":16822252,"url":"https://github.com/strml/mongoose-filter-denormalize","last_synced_at":"2025-07-20T09:12:22.841Z","repository":{"id":3784010,"uuid":"4861788","full_name":"STRML/mongoose-filter-denormalize","owner":"STRML","description":"Simple filtering and denormalization for Mongoose.","archived":false,"fork":false,"pushed_at":"2017-03-21T11:16:05.000Z","size":133,"stargazers_count":53,"open_issues_count":4,"forks_count":6,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-07-20T07:00:37.944Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/STRML.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2012-07-02T15:56:19.000Z","updated_at":"2023-03-29T11:20:26.000Z","dependencies_parsed_at":"2022-08-18T00:45:12.360Z","dependency_job_id":null,"html_url":"https://github.com/STRML/mongoose-filter-denormalize","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"purl":"pkg:github/STRML/mongoose-filter-denormalize","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/STRML%2Fmongoose-filter-denormalize","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/STRML%2Fmongoose-filter-denormalize/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/STRML%2Fmongoose-filter-denormalize/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/STRML%2Fmongoose-filter-denormalize/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/STRML","download_url":"https://codeload.github.com/STRML/mongoose-filter-denormalize/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/STRML%2Fmongoose-filter-denormalize/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":266094082,"owners_count":23875570,"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-10-13T11:02:57.385Z","updated_at":"2025-07-20T09:12:22.821Z","avatar_url":"https://github.com/STRML.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Mongoose-Filter-Denormalize\n\nSimple filtering and denormalization for Mongoose. Useful for REST APIs where you might not want to\nsend entire objects down the pipe. Allows you to store sensitive data directly on objects without worrying\nabout it being sent to end users.\n\n## Installation\n\n```\nnpm install mongoose-filter-denormalize\n```\n\n## Compatibility\n\n`mongoose \u003c= v3.4`\n\n## Filter Usage\n\nFiltering functionality is provided via a schema plugin.\n\n### Schema\n\n```javascript\nvar filter = require('mongoose-filter-denormalize').filter;\nvar ObjectId = mongoose.Schema.ObjectId;\nvar UserSchema = new Mongoose.schema({\n  name            :   String,\n  address         :   String,\n  fb              :  {\n      id              :   Number,\n      accessToken     :   String\n  },\n  writeOnlyField  :   String,\n  readOnlyField   :   String\n});\nUserSchema.plugin(filter, {\n  readFilter: {\n      \"owner\" : ['name', 'address', 'fb.id', 'fb.name', 'readOnlyField'],\n      \"public\": ['name', 'fb.name']\n  },\n  writeFilter: {\n      \"owner\" : ['name', 'address', 'fb.id', 'writeOnlyField']\n  },\n  // 'nofilter' is a built-in filter that does no processing, be careful with this\n  defaultFilterRole: 'nofilter',\n  sanitize: true, // Escape HTML in strings\n  compat: true // Enable compatibility for Mongoose versions prior to 3.6 (default false)\n});\n```\n\n### Example Read\n\n```javascript\nUser.findOne({name: 'Foo Bar'}, User.getReadFilterKeys('public')), function(err, user){\n  if(err) return next(err);\n  res.send({success: true, users: [user]});\n});\n```\n\n### Example Write\n\n```javascript\nUser.findById(req.params.id, function(err, user){\n  if(err) next(err);\n  if(user.id !== req.user.id) next(403);\n  user.extendWithWriteFilter(inputRecord, 'owner');  // Similar to jQuery.extend()\n  user.save(function(err, user){\n      if(err) return next(err);\n      user.applyReadFilter('owner'); // Make sure the doc you return does not contain forbidden fields\n      res.send({success: true, users: [user]});\n  });\n});\n```\n\n### Options\n\n- `readFilter` (Object):          Object mapping filtering profiles to string arrays of allowed fields.  Used when reading\n                                 a doc - useful for GET queries that must return only selected fields.\n- `writeFilter` (Object):         As above, but used when when applied during a PUT or POST.  This filters fields out of a given\n                                 object so they will not be written even when specified.\n                                 Useful for protected attributes like fb.accessToken.\n- `defaultFilterRole` (String)(default: 'nofilter'):   Profile to use when one is not given, or the given profile does not exist.\n- `sanitize` (Boolean)(default: false):           True to automatically escape HTML in strings.\n- `compat` (Boolean)(default: false):             True to enable compatibility with Mongoose versions prior to 3.6\n\n### Statics\n\nThis plugin adds the following statics to your schema:\n\n- `getReadFilterKeys(filterRole)`\n- `getWriteFilterKeys(filterRole)`\n- `applyReadFilter(input, filterRole)`\n- `applyWriteFilter(input, filterRole`\n- `_applyFilter(input, filterKeys)     // private helper`\n- `_getFilterKeys(type, filterRole)    // private helper`\n\n### Methods\n\nThis plugin adds the following methods to your schema:\n\n- `extendWithWriteFilter(input, filterRole)`\n- `applyReadFilter(filterRole)         // convenience method, calls statics.applyReadFilter`\n- `applyWriteFilter(filterRole)        // convenience method, calls statics.applyWriteFilter`\n\n## Denormalize Usage\n\nDenormalization functionality is provided via a schema plugin.\nThis plugin has support for, but does not require, the filter.js plugin in the same package.\n\n### Schema\n\n```javascript\nvar denormalize = require('mongoose-filter-denormalize').denormalize;\nvar ObjectId = mongoose.Schema.ObjectId;\nvar UserSchema = new Mongoose.schema({\n  name            :   String,\n  transactions    :   [{type:ObjectId, ref:'Transaction'}],\n  address         :   {type:ObjectId, ref:'Address'},\n  tickets         :   [{type:ObjectId, ref:'Ticket'}],\n  bankaccount     :   {type:ObjectId, ref:'BankAccount'}\n});\n\n// Running .denormalize() during a query will by default denormalize the selected defaults.\n// Excluded collections are never denormalized, even when asked for.\n// This is useful if passing query params directly to your methods.\nUserSchema.plugin(denormalize, {defaults: ['address', 'transactions', 'tickets'],\n                                exclude: 'bankaccount'});\n```\n\n### Querying\n\n```javascript\n// Create a query.\n// The 'conditions' object allows you to query on denormalized objects!\nvar opts = {\n  refs: [\"transactions\", \"address\"],    // Denormalize these refs. If blank, will use defaults\n  filter: \"public\";                     // Filter requires use of filter.js and profiles\n  conditions: {\n    address: {city : {$eq: \"Seattle\"}}  // Only return the user if he is in Seattle\n  }\n};\nUser.findOne({name: 'Foo Bar'}).denormalize(opts).run(function(err, user){\n  if(err) next(err);\n  res.send({success: true, users: [user]});\n});\n```\n\n### Options\n\n* `exclude`  (String[] or String):  References to never denormalize, even when explicitly asked\n                                   Use this when generating refs programmatically, to prevent unintended leakage.\n* `defaults` (String[] or String):  References to denormalize when called without options.\n                                   Defaults to all refs (except those in 'exclude').  Useful to define\n                                   this if you have hasMany references that can easily get large.\n* `suffix`   (String):              A suffix to add to all denormalized objects. This is not yet supported in Mongoose\n                                   but hopefully will be soon. E.g. a suffix of '_obj' would denormalize the story.comment\n                                   object to story.comment_obj, leaving the id in story.comment. This is necessary\n                                   for compatibility with ExtJS.\n\n### Notes\n\nIf you are building your array of refs to denormalize programmatically, make sure it returns\nan empty array if you do not want it to denormalize - falsy values will cause this plugin\nto use defaults.\n\n## Credits\n\n[Mongoose](https://github.com/LearnBoost/mongoose)\n\n\n## License\n\nMIT\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fstrml%2Fmongoose-filter-denormalize","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fstrml%2Fmongoose-filter-denormalize","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fstrml%2Fmongoose-filter-denormalize/lists"}