使用 include in (Sequelize) 显示没有嵌套对象的属性
display attributes without nesting objects using include in (Sequelize)
我有一个型号 SKUValue,它与另外两个型号 VariantOptions 和 ProductVariant 相关联。
我创建了一个 "left-joins" SKUValue 值 VariantOptions 和 [=24= 的 sequelize 查询]ProductVariant,带来带来结果。
后续查询
await SKUValue.findAll({
where: {
productId: '7bde8a1d-5f6c-4349-bfda-428399c88291'
},
include: [
{
model: VariantOption,
attributes: ['name']
},
{
model: ProductVariant,
attributes: ['name']
}
],
attributes: ['SKUId']
});
查询结果return是一个数组,其中属性VariantOption和ProductVariant是一个嵌套对象,只有一个场.
[
{
"SKUId": "72edd3ca-fa12-4234-ba8c-dd008eb416d5",
"VariantOption": {
"name": "Large"
},
"ProductVariant": {
"name": "Size"
}
},
{
"SKUId": "72edd3ca-fa12-4234-ba8c-dd008eb416d5",
"VariantOption": {
"name": "Red"
},
"ProductVariant": {
"name": "color"
}
}
]
如何以 VariantOption 和 ProductVariant 的结果不 return 嵌套的方式查询属性对象,但只是一个 stirng。
总的来说我希望结果是这样的
[
{
"SKUId": "72edd3ca-fa12-4234-ba8c-dd008eb416d5",
"VariantOption.name": "Large",
"ProductVariant.name": "Size"
},
{
"SKUId": "72edd3ca-fa12-4234-ba8c-dd008eb416d5",
"VariantOption.name": "Red",
"ProductVariant.name": "Color"
}
]
我找到了问题的答案。
使用Sequelize.literal,我们可以很方便的在外层属性中添加内层属性
return await SKUValue.findAll({
where: {
productId: '7bde8a1d-5f6c-4349-bfda-428399c88291'
},
include: [
{
model: VariantOption,
attributes: []
},
{
model: ProductVariant,
attributes: []
}
],
attributes: [
'SKUId',
[Sequelize.literal('"ProductVariant"."name"'), 'productVariant'],
[Sequelize.literal('"VariantOption"."name"'), 'variantOption']
]
});
我有一个型号 SKUValue,它与另外两个型号 VariantOptions 和 ProductVariant 相关联。 我创建了一个 "left-joins" SKUValue 值 VariantOptions 和 [=24= 的 sequelize 查询]ProductVariant,带来带来结果。
后续查询
await SKUValue.findAll({
where: {
productId: '7bde8a1d-5f6c-4349-bfda-428399c88291'
},
include: [
{
model: VariantOption,
attributes: ['name']
},
{
model: ProductVariant,
attributes: ['name']
}
],
attributes: ['SKUId']
});
查询结果return是一个数组,其中属性VariantOption和ProductVariant是一个嵌套对象,只有一个场.
[
{
"SKUId": "72edd3ca-fa12-4234-ba8c-dd008eb416d5",
"VariantOption": {
"name": "Large"
},
"ProductVariant": {
"name": "Size"
}
},
{
"SKUId": "72edd3ca-fa12-4234-ba8c-dd008eb416d5",
"VariantOption": {
"name": "Red"
},
"ProductVariant": {
"name": "color"
}
}
]
如何以 VariantOption 和 ProductVariant 的结果不 return 嵌套的方式查询属性对象,但只是一个 stirng。
总的来说我希望结果是这样的
[
{
"SKUId": "72edd3ca-fa12-4234-ba8c-dd008eb416d5",
"VariantOption.name": "Large",
"ProductVariant.name": "Size"
},
{
"SKUId": "72edd3ca-fa12-4234-ba8c-dd008eb416d5",
"VariantOption.name": "Red",
"ProductVariant.name": "Color"
}
]
我找到了问题的答案。
使用Sequelize.literal,我们可以很方便的在外层属性中添加内层属性
return await SKUValue.findAll({
where: {
productId: '7bde8a1d-5f6c-4349-bfda-428399c88291'
},
include: [
{
model: VariantOption,
attributes: []
},
{
model: ProductVariant,
attributes: []
}
],
attributes: [
'SKUId',
[Sequelize.literal('"ProductVariant"."name"'), 'productVariant'],
[Sequelize.literal('"VariantOption"."name"'), 'variantOption']
]
});