Git Product home page Git Product logo

Comments (6)

gpbl avatar gpbl commented on May 22, 2024

Hi @krisfields, I'm sorry the README is not so clear (something similar is also open in #14).

I can't help you directly right now (I need to reproduce the case first). Maybe you can dig into this test and understand where your code is different?

from denormalizr.

krisfields avatar krisfields commented on May 22, 2024

Is it that it's not clear or that it's inaccurate?

I dug into that test and was not able to see how mine differed at all, other than in the expected result. In fact, I copied/pasted what I believe is the relevant code from that test and still get a different result. Here's what I tried:

import { normalize, Schema, arrayOf } from 'normalizr'

const articleSchema = new Schema('articles')
const userSchema = new Schema('users')
const collectionSchema = new Schema('collections')

articleSchema.define({
  author: userSchema,
  collections: arrayOf(collectionSchema),
})

collectionSchema.define({
  curator: userSchema,
})

const article1 = {
      id: 1,
      title: 'Some Article',
      author: {
        id: 1,
        name: 'Dan',
      },
      collections: [{
        id: 1,
        name: 'Dan',
      }, {
        id: 2,
        name: 'Giampaolo',
      }],
    };
    const article2 = {
      id: 2,
      title: 'Other Article',
      author: {
        id: 1,
        name: 'Dan',
      },
    };
    const article3 = {
      id: 3,
      title: 'Without author',
      author: null,
    };

    const article4 = {
      id: 4,
      title: 'Some Article',
      author: {
        id: '',
        name: 'Deleted',
      },
      collections: [{
        id: '',
        name: 'Deleted',
      }],
    };

const response = {
  articles: [article1, article2, article3, article4],
}

const data = normalize(response, {
  articles: arrayOf(articleSchema),
})

const articles = [
  data.entities.articles['1'],
  data.entities.articles['2'],
]

const denormalized = denormalize(articles, data.entities, arrayOf(articleSchema))
console.log("articles denormalized = ", denormalized)

And the output was not an Array and was not nested, but instead simply had the keys for each object that should have been nested, like before when copying from the README.

articles denormalized = Object {0: Object, 1: Object}
{0: {
  author: 1,
  collections: [{0:1}, {1:2}],
  id: 1,
  title: "Some Article"
},
 1: {
  author: 1,
  id: 2,
  title: "Other Article"
}}

So I'm really curious and confused as to where I may have went wrong.

from denormalizr.

gpbl avatar gpbl commented on May 22, 2024

@krisfields I'm curious as well.

Could you please check you are using the last version of denormalizr?

npm ls denormalizr

I've setup a small repo to test this case: https://github.com/gpbl/denormalizr-test

git clone https://github.com/gpbl/denormalizr-test.git
cd denormalizr-test
npm install
npm start

it logs:

Normalized data
{ entities: 
   { articles: 
      { '1': { id: 1, title: 'Some Article', author: 1, collections: [ 1, 2 ] },
        '2': { id: 2, title: 'Other Article', author: 1 } },
     users: { '1': { id: 1, name: 'Dan' } },
     collections: 
      { '1': { id: 1, name: 'Dan' },
        '2': { id: 2, name: 'Giampaolo' } } },
  result: { articles: [ 1, 2 ] } }
Denormalized:
[ { id: 1,
    title: 'Some Article',
    author: { id: 1, name: 'Dan' },
    collections: [ { id: 1, name: 'Dan' }, { id: 2, name: 'Giampaolo' } ] },
  { id: 2, title: 'Other Article', author: { id: 1, name: 'Dan' } } ]

So it seems all correct to me (macOS, node v4.4.5).

from denormalizr.

krisfields avatar krisfields commented on May 22, 2024

Well, it works correctly for me when I run your test repo, but when I copy/paste the code from that repo's index.js file into my project, it no longer works correctly. The output is no longer an array, and no longer nested. I currently have no idea why but haven't had a lot of time to dig in yet. If you have any suggestions on what I might look into to determine what's going on, any help would be much appreciated.

mac, node v6.6.0

from denormalizr.

krisfields avatar krisfields commented on May 22, 2024

Ok, I guess I wasn't using the latest version of denormalizr. "npm upgrade normalizr --save" didn't actually upgrade it, which is what confused me. Now that I am using the latest version, everything works properly.

from denormalizr.

gpbl avatar gpbl commented on May 22, 2024

Awesome! Yes npm update won't work for minor release < 1.0. I use npm-check for updating dependencies. It is safer :)

from denormalizr.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.