Search code examples
javascriptnormalizr

Is it possible to normalizr string?


I am using normalizr. I try to normalize string, but I didn’t figure out.

const mails = [
  {
    "id": "mailId1",
    "conversationId": "conversationId1",
    "content": "Hi",
    "sender": {
      "emailAddress": { name: 'Jack', address: 'jack@example.com' }
    }
  },
  {
    "id": "mailId2",
    "conversationId": "conversationId1",
    "content": "Hello",
    "sender": {
      "emailAddress": { name: 'Rose', address: 'rose@example.com' }
    }
  }
]

const userSchema = new schema.Entity('users', {}, {
  idAttribute: value => value.emailAddress.address
});
const conversationIdSchema = new schema.Entity('conversationIds', {}, {
  idAttribute: value => value
});
const mailSchema = new schema.Entity('mails', {
  conversationId: conversationIdSchema,
  sender: userSchema
});

const normalizedData = normalize(mails, [mailSchema]);

Right now the code above will give me the results below:

(note conversationIdSchema is not working because it is wrong)

{
  result: { mails: ['mailId1', 'mailId2'] },
  entities: {
    users: {
      'jack@example.com': { name: 'Jack', address: 'jack@example.com' }
      'rose@example.com': { name: 'Rose', address: 'rose@example.com' }
    },
    mails: {
      // ...
    }
  }
}

I hope to have conversationIds like this:

{
  result: { mails: ['mailId1', 'mailId2'] },
  entities: {
    users: {
      'jack@example.com': { name: 'Jack', address: 'jack@example.com' }
      'rose@example.com': { name: 'Rose', address: 'rose@example.com' }
    },
    mails: {
      // ...
    },
    conversationIds: ['conversationId1']
  }
}

Solution

  • No, this is not possible. Normalizr works on objects, not strings.