Select 只有 Select 使用 sequelize 关联模型的属性

Select Only Selected attributes from associated models with sequelize

我有两个模型 User 和 Follow 通过关注模型关联为关注和关注者

用户模型

const user = (sequelize, DataTypes) => {
    const User = sequelize.define("user", {
        _id:  {
            type: Sequelize.UUID,
            defaultValue: Sequelize.UUIDV4,
            allowNull: false,
            primaryKey: true
          },
          walletAddress: {
            type: DataTypes.STRING,
            unique: true,
          },
          displayName : DataTypes.STRING,
          nftsCount: {
            type: DataTypes.INTEGER,
            defaultValue: 0,
          },
          followersCount: {
            type: DataTypes.INTEGER,
            defaultValue: 0,
          },
          followingCount: {
            type: DataTypes.INTEGER,
            defaultValue: 0,
          },
   
          verified : { type:  DataTypes.BOOLEAN , defaultValue: true },
          status: {
            type: DataTypes.ENUM,
            values: ['active', 'ban','inactive'],
            defaulValue: 'active'
          },
    
    });
  

      User.belongsToMany(models.User, {
        through: models.Follow,
        as: "followers",
        foreignKey: {
          name: "userId", //followerId
        },
      });
      User.belongsToMany(models.User, {
        through: models.Follow,
        as: "followings",
        foreignKey: {
          name: "followerId", //followingId
        },
      });
    };
  
    return User;
  };

关注模特

const follow = (sequelize, DataTypes) => {
    const Follow = sequelize.define("follow", {
      _id:  {
        type: Sequelize.UUID,
        defaultValue: Sequelize.UUIDV4,
        allowNull: false,
        primaryKey: true
      },
      userId: {
        type: Sequelize.UUIDV4,
      },
  
      followerId: {
        type:  Sequelize.UUIDV4,
      },
    });
    Follow.associate = (models) => {
      Follow.belongsTo(models.User, {
        foreignKey: 'followerId',
        as: 'followings'
      });
    };
    return Follow;
  };

现在用户查找查询

  const user = await User.findOne({
        where: { walletAddress: walletAddress },
        include: [ {
          model: User,
          as: 'Followings',
          attributes: ['_id',"walletAddress"],
        }, {
          model: User,
          as: 'Followers',
          attributes: ['_id',"walletAddress"],
        }],
        attributes: ['_id', 'walletAddress'],
      });

此查询结果如下:

 {
   "_id":"c772e44f-5a09-46da-a0a9-b98cf12e0ce7",
   "walletAddress":"0x36C76b7D95AAbeD138e55477A3d482148F2491f7",
   "displayName":"my user",
   "followersCount":2,
   "followingCount":1,
   "followings":[
      {
         "_id":"e20732d3-7f59-4c3d-b048-eda67bf7160d",
         "walletAddress":"0x6Ee818f9c2896D13630369998FF36cCF",
         "displayName":"user1",
         "follow":{
            "_id":"dd52e8ef-eb6d-4b14-a8f5-cc6438171bad",
            "userId":"e20732d3-7f59-4c3d-b048-eda67bf7160d",
            "followerId":"c772e44f-5a09-46da-a0a9-b98cf12e0ce7",
            "createdAt":"2022-04-20T12:18:54.000Z",
            "updatedAt":"2022-04-20T12:18:54.000Z"
         }
      }
   ],
   "followers":[
      {
         "_id":"64e23861-225b-4424-ac17-8ac08ba0dea2",
         "walletAddress":"0xBD6d523538d1fb6f48766888060F98",
         "displayName":"user2",
         "follow":{
            "_id":"ca2daea4-3272-4f96-b120-435beb97128a",
            "userId":"c772e44f-5a09-46da-a0a9-b98cf12e0ce7",
            "followerId":"64e23861-225b-4424-ac17-8ac08ba0dea2",
            "createdAt":"2022-04-20T12:18:54.000Z",
            "updatedAt":"2022-04-20T12:18:54.000Z"
         }
      },
      {
         "_id":"852f2ba1-dff4-4d65-9328-14acf7025411",
         "walletAddress":"0xA6Ee85031b16B94a5959Cf0c0fe60c9",
         "displayName":"user3",
         "follow":{
            "_id":"f039390b-0cac-4127-b791-bc5490025f08",
            "userId":"c772e44f-5a09-46da-a0a9-b98cf12e0ce7",
            "followerId":"852f2ba1-dff4-4d65-9328-14acf7025411",
            "createdAt":"2022-04-20T12:18:54.000Z",
            "updatedAt":"2022-04-20T12:18:54.000Z"
         }
      }
   ]
}

结果很好,但我不想在每个结果中都遵循完整模型,我尝试使用 raw:true 但仍然有相同的结果。

我期待的结果

 {
   "_id":"c772e44f-5a09-46da-a0a9-b98cf12e0ce7",
   "walletAddress":"0x36C76b7D95AAbeD138e55477A3d482148F2491f7",
   "displayName":"my user",
   "followersCount":2,
   "followingCount":1,
   "followings":[
      {
         "_id":"e20732d3-7f59-4c3d-b048-eda67bf7160d",
         "walletAddress":"0x6Ee818f9c2896D13630369998FF36cCF",
         "displayName":"user1",
      }
   ],
   "followers":[
      {
         "_id":"64e23861-225b-4424-ac17-8ac08ba0dea2",
         "walletAddress":"0xBD6d523538d1fb6f48766888060F98",
         "displayName":"user2",
      },
      {
         "_id":"852f2ba1-dff4-4d65-9328-14acf7025411",
         "walletAddress":"0xA6Ee85031b16B94a5959Cf0c0fe60c9",
         "displayName":"user3",
      }
   ]
}

只需为 through 选项的 attributes 指定一个空数组:

 const user = await User.findOne({
        where: { walletAddress: walletAddress },
        include: [ {
          model: User,
          as: 'Followings',
          attributes: ['_id',"walletAddress"],
          through: {
           attributes: []
          }
        }, {
          model: User,
          as: 'Followers',
          attributes: ['_id',"walletAddress"],
          through: {
           attributes: []
          }
        }],
        attributes: ['_id', 'walletAddress'],
      });