with-normalizr
v1.0.0
Published
Expose simplified functions to manipulate flattened data structure with normalizr.
Downloads
5
Readme
with-normalizr
Before I have been created
With normalizr library, we could create a customer schema with the following code:
import { schema } from 'normalizr';
const customerSchema = new schema.Entity('customer');
Let's say we want to transform customer object into normalised data structure, here is what normalizr official documentation says:
import { normalize } from 'normalizr';
const customer = { id: 1, name: 'marco' };
const normalisedCustomer = normalize(customer, customerSchema);
At the time we need to denormalise, we could do this:
import { denormalize } from 'normalizr';
const denormalisedCustomer = denormalize(normalisedCustomer.result, customerSchema, normalisedCustomer.entities);
Why am I here?
The above is literarily everything we would use with normalizr.
I have two unhappiness. One is that when denormalise, I have to pass normalised result and entities separately to the denormalize function although these two parameters are generated by normalize function at once.
The other is redundancy. Schema is created by normalizr but when interacting with schema, however, I have to import another function from normalizr to call schema. With Object Oriented Programming in mind, I feel this approach unsatisfied.
Now live with me
Now with with-normalizr, it becomes this: Defining a schema:
import { schema } from 'normalizr';
import withNormalizr from 'with-normalizr';
const customerSchema = withNormalizr(new schema.Entity('customer'));
Normalise:
const customer = { id: 1, name: 'marco' };
const normalisedCustomer = customerSchema.toNormalize(customer);
Denormalise:
const denormalisedCustomer = customerSchema.toDenormalize(normalisedCustomer);
One more thing
Moreover, with-normalizr is an HOC on schema instance, which means all usages provided by normalizr are still working.