gpt4 book ai didi

javascript - Sequelize : Changing the nested data structure

转载 作者:行者123 更新时间:2023-12-03 22:17:10 26 4
gpt4 key购买 nike

我想更改 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_iddepartment_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 查询的数据?我将衷心感谢您的帮助。

如果解决问题的方法有误,或者您需要模型模式,请告诉我。

最佳答案

我以一种微不足道的方式实现了它。它看起来像一个完整的胃。如果另一个人将最佳方式放入答案中,那就太好了。

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"
}

如果您运行 dataValues: {}, (...) ,sequelize 查询将像 console.log () 一样打印。如果您这样做,您将无法处理您的数据。所以 关键点 处理包含查询的变量后的数据如下:

data.get ({plain: true})

关于javascript - Sequelize : Changing the nested data structure,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56666728/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com