MySQL with Node: how to return nested data

Emille C. :

I have three tables: post, tag and post_tag.

CREATE TABLE post (
  id INT NOT NULL,
  title VARCHAR(45) NULL,
  PRIMARY KEY (id));

CREATE TABLE tag (
  id INT NOT NULL,
  tag VARCHAR(45) NULL,
  PRIMARY KEY (id));

CREATE TABLE post_tag (
  post_id INT NOT NULL,
  tag_id INT NOT NULL,
  PRIMARY KEY (post_id, tag_id),
  INDEX fk_post_tag_tag1_idx (tag_id ASC),
  INDEX fk_post_tag_post_idx (post_id ASC),
  CONSTRAINT fk_post_tag_post
    FOREIGN KEY (post_id)
    REFERENCES post (id),
  CONSTRAINT fk_post_tag_tag1
    FOREIGN KEY (tag_id)
    REFERENCES tag (id));

INSERT INTO post (id, title) VALUES (1, 'post 1');
INSERT INTO post (id, title) VALUES (2, 'post 2');
INSERT INTO tag (id, tag) VALUES (1, 'tag 1');
INSERT INTO tag (id, tag) VALUES (2, 'tag 2');
INSERT INTO post_tag (post_id, tag_id) VALUES (1, 1);
INSERT INTO post_tag (post_id, tag_id) VALUES (1, 2);
INSERT INTO post_tag (post_id, tag_id) VALUES (2, 1);
INSERT INTO post_tag (post_id, tag_id) VALUES (2, 2);

Then I can create a stored procedure to retrieve the first post with its tags:

DELIMITER $$
CREATE PROCEDURE select_posts_tags(IN id INT)
BEGIN
  SELECT *
  FROM post
    INNER JOIN post_tag pt ON post.id = pt.post_id
    INNER JOIN tag t ON t.id = pt.tag_id
      WHERE post.id = id
      GROUP BY post.id, t.id;
END $$
DELIMITER ;

And finally I call the stored procedure from Node:

var mysql = require("mysql");
var connection = mysql.createConnection({
  host: "127.0.0.1",
  user: "test_database",
  password: "test_database",
  database: "test_database",
});

connection.connect();

const sql = `CALL select_posts_tags(${1})`;

connection.query(sql, (error, results) =>
  console.log(JSON.stringify(results[0], null, 4))
);

connection.end();

But the result is an array of flat objects:

[
    {
        "id": 1,
        "title": "post 1",
        "post_id": 1,
        "tag_id": 1,
        "tag": "tag 1"
    },
    {
        "id": 2,
        "title": "post 1",
        "post_id": 1,
        "tag_id": 2,
        "tag": "tag 2"
    }
]

The result is an array of the same flat JSON object repeated twice with different tags;

How can I retrieve the tags as a nested array within «tags» key within the post objects? The result should be this:

[
  {
    id: 1,
    title: "post 1",
    tags: [
      {
        id: 1,
        tag: "tag 1",
      },
      {
        id: 2,
        tag: "tag 2",
      },
    ],
  },
];

Thanks!

GMB :

You could use json_arrayagg() and json_object() as follows:

SELECT 
    p.id, 
    p.title,
    json_arrayagg(json_object('id', t.id, 'tag', t.tag)) tags
FROM post p
INNER JOIN post_tag pt ON p.id = pt.post_id
INNER JOIN tag t ON t.id = pt.tag_id
WHERE p.id = ?
GROUP BY p.id, p.title;

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Firebase-realtime-database how to return the data within a nested child node as a list

How to return data in nested async/await function

How to return data from nested async function

mySQL JSON_TABLE query to return nested data

mysql return nested array

How to return from nested function in Node.js?

Node JS - How to return response from nested function?

How to insert nested json data in mysql table?

Spring Data Mongo: How to return nested object by its field?

How can I return my data in this nested promise model?

how can I access a nested array within a json data return?

How can search JSON for a value key and return the nested data?

rxjs operators, how to return nested data in a single object?

how can I get return the data in nested for python

How to fetch data from MySQL database with Node

Node: How to restructure nested JSON data getting and organize reusable?

How to Return Nested Promise

How to return nested JSON?

How To Return Nested Variable?

How to return the response of Node.js mysql query connection

How to return a JSON object from a mySQL query in node.js

how to return multi requests from mysql package in node js

How to make return wait for MySQL connection to end? Node.js

How to return '1' if data exists and '0' if there is no data with mysql CASE

How do I return data from a function in node?

Neo4j: How to return deep node data

How to return data from post request in Node js?

MongoDB with node.js - how to return data async

Mysql & Express, how to get my data and create nested array