如何获取关联中的数据数量? [续集],[PostgreSQL]

问题描述 投票:0回答:1

我需要从 fetchAll 查询中获取未读消息的数量。我只需要号码。 ----> 这是我所期望的以及其他数据 ->:"Unread_messages": 55,我不希望其他属性(消息)出现在那里。这是一个模拟代码,我已在其中进行了评论我需要号码:

static async fetchData(page: number, DataId: number) {

    const limit = 20;

    const offset = (page - 1) * limit;



    return await Data.findAndCountAll({

      include: [

        { model: SomeData1, as: 'someData1', required: false },

        {

          model: Data2,

          as: 'data2',

          attributes: ['id', 'name'],

          required: true,

          where: { is_deleted: false },

          include: [

            {

              model: Message,     //----> I need the count of this

              as: 'unreadMessageCount',

              where: {

               someDataid: DataId,

                status: 'UNREAD'

              },

              required: false

            }

          ]

        }

      ],

      where: { DataId: DataId },

      offset,

      limit,

      distinct: true

    });

  }

}
node.js postgresql sequelize-typescript
1个回答
0
投票

尝试通过属性添加它

{
                  model: Message,     //----> I need the count of this
                  as: 'unreadMessageCount',
                  where: {
                    someDataid: DataId,
                    status: 'UNREAD'
                  },
                  attributes: [
                    [Sequelize.fn("COUNT", Sequelize.col("id")), "unreadMessageCount"],
                  ],
                  required: false
                }
© www.soinside.com 2019 - 2024. All rights reserved.