如何在sequalize PostgreSQL中基于用户ID获得有组织的结果?

时间:2018-12-10 07:38:00

标签: postgresql group-by include sequelize.js

想分别组织属于特定用户的数据,聊天所属用户应该分别放在一个数组中怎么办? 用户表具有nameemailpasswordactive 聊天表中有message senderIdrecipId

我已经尝试过

  

const message_res = await models.Message.findAll({where: {$or: [{recipId:[2,3,5,1]}, { senderId: [2,3,5,1]}]}});

我得到了这个输出

[
    {
        "id": 1,
        "message": "some text message!!",
        "senderId": 5,
        "recipId": 2,
        "created_at": "2018-12-07T05:52:24.461Z",
        "updated_at": "2018-12-07T05:52:24.461Z",
        "User": {
            "id": 2,
            "name": "deen",
            "email": "deena@gmail.com",
            "password": "$2a$$7trQczrSSYKUF7zWPlbnh58e",
            "active": true,
            "created_at": "2018-12-07T05:44:05.323Z",
            "updated_at": "2018-12-07T05:44:05.323Z"
        }
    },
    {
        "id": 2,
        "message": "message",
        "senderId": 5,
        "recipId": 1,
        "created_at": "2018-12-07T05:52:39.578Z",
        "updated_at": "2018-12-07T05:52:39.578Z",
        "User": {
            "id": 1,
            "name": "james",
            "email": "james@gmail.com",
            "password": "$2a$10$XGU4XiKbzSqjjjYdNaAcG",
            "active": true,
            "created_at": "2018-12-07T05:44:05.323Z",
            "updated_at": "2018-12-07T05:44:05.323Z"
        }
    },
    {
        "id": 3,
        "message": "test1",
        "senderId": 1,
        "recipId": 2,
        "created_at": "2018-12-07T05:53:22.827Z",
        "updated_at": "2018-12-07T05:53:22.827Z",
        "User": {
            "id": 2,
            "name": "deen",
            "email": "deena@gmail.com",
            "password": "$2a$$7trQczrSSYKUF7zWPlbnh58e",
            "active": true,
            "created_at": "2018-12-07T05:44:05.323Z",
            "updated_at": "2018-12-07T05:44:05.323Z"
        }
    },
]



Expected output 

[{

        "id": 1,
        "name": "james",
        "email": "james@gmail.com",
        "password": "$2a$10$XGU4XiKbzSqjjjYdNaAcG",
        "active": true,
        "created_at": "2018-12-07T05:44:05.323Z",
        "updated_at": "2018-12-07T05:44:05.323Z",
        "chat": [{
            "id": 2,
            "message": "message",
            "senderId": 5,
            "recipId": 1,
            "created_at": "2018-12-07T05:52:39.578Z",
            "updated_at": "2018-12-07T05:52:39.578Z",
        },{
           "id": 3,
           "message": "test1",
           "senderId": 1,
            "recipId": 2,
            "created_at": "2018-12-07T05:53:22.827Z",
            "updated_at": "2018-12-07T05:53:22.827Z"
            }
        ]
    },{

        "id": 2,
        "name": "deen",
        "email": "deena@gmail.com",
        "password": "$2a$$7trQczrSSYKUF7zWPlbnh58e",
        "active": true,
        "created_at": "2018-12-07T05:44:05.323Z",
        "updated_at": "2018-12-07T05:44:05.323Z"
        "chat": [{
            "id": 1,
            "message": "some text message!!",
            "senderId": 5,
            "recipId": 2,
            "created_at": "2018-12-07T05:52:24.461Z",
            "updated_at": "2018-12-07T05:52:24.461Z",
        },{
           "id": 3,
           "message": "test1",
           "senderId": 1,
            "recipId": 2,
            "created_at": "2018-12-07T05:53:22.827Z",
            "updated_at": "2018-12-07T05:53:22.827Z"
            }
        ]
    },
    like i need user 5 details along with his chat]

0 个答案:

没有答案