关联关联未正确分组

时间:2019-02-12 20:20:41

标签: javascript sequelize.js feathersjs

使用生成器在FeathersJS应用上工作。尝试使用sequelize建立模型关联时,所获取的数据并不像我期望的那样结构化。

我已经阅读了大量的sequelize文档,但无法找到正确的方法来告诉sequelize将代码构造为数组。

这是我正在使用的相关代码。

cars.model.js

const Sequelize = require('sequelize');
const DataTypes = Sequelize.DataTypes;

module.exports = function (app) {
  const sequelizeClient = app.get('sequelizeClient');
  const cars = sequelizeClient.define('cars', {
    id: {
      type: DataTypes.BIGINT,
      primaryKey: true,
      allowNull: false
    },
    name: {
      type: DataTypes.STRING,
      allowNull: false
    },
    type: {
      type: DataTypes.STRING,
      allowNull: false
    }
  }, {
    hooks: {
      beforeCount(options) {
        options.raw = true;
      }
    }
  });

  cars.associate = function (models) {
    models.cars.hasMany(models.tires, { as: 'tires'});
  };

  return cars;
};

tires.model.js

const Sequelize = require('sequelize');
const DataTypes = Sequelize.DataTypes;

module.exports = function (app) {
  const sequelizeClient = app.get('sequelizeClient');
  const tires = sequelizeClient.define('tires', {
    id: {
      type: DataTypes.BIGINT,
      primaryKey: true,
      allowNull: false
    },
    car_id: {
      type: DataTypes.BIGINT,
      allowNull: false
    },
    type: {
      type: DataTypes.STRING,
      allowNull: false
    },
    position: {
      type: DataTypes.STRING,
      allowNull: false
    }
  }, {
    hooks: {
      beforeCount(options) {
        options.raw = true;
      }
    }
  });

  tires.associate = function (models) {
    models.tires.belongsTo(models.cars);
  };

  return tires;
};

这是我创建的用于定义包含的钩子。它在“之前”挂钩中针对“查找”和“获取”查询进行设置。 include-car-tires.js

module.exports = function (options = {}) {
  return async context => {

    if (context.params.query.include) {
      const TiresModel = context.app.services.tires.Model;
      context.params.sequelize = {
         include: [{
             model: TiresModel,
             as: 'tires'
         }]
      };
      delete context.params.query.include;
    }

    return context;
  };
};

我会得到什么...

{
    "total":2,
    "limit":10,
    "skip":0,
    "data":[
        {
            "id":1,
            "name":"sally",
            "type":"mustang",
            "tires.id":1,
            "tires.car_id":1,
            "tires.type":"slick",
            "tires.position":"front right"
        },
        {
            "id":1,"name":"sally",
            "type":"mustang",
            "tires.id":2,
            "tires.car_id":1,
            "tires.type":"slick",
            "tires.position":"front left"
        },
        {
            "id":1,
            "name":"sally",
            "type":"mustang",
            "tires.id":3,
            "tires.car_id":1,
            "tires.type":"slick",
            "tires.position":"rear right"
        },
        {
            "id":1,
            "name":"sally",
            "type":"mustang",
            "tires.id":4,
            "tires.car_id":1,
            "tires.type":"slick",
            "tires.position":"rear left"
        },
        {
            "id":2,
            "name":"herby",
            "type":"beetle",
            "tires.id":5,
            "tires.car_id":2,
            "tires.type":"winter",
            "tires.position":"front right"
        },
        {
            "id":2,
            "name":"herby",
            "type":"beetle",
            "tires.id":6,
            "tires.car_id":2,
            "tires.type":"winter",
            "tires.position":"front left"
        },
        {
            "id":2,
            "name":"herby",
            "type":"beetle",
            "tires.id":7,
            "tires.car_id":2,
            "tires.type":"winter",
            "tires.position":"rear right"
        },
        {
            "id":2,
            "name":"herby",
            "type":"beetle",
            "tires.id":8,
            "tires.car_id":2,
            "tires.type":"winter",
            "tires.position":"rear left"
        }
    ]
}

我期望什么...

{
    "total":2,
    "limit":10,
    "skip":0,
    "data":[
        {
            "id":1,
            "name":"sally",
            "type":"mustang",
            "tires:[
                {
                    "id":1,
                    "car_id":1,
                    "type":"slick",
                    "position":"front right"
                },
                {
                    "tires.id":2,
                    "tires.car_id":1,
                    "tires.type":"slick",
                    "tires.position":"front left"
                },
                {
                    "tires.id":3,
                    "tires.car_id":1,
                    "tires.type":"slick",
                    "tires.position":"rear right"
                },
                {
                    "tires.id":4,
                    "tires.car_id":1,
                    "tires.type":"slick",
                    "tires.position":"rear left"
                }
            ]
        },
        {
            "id":2,
            "name":"herby",
            "type":"beetle",
            "tires:[
                {
                    "id":5,
                    "car_id":2,
                    "type":"winter",
                    "position":"front right"
                },
                {
                    "tires.id":6,
                    "tires.car_id":2,
                    "tires.type":"winter",
                    "tires.position":"front left"
                },
                {
                    "tires.id":7,
                    "tires.car_id":2,
                    "tires.type":"winter",
                    "tires.position":"rear right"
                },
                {
                    "tires.id":8,
                    "tires.car_id":2,
                    "tires.type":"winter",
                    "tires.position":"rear left"
                }
            ]
        }
    ]
}

0 个答案:

没有答案