【问题标题】:How to normalize data by different schemas based on the entity parent's value with normalizr?如何使用 normalizr 根据实体父级的值通过不同的模式对数据进行规范化?
【发布时间】:2019-02-21 11:33:47
【问题描述】:

我想以这种格式规范化数据:

    [
      {
        id: 3,
        status: "active",
        created: "2017-07-03T15:36:11+02:00",
        published: "2017-07-03T15:38:07+02:00",
        linkables: [
          {
            data: {
              content: "Text content of the linkable",
              id: 200
            }
            linkable_type: "content",
            module_id: 3,
            module_type: "case_study"
          },
          {
            data: {
              content: "https://foobar.wee/url_of_the_media.png",
              id: 205
            }
            linkable_type: "media",
            module_id: 3,
            module_type: "case_study"
          },
        ]
      },
      ...
    ]

变成这样的:

    {
      entities: {
        contents: {
          200: {
            content: "Text content of the linkable",
            id: 200
          }
        },
        media: {
          205: {
            content: "https://foobar.wee/url_of_the_media.png",
            id: 205
          }
        },
        linkables: {
          3_case_study_content_200: {
            data: 200, // in the contents key
            linkable_type: "content",
            module_id: 3,
            module_type: "case_study"
          },
          3_case_study_media_205: {
            data: 205, // in the media key
            linkable_type: "media",
            module_id: 3,
            module_type: "case_study"
          }
        },
        caseStudies: {
          3: {
              id: 3,
              status: "active",
              created: "2017-07-03T15:36:11+02:00",
              linkables: ["3_case_study_content_200", "3_case_study_media_205"]
          }
        }
      },
      result: [3]
    }

由于这个 PR https://github.com/paularmstrong/normalizr/pull/132 添加了一个函数,因此使用 2.x 版本的 normalizr 可以轻松实现这一点,该函数可以用来动态决定在通过父对象中的值进行规范化时使用哪种方案。

但是,在 3.x.x 版本中,这个附加功能没有实现。根据docs,我会说schemaAttribute 可以完成这项工作。

不幸的是,拥有这段代码并没有给我想要的结果......

    export const caseStudies = new schema.Entity('caseStudies');

    export const media = new schema.Entity('media',);
    export const content = new schema.Entity('contents');

    export const linkablesSchema = new schema.Entity(
      'linkables',
      {},
      {
        idAttribute: (itm) => {
          const id = itm.data.id;
          return `${itm.module_id}_${itm.module_type}_${itm.linkable_type}_${id}`;
        }
      }
    );

    export const linkableSchemaMap = {
      content,
      media
    };

    caseStudies.define({
      linkables: [linkablesSchema]
    });


    export const lschema = new schema.Array(linkableSchemaMap, (value, parent) => {
      return parent.linkable_type; // this would replace the PR mentioned above?
    });

    linkablesSchema.define({
      data: lschema
    });

感谢您的帮助!

【问题讨论】:

    标签: javascript schema normalizr


    【解决方案1】:

    想到的最直接的方法是在linkablesSchema 选项内使用processStrategylinkable_type 值作为属性应用:

    export const caseStudies = new schema.Entity("caseStudies");
    export const mediaSchema = new schema.Entity("media");
    export const contentsSchema = new schema.Entity("contents");
    export const linkablesSchema = new schema.Entity("linkables", {}, {
      idAttribute: itm => `${itm.module_id}_${itm.module_type}_${itm.linkable_type}_${itm.data.id}`,
      processStrategy: (value) => ({ 
        ...value,
        data: value.data.id,
        [value.linkable_type]: value.data,
      })
    });
    
    linkablesSchema.define({
      content: contentsSchema,
      media: mediaSchema
    })
    
    caseStudies.define({
      linkables: [linkablesSchema]
    });
    
    normalize(data, [caseStudies]);
    

    这是sandbox。希望对您有所帮助。

    【讨论】:

    • 回复一下怎么样?
    • 抱歉耽搁了,当可链接对象在其数据对象中包含另一个可链接对象数组时,当前面临一个问题,但主要问题已解决。谢谢
    • 很高兴为您提供帮助。如果您提供更多信息,我会尝试帮助嵌套可链接对象
    • 我遇到了嵌套可链接对象的问题...例如,想象一下,可链接对象在其数据对象中有另一个可链接数组,我想再次通过链接模式对其进行规范化,我遇到了Cannot read property 'normalize' of undefined at visit
    • 更新了沙盒。这是你的意思吗?
    猜你喜欢
    • 2017-06-09
    • 2018-10-10
    • 2021-04-14
    • 2018-03-05
    • 2018-05-23
    • 2020-07-31
    • 2022-10-20
    • 2017-10-05
    • 1970-01-01
    相关资源
    最近更新 更多