npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

denormalize-mongoose

v0.0.4

Published

Bidirectional denormalize for Mongoose with own type and plugin

Downloads

66

Readme

denormalize-mongoose

denormalize-mongoose is a bidirectional denormalize plugin for Mongoose from version 4.4

node-current

Description

Plugin has to be used with a custom Type for Mongoose Schema called Denormalize, that have options to config how data have to be denormalized.
It adds pre save handlers to your Schema and can add post remove handler (optional)

Installation

Plugin can be installed globally once from the library, but it needed to be used before mongoose initialization:

require('denormalize-mongoose')();
const mongoose = require('mongoose');

Or can be installed manually to every needed schema:

const { Denormalize, denormalizePlugin } = require('denormalize-mongoose');
const mongoose = require('mongoose');

const schema = new mongoose.Schema({
  name: String,
  post: {
    type: Denormalize,
    paths: ['createdAt', 'title', 'description'],
  },
});

If you use middlewares in Schema - manually initialization of plugin is recommended after all middlewares you want to add.

Options

paths - main option, it defines what fields will be taken from original Document during denormalize process.
It can be an Array of strings or Object, also it can be combined.

const { Denormalize, denormalizePlugin } = require('denormalize-mongoose');
const { Schema, SchemaTypes } = require('mongoose');

const userSchema = new Schema({
  firstName: String,
  lastName: String,
  company: String,
  comments: [{
    type: Denormalize,
    of: SchemaTypes.ObjectId,
    paths: ['text'],
    ref: 'Comment',
  }],
  commentsData: [{
    text: String,
  }]
});

userSchema.virtual('fullName').get(function getFullName() {
  return `${this.firstName} ${this.lastName}`;
});

const commentSchema = new Schema({
  user: {
    type: Denormalize,
    of: SchemaTypes.ObjectId,
    paths: ['fullName', 'company'],
    ref: 'User',
  },
  userData: {
    fullName: String,
    company: String,
  },
  text: String,
});

userSchema.plugin(denormalizePlugin);
commentSchema.plugin(denormalizePlugin);

const User = model('User', userSchema);
const Comment = model('Comment', commentSchema);

As you can see from Schema, plugin can take even virtual fields from other Documents, it's because it populates all the Denormalize fields to Document, so all operations are running on Documents

suffix and to options doing similar things. They represent field of document where to denormalize data.
to - is full path to Object or Array of Objects field to denormalize data, if it defines - suffix will be ignored
suffix - string that will be added to name of field where Denormalize Type. That joined string will be used to denormalize data (as with to option), 'Data' by default

const userSchema = new Schema({
  comments: [{
    type: Denormalize,
    of: SchemaTypes.ObjectId,
    suffix: 'SomeEnding',
    paths: ['text'],
    ref: 'Comment',
  }],
  commentsSomeEnding: [{
    text: String,
  }]
});

is the same as:

const userSchema = new Schema({
  comments: [{
    type: Denormalize,
    of: SchemaTypes.ObjectId,
    to: 'commentsSomeEnding',
    paths: ['text'],
    ref: 'Comment',
  }],
  commentsSomeEnding: [{
    text: String,
  }]
});

of - option represents Type of data used for Document id, can be everything that Mongoose supports: ObjectId, String, Number, like it will be stored in DB. ObjectId by default

ref - option is using in the same way as in Mongoose docs, so it can be used for population data too.

Additional

Denormalize operation can also be used with nested fields, so paths can be an Object that represents paths inside Document:

const userSchema = new Schema({
  comments: [{
    type: Denormalize,
    of: SchemaTypes.ObjectId,
    to: 'commentsSomeEnding',
    paths: {
      text: {
        title: 1,
        desctiption: 'info' // Will rename field in denormalized data
      }
    },
    ref: 'Comment',
  }],
  commentsSomeEnding: [{
    text: String,
  }]
});