# mongoose-normalizr

> Generate normalizr schemas from mongoose schemas!

Latest version **1.8.2** (published 2019-10-25) · MIT license · 0 weekly downloads

## Install

```sh
npm install mongoose-normalizr
pnpm add mongoose-normalizr
yarn add mongoose-normalizr
bun add mongoose-normalizr
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

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

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.8.2 |
| Published | 2019-10-25 |
| First published | 2017-12-27 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Node | 12.8.0 |
| Dependencies | 1 |
| Unpacked size | 28.1 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 4 |
| Maintainers | saiichihashimoto |
| Keywords | mongoose, mongoose-model, mongoose-schema, normalizr, normalizr-schema |

## Links

- npm: https://www.npmjs.com/package/mongoose-normalizr
- Repository: https://github.com/saiichihashimoto/mongoose-normalizr
- Homepage: https://saiichihashimoto.github.io/mongoose-normalizr/
- Issues: https://github.com/saiichihashimoto/mongoose-normalizr/issues
- npm.io page: https://npm.io/package/mongoose-normalizr

## Dependencies (1)

- [mongoose-legacy-pluralize](https://npm.io/package/mongoose-legacy-pluralize.md) 1.0.2

## Alternatives

- [angular-pipes](https://npm.io/package/angular-pipes.md) — 5.6K weekly downloads
- [@ng-web-apis/midi](https://npm.io/package/@ng-web-apis/midi.md) — 2.6K weekly downloads
- [happn-3](https://npm.io/package/happn-3.md) — 1.6K weekly downloads
- [@opensip-cli/lang-go](https://npm.io/package/@opensip-cli/lang-go.md) — 1.2K weekly downloads
- [mongoose-typescript](https://npm.io/package/mongoose-typescript.md) — 85 weekly downloads

## Recent versions

- 1.8.2 (latest) — 2019-10-25
- 1.8.1 — 2019-02-24
- 1.8.0 — 2019-02-24
- 1.7.1 — 2019-02-23
- 1.7.0 — 2019-02-20
- 1.6.0 — 2019-02-20
- 1.5.2 — 2019-02-20
- 1.5.1 — 2019-02-16
- 1.5.0 — 2019-02-14
- 1.4.10 — 2019-02-14
- 1.4.9 — 2018-06-13
- 1.4.8 — 2018-06-04
- 1.4.7 — 2018-06-01
- 1.4.6 — 2018-05-27
- 1.4.5 — 2018-05-01
- … 16 more at https://npm.io/package/mongoose-normalizr/versions

## README

[![current version](https://img.shields.io/npm/v/mongoose-normalizr.svg)](https://www.npmjs.com/package/mongoose-normalizr)
[![Build Status](https://travis-ci.org/saiichihashimoto/mongoose-normalizr.svg?branch=master)](https://travis-ci.org/saiichihashimoto/mongoose-normalizr)
[![Coverage Status](https://coveralls.io/repos/github/saiichihashimoto/mongoose-normalizr/badge.svg?branch=master)](https://coveralls.io/github/saiichihashimoto/mongoose-normalizr?branch=master)
[![semantic-release](https://img.shields.io/badge/%20%20%F0%9F%93%A6%F0%9F%9A%80-semantic--release-e10079.svg)](https://github.com/semantic-release/semantic-release)
[![Commitizen friendly](https://img.shields.io/badge/commitizen-friendly-brightgreen.svg)](http://commitizen.github.io/cz-cli/)
[![Greenkeeper badge](https://badges.greenkeeper.io/saiichihashimoto/mongoose-normalizr.svg)](https://greenkeeper.io/)

Generate [normalizr](https://www.npmjs.com/package/normalizr) schemas from [mongoose](https://www.npmjs.com/package/mongoose) schemas!

normalizr and mongoose both define relationships between the same objects. Define the mongoose relationships and get the same normalizr relationships without repeating yourself.

# Installation

```bash
npm install --save mongoose-normalizr
```

# Usage

```javascript
import mongoose from 'mongoose';
import normalizr from 'normalizr';
import mongooseNormalizr from 'mongoose-normalizr';

const Foo = mongoose.Schema({
	bar: { ref: 'Bar', type: mongoose.Schema.Types.ObjectId },
});
const Bar = mongoose.Schema({
	foos: [{ ref: 'Foo', type: mongoose.Schema.Types.ObjectId }],
});

const normalizrs = mongooseNormalizr({
	Foo,
	Bar,
});

const denormalizedFoo = {
	id:  'foo1',
	bar: {
		id:   'bar1',
		foos: [
			{
				id: 'foo2',
			},
			{
				id:  'foo3',
				bar: {
					id: 'bar2',
				},
			},
		],
	},
};

console.log('normalized:', normalizr.normalize(denormalizedFoo, normalizrs.foos));
```

```json
{
  "result": "foo1",
  "entities": {
    "foos": {
      "foo1": {
        "id": "foo1",
        "bar": "bar1"
      },
      "foo2": {
        "id": "foo2"
      },
      "foo3": {
        "id": "foo3",
        "bar": "bar2"
      }
    },
    "bars": {
      "bar1": {
        "id": "bar1",
        "foos": [
          "foo2",
          "foo3"
        ]
      },
      "bar2": {
        "id": "bar2"
      }
    }
  }
}
```

# Features

- Built specifically for the browser! *Works server-side, as well.*
- Traverses arrays and objects to find deep references.
- Supports [Subdocuments](http://mongoosejs.com/docs/subdocs.html).
- Supports [Populateable Virtuals](http://mongoosejs.com/docs/populate.html#populate-virtuals).
- Supports [Dynamic References](https://mongoosejs.com/docs/populate.html#dynamic-ref).
  - It does this by generating normalizr [Unions](https://github.com/paularmstrong/normalizr/blob/master/docs/api.md#uniondefinition-schemaattribute). *Unions don't [normalize to an id](https://github.com/paularmstrong/normalizr/blob/master/docs/api.md#usage-5) like Entities do.*
- Supports [Discriminators](http://mongoosejs.com/docs/discriminators.html).
  - It does this by generating normalizr [Unions](https://github.com/paularmstrong/normalizr/blob/master/docs/api.md#uniondefinition-schemaattribute). *Unions don't [normalize to an id](https://github.com/paularmstrong/normalizr/blob/master/docs/api.md#usage-5) like Entities do.*
- Works on *all* versions of normalizr, all the way back to `0.1.1`.

# ```mongooseNormalizr(schemas)```

- ```schemas```: **required**: An object mapping mongoose model names (**not** collection names) to mongoose schemas. Instead of a mongoose schema, you may supply an object with the following properties:
	- ```schema```: **required** The mongoose schema to use.
	- ```define```: If `false`, produces an empty normalizr schema and doesn't follow any references. Defaults to value of `enable`.
	- ```reference```: If `false`, other produced schemas will ignore references to this schema. Defaults to value of `enable`.
	- ```enable```: Shorthand for `define` && `reference`. Defaults to `true`.

See [our tests](https://github.com/saiichihashimoto/mongoose-normalizr/blob/master/src/index.spec.js) for examples!

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