Saturday 13 May 2017

SequelizeJS primaryKey related include

I am currently working on a REST API, working with SequelizeJS and Express.

I'm used to Django Rest Framework and I'm trying to find a similar function : I have a table User and a table PhoneNumber. I want to be able to return a user in JSON, including the list of the primarykeys of its phone numbers like this :

{
  "firstName": "John",
  "lastName": "Doe",
  "phoneNumbers": [23, 34, 54],
}

Is there a way to do this simply and efficiently in sequelize or do I have to write functions that transform the fields like :

"phoneNumbers": [
    { "id": 23, "number": "XXXXXXXXXX" },
    { "id": 34, "number": "XXXXXXXXXX" },
    { "id": 54, "number": "XXXXXXXXXX" }
      ]

into what I have above ?

Thank you, Giltho



via Giltho

No comments:

Post a Comment