Has the user liked the post before or not using Sequelize

前端 未结 2 795
南旧
南旧 2021-01-28 07:10

Currently when a user likes a post, that like record gets added to my Likes table with the userId and the postId.

Now, when a user is looking at a post, I want to determi

相关标签:
2条回答
  • 2021-01-28 08:04

    You don't need to request Like once again, you got all post's likes at your hand:

    for (const post of plainPosts) {
     // check if we have any like among posts' likes that is made by a certain user
     const isLiked = post.Likes.some(x => x.userId === req.user.id);
     const { Post_Images, ...postAttributes } = post;
     ...
    
    0 讨论(0)
  • 2021-01-28 08:13

    You don't need to specify all the fields unless you want to override something, otherwise Sequelize can generate most of the columns for you.

    const User = sequelize.define(
      'user',
      {
        name: {
          type: Sequelize.STRING,
        },
      },
      { /* options */ }
    );
    
    const Post = sequelize.define(
      'post',
      {
        title: {
          type: Sequelize.STRING,
        },
      },
      { /* options */ }
    );
    
    // the join table so you can reference it, but doesn't need any columns including primary key (unless you want to a "super join")
    const Likes = sequelize.define(
      'likes',
      {}, // no columns here
      { /* options */ }
    );
    

    Creating the associations between the models will create most of the foreignKey fields automatically. Use the through keyword on the Likes relationships to make it many-to-many.

    
    // Users can have many Posts
    User.hasMany(Post);
    
    // Posts belong to one User
    Post.belongsTo(User);
    
    // Users can like more than one Post through the `likes` join table
    User.hasMany(Post, { as: 'likes', through: 'likes' });
    
    // Posts can be liked by more than one User through the `likes` join table
    Post.hasMany(User, { as: 'likes', through: 'likes' });
    

    You don't need to store the number of likes because you can summarize it through the join table.

    // Get the 'likes' count for a Post, instead of saving it on the post
    const posts = await Post.findAll({
      attributes: {
        include: [
          [sequelize.fn('COUNT', sequelize.col('likes.userId')), 'likesCount'],
        ],
      },
      include: [
        {
          model: User,
          as: 'likes',
          though: 'likes',
          attributes: [],
          required: false,
        },
      ],
    });
    
    // `posts` will be an array of Post instances that have a likesCount property
    posts.forEach((post) => {
      console.log(`The post ${post.title} has ${post.likesCount} likes.`);
    });
    

    For an individual (or more than one) Post you can get the list of users who liked it via the post (or use the Like model and it's relationships).

    
    // Get the user 'likes' for a Post
    const post = await Post.findByPk(postId, {
      include: [
        {
          model: User,
          as: 'likes',
          though: 'likes',
          required: false,
        },
      ],
    });
    
    post.likes.forEach((like) => {
      console.log(`The user ${like.name} has liked the post ${post.title}.`);
    });
    
    
    0 讨论(0)
提交回复
热议问题