续集:更改嵌套的数据结构

时间:2019-06-19 11:32:24

标签: javascript object orm nested sequelize.js

我想更改orm查询返回的数据结构。总共有四个表。 productcategory是多对多关系,有一个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' } ]

所以我很困惑。如何处理序列查询中的数据?非常感谢您的帮助。

请让我知道解决问题的方法是否错误,或者您需要模型架构。

1 个答案:

答案 0 :(得分:0)

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

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: {}, (...),则续集查询将像console.log ()一样打印。如果这样做,将无法处理您的数据。因此,对包含查询的变量之后的数据进行处理是关键点

data.get ({plain: true})