Search code examples
normalizr

Skip entity based on a certain attribute


I am trying to skip an entity. I tried to do so by returning undefined from idAttribute function however it won't skip. Is this possible?

var { schema, normalize } = require("normalizr")

// Define your article 
const article = new schema.Entity('articles', undefined, {
  idAttribute: value => value.id
});

const reply = {
  articles: [ { id:1, commenter:'foo' }, { id:2, commenter:'bar' }, { dummy:true } ]
}

const normalizedData = normalize(reply, { articles: [ article ] });

console.log('normalizedData.entities:', JSON.stringify(normalizedData.entities));

This gives us the data, normalizedData.entities of:

articles: {
  1: {commenter: "foo", id: 1}
  2: {commenter: "bar", id: 2}
  undefined: {dummy: true}
}

However I want to skip any entries that don't have the key id to not be there. This is a very simplified case of my actual case.


Solution

  • Normalizr does not include complexities for your specific case. You will have to filter those out as another step after the normalization process.