Схема нормализ: вложенные сущности не нормализуются - PullRequest
0 голосов
/ 30 апреля 2018

У меня есть сущность "term_attributes", с двумя вложенными сущностями, которые не нормализуются. Я использую normalizr: "^ 3.2.2".

Исходные данные представляют собой массив продуктов, вот соответствующий бит:

[{
        "id": 9, 
        "price": "184.90",
        "term_attributes": [ 
        {
                "id": 98,
                "attribute": {
                    "id": 1,
                    "name": "Color",
                    "slug": "color"
                },
                "term": {
                    "id": 94,
                    "name": "Bags",
                    "slug": "bags"
                }
            }, 

Нормализующий код:

export const termSchema = new schema.Entity('terms');
export const attributeSchema = new schema.Entity('attributes');

export const termAttributeSchema = new schema.Entity('term_attributes', { idAttribute: 'id'},{
    attribute: attributeSchema,
    term: termSchema
});

export const termAttributeListSchema = new schema.Array(termAttributeSchema);

export const productSchema = new schema.Entity('products', {
    term_attributes: termAttributeListSchema,
});

edit : забыл добавить productListSchema (не важно, хотя):

export const productListSchema = new schema.Array(productSchema);

Term_attributes являются нормализованными, но не его вложенными объектами ( атрибуты и термины ). Вот результат:

{
    "entities": {
"27": {
     "id": 27, 
     "price": "184.90",
     "term_attributes": [105, 545, 547, 2, 771]
},

"term_attributes": {

            "2": {
                "id": 2,
                "attribute": {
                    "id": 1,
                    "name": "Color",
                    "slug": "color"
                },
                "term": {
                    "id": 2,
                    "name": "Fashion",
                    "slug": "fashion"
                }
            },

И если я удалю «idAttribute» из termAttributeSchema, нормализация завершится неудачей:

export const termAttributeSchema = new schema.Entity('term_attributes', {
    attribute: attributeSchema,
    term: termSchema
});

^^ Что здесь не так?

Обновление

Решение Пола Армстронга, представленное ниже, работает, и я просто пропускаю termAttributeListSchema и вместо этого используйте термин AttributeSchema: term_attributes: termAttributeSchema.

1 Ответ

0 голосов
/ 30 апреля 2018

Ваш productSchema неверен. Следует явно указать, что term_attributes является массивом, что можно сделать двумя способами:

Использование Array сокращение:

export const productSchema = new schema.Entity('products', {
    term_attributes: [termAttributeListSchema]
});

Или используя schema.Array:

export const productSchema = new schema.Entity('products', {
    term_attributes: new schema.Array(termAttributeListSchema)
});
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...