将嵌套规范化为多对多

时间:2018-05-21 15:15:37

标签: javascript normalizr

我希望找到一种方法来规范嵌套的多对多关系。

但是,当模型深度较低时,它就一直在运行,例如下面示例中的类别。在输出中,位置ID 27仅包含类别ID 17(我假设这是因为这是位置27的最后一个版本显示的内容)。规范化此类数据的最佳方法是什么?

在该位置使用商店ID是个好主意吗?即。

"locations": {
    "1-27": {
        "id": 27,
        "label": "Birmingham",
        "categories": [
            1,
            2,
            7
        ]
    },
    "2-27": {
        "id": 27,
        "label": "Birmingham",
        "categories": [
            1,
            7
        ]
    }
},

感谢您花时间阅读本文!

其他数据详细信息

数据库

Shops
    id
    label
Locations
    id
    label
Location_Shop
    location_id
    shop_id
Categories
    id
    label
Category_Location
    category_id
    location_id

示例数据

    [
    {
        id: 1,
        label: 'First Shop',
        locations: [
            {
                id: 27,
                label: 'Birmingham',
                categories: [
                    {
                        id: 1,
                        label: 'Car Park',
                    },
                    {
                        id: 2,
                        label: 'Petrol Station',
                    },
                    {
                        id: 7,
                        label: 'Bakery',
                    },
                ],
            },
        ],
    },
    {
        id: 2,
        label: 'Second Shop',
        locations: [
            {
                id: 27,
                label: 'Birmingham',
                categories: [
                    {
                        id: 1,
                        label: 'Car Park',
                    },
                    {
                        id: 7,
                        label: 'Bakery',
                    },
                ],
            },
        ],
    },
]

代码

const categorySchema = new schema.Entity('categories');
const locationSchema = new schema.Entity('locations', {
    categories: [categorySchema],
});
const shopSchema = new schema.Entity('shops', {
    locations: [locationSchema],
});

输出

{
    "entities": {
        "categories": {
            "1": {
                "id": 1,
                "label": "Car Park"
            },
            "2": {
                "id": 2,
                "label": "Petrol Station"
            },
            "7": {
                "id": 7,
                "label": "Bakery"
            }
        },
        "locations": {
            "27": {
                "id": 27,
                "label": "Birmingham",
                "categories": [
                    1,
                    7
                ]
            }
        },
        "shops": {
            "1": {
                "id": 1,
                "label": "First Shop",
                "locations": [
                    27
                ]
            },
            "2": {
                "id": 2,
                "label": "Second Shop",
                "locations": [
                    27
                ]
            }
        }
    },
    "result": [
        1,
        2
    ]
}

0 个答案:

没有答案