What is normalizr?
Normalizr is a powerful library for normalizing nested JSON data. It helps in transforming complex nested data structures into a flat structure, making it easier to manage and work with in applications, especially in state management scenarios.
What are normalizr's main functionalities?
Define Schemas
Normalizr allows you to define schemas for your data. In this example, we define schemas for users, comments, and articles, where comments have a relationship with users, and articles have relationships with both users and comments.
const { schema } = require('normalizr');
const user = new schema.Entity('users');
const comment = new schema.Entity('comments', {
commenter: user
});
const article = new schema.Entity('articles', {
author: user,
comments: [comment]
});
Normalize Data
Once schemas are defined, you can use the `normalize` function to transform your nested data into a normalized form. This example shows how to normalize a nested JSON object representing an article with an author and comments.
const { normalize } = require('normalizr');
const originalData = {
id: '123',
author: {
id: '1',
name: 'Paul'
},
title: 'My awesome blog post',
comments: [
{
id: '324',
commenter: {
id: '2',
name: 'Nicole'
}
}
]
};
const normalizedData = normalize(originalData, article);
console.log(JSON.stringify(normalizedData, null, 2));
Denormalize Data
Normalizr also provides a `denormalize` function to convert normalized data back into its original nested form. This example demonstrates how to denormalize data using the previously defined schemas.
const { denormalize } = require('normalizr');
const normalizedData = {
result: '123',
entities: {
articles: {
'123': { id: '123', author: '1', title: 'My awesome blog post', comments: ['324'] }
},
users: {
'1': { id: '1', name: 'Paul' },
'2': { id: '2', name: 'Nicole' }
},
comments: {
'324': { id: '324', commenter: '2' }
}
}
};
const denormalizedData = denormalize('123', article, normalizedData.entities);
console.log(JSON.stringify(denormalizedData, null, 2));
Other packages similar to normalizr
normalizr-immutable
normalizr-immutable is a fork of normalizr that works with Immutable.js data structures. It provides similar functionality to normalizr but is designed to work seamlessly with Immutable.js, making it a good choice for applications that use Immutable.js for state management.
redux-orm
redux-orm is a library for managing relational data in Redux. It provides an ORM-like interface for defining models and relationships, and it integrates with Redux to manage normalized data. Unlike normalizr, which focuses on normalization and denormalization, redux-orm provides a more comprehensive solution for managing relational data in Redux applications.
normalizr
📣 Normalizr is no longer maintained
Due to lack of ability to find an invested maintainer and inability to find time to do routine maintenance and community building, this package is no longer maintained. Please see the discussion 🤝 Maintainer help wanted for more information.
FAQs
Should I still use Normalizr?
If you need it, yes. Normalizr is and has been at a stable release for a very long time, used by thousands of others without issue.
What should I do if I want other features or found a bug?
Fork Normalizr on Github and maintain a version yourself.
Can I contribute back to Normalizr?
There are no current plans to resurrect this origin of Normalizr. If a forked version becomes sufficiently maintained and popular, please reach out about merging the fork and changing maintainers.