gpbl / denormalizr

Denormalize data normalized with normalizr
https://www.npmjs.com/package/denormalizr
MIT License
228 stars 24 forks source link

Denormalizing setup problem #27

Closed dhlavaty closed 7 years ago

dhlavaty commented 8 years ago

I'm trying to normalize and denormalize a simple object structure, but I'm really stuck. I think that I do everything according documentation, but without any luck.

You can try my example here: https://jsbin.com/heyayodagi/5/edit?js,console

Code:

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);

But denormalized output looks like this:

{
  'id': 'chart-1',
  'chart_title': 'Back to the future',
  'series': {
    '0': '11',
    '1': '22'
  },
  'y_axes': {
    '0': '133'
  },
  'x_axis': {
    'title': 'Days'
  }
}

Am I doing something wrong, or there is an issue in this library?

mhaddon commented 8 years ago

You might have the same problem as i did https://github.com/gpbl/denormalizr/issues/24

go to the denormalizr source and go to lib/index.js and change line 172 to 178 with this:

if (schema instanceof _EntitySchema2.default || typeof obj === "number" || typeof obj === "string") {
    return denormalizeEntity(obj, entities, schema, bag);
} else if (schema instanceof _IterableSchema2.default || Array.isArray(obj)) {
    return denormalizeIterable(obj, entities, schema, bag);
} else if (schema instanceof _UnionSchema2.default) {
    return denormalizeUnion(obj, entities, schema, bag);
}

its not an elegant fix, but ive found its made the library work for me

gpbl commented 7 years ago

This should be solved in the latest version. Please upgrade normalizr and denormalizr thanks!