Home PC Games Linux Windows Database Network Programming Server Mobile  
           
  Home \ Programming \ JSON data normalization (normalize)     - Git delete files (Linux)

- VirtualBox - Linux system virtual machine shared folders (Linux)

- Linux serial debugging tools xgcom install (Linux)

- Linux System Getting Started Learning: The Linux log (Linux)

- Grading defense against Linux server attacks (Linux)

- High-performance JavaScript loops and flow control (Programming)

- Do not enter password login ssh (Server)

- To create someone else can not afford to delete the administrator user (Linux)

- Sorting algorithm of dichotomy (binary) insertion sort algorithm (Programming)

- Linux file permissions to modify the command: chmod (Linux)

- Scope of variables in C # (Programming)

- Applications Linux leap second problem caused (Linux)

- Getting Started with Linux system to learn: how to check the version of SSH on Linux (Linux)

- Java Foundation - Getting Start (Programming)

- Linux System Getting Started Tutorial: How do you know Shell which is currently being used (Linux)

- Erase do with HTML5 Canvas and diffusion effect (Programming)

- Use lsof restore accidentally deleted log files or database (Linux)

- bash login and welcome message: / etc / issue, / etc / motd (Linux)

- Difference Docker mirror and containers (Server)

- Terminal multiplexing tool tmux use (Linux)

 
         
  JSON data normalization (normalize)
     
  Add Date : 2018-11-21      
         
         
         
  Summary
When developing complex applications, it is inevitable there will be some reference data with each other. I suggest you as much as possible the state paradigm, there is no nesting. Put all the data into one object, each data ID is the primary key, the data refer to each other by different ID to find. The thought of the application state database. This method normalizr documentation has elaborated. normalizr ...

When developing complex applications, it is inevitable there will be some reference data with each other. I suggest you as much as possible the state paradigm, there is no nesting. Put all the data into one object, each data ID is the primary key, the data refer to each other by different ID to find. The thought of the application state database. This method normalizr documentation has elaborated.

normalizr: nested JSON format flat, it is easy to use Redux;

aims
Our goal is to:

[{
  id: 1,
  title: 'Some Article',
  author: {
    id: 1,
    name: 'Dan'
  }
}, {
  id: 2,
  title: 'Other Article',
  author: {
    id: 1,
    name: 'Dan'
  }
}]
Each object arrays are a blend of the three dimensions of the article, the author
The data paradigm should split up these two dimensions, both through contact id associated to
We describe the structure described above: - returns an array (array) - array of objects contained in another schema - user

It should be more reasonable, and should be converted:

{
  result: [1, 2],
  entities: {
    articles: {
      1: {
        id: 1,
        title: 'Some Article',
        author: 1
      },
      2: {
        id: 2,
        title: 'Other Article',
        author: 1
      }
    },
    users: {
      1: {
        id: 1,
        name: 'Dan'
      }
    }
  }
}
how to use
Watch the best example is the official test file: https: //github.com/gaearon/normalizr/blob/master/test/index.js

First introduced normalizr:

import {normalize, Schema, arrayOf} from 'normalizr';
Defined schema:

    var article = new Schema ( 'articles'),
        user = new Schema ( 'users'),
        collection = new Schema ( 'collections'),
        feedSchema,
        input;
Define rules:

    article.define ({
      author: user,
      collections: arrayOf (collection)
    });

    collection.define ({
      curator: user
    });

    feedSchema = {
      feed: arrayOf (article)
    };
test:

    input = {
      feed: [{
        id: 1,
        title: 'Some Article',
        author: {
          id: 3,
          name: 'Mike Persson'
        },
        collections: [{
          id: 1,
          title: 'Awesome Writing',
          curator: {
            id: 4,
            name: 'Andy Warhol'
          }
        }, {
          id: 7,
          title: 'Even Awesomer',
          curator: {
            id: 100,
            name: 'T.S. Eliot'
          }
        }]
      }, {
        id: 2,
        title: 'Other Article',
        collections: [{
          id: 2,
          title: 'Neverhood',
          curator: {
            id: 120,
            name: 'Ada Lovelace'
          }
        }],
        author: {
          id: 2,
          name: 'Pete Hunt'
        }
      }]
    };

    Object.freeze (input);

    normalize (input, feedSchema) .should.eql ({
      result: {
        feed: [1, 2]
      },
      entities: {
        articles: {
          1: {
            id: 1,
            title: 'Some Article',
            author: 3,
            collections: [1, 7]
          },
          2: {
            id: 2,
            title: 'Other Article',
            author: 2,
            collections: [2]
          }
        },
        collections: {
          1: {
            id: 1,
            title: 'Awesome Writing',
            curator: 4
          },
          2: {
            id: 2,
            title: 'Neverhood',
            curator: 120
          },
          7: {
            id: 7,
            title: 'Even Awesomer',
            curator: 100
          }
        },
        users: {
          2: {
            id: 2,
            name: 'Pete Hunt'
          },
          3: {
            id: 3,
            name: 'Mike Persson'
          },
          4: {
            id: 4,
            name: 'Andy Warhol'
          },
          100: {
            id: 100,
            name: 'T.S. Eliot'
          },
          120: {
            id: 120,
            name: 'Ada Lovelace'
          }
        }
      }
    });
Advantage
Assuming that the request / articles to return data schema as follows:

articles: article *

article: {
  author: user,
  likers: user *
  primary_collection: collection?
  collections: collection *
}

collection: {
  curator: user
}
If not normalization, store needs to know what the various structures of the API, such as UserStore contains a lot of boilerplate code to obtain a new user, such as the following:
switch (action.type) {
  case ActionTypes.RECEIVE_USERS:
    mergeUsers (action.rawUsers);
    break;

  case ActionTypes.RECEIVE_ARTICLES:
    action.rawArticles.forEach (rawArticle => {
      mergeUsers ([rawArticle.user]);
      mergeUsers (rawArticle.likers);

      mergeUsers ([rawArticle.primaryCollection.curator]);
      rawArticle.collections.forEach (rawCollection => {
        mergeUsers (rawCollection.curator);
      });
    });

    UserStore.emitChange ();
    break;
  }
store expressed do not feel tired, love ah! ! Each store must be returned to the various foreach to obtain the desired data.

A paradigm to it:

const article = new Schema ( 'articles');
const user = new Schema ( 'users');

article.define ({
  author: user,
  contributors: arrayOf (user),
  meta: {
    likes: arrayOf ({
      user: user
    })
  }
});

// ...

const json = getArticleArray ();
const normalized = normalize (json, arrayOf (article));
After consolidation paradigm, or do not you Airi Airi, users of the object is always in action.entities.users:

  const {action} = payload;

  if (action.response && action.response.entities && action.response.entities.users) {
    mergeUsers (action.response.entities.users);
    UserStore.emitChange ();
    break;
  }
More examples (from the test file)
Standardized single file
    var article = new Schema ( 'articles'),
        input;

    input = {
      id: 1,
      title: 'Some Article',
      isFavorite: false
    };

    Object.freeze (input);

    normalize (input, article) .should.eql ({
      result: 1,
      entities: {
        articles: {
          1: {
            id: 1,
            title: 'Some Article',
            isFavorite: false
          }
        }
      }
    });
Standardized embedded object, and delete the extra key
Sometimes the back-end interface will return a lot of extra fields, maybe even duplicate fields; for example, in the example below typeId and type.id is repeated; attention is key process parameters through artcle.define defined before.

    var article = new Schema ( 'articles'),
        type = new Schema ( 'types'),
        input;

    // Define inline rules
    article.define ({
      type: type
    });

    input = {
      id: 1,
      title: 'Some Article',
      isFavorite: false,
      typeId: 1,
      type: {
        id: 1,
      }
    };

    Object.freeze (input);

    // AssignEntity delete additional data returned by the backend
    var options = {
      assignEntity: function (obj, key, val) {
        obj [key] = val;
        delete obj [key + 'Id'];
      }
    };

    normalize (input, article, options) .should.eql ({
      result: 1,
      entities: {
        articles: {
          1: {
            id: 1,
            title: 'Some Article',
            isFavorite: false,
            type: 1
          }
        },
        types: {
          1: {
            id: 1
          }
        }
      }
    });
Add additional data
And contrary to the previous example, mergeIntoEntity for data fusion of multiple homogeneous different information together to resolve the conflict.

In the example below, author and reviewer are the same person, except that leave contact phone, the latter leaving the contact mailbox, but in any case are the same person;

At this point you can use two data mergeIntoEntity fused together; (Note that this is valueOf rules)

    var author = new Schema ( 'authors'),
        input;

    input = {
      author: {
        id: 1,
        name: 'Ada Lovelace',
        contact: {
          phone: '555-0100'
        }
      },
      reviewer: {
        id: 1,
        name: 'Ada Lovelace',
        contact: {
          email: 'ada@lovelace.com'
        }
      }
    }

    Object.freeze (input);

    var options = {
      mergeIntoEntity: function (entityA, entityB, entityKey) {
        var key;

        for (key in entityB) {
          if (! entityB.hasOwnProperty (key)) {
            continue;
          }

          if (! entityA.hasOwnProperty (key) || isEqual (entityA [key], entityB [key])) {
            entityA [key] = entityB [key];
            continue;
          }

          if (isObject (entityA [key]) && isObject (entityB [key])) {
            merge (entityA [key], entityB [key])
            continue;
          }

          console.warn ( 'Unequal data!');
        }
      }
    };

    normalize (input, valuesOf (author), options) .should.eql ({
      result: {
        author: 1,
        reviewer: 1
      },
      entities: {
        authors: {
          1: {
            id: 1,
            name: 'Ada Lovelace',
            contact: {
              phone: '555-0100',
              email: 'ada@lovelace.com'
            }
          }
        }
      }
    });
Standardization of the specified attribute
Sometimes the object has no id attribute, or we do not want to press id attribute normalization, using idAttribute specified;

The following example is the use of slug as standardized key:

var article = new Schema ( 'articles', {idAttribute: 'slug'}),
        input;

    input = {
      id: 1,
      slug: 'some-article',
      title: 'Some Article',
      isFavorite: false
    };

    Object.freeze (input);

    normalize (input, article) .should.eql ({
      result: 'some-article',
      entities: {
        articles: {
          'Some-article': {
            id: 1,
            slug: 'some-article',
            title: 'Some Article',
            isFavorite: false
          }
        }
      }
    });
Create custom properties
Sometimes I think you have created a key, although today was created last year and the name of the article is Happy, but obviously is not the same, in order to distinguish time, you can use a custom function to generate the desired key.

    function makeSlug (article) {
      var posted = article.posted,
          title = article.title.toLowerCase () replace ( '', '-').;

      return [title, posted.year, posted.month, posted.day] .join ( '-');
    }

    var article = new Schema ( 'articles', {idAttribute: makeSlug}),
        input;

    input = {
      id: 1,
      title: 'Some Article',
      isFavorite: false,
      posted: {
        day: 12,
        month: 3,
        year: 1983
      }
    };

    Object.freeze (input);

    normalize (input, article) .should.eql ({
      result: 'some-article-1983-3-12',
      entities: {
        articles: {
          'Some-article-1983-3-12': {
            id: 1,
            title: 'Some Article',
            isFavorite: false,
            posted: {
              day: 12,
              month: 3,
              year: 1983
            }
          }
        }
      }
    });
Normalization Array
Returned by the backend data is often a string array majority, this time played a significant role in standardization, standardization of data compression while again;

   var article = new Schema ( 'articles'),
        input;

    input = [{
      id: 1,
      title: 'Some Article'
    }, {
      id: 2,
      title: 'Other Article'
    }];

    Object.freeze (input);

    normalize (input, arrayOf (article)). should.eql ({
      result: [1, 2],
      entities: {
        articles: {
          1: {
            id: 1,
            title: 'Some Article'
          },
          2: {
            id: 2,
            title: 'Other Article'
          }
        }
      }
    });
Extracting a plurality of schema
Mentioned above situation is relatively simple, involving only a single schema extraction result of the case; in reality, you often want to abstract the multiple schema, such as the bottom, I think pulled out of tutorials (tutorials) and articles (article) two schema, At this point you need to specify these two options are distinguished by schemaAttribute schema fields:

    var article = new Schema ( 'articles'),
        tutorial = new Schema ( 'tutorials'),
        articleOrTutorial = {articles: article, tutorials: tutorial},
        input;

    input = [{
      id: 1,
      type: 'articles',
      title: 'Some Article'
    }, {
      id: 1,
      type: 'tutorials',
      title: 'Some Tutorial'
    }];

    Object.freeze (input);

    normalize (input, arrayOf (articleOrTutorial, {schemaAttribute: 'type'})). should.eql ({
      result: [
        {Id: 1, schema: 'articles'},
        {Id: 1, schema: 'tutorials'}
      ],
      entities: {
        articles: {
          1: {
            id: 1,
            type: 'articles',
            title: 'Some Article'
          }
        },
        tutorials: {
          1: {
            id: 1,
            type: 'tutorials',
            title: 'Some Tutorial'
          }
        }
      }
    });
In this example, although the article id is 1, but obviously they are different articles because an ordinary article is a tutorial article; therefore according to data pulled schema dimension;

Here arrayOf (articleOrTutorial) in articleOrTutorial is an object that contains multiple attributes, which means that input should be articleOrTutorial in one case;

Sometimes the original data and attributes we define some differences, can now be set as a function of the value schemaAttribute, the original property has been properly machined; such property is the original tutorial, but pulled out of the schema name for the tutorials, a difference of a s:

    function guessSchema (item) {
      return item.type + 's';
    }

    var article = new Schema ( 'articles'),
        tutorial = new Schema ( 'tutorials'),
        articleOrTutorial = {articles: article, tutorials: tutorial},
        input;

    input = [{
      id: 1,
      type: 'article',
      title: 'Some Article'
    }, {
      id: 1,
      type: 'tutorial',
      title: 'Some Tutorial'
    }];

    Object.freeze (input);

    normalize (input, arrayOf (articleOrTutorial, {schemaAttribute: guessSchema})). should.eql ({
      result: [
        {Id: 1, schema: 'articles'},
        {Id: 1, schema: 'tutorials'}
      ],
      entities: {
        articles: {
          1: {
            id: 1,
            type: 'article',
            title: 'Some Article'
          }
        },
        tutorials: {
          1: {
            id: 1,
            type: 'tutorial',
            title: 'Some Tutorial'
          }
        }
      }
    });
The above is an array of circumstances, for ordinary objects are possible, the rules can be changed valueOf:

   var article = new Schema ( 'articles'),
        tutorial = new Schema ( 'tutorials'),
        articleOrTutorial = {articles: article, tutorials: tutorial},
        input;

    input = {
      one: {
        id: 1,
        type: 'articles',
        title: 'Some Article'
      },
      two: {
        id: 2,
        type: 'articles',
        title: 'Another Article'
      },
      three: {
        id: 1,
        type: 'tutorials',
        title: 'Some Tutorial'
      }
    };

    Object.freeze (input);

    normalize (input, valuesOf (articleOrTutorial, {schemaAttribute: 'type'})). should.eql ({
      result: {
        one: {id: 1, schema: 'articles'},
        two: {id: 2, schema: 'articles'},
        three: {id: 1, schema: 'tutorials'}
      },
      entities: {
        articles: {
          1: {
            id: 1,
            type: 'articles',
            title: 'Some Article'
          },
          2: {
            id: 2,
            type: 'articles',
            title: 'Another Article'
          }
        },
        tutorials: {
          1: {
            id: 1,
            type: 'tutorials',
            title: 'Some Tutorial'
          }
        }
      }
    });
schemaAttribute is the case the function is not listed, and said the same;

Standardization of embedded case
The above object is relatively simple, the original is flattened; if the object format is slightly more complex, such as a multi-authored each article case. In this case requires the use of prior statements define the hierarchical relationship between the schema:

    var article = new Schema ( 'articles'),
        user = new Schema ( 'users'),
        input;

    article.define ({
      author: user
    });

    input = {
      id: 1,
      title: 'Some Article',
      author: {
        id: 3,
        name: 'Mike Persson'
      }
    };

    Object.freeze (input);

    normalize (input, article) .should.eql ({
      result: 1,
      entities: {
        articles: {
          1: {
            id: 1,
            title: 'Some Article',
            author: 3
          }
        },
        users: {
          3: {
            id: 3,
            name: 'Mike Persson'
          }
        }
      }
    });
The above is not that simple? So to give you a more complex case, remain the same. We ultimately want pulled out of articles, users and collections of these three schema, so long as the definition of the three schema on the line,

Then use intricate among the three schema define the method declaration relationship;

The outermost feed only attribute does not need to be defined;

    var article = new Schema ( 'articles'),
        user = new Schema ( 'users'),
        collection = new Schema ( 'collections'),
        feedSchema,
        input;

    article.define ({
      author: user,
      collections: arrayOf (collection)
    });

    collection.define ({
      curator: user
    });

    feedSchema = {
      feed: arrayOf (article)
    };

    input = {
      feed: [{
        id: 1,
        title: 'Some Article',
        author: {
          id: 3,
          name: 'Mike Persson'
        },
        collections: [{
          id: 1,
          title: 'Awesome Writing',
          curator: {
            id: 4,
            name: 'Andy Warhol'
          }
        }, {
          id: 7,
          title: 'Even Awesomer',
          curator: {
            id: 100,
            name: 'T.S. Eliot'
          }
        }]
      }, {
        id: 2,
        title: 'Other Article',
        collections: [{
          id: 2,
          title: 'Neverhood',
          curator: {
            id: 120,
            name: 'Ada Lovelace'
          }
        }],
        author: {
          id: 2,
          name: 'Pete Hunt'
        }
      }]
    };

    Object.freeze (input);

    normalize (input, feedSchema) .should.eql ({
      result: {
        feed: [1, 2]
      },
      entities: {
        articles: {
          1: {
            id: 1,
            title: 'Some Article',
            author: 3,
            collections: [1, 7]
          },
          2: {
            id: 2,
            title: 'Other Article',
            author: 2,
            collections: [2]
          }
        },
        collections: {
          1: {
            id: 1,
            title: 'Awesome Writing',
            curator: 4
          },
          2: {
            id: 2,
            title: 'Neverhood',
            curator: 120
          },
          7: {
            id: 7,
            title: 'Even Awesomer',
            curator: 100
          }
        },
        users: {
          2: {
            id: 2,
            name: 'Pete Hunt'
          },
          3: {
            id: 3,
            name: 'Mike Persson'
          },
          4: {
            id: 4,
            name: 'Andy Warhol'
          },
          100: {
            id: 100,
            name: 'T.S. Eliot'
          },
          120: {
            id: 120,
            name: 'Ada Lovelace'
          }
        }
      }
    });
Embedded Array tilt +
   var article = new Schema ( 'articles'),
        tutorial = new Schema ( 'tutorials'),
        articleOrTutorial = {articles: article, tutorials: tutorial},
        user = new Schema ( 'users'),
        collection = new Schema ( 'collections'),
        feedSchema,
        input;

    article.define ({
      author: user,
      collections: arrayOf (collection)
    });

    tutorial.define ({
      author: user,
      collections: arrayOf (collection)
    });

    collection.define ({
      curator: user
    });

    feedSchema = {
      feed: arrayOf (articleOrTutorial, {schemaAttribute: 'type'})
    };

    input = {
      feed: [{
        id: 1,
        type: 'articles',
        title: 'Some Article',
        author: {
          id: 3,
          name: 'Mike Persson'
        },
        collections: [{
          id: 1,
          title: 'Awesome Writing',
          curator: {
            id: 4,
            name: 'Andy Warhol'
          }
        }, {
          id: 7,
          title: 'Even Awesomer',
          curator: {
            id: 100,
            name: 'T.S. Eliot'
          }
        }]
      }, {
        id: 1,
        type: 'tutorials',
        title: 'Some Tutorial',
        collections: [{
          id: 2,
          title: 'Neverhood',
          curator: {
            id: 120,
            name: 'Ada Lovelace'
          }
        }],
        author: {
          id: 2,
          name: 'Pete Hunt'
        }
      }]
    };

    Object.freeze (input);

    normalize (input, feedSchema) .should.eql ({
      result: {
        feed: [
          {Id: 1, schema: 'articles'},
          {Id: 1, schema: 'tutorials'}
        ]
      },
      entities: {
        articles: {
          1: {
            id: 1,
            type: 'articles',
            title: 'Some Article',
            author: 3,
            collections: [1, 7]
          }
        },
        tutorials: {
          1: {
            id: 1,
            type: 'tutorials',
            title: 'Some Tutorial',
            author: 2,
            collections: [2]
          }
        },
        collections: {
          1: {
            id: 1,
            title: 'Awesome Writing',
            curator: 4
          },
          2: {
            id: 2,
            title: 'Neverhood',
            curator: 120
          },
          7: {
            id: 7,
            title: 'Even Awesomer',
            curator: 100
          }
        },
        users: {
          2: {
            id: 2,
            name: 'Pete Hunt'
          },
          3: {
            id: 3,
            name: 'Mike Persson'
          },
          4: {
            id: 4,
            name: 'Andy Warhol'
          },
          100: {
            id: 100,
            name: 'T.S. Eliot'
          },
          120: {
            id: 120,
            name: 'Ada Lovelace'
          }
        }
      }
    });
Embedded + object (re-embedded)
See the following valuesOf (arrayOf (user)) no, it indicates that the property is an object, each array values ​​inside the object is an array of User objects;

    var article = new Schema ( 'articles'),
        user = new Schema ( 'users'),
        feedSchema,
        input;

    article.define ({
      collaborators: valuesOf (arrayOf (user))
    });

    feedSchema = {
      feed: arrayOf (article),
      suggestions: valuesOf (arrayOf (article))
    };

    input = {
      feed: [{
        id: 1,
        title: 'Some Article',
        collaborators: {
          authors: [{
            id: 3,
            name: 'Mike Persson'
          }],
          reviewers: [{
            id: 2,
            name: 'Pete Hunt'
          }]
        }
      }, {
        id: 2,
        title: 'Other Article',
        collaborators: {
          authors: [{
            id: 2,
            name: 'Pete Hunt'
          }]
        }
      }, {
        id: 3,
        title: 'Last Article'
      }],
      suggestions: {
        1: [{
          id: 2,
          title: 'Other Article',
          collaborators: {
            authors: [{
              id: 2,
              name: 'Pete Hunt'
            }]
          }
        }, {
          id: 3,
          title: 'Last Article'
        }]
      }
    };

    Object.freeze (input);

    normalize (input, feedSchema) .should.eql ({
      result: {
        feed: [1, 2, 3],
        suggestions: {
          1: [2, 3]
        }
      },
      entities: {
        articles: {
          1: {
            id: 1,
            title: 'Some Article',
            collaborators: {
              authors: [3],
              reviewers: [2]
            }
          },
          2: {
            id: 2,
            title: 'Other Article',
            collaborators: {
              authors: [2]
            }
          },
          3: {
            id: 3,
            title: 'Last Article'
          }
        },
        users: {
          2: {
            id: 2,
            name: 'Pete Hunt'
          },
          3: {
            id: 3,
            name: 'Mike Persson'
          }
        }
      }
    });
There are far more complex, this time to spend valuesOf (userOrGroup, {schemaAttribute: 'type'}) on:

    var article = new Schema ( 'articles'),
        user = new Schema ( 'users'),
        group = new Schema ( 'groups'),
        userOrGroup = {users: user, groups: group},
        feedSchema,
        input;

    article.define ({
      collaborators: valuesOf (userOrGroup, {schemaAttribute: 'type'})
    });

    feedSchema = {
      feed: arrayOf (article),
      suggestions: valuesOf (arrayOf (article))
    };

    input = {
      feed: [{
        id: 1,
        title: 'Some Article',
        collaborators: {
          author: {
            id: 3,
            type: 'users',
            name: 'Mike Persson'
          },
          reviewer: {
            id: 2,
            type: 'groups',
            name: 'Reviewer Group'
          }
        }
      }, {
        id: 2,
        title: 'Other Article',
        collaborators: {
          author: {
            id: 2,
            type: 'users',
            name: 'Pete Hunt'
          }
        }
      }, {
        id: 3,
        title: 'Last Article'
      }],
      suggestions: {
        1: [{
          id: 2,
          title: 'Other Article'
        }, {
          id: 3,
          title: 'Last Article'
        }]
      }
    };

    Object.freeze (input);

    normalize (input, feedSchema) .should.eql ({
      result: {
        feed: [1, 2, 3],
        suggestions: {
          1: [2, 3]
        }
      },
      entities: {
        articles: {
          1: {
            id: 1,
            title: 'Some Article',
            collaborators: {
              author: {id: 3, schema: 'users'},
              reviewer: {id: 2, schema: 'groups'}
            }
          },
          2: {
            id: 2,
            title: 'Other Article',
            collaborators: {
              author: {id: 2, schema: 'users'}
            }
          },
          3: {
            id: 3,
            title: 'Last Article'
          }
        },
        users: {
          2: {
            id: 2,
            type: 'users',
            name: 'Pete Hunt'
          },
          3: {
            id: 3,
            type: 'users',
            name: 'Mike Persson'
          }
        },
        groups: {
          2: {
            id: 2,
            type: 'groups',
            name: 'Reviewer Group'
          }
        }
      }
    });
Recursive call
For example, certain people are concerned about another person, you wrote a series of articles, the articles by other users to subscribe to is the case:

    var article = new Schema ( 'articles'),
        user = new Schema ( 'users'),
        collection = new Schema ( 'collections'),
        feedSchema,
        input;

    user.define ({
      articles: arrayOf (article)
    });

    article.define ({
      collections: arrayOf (collection)
    });

    collection.define ({
      subscribers: arrayOf (user)
    });

    feedSchema = {
      feed: arrayOf (article)
    };

    input = {
      feed: [{
        id: 1,
        title: 'Some Article',
        collections: [{
          id: 1,
          title: 'Awesome Writing',
          subscribers: [{
            id: 4,
            name: 'Andy Warhol',
            articles: [{
              id: 1,
              title: 'Some Article'
            }]
          }, {
            id: 100,
            name: 'T.S. Eliot',
            articles: [{
              id: 1,
              title: 'Some Article'
            }]
          }]
        }, {
          id: 7,
          title: 'Even Awesomer',
          subscribers: [{
            id: 100,
            name: 'T.S. Eliot',
            articles: [{
              id: 1,
              title: 'Some Article'
            }]
          }]
        }]
      }]
    };

    Object.freeze (input);

    normalize (input, feedSchema) .should.eql ({
      result: {
        feed: [1]
      },
      entities: {
        articles: {
          1: {
            id: 1,
            title: 'Some Article',
            collections: [1, 7]
          }
        },
        collections: {
          1: {
            id: 1,
            title: 'Awesome Writing',
            subscribers: [4, 100]
          },
          7: {
            id: 7,
            title: 'Even Awesomer',
            subscribers: [100]
          }
        },
        users: {
          4: {
            id: 4,
            name: 'Andy Warhol',
            articles: [1]
          },
          100: {
            id: 100,
            name: 'T.S. Eliot',
            articles: [1]
          }
        }
      }
    });
The above is fairly good, some schema directly recursive statement, such as the relationship between children and their parents:

    var user = new Schema ( 'users'),
        input;

    user.define ({
      parent: user
    });

    input = {
      id: 1,
      name: 'Andy Warhol',
      parent: {
        id: 7,
        name: 'Tom Dale',
        parent: {
          id: 4,
          name: 'Pete Hunt'
        }
      }
    };

    Object.freeze (input);

    normalize (input, user) .should.eql ({
      result: 1,
      entities: {
        users: {
          1: {
            id: 1,
            name: 'Andy Warhol',
            parent: 7
          },
          7: {
            id: 7,
            name: 'Tom Dale',
            parent: 4
          },
          4: {
            id: 4,
            name: 'Pete Hunt'
          }
        }
      }
    });
Automatic merge property
In an array which, if consistent with the id attribute, automatically extracts attribute merging into one:

    var writer = new Schema ( 'writers'),
        book = new Schema ( 'books'),
        schema = arrayOf (writer),
        input;

    writer.define ({
      books: arrayOf (book)
    });

    input = [{
      id: 3,
      name: 'Jo Rowling',
      isBritish: true,
      location: {
        x: 100,
        y: 200,
        nested: [ 'hello', {
          world: true
        }]
      },
      books: [{
        id: 1,
        soldWell: true,
        name: 'Harry Potter'
      }]
    }, {
      id: 3,
      name: 'Jo Rowling',
      bio: 'writer',
      location: {
        x: 100,
        y: 200,
        nested: [ 'hello', {
          world: true
        }]
      },
      books: [{
        id: 1,
        isAwesome: true,
        name: 'Harry Potter'
      }]
    }];

    normalize (input, schema) .should.eql ({
      result: [3, 3],
      entities: {
        writers: {
          3: {
            id: 3,
            isBritish: true,
            name: 'Jo Rowling',
            bio: 'writer',
            books: [1],
            location: {
              x: 100,
              y: 200,
              nested: [ 'hello', {
                world: true
              }]
            }
          }
        },
        books: {
          1: {
            id: 1,
            isAwesome: true,
            soldWell: true,
            name: 'Harry Potter'
          }
        }
      }
    });
If there is a conflict in the merge process will prompt and property conflicts automatically removed; for example, below the same author wrote a book, a description of the object in the "sell well", while in another object actually described "poor selling" obviously there is a problem:

    var writer = new Schema ( 'writers'),
        book = new Schema ( 'books'),
        schema = arrayOf (writer),
        input;

    writer.define ({
      books: arrayOf (book)
    });

    input = [{
      id: 3,
      name: 'Jo Rowling',
      books: [{
        id: 1,
        soldWell: true,
        name: 'Harry Potter'
      }]
    }, {
      id: 3,
      name: 'Jo Rowling',
      books: [{
        id: 1,
        soldWell: false,
        name: 'Harry Potter'
      }]
    }];

    var warnCalled = false,
        realConsoleWarn;

    function mockWarn () {
      warnCalled = true;
    }

    realConsoleWarn = console.warn;
    console.warn = mockWarn;

    normalize (input, schema) .should.eql ({
      result: [3, 3],
      entities: {
        writers: {
          3: {
            id: 3,
            name: 'Jo Rowling',
            books: [1]
          }
        },
        books: {
          1: {
            id: 1,
            soldWell: true,
            name: 'Harry Potter'
          }
        }
      }
    });

    warnCalled.should.eq (true);
    console.warn = realConsoleWarn;
Incoming nonexistent schema specification
If the application schma specification does not exist, you pass, you will create a new parent properties:

    var writer = new Schema ( 'writers'),
        schema = writer,
        input;
    input = {
      id: 'constructor',
      name: 'Constructor',
      isAwesome: true
    };

    normalize (input, schema) .should.eql ({
      result: 'constructor',
      entities: {
        writers: {
          constructor: {
            id: 'constructor',
            name: 'Constructor',
            isAwesome: true
          }
        }
      }
     
         
         
         
  More:      
 
- Add your own kernel and ramfs based on an existing Linux LiveCD (Linux)
- Java data structures - order linear form of table ArrayList (Programming)
- Java in the final qualifier (Programming)
- CentOS 6.x Basic System Optimization after installation (Linux)
- CentOS / Linux restart active NIC connection paths / network configure error Error Resolution (Linux)
- Simple Calendar C language (Programming)
- Android HTTP request with Get Information (Programming)
- 10 Codes of good practice PHP (Programming)
- 5 tips to improve your Linux desktop security (Linux)
- RPM package management under Linux (Linux)
- System with Windows Remote Desktop to connect Ubuntu 15.04 (Linux)
- Linux Open coredump (Linux)
- Gentoo: startx problem appears Failed to load module (Linux)
- Android imitation UC browser scroll up and down functions (Programming)
- Linux command line under HTTP traffic sniffing tool: httpry (Linux)
- Java implementation chain store binary search tree (recursive method) (Programming)
- CentOS 6 Install Linux kernel source (Linux)
- Linux installed xdotool simulate keystrokes and mouse movements (Linux)
- Use the TC flow control test under Linux (Linux)
- How to use Linux to download music from Grooveshark (Linux)
     
           
     
  CopyRight 2002-2020 newfreesoft.com, All Rights Reserved.