I have two MySQL tables
category(id, name)
product(id, name, category_id)
product(category_id) is a foreign key to category(id), the relationship is one to many to one relationship so one category can have numerous products.
I am trying to get this from the database and return it in JSON format for multiple categories, so it would look like this:
[
{
"id": 1,
"name": "Category 1",
"products": [
{
"id": 1,
"name": "Chicken"
},
{
"id": 2,
"name": "Beef"
}
]
},
{
"id": 2,
"name": "Category 2",
"products": [
{
"id": 3,
"item": "Fries"
},
{
"id": 4,
"item": "Burgers"
}
]
}
]
Where the products are nested within the entity of their corresponding category.
I am a newbie and not sure how i would reproduce this using nodeJs from MySQL. Kindly assist