我想更改orm查询返回的数据结构。总共有四张 table 。 product
,category
是多对多关系,有一个product_category
表作为桥梁,一共四张表,包括department
表。关联关系如下:
// product
product.belongsToMany(models.category, {
through: 'product_category',
foreignKey: 'product_id'
});
// product_category
product_category.belongsTo(models.product, {
foreignKey: 'product_id'
});
product_category.belongsTo(models.category, {
foreignKey: 'category_id'
});
// category
category.belongsToMany(models.product, {
through: 'product_category',
foreignKey: 'category_id'
});
category.belongsTo(models.department, {
foreignKey: 'department_id'
});
// department
department.hasMany(models.category, {
foreignKey: 'department_id'
});
通过上面的表结构,得到如下查询,获取department_id
对应的产品:
const query = await product.findOne({
where: { product_id: id },
include: {
model: category,
attributes: ['category_id', ['name', 'category_name']],
include: {
model: department,
attributes: ['department_id', ['name', 'department_name']]
}
},
attributes: []
});
const data = query.categories;
生成的json数据如下:
"data": [
{
"category_id": 1,
"category_name": "French",
"department": {
"department_id": 1,
"department_name": "Regional"
},
"product_category": {
"product_id": 1,
"category_id": 1
}
}
]
我想把上面的数据做成如下:
"data": [
{
"category_id": 1,
"category_name": "French",
"department_id": 1,
"department_name": "Regional"
}
]
为了像上面那样处理数据,有两种方法:修改基于sql的orm查询和在javascript中处理product
值。
不过由于我是用orm学sql的,所以不知道第一种方法,所以决定用第二种方法。
第一次尝试
我做了两次尝试。请注意,该框架使用 koa.js。第一条如下:
const query = await product.findOne({
where: { product_id: id },
include: {
model: category,
attributes: ['category_id', ['name', 'category_name']],
include: {
model: department,
attributes: ['department_id', ['name', 'department_name']]
}
},
attributes: []
});
const data = query.categories.map(
({ category_id, category_name, department }) => ({
category_id,
category_name,
department_id: department.department_id,
department_name: department.department_name
})
);
ctx.body = data;
正文如下:
"data": [
{
"category_id": 1,
"department_id": 1
}
]
..??有些奇怪,所以我稍微改变了返回值:
({ category_id, category_name, department }) => ({
// category_id,
// category_name,
department_id: department.department_id,
department_name: department.department_name
})
输出的json值是:
"data": [
{
"department_id": 1
}
]
相反,您注释了department_id
、department_name
:
({ category_id, category_name, department }) => ({
category_id,
category_name,
// department_id: department.department_id,
// department_name: department.department_name
})
生成的 json 值为:
"data": [
{
"category_id": 1
}
]
我无法以任何其他方式做到这一点。
第二次尝试
await product
.findOne({
where: { product_id: id },
include: {
model: category,
attributes: ['category_id', ['name', 'category_name']],
include: {
model: department,
attributes: ['department_id', ['name', 'department_name']]
}
},
attributes: []
})
.then(query => {
const data = query.categories.map(
({ category_id, category_name, department }) => ({
category_id,
category_name,
department_id: department.department_id,
department_name: department.department_name
})
);
ctx.body = data;
});
两种方法的结果相同,所以我不知道该怎么办。
所以我将变量映射到带有 json 数据的嵌套数组。我得到了我想要的结果:
const data = {
categories: [
{
category_id: 1,
category_name: 'French',
department: { department_id: 1, department_name: 'Regional' },
product_category: { product_id: 1, category_id: 1 }
}
]
};
const product = data.categories.map(
({ category_id, category_name, department }) => ({
category_id,
category_name,
department_id: department.department_id,
department_name: department.department_name
})
);
console.log(product);
// [ { category_id: 1,
// category_name: 'French',
// department_id: 1,
// department_name: 'Regional' } ]
所以我很困惑。如何处理来自后续查询的数据?我非常感谢您的帮助。
如果解决问题的方法是错误的或者您需要模型架构,请告诉我。
最佳答案
我以一种简单的方式实现了它。看起来就像一个完整的胃。如果另一个人能将最佳方法纳入答案,那就太好了。
const getProduct = () => {
const a = query.categories[0];
const b = a.get({ plain: true });
const { category_id, category_name } = b;
const { department_id, department_name } = b.department;
return {
category_id,
category_name,
department_id,
department_name
};
};
ctx.body = getProduct();
Json数据输出:
"product": {
"category_id": 1,
"category_name": "French",
"department_id": 1,
"department_name": "Regional"
}
如果运行 console.log ()
, Sequelize 查询将像 dataValues: {}, (...)
一样打印。如果您这样做,您将无法处理您的数据。因此,处理包含查询的变量之后的数据是关键点,如下所示:
data.get ({plain: true})
关于javascript - Sequelize : Changing the nested data structure,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56666728/