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

feat(entity) add key naming strategy #443

Closed
wants to merge 2 commits into from

Conversation

gogoout
Copy link

@gogoout gogoout commented May 11, 2020

Problem

It's just confuse to having the same property sometimes to be Id, sometimes to be an object/array. And that problem is particularly annoying when it comes to using TypeScript, as user have to define the type either like user: number|User or user: any.

Found one issue on this one.
https://github.com/paularmstrong/normalizr/issues/423

Solution

Add an optional option to suffixed id to the property.

      const user = new schema.Entity('users', {}, { keyNamingStrategy: 'camelCase' });
      const comment = new schema.Entity('comments', {}, { keyNamingStrategy: 'camelCase' });
      const article = new schema.Entity(
        'articles',
        {
          author: user,
          comments: [comment]
        },
        { keyNamingStrategy: 'camelCase' }
      );

      const input = {
        id: '123',
        title: 'A Great Article',
        author: {
          id: '8472',
          name: 'Paul'
        },
        body: 'This article is great.',
        comments: [
          {
            id: 'comment-123-4738',
            comment: 'I like it!'
          }
        ]
      };

result

"entities": Object {
    "articles": Object {
      "123": Object {
        "authorId": "8472",
        "body": "This article is great.",
        "commentsIds": Array [
          "comment-123-4738",
        ],
        "id": "123",
        "title": "A Great Article",
      },
    },
    "comments": Object {
      "comment-123-4738": Object {
        "comment": "I like it!",
        "id": "comment-123-4738",
      },
    },
    "users": Object {
      "8472": Object {
        "id": "8472",
        "name": "Paul",
      },
    },
  },
  "result": "123",

TODO

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

@gogoout
Copy link
Author

gogoout commented May 11, 2020

Hi @paularmstrong , please let me know if you are happy with this request. I'll add the document if you are happy about the code stuff.

@coveralls
Copy link

coveralls commented May 11, 2020

Coverage Status

Coverage remained the same at 100.0% when pulling 23497c1 on gogoout:feature/idrule into db5b4b0 on paularmstrong:master.

@paularmstrong
Copy link
Owner

As noted in the issue that you linked, this is a major change that would be better off in a fork of normalizr.

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