Perform query - relation Many to Many - Sequelize

Asked

Viewed 19 times

1

I have a problem to perform a query using the Sequelize ORM.

The structure is as follows: Migrations/tables: Developers, Technologists, developers_dechnologies.

The relationship between developers and Technologies is Many to Many, developers_dechnologies I use as a junction.

Through a select, I need to return all the developers and all the Technologies linked to them, which have the technology "x".

For example:

I have these 3 devs registered:

[
  {
    "id": 96,
    "name": "Jon",
    "technologies": [
      {
        "id": 6,
        "name": "React"
      },
      {
        "id": 7,
        "name": "Messageria"
      }
    ]
  },
  {
    "id": 97,
    "name": "Doe",
    "technologies": [
      {
        "id": 6,
        "name": "React"
      },
      {
        "id": 8,
        "name": "Javascript"
      }
    ]
  },
  {
    "id": 98,
    "name": "Mark",
    "technologies": [
      {
        "id": 1,
        "name": "PHP"
      },
      {
        "id": 2,
        "name": "Laravel"
      }
    ]
  }
]

In the front-end the user can filter the devs by technology, if the technology selected is React, need to return the result below, because they are the only devs that have React.

  {
    "id": 96,
    "name": "Jon",
    "technologies": [
      {
        "id": 6,
        "name": "**React**"
      },
      {
        "id": 7,
        "name": "Messageria"
      }
    ]
  },
  {
    "id": 97,
    "name": "Doe",
    "technologies": [
      {
        "id": 6,
        "name": "**React**"
      },
      {
        "id": 8,
        "name": "Javascript"
      }
    ]
  },

In the consultation I am not able to return the other technologies that are tied to the Veloper, only the one that is in the Where clause. My query:

  const developers = await Developer.findAll({
    attributes: ['id', 'name'],
    include: [
      {
        model: Technologies,
        as: 'technologies',
        all: true,
        attributes: ['id', 'name'],
        where: [{ id: tech }],
        through: { attributes: [] },
      },
    ],

I understand that this happens precisely because I am selecting only the id corresponding to a technology.. How could I return the others? What would be the correct logic?

A front-end print to understand a little better: inserir a descrição da imagem aqui

Selecting only React:

Here I would need beyond React, the other technologies attached to that dev.

inserir a descrição da imagem aqui

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.