Skip to content
This repository has been archived by the owner on Nov 17, 2017. It is now read-only.

Denormalizing setup problem #27

Closed
dhlavaty opened this issue Oct 27, 2016 · 2 comments
Closed

Denormalizing setup problem #27

dhlavaty opened this issue Oct 27, 2016 · 2 comments

Comments

@dhlavaty
Copy link

dhlavaty commented Oct 27, 2016

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
Copy link

mhaddon commented Oct 30, 2016

You might have the same problem as i did
#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
Copy link
Owner

gpbl commented Feb 12, 2017

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

@gpbl gpbl closed this as completed Feb 12, 2017
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants