# mongoose-common-plugin

> Common plugin for Mongoose with standard schema fields and localization support

Latest version **4.0.1** (published 2026-01-17) · MIT license · 0 weekly downloads

## Install

```sh
npm install mongoose-common-plugin
pnpm add mongoose-common-plugin
yarn add mongoose-common-plugin
bun add mongoose-common-plugin
```

## Health

**Score 45/100 (D)** — status: stable.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

## Facts

| | |
|---|---|
| Version | 4.0.1 |
| Published | 2026-01-17 |
| First published | 2017-09-18 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Node | >=14 |
| Dependencies | 7 |
| Unpacked size | 10.7 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 8 |
| Author | Nick Baugh |
| Maintainers | niftylettuce, titanism |
| Keywords | common, created, field, helper, i10n, i18n, id, koa, lad, lass, locale, locales, localization, mongoose, plugin, schema, timestamp, updated, virtual |

## Links

- npm: https://www.npmjs.com/package/mongoose-common-plugin
- Repository: https://github.com/ladjs/mongoose-common-plugin
- Issues: https://github.com/ladjs/mongoose-common-plugin/issues
- npm.io page: https://npm.io/package/mongoose-common-plugin

## Dependencies (7)

- [boolean](https://npm.io/package/boolean.md) ^3.2.0
- [speakingurl](https://npm.io/package/speakingurl.md) ^14.0.1
- [mongoose-hidden](https://npm.io/package/mongoose-hidden.md) ^1.9.1
- [mongoose-omit-common-fields](https://npm.io/package/mongoose-omit-common-fields.md) ^0.0.6
- [@ladjs/mongoose-error-messages](https://npm.io/package/@ladjs/mongoose-error-messages.md) ^1.0.0
- [@ladjs/mongoose-unique-validator](https://npm.io/package/@ladjs/mongoose-unique-validator.md) ^5.0.0
- [mongoose-validation-error-transform](https://npm.io/package/mongoose-validation-error-transform.md) ^0.0.6

## Alternatives

- [@sindresorhus/slugify](https://npm.io/package/@sindresorhus/slugify.md) — 3.7M weekly downloads
- [solid-js](https://npm.io/package/solid-js.md) — 2.7M weekly downloads
- [expo-glass-effect](https://npm.io/package/expo-glass-effect.md) — 2.5M weekly downloads
- [nanoassert](https://npm.io/package/nanoassert.md) — 780.8K weekly downloads
- [@ffmpeg/ffmpeg](https://npm.io/package/@ffmpeg/ffmpeg.md) — 529.5K weekly downloads

## Recent versions

- 4.0.1 (latest) — 2026-01-17
- 4.0.0 — 2022-11-28
- 3.0.1 — 2022-11-17
- 3.0.0 — 2022-07-03
- 2.0.3 — 2021-10-19
- 2.0.2 — 2020-07-24
- 2.0.1 — 2020-05-30
- 1.0.1 — 2020-05-12
- 1.0.0 — 2020-05-10
- 0.1.1 — 2019-09-05
- 0.1.0 — 2019-01-09
- 0.0.6 — 2017-12-02
- 0.0.5 — 2017-12-01
- 0.0.4 — 2017-12-01
- 0.0.3 — 2017-11-30
- … 1 more at https://npm.io/package/mongoose-common-plugin/versions

## README

# mongoose-common-plugin

[![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)
[![code style](https://img.shields.io/badge/code_style-XO-5ed9c7.svg)](https://github.com/sindresorhus/xo)
[![styled with prettier](https://img.shields.io/badge/styled_with-prettier-ff69b4.svg)](https://github.com/prettier/prettier)
[![made with lass](https://img.shields.io/badge/made_with-lass-95CC28.svg)](https://lass.js.org)
[![license](https://img.shields.io/github/license/ladjs/mongoose-common-plugin.svg)]()

> Common plugin for Mongoose with standard schema fields and localization support


## Table of Contents

* [Install](#install)
* [Usage](#usage)
* [Options](#options)
* [Localized Error Messages](#localized-error-messages)
* [Contributors](#contributors)
* [License](#license)


## Install

[npm][]:

```sh
npm install mongoose-common-plugin
```


## Usage

```js
const mongooseCommonPlugin = require('mongoose-common-plugin');
const mongoose = require('mongoose');

const User = new mongoose.Schema({});

User.plugin(mongooseCommonPlugin, { object: 'user' });

module.exports = mongoose.model('User', User);
```


## Options

> Default options shown below:

```js
{
  // *REQUIRED*
  // this should be the name of the model lower-cased (e.g. User)
  // inspired by Stripe's API design (e.g. `"object": "charge"`)
  object: '',

  // whether or not your fields/database design is camelCased
  camelCase: false,

  // whether or not to add virtual getter/setter localization support
  // (super useful for adding Mongoose validation errors that are localized)
  locale: true,

  // default locale used (e.g. you could use `i18n.defaultLocale()` if using @ladjs/i18n)
  defaultLocale: 'en',

  // whether or not to use `mongoose-omit-common-fields`
  omitCommonFields: true,

  // either an Array or Object
  // these will get added to `mongoose-hidden`
  // and are extra fields you'd like to ignore from toJSON calls
  // (e.g. `omitExtraFields: [ 'some_field_to_ignore' ]`)
  // note that we automatically add the `-` prefix to keys for Arrays passed
  omitExtraFields: [],

  // options to pass to `mongoose-unique-validator`
  uniqueValidator: {
    message: require('@ladjs/mongoose-error-messages').general.unique
  },

  // options to pass to `mongoose-validation-error-transform`
  // <https://github.com/niftylettuce/mongoose-validation-error-transform>
  validationErrorTransform: {},

  // options to pass to `mongoose-hidden`
  // <https://github.com/mblarsen/mongoose-hidden>
  mongooseHidden: {}
}
```


## Localized Error Messages

By default the `options.locale` value is `true`. Therefore you can add localized error messages using [i18n][].

> User model definition:

```js
const i18n = require('i18n');
const mongoose = require('mongoose');
const validator = require('validator');

const User = new mongoose.Schema({
  email: {
    type: String,
    required: true,
    index: true,
    validate: (val, fn) => {
      if (validator.isEmail(val))
        return fn();
      fn(false, i18n.__({
        phrase: 'Invalid email address',
        locale: this.locale
      });
    }
  }
});
```

> Route middleware (assumes you're using something like [@ladjs/i18n][ladjs-i18n]):

```js
try {
  user.locale = ctx.request.locale;
  await user.validate();
} catch (err) {
  // will throw localized validation error message for `user.email`
  return ctx.throw(Boom.badRequest(err));
}
```


## Contributors

| Name           | Website                    |
| -------------- | -------------------------- |
| **Nick Baugh** | <http://niftylettuce.com/> |


## License

[MIT](LICENSE) © [Nick Baugh](http://niftylettuce.com/)


##

[npm]: https://www.npmjs.com/

[i18n]: https://github.com/mashpie/i18n-node

[ladjs-i18n]: https://github.com/ladjs/i18n

---
_Source: https://npm.io/package/mongoose-common-plugin · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
