Home > Net >  Select Only Selected attributes from associated models with sequelize
Select Only Selected attributes from associated models with sequelize

Time:04-21

I have two models User and Follow that is associated as following and followers through Follow model

User Model

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;
  };

Follow Model

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;
  };

Now on User find Query

  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'],
      });

after this Query results are following :

 {
   "_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"
         }
      }
   ]
}

result is fine but i don't want follow full model in each result , i tried with raw:true but still have same results .

result i am expecting

 {
   "_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",
      }
   ]
}

CodePudding user response:

Just indicate an empty array for attributes of through option:

 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'],
      });
  • Related