0

Currently running into an issue with optimizing current code implementation for creating a tree-like structure of the returned contents. The contents to be returned are basically content (think of them as posts), and they each can have nested posts (when someone comments or reacts to them). A sample structure of the content object looks something like this:

{
  "id": 161,
  "content": "test comment 3",
  "content_type": "c",
  "post_id": 125,
  "created_by": "59e3fce9-9ae4-4e9e-a5ff-75e4131b5117",
  "updated_by": "59e3fce9-9ae4-4e9e-a5ff-75e4131b5117",
  "created_at": "2022-11-15T20:10:13.616Z",
  "updated_at": "2022-11-15T20:10:13.616Z",
  "parent_id": 126,
  "name": "Testing Member_002",
  "level": 2,
  "related_content": [
    {
      "id": 162,
      "content": "test reaction 3",
      "content_type": "r",
      "post_id": 125,
      "created_by": "59e3fce9-9ae4-4e9e-a5ff-75e4131b5117",
      "updated_by": "59e3fce9-9ae4-4e9e-a5ff-75e4131b5117",
      "created_at": "2022-11-15T20:10:13.616Z",
      "updated_at": "2022-11-15T20:10:13.616Z",
      "parent_id": 161,
      "name": "Testing Member_002",
      "level": 3,
    }
  ]
}

If that specific post doesn't have any child content (where it is pointing to an existing parent_id), then it will not have that related_content field (you can imagine it as being an empty array there).

I've optimized some of the code by storing them as k-v pairs of parent_id to posts from the initial query (to fetch all the posts with the current root passed in), but for nested levels, I've found myself having to access directly those levels which introduces some extra complexity.

My goal is to create a tree-like response structure, where there will always be a root node (that will be the top-level post), followed by its adjacent content records.

Code snippet for where it builds the final tree response:

// function builds the comments and reactions for the final response structure for the appropriate levels
const append_comments_reactions = (arr) => {
  arr.forEach((post) => {
    // this iteration only contains top level post
    const content_id = post.id;
    post.related_content = [];

    // if the current level post has adjacent child posts
    if (Object.keys(posts_dict).includes(content_id.toString())) {
      // look up adjacent child comments
      if (posts_dict[content_id]) {
        post.related_content.push(...posts_dict[content_id]);
      }
    }
  });
};

// build the first level
append_comments_reactions(all_filtered_posts);

// build second level
all_filtered_posts.forEach((post) => {
  append_comments_reactions(post.related_content);
});

// need to verify with team on how many nested levels of comments/reactions we can have
// build third level
all_filtered_posts[0].related_content.forEach((post) => {
  append_comments_reactions(post.related_content);
});

'posts_dict' is a dictionary that stores the current level as the key, and the value will be an array of all the posts that are on the current level. If the level is 0, then it is the root, and if level is 1, then it is a child of a post from the previous level (the parent is found as the parent_id). It looks like this:

{
  "0": [
    {
      id: 125,
      account_id: '5',
      content: 'Hi! My name is Bradley and I will be your team coach! Are you guys excited for the first challenge? The team who has the most completed SMART Goals for this week will win!',
      content_type: 'p',
      post_id: null,
      created_by: '5',
      updated_by: '5',
      created_at: 2022-11-08T19:40:47.258Z,
      updated_at: 2022-11-08T19:40:47.258Z,
      parent_id: null,
      name: 'Bradley Reyes',
      level: 0,
    }
  ],
  "1": [
    {
      id: 160,
      account_id: '59e3fce9-9ae4-4e9e-a5ff-75e4131b5117',
      content: 'test comment 3',
      content_type: 'c',
      post_id: 125,
      created_by: '59e3fce9-9ae4-4e9e-a5ff-75e4131b5117',
      updated_by: '59e3fce9-9ae4-4e9e-a5ff-75e4131b5117',
      created_at: 2022-11-15T20:03:39.416Z,
      updated_at: 2022-11-15T20:03:39.416Z,
      parent_id: 125,
      name: 'Testing Member_002',
      level: 1,
    },
    {
      id: 126,
      account_id: '127697b7-bab7-47d2-b8f1-c483ee127f66',
      content: 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam at mauris dui. Nam ut urna ligula. Mauris viverra dolor et aliquet fringilla. Sed vulputate eros non tincidunt dignissim.',
      content_type: 'c',
      post_id: 125,
      created_by: '127697b7-bab7-47d2-b8f1-c483ee127f66',
      updated_by: '127697b7-bab7-47d2-b8f1-c483ee127f66',
      created_at: 2022-11-08T23:13:33.554Z,
      updated_at: 2022-11-08T23:13:33.554Z,
      parent_id: 125,
      name: 'Testing Member_001',
      level: 1,
    }
  ]
}

Currently the implementation works, but I can't imagine how slow it will become if it has even more data, or if there are more levels to dig through (currently only going through 4 levels deep).

The goal of the structure is this:

[
  {
    id: 125,
    account_id: '5',
    content: 'Hi! My name is Bradley and I will be your team coach! Are you guys excited for the first challenge? The team who has the most completed SMART Goals for this week will win!',
    content_type: 'p',
    post_id: null,
    created_by: '5',
    updated_by: '5',
    created_at: 2022-11-08T19:40:47.258Z,
    updated_at: 2022-11-08T19:40:47.258Z,
    parent_id: null,
    name: 'Bradley Reyes',
    level: 0,
    related_content: [
      {
        {
          id: 160,
          account_id: '59e3fce9-9ae4-4e9e-a5ff-75e4131b5117',
          content: 'test comment 3',
          content_type: 'c',
          post_id: 125,
          created_by: '59e3fce9-9ae4-4e9e-a5ff-75e4131b5117',
          updated_by: '59e3fce9-9ae4-4e9e-a5ff-75e4131b5117',
          created_at: 2022-11-15T20:03:39.416Z,
          updated_at: 2022-11-15T20:03:39.416Z,
          parent_id: 125,
          name: 'Testing Member_002',
          level: 1,
        },
        {
          id: 126,
          account_id: '127697b7-bab7-47d2-b8f1-c483ee127f66',
          content: 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam at mauris dui. Nam ut urna ligula. Mauris viverra dolor et aliquet fringilla. Sed vulputate eros non tincidunt dignissim.',
          content_type: 'c',
          post_id: 125,
          created_by: '127697b7-bab7-47d2-b8f1-c483ee127f66',
          updated_by: '127697b7-bab7-47d2-b8f1-c483ee127f66',
          created_at: 2022-11-08T23:13:33.554Z,
          updated_at: 2022-11-08T23:13:33.554Z,
          parent_id: 125,
          name: 'Testing Member_001',
          level: 1,
        }
      }
    ]
  }
]

Any help or clarifications will be appreciated, thank you.

0 Answers0