【发布时间】:2019-06-19 11:32:24
【问题描述】:
我想更改 orm 查询返回的数据结构。总共有四张桌子。 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' } ]
所以我很困惑。如何处理来自 sequelize 查询的数据?非常感谢您的帮助。
如果解决问题的方法有误或者您需要模型架构,请告诉我。
【问题讨论】:
标签: javascript object orm nested sequelize.js