'sequelize. Find all posts and add column "isLiked" from table "likes" with boolean value if current user marked this post with 'like'

i have two models:

const Users = sequelize.define('users', {
    id: { type: DataTypes.INTEGER, primaryKey: true, unique: true, allowNull: false },
    name: { type: DataTypes.STRING, allowNull: false },
    role: {
        type: DataTypes.STRING,
        values: [
            'student',
            'headman',
            'admin'
        ],
        defaultValue: 'student',
        allowNull: false
    },
    avatarImage: {
        type: DataTypes.TEXT,
        allowNull: false
    }
})

and

const Posts = sequelize.define('posts', {
    id: { type: DataTypes.INTEGER, primaryKey: true, autoIncrement: true },
    title: { type: DataTypes.STRING, allowNull: false },
    content: { type: DataTypes.TEXT, allowNull: false },
    deadline: { type: DataTypes.DATE, allowNull: false },
    postType: {
        type: DataTypes.STRING,
        values: [
            'homework',
            'news',
        ],
        defaultValue: 'news', allowNull: false
    }
})

boudned by many to many relation through model Likes

const Likes = sequelize.define('likes', {
    id: { type: DataTypes.INTEGER, primaryKey: true, autoIncrement: true },
    postType: {
        type: DataTypes.STRING,
        values: [
            'homework',
            'news',
        ],
        allowNull: false
    }

})

//relations 
Users.belongsToMany(Posts, {
    through: { model: Likes, unique: false },
    as: "postsLiked",
    foreignKey: "userId",
});
Posts.belongsToMany(Users, {
    through: { model: Likes, unique: false },
    as: "usersLiked",
    foreignKey: "postId",
});

So, i want to get all posts with additional column "isLiked" on each one, with boolean value (true, if user, which makes request, marked this post with 'like'. In other way - false). I don't need all list of users likes this post, just check for one.

So, i have this query in my controller

async getAll(req, res) {
  let homeworks = await Posts.findAll({
          where: {
              postType: 'homework'
          },
          include: [
              {
                  model: Subjects,
                  as: 'subject',
                  attributes: ['id', 'title', 'fullName']
              },
              {
                  model: Users,
                  as: 'usersLiked',
                  //here i get list of all users likes this post
                  //if i add propery where (where:{id: user.id}), i get only posts liked by my user
s                    }
          ],

      })
}
        

So, can you please help me with this?

//here is the response i have now
//current user.id is 1

{
    "id": 2,
    "title": "Post 2",
    ...post fields ...
    "subject": {...},
    "usersLiked": [
        {
            "id": 1,
            "name": "Max Sobolev",
        },
        {
            "id": 2,
            "name": "Albert Cooper",
        }
    ]
},

//what i want to see

{
    "id": 2,
    "title": "Post 2",
    ...post fields ...
    "subject": {...},
    "isLiked": true
},


Solution 1:[1]

UPDATE 13.04: So, i found how i can get all posts with "where" property: i need just use require: false propery

homeworks = await Posts.findAll({
  where: {
      postType: 'homework',
      deadline: {
          [Op.gte]: moment()
      }
  },
  include: [
      {
          model: Subjects,
          as: 'subject',
          attributes: ['id', 'title', 'fullName']
      },
      {
          model: Users,
          as: 'usersLiked',
          where: {
              id: req.user.id,
          },
          required: false, // <--- and now i get all posts with empty array 'userLiked' if current user didn't liked this post
      }
  ],


})

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Maxim