Работа над приложением 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;
};
tyres.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;
};
и это хук, который я создал для определения включений.Он устанавливается в хуке «before» для запросов «find» и «get».include-car-tyres.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"
}
]
}
]
}