Хотели бы организовать данные, которые принадлежат определенным пользователям отдельно, пользователь чата должен находиться в массиве отдельно, как это сделать?В таблице пользователей есть name
, email
, password
& active
В таблице чатов есть message
senderId
, recipId
Я пробовал это
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]