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

mobx-state-tree-normalizr

v1.0.10

Published

A tool for normalize data from mobx-state-tree types.model

Readme

mobx-state-tree-normalizr

Mobx-state-tree-normalizr helps you normalize data with mobx-state-tree's model definition.

Install

Install from NPM repository using yarn or npm

npm install mobx-state-tree-normalizr
yarn add mobx-state-tree-normalizr

Motivation

Mobx-state-tree provides a way to denomalize related data with reference but lacks of data normalization support. In order to nomalize the data retrieved from the backend service, people may use third-party libraries like normalizr. However, these kinds of packages may need you to define another schema other than mobx-state-tree built-in type.model for a specific resource which will introduce duplication to the schema definition.

Mobx-state-tree-normalizr is tool that aims to normalize your data based on mobx-state-tree built-in types.model without third-party schema definition.

Notes: If you are not familiar with the nomalization term, please take a look at its documentation for more information.

Instructions

normalize

mobx-state-tree-normalizr only provide a method call normalize, it take two params:

  1. First parameter: originalData(the nested data you might fetch from somewhere)
  2. Second parameter: model(the model of mobx-state-tree)

parameter: model

We use mobx-state-tree's types.model to normalize our data. But because model in mst is very powerful and elastic, we need some constraints when you define your model:

  1. Mobx-state-tree-normalizr use model's name to define your entity type, so you should define your model like this types.model('some name', {....}). If the name parameter is not given, We will consider that this model corresponds to not an entity.

  2. An entity must have a primary key distinguishing itself from other entities with the same type. The primary key will be the attribute with the types.identifier type in your model if provided and will use id as the default value.

Usage

The original data might look like this:

const originalData = {
  id: '123',
  title: 'My awesome blog post',
  author: {
    id: '1',
    name: 'Nana',
    gender: 'm'
  }
}

Now, when we use mobx-state-tree, we can build a model for this data structure like this:

import { types } from 'mobx-state-tree'

const User = types.model('user', {
  id: types.identifier,
  name: types.string,
  gender: types.enumeration('gender', ['m', 'n'])
})

const Article = types.model('article', {
  id: types.string,
  title: types.string,
  author: types.maybe(types.reference(types.late(() => User)))
})

After that, we can use these models as schemas to normalize our nested data

import normalize from 'mobx-state-tree-normalizr'

const normalizedData = normalize(originalData, Article)

Now, normalizedData will be:

{
  result: '123',
  entities: {
    'article': {
      '123': {
        id: '123',
        title: 'My awesome blog post',
        author: '1'
      }
    },
    'user': {
      '1': {id: '1', name: 'Nana', gender: 'm'}
    }
  }
}