const original = {
id: 'chart-1',
chart_title: 'Back to the future',
series: [{
id: '11',
label: 'Visitors',
color: '#ff0000',
dashed: false,
yaxis: '133'
},
{
id: '22',
label: 'Visitors prediction',
color: '#ff5500',
dashed: true,
yaxis: '133'
}],
y_axes: [{
id: '133',
title: 'Total revenue'
}],
x_axis: {
title: 'Days'
}
};
// SCHEMA
const lineChartSchema = new Schema('line_charts', { idAttribute: 'id' });
const seriesSchema = new Schema('series_out', { idAttribute: 'id' });
const yAxeSchema = new Schema('y_axes_out', { idAttribute: 'id' });
lineChartSchema.define({
series: arrayOf(seriesSchema),
y_axes: arrayOf(yAxeSchema)
});
// NOW WE WILL NORMALIZE IT
const norm = normalize(original, lineChartSchema);
console.log('Normalized version:');
console.log(norm);
// NOW WE WILL TRY TO GO BACK AND DENORMALIZE
// try 1
const denorm = denormalize(norm.entities.line_charts['chart-1'], norm.entities, lineChartSchema);
// try 2 - docs say first parameter can be "a single id"
// const denorm = denormalize('chart-1', norm.entities, lineChartSchema);
console.log('Trying to de-normalize it back:');
console.log(denorm);
{
'id': 'chart-1',
'chart_title': 'Back to the future',
'series': {
'0': '11',
'1': '22'
},
'y_axes': {
'0': '133'
},
'x_axis': {
'title': 'Days'
}
}