All Projects → gpbl → Denormalizr

gpbl / Denormalizr

Licence: mit
Denormalize data normalized with normalizr

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to Denormalizr

Retalk
🐤 The Simplest Redux
Stars: ✭ 168 (-27.27%)
Mutual labels:  flux
Multi Tenancy
Flux v1: Manage a multi-tenant cluster with Flux and Kustomize
Stars: ✭ 180 (-22.08%)
Mutual labels:  flux
Deox
Functional Type-safe Flux Standard Utilities
Stars: ✭ 200 (-13.42%)
Mutual labels:  flux
Leetcode Solutions
🏋️ Python / Modern C++ Solutions of All 2111 LeetCode Problems (Weekly Update)
Stars: ✭ 2,787 (+1106.49%)
Mutual labels:  data-structure
Ordereddictionary
Ordered dictionary data structure implementation in Swift
Stars: ✭ 176 (-23.81%)
Mutual labels:  data-structure
Bedrock
Build a Node web app with user authentication, security, and more in under 10 minutes. Now supports React Hot Loading for super-fast development. 👌
Stars: ✭ 187 (-19.05%)
Mutual labels:  flux
Redux Zero
A lightweight state container based on Redux
Stars: ✭ 1,977 (+755.84%)
Mutual labels:  flux
Smitty
Tiny flux implementation built on mitt
Stars: ✭ 210 (-9.09%)
Mutual labels:  flux
Leetcode
High-quality LeetCode solutions
Stars: ✭ 178 (-22.94%)
Mutual labels:  data-structure
K8s Gitops
GitOps principles to define kubernetes cluster state via code. Community around [email protected] is on discord: https://discord.gg/7PbmHRK
Stars: ✭ 192 (-16.88%)
Mutual labels:  flux
Geometricflux.jl
Geometric Deep Learning for Flux
Stars: ✭ 175 (-24.24%)
Mutual labels:  flux
Reactor Addons
Official modules for the Reactor project
Stars: ✭ 175 (-24.24%)
Mutual labels:  flux
Metalhead.jl
Computer vision models for Flux
Stars: ✭ 191 (-17.32%)
Mutual labels:  flux
General Store
Simple, flexible store implementation for Flux. #hubspot-open-source
Stars: ✭ 171 (-25.97%)
Mutual labels:  flux
Flooks
🍸 A state manager for React Hooks
Stars: ✭ 201 (-12.99%)
Mutual labels:  flux
Influxdb Client Python
InfluxDB 2.0 python client
Stars: ✭ 165 (-28.57%)
Mutual labels:  flux
Typescript Webpack React Redux Boilerplate
React and Redux with TypeScript
Stars: ✭ 182 (-21.21%)
Mutual labels:  flux
Blazor Fluxor
DEPRECATED PROJECT. See FLUXOR
Stars: ✭ 216 (-6.49%)
Mutual labels:  flux
Tinystate
A tiny, yet powerful state management library for Angular
Stars: ✭ 207 (-10.39%)
Mutual labels:  flux
C Macro Collections
Easy to use, header only, macro generated, generic and type-safe Data Structures in C
Stars: ✭ 192 (-16.88%)
Mutual labels:  data-structure

Deprecated

⚠️ This package is deprecated. Please use normalizr's built-in denormalize(), thanks!


denormalizr takes data and entities normalized by normalizr, and returns its complete tree – including nested entities.

This module is useful when consuming normalized data, e.g. in redux selectors. While normalizr is great on making data consistent between the app, reassembling entities can be a tedious work. Denormalizr can help!

npm version npm downloads build status Code Climate Coveralls

npm install denormalizr --save
import { denormalize } from "denormalizr";
const denormalized = denormalize(entity, entities, entitySchema);

Documentation

API

denormalize (entity, entities, schema) -> Object|Array|Immutable.Map|Immutable.List

Params

entity {Object|Array|Number|String|Immutable.Map|Immutable.List}

The entity to denormalize, its id, or an array of entities or ids.

entities {Object|Immutable.Map}

An object to entities used to denormalize entity and its referred entities.

entitySchema {schema.Entity}

The normalizr schema used to define entity.

Returns

The denormalized object (or Immutable.Map), or an array of denormalized objects (or an Immutable.List).

Examples

For the following examples, consider to have a JSON response from a REST API consisting in a list of articles, where each article has a author field.

{
  "articles": [{
    "id": 1,
    "title": "10 mindblowing reasons to prefer composition over inheritance",
    "author": {
      "id": 1,
      "name": "Dan"
    },
  }, {
    "id": 2,
    "title": "You won't believe what this high order component is doing",
    "author": {
      "id": 1,
      "name": "Dan"
    }
  }]
}

To normalize this response with normalizr, we can define two Schemas: articleSchema and authorSchema.

import { normalize, schema } from 'normalizr';

const articleSchema = new schema.Entity('articles');
const authorSchema = new schema.Entity('authors');
const articleList = new schema.Array(articleSchema);

articleSchema.define({
  author: authorSchema,
});

const normalized = normalize(response, {
  articles: articleList,
})

This way we have the usual normalized object with entities:

// content of normalized
{ entities: 
   { articles: 
      { '1': 
         { id: 1,
           title: '10 mindblowing reasons to prefer composition over inheritance',
           author: 1 },
        '2': 
         { id: 2,
           title: 'You won\'t believe what this high order component is doing',
           author: 1 } },
     authors: 
      { '1': 
         { id: 1, 
          name: 'Dan' } } },
  result: { articles: [ 1, 2 ] } }

Let say we want to display the articles with ids 1 and 2, and for each article its author.

In order to get the whole author object for each article, we need to loop over the author entities:

const articleIds = [1, 2];
const articles = articleIds.map(id => {
  const article = normalized.entities.articles[id];
  article.author = normalized.entities.authors[article.author];
})

We are basically reverting to the original JSON response. We are, indeed, denormalizing.

Without the need to know the entity's shapes, we can use denormalizr to simplify this process. Thus:

import { denormalize } from 'denormalizr';

const articles = denormalize([1,2], normalized.entities, articleList);

articles contains now the selected articles with the authors in them:

// console.log(articles)
[ { id: 1,
    title: '10 mindblowing reasons to prefer composition over inheritance',
    author: { id: 1, name: 'Dan' } },
  { id: 2,
    title: 'You won\'t believe what this high order component is doing',
    author: { id: 1, name: 'Dan' } } ]

denormalize() accepts as first parameter the entity we want to denormalize, which can be a single object, an array of object, a single id or an array of ids. The second parameter is the whole entities object, which is consumed when the entity schema (third parameter) has references to one or more entities.

Denormalize a single object

const article = normalized.entities.articles['1'];
const denormalized = denormalize(article, normalized.entities, articleSchema);
// console.log(denormalized)
{
  id: 1,
  title: 'Some Article',
  author: {
    id: 1,
    name: 'Dan'
  },
}

Denormalize a list of objects

const article1 = normalized.entities.articles['1'];
const article2 = normalized.entities.articles['2'];

const denormalized = denormalize([article1, article2], normalized.entities, articleListSchema);
// console.log(denormalized)
[{
  id: 1,
  title: '10 mindblowing reasons to prefer composition over inheritance',
  author: {
    id: 1,
    name: 'Dan'
  },
},{
  id: 2,
  title: 'You won\'t believe what this high order component is doing',
  author: {
    id: 1,
    name: 'Dan'
  },
}]

Denormalize by passing the id

const denormalized = denormalize(1, normalized.entities, articleSchema);
// console.log(denormalized);
{
  id: 1,
  title: '10 mindblowing reasons to prefer composition over inheritance',
  author: {
    id: 1,
    name: 'Dan'
  },
}

Denormalize by passing a list of ids

const denormalized = denormalize([1, 2], normalized.entities, articleListSchema);
// console.log(denormalized)
[{
  id: 1,
  title: '10 mindblowing reasons to prefer composition over inheritance',
  author: {
    id: 1,
    name: 'Dan'
  },
},{
  id: 2,
  title: 'You won\'t believe what this high order component is doing',
  author: {
    id: 1,
    name: 'Dan'
  },
}]

Recursive schemas

Denormalizr can handle circular references caused by recursive schemas (see #2).

For example, take these schemas, where articles have an author property containing a list of articles:

const articleSchema = new schema.Entity('articles');
const authorSchema = new schema.Entity('author');
const articleList = new schema.Array(articleSchema);

articleSchema.define({
  author: authorSchema,
});

authorSchema.define({
  articles: articleList,
});

const JSONResponse = {
  "articles": [{
    "id": 2,
    "title": "You won\'t believe what this high order component is doing",
    "author": {
      "id": 1,
      "name": 'Dan',
      "articles": [2],
    },
  }],
};

const normalized = normalize(JSONResponse, {
  articles: articleList,
});

const article = data.entities.articles['2'];
const denormalized = denormalize(article, data.entities, articleSchema);

console.log(denormalized.author.articles[0] === denormalized)); // true

Usage with Immutable

Denormalizr works well with immutable-js, however recursive schemas are not supported:

// This nested article contains only a reference to the author's id:
denormalized.author.articles[0].author === 1

Related work:

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].