mysql - 在 sequelize.js 中,是否可以取消嵌套包含嵌套的查询结果?

标签 mysql node.js orm sequelize.js

我希望能够在查询中嵌套我的包含,但我希望结果不嵌套。

例如:

db.a.findAll({
    where: {id: id},
    include: [
        { model: db.b, include: [
            { model: db.c, include: [
                { model: db.d }
            ]}
        ]}
    ]
})
...

返回这样的东西:

[
    {
        a: {
            ...
            b: {
                ...
                c: {
                    ...
                    d: {
                        ...
                    }
                }
            }
        }
    }
]

但我想要这个:

[
    {
        a: {
            ...
            b: {
                ...
            },
            c: {
            ...
            },
            d: {
                ...
            }
        }
    }
]

这是否可以在不重构模型/表格的情况下实现?

最佳答案

我找到了如何执行此操作,但这不是最理想的方法。您可以像这样手动重新链接对象:

db.a.findAll({
    where: {id: id},
    include: [
        { model: db.b, include: [
            { model: db.c, include: [
                { model: db.d }
            ]}
        ]}
    ]
}).then(function(a) {
    //you may need to iterate over an array
    a.setDataValue('d', a.b.c.d);
    a.b.c.setDataValue('d', null);
    a.setDataValue('c', a.b.c);
    a.b.setDataValue('c', null);
}

关于mysql - 在 sequelize.js 中,是否可以取消嵌套包含嵌套的查询结果?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43786556/

相关文章:

Node.js + puppeteer 在 docker 中失败 : error while loading shared libraries: libX11-xcb

python - Django 使用 .extra() 仅查询模型的一个字段,而不使用 .defer() 或 .only()

Node.js(Express.js + Sequelize.js ORM) : Why does query not work with quoted table name?

MySQL WHERE <多列> IN <子查询>

mysql - 如何将这 3 个 SQL 查询的输出(单个记录)合并到单个输出记录中?

javascript - 我可以在客户端使用 PostgreSQL (pg) (express/node.js)

javascript - Node js 数据库连接在单独的模块中

mysql - 在 MySQL 中引用一组不同的相关 ID 的一个字段

php - 从 php 编辑表格中的字段

orm - DDD 中的领域对象建模哪一种更好?