1.8.2 • Published 5 years ago

mongoose-normalizr v1.8.2

Weekly downloads
2
License
MIT
Repository
github
Last release
5 years ago

current version Build Status Coverage Status semantic-release Commitizen friendly Greenkeeper badge

Generate normalizr schemas from 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

npm install --save mongoose-normalizr

Usage

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));
{
  "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

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 for examples!

1.8.2

5 years ago

1.8.1

5 years ago

1.8.0

5 years ago

1.7.1

5 years ago

1.7.0

5 years ago

1.6.0

5 years ago

1.5.2

5 years ago

1.5.1

5 years ago

1.5.0

5 years ago

1.4.10

5 years ago

1.4.9

6 years ago

1.4.8

6 years ago

1.4.7

6 years ago

1.4.6

6 years ago

1.4.5

6 years ago

1.4.4

6 years ago

1.4.3

6 years ago

1.4.2

6 years ago

1.4.1

6 years ago

1.4.0

6 years ago

1.3.0

6 years ago

1.2.1

6 years ago

1.2.0

6 years ago

1.1.6

6 years ago

1.1.5

6 years ago

1.1.4

6 years ago

1.1.3

6 years ago

1.1.2

6 years ago

1.1.1

6 years ago

1.1.0

6 years ago

1.0.0

6 years ago