Search code examples
javascriptrethinkdb

I have an array of objects and each object has a "user_id" key and I need to get more information from this user in Rethinkdb


Hello and thanks to everyone. I'm getting data with reql in rethinkdb. I have a json object, inside this object there is an array of objects called "users". In each of these objects I have an "id" key and I need to obtain more user fields from this "id" and add these fields in the same user object.

This is the code to obtain the information:

const id = req.id

r.table("chats").filter(function (chat) { // Get chats
    return chat("users").contains(function (user) {
        return user("id").eq(id)
    })
}).map(function (chat) { // Get messages from every chat
    return chat.merge({
        "messages":
            r.table("messages").eqJoin("user_id", r.table('users'))
                .zip()
                .pluck("id", "creation_date", "message", "email", "username", "status") 
                .orderBy(r.asc('creation_date'))
                .coerceTo('array')
    })
}).run(connection).then(function (cursor) {
    return cursor.toArray()
}).then(function (chats) {
    res.json({ success: 1, chats })
}).catch(function (err) {
    sendError(res, "Error -> " + err)
})

And this is the result:

{
"success": 1,
"chats": [
    {
        "id": "8c4a58ef-4fb2-4084-af28-d9d04da0651c",
        "image": null,
        "is_group": false,
        "messages": [
            {
                "creation_date": "Fri Dec 21 2018 09:21:17 GMT+0100 (CET)",
                "email": "[email protected]",
                "id": "6c240de3-94e7-4b1c-9ef3-218dc71d8b0a",
                "message": "Felices fiestas por cierto, saludos a la familia",
                "status": "SAVED",
                "username": "John García"
            },
            {
                "creation_date": "Fri Dec 21 2018 09:21:27 GMT+0100 (CET)",
                "email": "[email protected]",
                "id": "72b4350d-a025-4dc1-9038-8e13c3ec12dc",
                "message": "Bien, estoy en la oficina 😃",
                "status": "SENT",
                "username": "Chester López"
            }
        ],
        "name": null,
        "users": [
            {
                "id": "6c240de3-94e7-4b1c-9ef3-218dc71d8b0a"
            },
            {
                "id": "72b4350d-a025-4dc1-9038-8e13c3ec12dc"
            }
        ]
    },
    {
        "id": "3a3501f3-7a17-4261-a0aa-3cae94f58f18",
        "image": null,
        "is_group": false,
        "messages": [
            {
                "creation_date": "Fri Dec 21 2018 09:21:27 GMT+0100 (CET)",
                "email": "[email protected]",
                "id": "6c240de3-94e7-4b1c-9ef3-218dc71d8b0a",
                "message": "Bien, estoy en la oficina 😃",
                "status": "SENT",
                "username": "John García"
            },
            {
                "creation_date": "Fri Dec 21 2018 09:22:53 GMT+0100 (CET)",
                "email": "[email protected]",
                "id": "6c240de3-94e7-4b1c-9ef3-218dc71d8b0a",
                "message": "Espero que estéis todos genial",
                "status": "SENDING",
                "username": "John García"
            },
            {
                "creation_date": "Thu Dec 20 2018 16:41:02 GMT+0100 (CET)",
                "email": "[email protected]",
                "id": "72b4350d-a025-4dc1-9038-8e13c3ec12dc",
                "message": "¿How are you?",
                "status": "SEEN",
                "username": "Chester López"
            }
        ],
        "name": null,
        "users": [
            {
                "id": "6c240de3-94e7-4b1c-9ef3-218dc71d8b0a"
            },
            {
                "id": "72b4350d-a025-4dc1-9038-8e13c3ec12dc"
            }
        ]
    }
  ]
}

I need each user object to become:

{
    "id": "6c240de3-94e7-4b1c-9ef3-218dc71d8b0a"
    "email": "[email protected]",
    "image": null,
    "username": "John García"
}

I've tried doing this and many more tests but it does not work:

.map(function(chat){ // Get info from every user in every chat
    return chat("users").map(function(user) {
                return r.table("users").filter({id: user("id")})
                        .pluck("id", "email", "username", "image")
    })
})

Does anyone know how to do it? Thank you.


Solution

  • The solution is using concapmap. I hope this helps someone.

    const id = req.id
    
    r.table("chats").filter(chat => {
        return chat("users").contains(function (user) {
            return user("id").eq(id)
        })
    }).map(chat => {
        return chat.merge({
            "messages":
                r.table("messages").eqJoin("user_id", r.table('users'))
                    .zip()
                    .filter(message => {
                        return message("user_id").eq(message("id")).and(message("chat_id").eq(chat("id")))
                    })
                    .pluck("id", "creation_date", "message", "email", "username", "status")
                    .orderBy(r.asc('creation_date'))
                    .coerceTo('array'),
            "users":
                chat("users").concatMap(user => {
                    return r.table("users").filter(usu => {
                        return usu("id").eq(user("id"))
                    }).pluck("id", "email", "username", "image")
                })
        })
    })
    .run(connection).then(function (cursor) {
        return cursor.toArray()
    }).then(function (chats) {
        res.json({ success: 1, chats })
    }).catch(function (err) {
            sendError(res, "Error -> " + err)
    })