Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Sequelize (pg)
- // Модель Group
- module.exports = (sequelize, DataTypes) => {
- const Group = sequelize.define('Group', {
- name: {
- type: DataTypes.STRING,
- unique: true,
- },
- }, {});
- Group.associate = function (models) {
- Group.belongsToMany(models.Lesson, { through: 'GroupLesson' });
- };
- return Group;
- };
- // Модель Lesson
- module.exports = (sequelize, DataTypes) => {
- const Lesson = sequelize.define('Lesson', {
- day: DataTypes.NUMERIC(2),
- }, {});
- Lesson.associate = function(models) {
- Lesson.belongsTo(models.Audience); // простые модели, со строками name
- Lesson.belongsTo(models.Teacher);
- Lesson.belongsTo(models.Discipline);
- Lesson.belongsTo(models.LessonTime);
- Lesson.belongsToMany(models.Group, { through: 'GroupLesson' });
- };
- return Lesson;
- };
- /* Пытаюсь получить JSON со всеми Lesson для данной Group. */
- Group.find({
- where: {
- id: req.params.id, // express stuff
- },
- attributes: ['name'],
- include: [{
- association: 'Lessons',
- attributes: ['day'],
- include: ['Teacher', 'Audience', 'Discipline', 'Groups']
- }],
- })
- .then(r => {
- res.json(r); // express stuff
- });
- });
- /* Но в данных есть лишние данные от ассоциации many-to-many: */
- {
- "name": "SAMPLE",
- "Lessons": [{
- "day": "1",
- "Teacher": {
- "id": 1,
- "name": "SAMPLE"
- },
- "Audience": {
- "id": 1,
- "name": "SAMPLE"
- },
- "Discipline": {
- "id": 1,
- "name": "SAMPLE"
- },
- "Groups": [{
- "id": 1,
- "name": "SAMPLE",
- "GroupLesson": { // <- лишний объект
- "GroupId": 1,
- "LessonId": 1
- }
- }],
- "GroupLesson": { // <- лишний объект
- "GroupId": 1,
- "LessonId": 1
- }
- }]
- }
- /* Как избавиться от лишних объектов? */
Add Comment
Please, Sign In to add comment