Search code examples
reduxnormalizr

Normalizr - Setting key of nested responses in results


I see in the normalizer examples that they have a name property "users" being returned on the results object:

{
  result: { users: [ 1, 2 ] },
  entities: {
    users: {
      '1': { id: 1 },
      '2': { id: 2 }
    }
  }
}

I can't seem to figure out how to do this with the nested api response I'm getting. I have both a user and an address reducer that I am trying to pass the results of the normalized response into.

I have a JSON response that looks like this:

[
    {
        id: 1
        first_name: First,
        last_name: Last,
        address: {
            data: [
                {
                    id: 1,
                    address_one: '123 Street Ave',
                    address_two: '',
                    city: 'Some City',
                    state: 'CA',
                    zip: '1234'
                }
            ]
        }
    },
    {
        id: 1
        first_name: First,
        last_name: Last,
        address: {
            data: [
                {
                    id: 2,
                    address_one: '123 Street Ave',
                    address_two: '',
                    city: 'Some City',
                    state: 'CA',
                    zip: '1234'
                },
                {
                    id: 3,
                    address_one: '321 Avenue Road',
                    address_two: 'Suite 101',
                    city: 'Some City',
                    state: 'CA',
                    zip: '1234'
                }
            ]
        }
    }
]

My schema looks like this:

import { schema } from 'normalizr'

/**
 * Addresses
 */
const address = new schema.Entity('addresses');
const arrayOfAddresses = new schema.Array(address);

/**
 * User
 */
export const user = new schema.Entity('users', {
   addresses: arrayOfAddresses
});

user.define({
    addresses: {
        data: arrayOfAddresses
    }
})

export const arrayOfUsers = new schema.Array(user)

Doing: let result = normalize(data, schema.arrayOfUseres) returns:

{
    entities: {
        addresses: /* The address data */
        users: /* The users with an array of the address ids attached to them*/
    },
    result: [1, 2]
}

What I would really like is the result object to have both the users and the addresses in it:

{
    entities: {
        addresses: /* The address data */
        users: /* The users with an array of the address ids attached to them */
    },
    result: {
        addresses: [1, 2, 3]
        users: [1, 2]
    }
}

Is this possible? I've tried several variations of

import * as schema from './schema'
normalize(data, { users: [schema.user], addresses: [schema.address] }

But this just errors out and returns my data back into the result object.


Solution

  • Is this possible?

    No. The result value is always set to match the top structure of your input data per the schema. If your input data doesn't have a first-level key addresses, it won't be possible.