Skip to content
This repository was archived by the owner on Mar 20, 2022. It is now read-only.

added a fallback strategy for denormalization #422

Merged
merged 3 commits into from
Feb 22, 2020

Conversation

bjartebore
Copy link
Contributor

@bjartebore bjartebore commented Oct 1, 2019

Problem

In some cases I would like to denormalize a structure with a list of Ids instead of a list of subobjects (reasons could be to limit fetched data). When i normalize this structure the normalization works, I am left with the entity ids, but there is no way to create stubs for the entities itself, which I find ok since there could be an instance of the same object somewhere else in the structure.

But when denormalizing this I will get back an array with undefined items.

Solution

A possible solution to this could be to add a possibility to add a fallbackStrategy option to the EntitySchema where we could write our own fallback method for denormalization.

TODO

  • Add & update tests
  • Ensure CI is passing (lint, tests, flow)
  • Update relevant documentation

@bjartebore
Copy link
Contributor Author

#421

@coveralls
Copy link

coveralls commented Oct 1, 2019

Coverage Status

Coverage remained the same at 100.0% when pulling 65439eb on bjartebore:master into a8ac881 on paularmstrong:master.

Copy link
Owner

@paularmstrong paularmstrong left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like that this has a very low added complexity. Though I feel like it needs really strong documentation with example code of when and why you would use it. Can you please add to the docs?

… passing entity when the fallback is only executed when entity is undefined, + updated tests and documentation
@bjartebore
Copy link
Contributor Author

I have updated the relevant documentation, I hope it will suffice :)

Copy link
Owner

@paularmstrong paularmstrong left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@paularmstrong paularmstrong merged commit 06c4551 into paularmstrong:master Feb 22, 2020
@mecp
Copy link

mecp commented May 8, 2020

Hi guys, Is there a way to skip missing references while denormalizing to avoid having [undefined, undefined, undefined] situation?

for example here, in a use case I have, I want denormalizer to return c: [] instead of c: [undefined, undefined, undefined]

const { schema, denormalize } = require('normalizr');

const a = {
  id: 1,
  b: 1,
  c: [1, 2, 3]
}

const cSchema = new schema.Entity('c');
const aSchema = new schema.Entity('a', { c: [cSchema] });

const result = denormalize([1], [aSchema], { a: { 1: a }, c: {} })

available to run here https://runkit.com/mecp/5eb5baa8aad698001bb12788

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants