3

What would you do, if you had a normalized redux store and using ids for entities, but also have other additional unique identifiers and need to use them as well.

Example

{ 
   entities: {
        users: {
           1: {
              name: 'foo',
              id: 1, //unique
              uuid: '3d89afe9-6a85-46ed-ac0d-28e10b21d09e' // unique
            },
            // ...
        }
   }
}

Most of the I'm using id property to find an entity, but sometimes i just have the uuid property.

Is there a way to use a more advanced schema to have a second dictionary which maps uuid to id, use es6 Map to have a key which consists of both id and uuid or some other way i didnt think of?

Following would be useful:

{ 
   entities: {
        users: {
           1: {
              name: 'foo',
              id: 1, //unique
              uuid: '3d89afe9-6a85-46ed-ac0d-28e10b21d09e' // unique
            },
            // ...
        }
        usersUuidToIdMapping: {
           '3d89afe9-6a85-46ed-ac0d-28e10b21d09e': 1
           // ...
        }   

   }
}

Best, Faruk

farukg
  • 523
  • 4
  • 12

1 Answers1

0

If you want to define the uuid as the id, you can use a string on the idAttribute option when you define your Entity.

new schema.Entity('users', {}, { idAttribute: 'uuid' })

If you want to use both, the idAttribute can be a function that has three arguments value, parent and key.

new schema.Entity('users', {}, {
  idAttribute: user => user.uuid ? `${user.id}-${user.uuid}` : user.id
})

More information here: https://github.com/paularmstrong/normalizr/blob/master/docs/api.md#idattribute-usage

Henrique Limas
  • 307
  • 1
  • 10
  • 1
    Hi, thx, but i didnt want to combine the ids. I wanted to be able to find items either based on uuid or id. – farukg Mar 29 '18 at 15:14