Home PC Games Linux Windows Database Network Programming Server Mobile  
           
  Home \ Programming \ JSON data normalization (normalize)     - Orionode source Linux Mint installation (Linux)

- Configure the ASM process on Red Hat Linux 6.5 (Database)

- How to install the client sqlplus under linux (Database)

- Java and C / C ++ data conversion when network communication (Programming)

- Linux how to handle file names that contain spaces and special characters (Linux)

- Oracle SDE and maintain common commands - Display space (Database)

- independently configurable PHP environment under CentOS6.5 (Server)

- Redis data types Introduction (Database)

- Through eight skills to let you become a super Linux end-user (Linux)

- Initialization and starting process of Linux (Linux)

- Ubuntu 14.04 Solution login interface infinite loop (Linux)

- 10 Linux in the passwd command examples (Linux)

- Doubly linked list basic operations (Linux)

- Elementary OS Freya global menu (Linux)

- Orabbix binding Python send graphical reports (Linux)

- Disk storage structure and file recovery experiment (FAT file system) (Linux)

- Android development environment to build under Fedora 13 (Linux)

- RabbitMQ user roles and access control (Linux)

- Java memory area Explanation (Programming)

- MySQL restart process can not be taken lightly (Database)

 
         
  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:      
 
- Three strategies to teach you to release the device memory (Linux)
- Design and implementation of environment sniffer running under Linux (Linux)
- Source code compiled by the installation program under Linux (Linux)
- Arronax allows you to easily create desktop startup file (Linux)
- Ubuntu system grub repair method (Linux)
- CRF ++ Linux use (Linux)
- MBR partitions under Linux (Linux)
- systemd-nspawn Quick Guide (Linux)
- Python KNN algorithm of actual realization (Programming)
- C ++ Object Model Comments (Programming)
- To explore the caching mechanism for Android ListView (Programming)
- When a software RAID data recovery and reconstruction of how failure (Linux)
- Linux platform NTOP Installation and Configuration (Linux)
- C # compiler to achieve functional use in the runtime (Programming)
- Orionode source Linux Mint installation (Linux)
- init level settings for Linux / etc / inittab file into six (restart) or does not support the level of solution (Linux)
- Ubuntu How to install Pacman (Linux)
- Java implementation chain store binary tree (Programming)
- Https (SSL / TLS) Detailed principles (Server)
- Java interface (Programming)
     
           
     
  CopyRight 2002-2022 newfreesoft.com, All Rights Reserved.