{"id":18926811,"url":"https://github.com/ladjs/mongoose-common-plugin","last_synced_at":"2025-04-15T13:33:28.557Z","repository":{"id":25393826,"uuid":"103884806","full_name":"ladjs/mongoose-common-plugin","owner":"ladjs","description":"Common plugin for Mongoose with standard schema fields and localization support","archived":false,"fork":false,"pushed_at":"2022-11-28T07:29:57.000Z","size":509,"stargazers_count":8,"open_issues_count":0,"forks_count":2,"subscribers_count":3,"default_branch":"master","last_synced_at":"2024-10-07T05:42:46.568Z","etag":null,"topics":["common","created","helper","i10n","i18n","id","locale","locales","localization","localized","locals","mongoose","plugin","plugins","security","timestamp","updated","virtual","virtuals"],"latest_commit_sha":null,"homepage":"https://lad.js.org","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/ladjs.png","metadata":{"files":{"readme":"README.md","changelog":null,"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":"2017-09-18T03:03:50.000Z","updated_at":"2022-11-28T07:36:31.000Z","dependencies_parsed_at":"2023-01-14T02:40:17.320Z","dependency_job_id":null,"html_url":"https://github.com/ladjs/mongoose-common-plugin","commit_stats":null,"previous_names":[],"tags_count":17,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ladjs%2Fmongoose-common-plugin","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ladjs%2Fmongoose-common-plugin/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ladjs%2Fmongoose-common-plugin/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ladjs%2Fmongoose-common-plugin/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ladjs","download_url":"https://codeload.github.com/ladjs/mongoose-common-plugin/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":223672916,"owners_count":17183618,"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":["common","created","helper","i10n","i18n","id","locale","locales","localization","localized","locals","mongoose","plugin","plugins","security","timestamp","updated","virtual","virtuals"],"created_at":"2024-11-08T11:17:14.532Z","updated_at":"2024-11-08T11:17:15.076Z","avatar_url":"https://github.com/ladjs.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# mongoose-common-plugin\n\n[![build status](https://github.com/ladjs/mongoose-common-plugin/actions/workflows/ci.yml/badge.svg)](https://github.com/ladjs/mongoose-common-plugin/actions/workflows/ci.yml)\n[![code style](https://img.shields.io/badge/code_style-XO-5ed9c7.svg)](https://github.com/sindresorhus/xo)\n[![styled with prettier](https://img.shields.io/badge/styled_with-prettier-ff69b4.svg)](https://github.com/prettier/prettier)\n[![made with lass](https://img.shields.io/badge/made_with-lass-95CC28.svg)](https://lass.js.org)\n[![license](https://img.shields.io/github/license/ladjs/mongoose-common-plugin.svg)]()\n\n\u003e Common plugin for Mongoose with standard schema fields and localization support\n\n\n## Table of Contents\n\n* [Install](#install)\n* [Usage](#usage)\n* [Options](#options)\n* [Localized Error Messages](#localized-error-messages)\n* [Contributors](#contributors)\n* [License](#license)\n\n\n## Install\n\n[npm][]:\n\n```sh\nnpm install mongoose-common-plugin\n```\n\n\n## Usage\n\n```js\nconst mongooseCommonPlugin = require('mongoose-common-plugin');\nconst mongoose = require('mongoose');\n\nconst User = new mongoose.Schema({});\n\nUser.plugin(mongooseCommonPlugin, { object: 'user' });\n\nmodule.exports = mongoose.model('User', User);\n```\n\n\n## Options\n\n\u003e Default options shown below:\n\n```js\n{\n  // *REQUIRED*\n  // this should be the name of the model lower-cased (e.g. User)\n  // inspired by Stripe's API design (e.g. `\"object\": \"charge\"`)\n  object: '',\n\n  // whether or not your fields/database design is camelCased\n  camelCase: false,\n\n  // whether or not to add virtual getter/setter localization support\n  // (super useful for adding Mongoose validation errors that are localized)\n  locale: true,\n\n  // default locale used (e.g. you could use `i18n.defaultLocale()` if using @ladjs/i18n)\n  defaultLocale: 'en',\n\n  // whether or not to use `mongoose-omit-common-fields`\n  omitCommonFields: true,\n\n  // either an Array or Object\n  // these will get added to `mongoose-hidden`\n  // and are extra fields you'd like to ignore from toJSON calls\n  // (e.g. `omitExtraFields: [ 'some_field_to_ignore' ]`)\n  // note that we automatically add the `-` prefix to keys for Arrays passed\n  omitExtraFields: [],\n\n  // options to pass to `mongoose-unique-validator`\n  uniqueValidator: {\n    message: require('@ladjs/mongoose-error-messages').general.unique\n  },\n\n  // options to pass to `mongoose-validation-error-transform`\n  // \u003chttps://github.com/niftylettuce/mongoose-validation-error-transform\u003e\n  validationErrorTransform: {},\n\n  // options to pass to `mongoose-hidden`\n  // \u003chttps://github.com/mblarsen/mongoose-hidden\u003e\n  mongooseHidden: {}\n}\n```\n\n\n## Localized Error Messages\n\nBy default the `options.locale` value is `true`. Therefore you can add localized error messages using [i18n][].\n\n\u003e User model definition:\n\n```js\nconst i18n = require('i18n');\nconst mongoose = require('mongoose');\nconst validator = require('validator');\n\nconst User = new mongoose.Schema({\n  email: {\n    type: String,\n    required: true,\n    index: true,\n    validate: (val, fn) =\u003e {\n      if (validator.isEmail(val))\n        return fn();\n      fn(false, i18n.__({\n        phrase: 'Invalid email address',\n        locale: this.locale\n      });\n    }\n  }\n});\n```\n\n\u003e Route middleware (assumes you're using something like [@ladjs/i18n][ladjs-i18n]):\n\n```js\ntry {\n  user.locale = ctx.request.locale;\n  await user.validate();\n} catch (err) {\n  // will throw localized validation error message for `user.email`\n  return ctx.throw(Boom.badRequest(err));\n}\n```\n\n\n## Contributors\n\n| Name           | Website                    |\n| -------------- | -------------------------- |\n| **Nick Baugh** | \u003chttp://niftylettuce.com/\u003e |\n\n\n## License\n\n[MIT](LICENSE) © [Nick Baugh](http://niftylettuce.com/)\n\n\n##\n\n[npm]: https://www.npmjs.com/\n\n[i18n]: https://github.com/mashpie/i18n-node\n\n[ladjs-i18n]: https://github.com/ladjs/i18n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fladjs%2Fmongoose-common-plugin","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fladjs%2Fmongoose-common-plugin","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fladjs%2Fmongoose-common-plugin/lists"}