Modifying the nested data organization in Sequelize

I'm looking to adjust the data structure retrieved from an ORM query involving four tables. The product and category tables have a many-to-many relationship, with the product_category table serving as a bridge. Additionally, there's a fourth table called department. Here is how the associations are set up:

// product
product.belongsToMany(models.category, {
      through: 'product_category',
      foreignKey: 'product_id'
});

// product_category
product_category.belongsTo(models.product, {
      foreignKey: 'product_id'
});
product_category.belongsTo(models.category, {
      foreignKey: 'category_id'
});

// category
category.belongsToMany(models.product, {
      through: 'product_category',
      foreignKey: 'category_id'
});
category.belongsTo(models.department, {
      foreignKey: 'department_id'
});

// department
department.hasMany(models.category, {
      foreignKey: 'department_id'
});

Using this table structure, an ORM query like the following can be used to retrieve products corresponding to a specific department_id:

const query = await product.findOne({
   where: { product_id: id },
   include: {
     model: category,
     attributes: ['category_id', ['name', 'category_name']],
     include: {
       model: department,
       attributes: ['department_id', ['name', 'department_name']]
     }
   },
   attributes: []
});

const data = query.categories;

The resulting JSON data looks like this:

"data": [
    {
        "category_id": 1,
        "category_name": "French",
        "department": {
            "department_id": 1,
            "department_name": "Regional"
        },
        "product_category": {
            "product_id": 1,
            "category_id": 1
        }
    }
]

My goal is to transform the above data to look like this:

"data": [
    {
         "category_id": 1,
         "category_name": "French",
         "department_id": 1,
         "department_name": "Regional"
    }
]

In order to achieve this transformation, I've explored two methods - modifying the SQL-based ORM query or manipulating the product value in JavaScript. While I wasn't familiar with the first method due to my SQL learning background, I attempted the second method.


First Attempt

I made two attempts, taking into account that the framework utilizes Koa.js. The first attempt looked like this:

const query = await product.findOne({
  where: { product_id: id },
  include: {
    model: category,
    attributes: ['category_id', ['name', 'category_name']],
    include: {
      model: department,
      attributes: ['department_id', ['name', 'department_name']]
    }
  },
  attributes: []
});

const data = query.categories.map(
      ({ category_id, category_name, department }) => ({
        category_id,
        category_name,
        department_id: department.department_id,
        department_name: department.department_name
      })
    );

ctx.body = data;

The resulting output was:

"data": [
    {
        "category_id": 1,
        "department_id": 1
    }
]

After observing some discrepancies, I slightly adjusted the return values which resulted in:

({ category_id, category_name, department }) => ({
        // category_id,
        // category_name,
        department_id: department.department_id,
        department_name: department.department_name
      })

This led to the following JSON response:

"data": [
    {
        "department_id": 1
    }
]

Alternatively, by omitting department_id and department_name:

({ category_id, category_name, department }) => ({
        category_id,
        category_name,
        // department_id: department.department_id,
        // department_name: department.department_name
      })

The JSON output was:

"data": [
    {
        "category_id": 1
    }
]

I couldn't find another way to manipulate the data.

Second Attempt

await product
.findOne({
  where: { product_id: id },
  include: {
    model: category,
    attributes: ['category_id', ['name', 'category_name']],
    include: {
      model: department,
      attributes: ['department_id', ['name', 'department_name']]
    }
  },
  attributes: []
})
.then(query => {
  const data = query.categories.map(
    ({ category_id, category_name, department }) => ({
      category_id,
      category_name,
      department_id: department.department_id,
      department_name: department.department_name
    })
  );

  ctx.body = data;
});

Both approaches yielded similar results, leaving me uncertain on how to proceed.


I tried mapping variables within nested arrays containing JSON data, which produced the desired outcome:

const data = {
  categories: [
    {
      category_id: 1,
      category_name: 'French',
      department: { department_id: 1, department_name: 'Regional' },
      product_category: { product_id: 1, category_id: 1 }
    }
  ]
};

const product = data.categories.map(
  ({ category_id, category_name, department }) => ({
    category_id,
    category_name,
    department_id: department.department_id,
    department_name: department.department_name
  })
);

console.log(product);

// [ { category_id: 1,
//    category_name: 'French',
//    department_id: 1,
//    department_name: 'Regional' } ]

Despite these efforts, I felt perplexed. How should I handle data obtained from a Sequelize query? Your guidance would be greatly appreciated.

Please inform me if my problem-solving approach is flawed or if you require further details on the model schema.

Answer №1

I approached this problem in a straightforward manner, resulting in a comprehensive solution. However, I believe there is room for improvement and welcome suggestions on the Best Way to enhance it.

const getProduct = () => {
  const a = query.categories[0];
  const b = a.get({ plain: true });
  const { category_id, category_name } = b;
  const { department_id, department_name } = b.department;

  return {
    category_id,
    category_name,
    department_id,
    department_name
  };
};

ctx.body = getProduct();

Here is the JSON data output:

"product": {
    "category_id": 1,
    "category_name": "French",
    "department_id": 1,
    "department_name": "Regional"
}

When running console.log(), the sequelize query will be displayed as dataValues: {}, (...). To effectively process the data, it is crucial to utilize the following method after storing the query in a variable:

data.get ({plain: true})

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Focusing on a particular iframe

I am currently using the "Music" theme from Organic Theme on my WordPress site and have inserted this code to prevent SoundCloud and MixCloud oEmbeds from stretching the page width: iframe, embed { height: 100%; width: 100%; } Although the fitvid ...

The plugin needed for the 'autoprefixer' task could not be located

Starting out in the world of Angular 2 development can be overwhelming, especially when trying to integrate with the Play framework on the back-end side. I recently came across a helpful post by Denis Sinyakov that walks through setting up this integratio ...

Allowing domain access when using axios and express

I have a server.js file where I use Express and run it with node: const express = require("express"); const app = express(), DEFAULT_PORT = 5000 app.set("port", process.env.PORT || DEFAULT_PORT); app.get("/whatever", function (req, r ...

Can you outline the key distinctions between AngularJS and ReactJS?

Looking to create a website that will be converted into a mobile application, I realize that my expertise lies more in desktop and Android native development rather than web client side development. After some research, I have decided to utilize HTML5, CSS ...

JavaScript string manipulation function

Hey everyone, I need help finding a one-line solution in JavaScript to determine if a string contains a semicolon without using a loop. If anyone knows how to do this, please share your knowledge! ...

Utilizing the import feature for structuring the routes folder in Express with Node.js

Recently, I made the switch to using ECMAScript (ES6) for my NodeJS Rest API with Express, and I've encountered a few challenges when working with the new keyword import In the past, I would organize my routes folder like this: Let's imagine th ...

JavaScript Processing Multiple Input Values to Produce an Output

Hello, I am working on creating a stamp script that will allow users to enter their name and address into three fields. Later, these fields will be displayed in the stamp edition. Currently, I have set up 3 input fields where users can input their data. He ...

Error: Unable to retrieve the name property of an undefined object within a React component that is being used as a child component within another parent component

FundraiserScreen.js // Import necessary components and dependencies; // Import Fundraiser component; const Children = ({ loading, error, fundraiserData }) => { if (loading) // Show skeleton loading HTML if (!error) return <Fundraiser fundraiser={fund ...

Creating an AngularJS directive specifically for a certain <div> tag

Recently, I began learning angularjs and came across a script to change the font size. However, this script ended up changing all <p> tags on the entire webpage. Is there a way to modify the font size of <p> tags only within the <div class=" ...

How can nested json be sorted effectively based on two specific fields?

Example Data: [{ 'ID': objID(abc123), 'Department': 'IT', 'Employees': [ { 'ID': 3, 'StartDate': '24-12-2022T08:30', 'active': true }, { ...

Using AngularJS to filter search results based on two user-provided input parameters

Currently, I am working on an Angular application that presents movies in a table format with search input and dropdown filter by genres. As I work on this project, my main focus is finding a way to filter search results based on both the text entered in t ...

Revamping the login interface for enhanced user

Whenever I attempt to login by clicking the login button, there seems to be an issue as it does not redirect me to any other page. Instead, I am left on the same page where I initially clicked the button. The intended behavior is for users to be redirected ...

I am not getting any reply in Postman - I have sent a patch request but there is no response showing up in the Postman console

const updateProductInfo = async (req, res) => { const productId = req.params.productId; try { const updatedProduct = await Product.findOneAndUpdate({ _id: productId }, { $set: req.body }); console.log("Product updat ...

Choosing a request date that falls within a specified range of dates in PHP Laravel

In my database, I currently store two dates: depart_date and return_date. When a user is filling out a form on the view blade, they need to select an accident_date that falls between depart_date and return_date. Therefore, before submitting the form, it ne ...

Tips for concealing JavaScript and web framework version details from Weppalyzer

To enhance security, we are looking to conceal all JS and Bootstrap information. I have employed the help of the Weppalyzer tool for this purpose. https://i.stack.imgur.com/iLMGF.png Does anyone know how to effectively hide these details? I attempted set ...

Ways to prevent the need for multiple if/else statements and repetitious function instances

Check out this code snippet in Javascript: https://pastebin.com/zgJdYhzN. The purpose of the code is to fade in text when scrolling reaches a specific point. While it does work, I want to optimize it for multiple pages without creating separate instances ...

What could be the reason for req.route displaying the previous route instead of

Question: const router = express.Router(); router .route('/:id') .delete( validate(messageValidator.deleteById), MessageController.deleteById, ) .get( validate(messageValidator.getById), MessageController.getById, ); ...

Utilizing a Meteor Method within a Promise Handler [Halting without Throwing an Error]

I've been working on integrating the Gumroad-API NPM package into my Meteor app, but I've run into some server-side issues. Specifically, when attempting to make a Meteor method call or insert data into a collection within a Promise callback. Be ...

Tips for reconstructing a path in Raphael JS

I am encountering a unique issue with my implementation of Raphael JS. Currently, I am working on drawing a donut element and seeking advice similar to the content found in this helpful link How to achieve 'donut holes' with paths in Raphael) var ...

Encountered difficulties sending JSON data to a REST endpoint using Node.js

Is there a way to bulk insert JSON data into MongoDB using Mongoose? I am aware of the insertMany method, but I'm encountering difficulties with extracting the correct req.body. Below is an image of my setup in Postman. Here is my Node.js code: rout ...