Sequelizejs:如何热切地加载一个“结构化”的#39; assosiation?

时间:2017-12-12 21:19:51

标签: mysql express sequelize.js eager-loading populate

我有这样的数据库方案: Scheme image

我唯一的关系是:

db.routines.hasMany(db.elements, {constraints: false});

我试图找到一个例程,所有元素都已填充/加载,但我不知道如何实现这一点,我尝试这样做:

db.routines.findAll({
    include: [{ all: true }]
}).then(routines => {
    res.send(routines)
})

db.routines.findAll({
    include: [{
        model: db.elements
    }]
}).then(routines => {
    res.send(routines)
})

两者都会产生以下结果:

[
    {
        "id": "f45273da-eb13-45a2-852c-81b8eee14eb8",
        "element_1": "8d609633-1554-41cd-a47e-244508ebe77c",
        "element_2": "8d609633-1554-41cd-a47e-244508ebe77c",
        "element_3": "8d609633-1554-41cd-a47e-244508ebe77c",
        "element_4": "8d609633-1554-41cd-a47e-244508ebe77c",
        "element_5": "8d609633-1554-41cd-a47e-244508ebe77c",
        "diff1": null,
        "diff2": null,
        "created_at": "2017-12-12T21:04:15.000Z",
        "updated_at": "2017-12-12T21:04:15.000Z",
        "deleted_at": null,
        "elements": []
    }
]

但我正在寻找的是这样的:

[
    {
        "id": "f45273da-eb13-45a2-852c-81b8eee14eb8",
        "element_1": {
            "id": "8d609633-1554-41cd-a47e-244508ebe77c",
            "name_en": "Backflip",
            "name_nl": "Salto",
            .....
        },
        "element_2": {
            "id": "8d609633-1554-41cd-a47e-244508ebe77c",
            "name_en": "Backflip",
            "name_nl": "Salto",
            .....
        },
        "element_3": {
            "id": "8d609633-1554-41cd-a47e-244508ebe77c",
            "name_en": "Backflip",
            "name_nl": "Salto",
            .....
        },
        .....
    }
]

我如何填充这个?我找到的唯一例子是检索元素对象,这不是我想要的,因为它们必须有一个特定的索引。

0 个答案:

没有答案