1

I have a data structure of nodes like so:

[
  {
      id: 'admin-sctask56184526_i_courses',
      type: 'branchNode',
      data: {
        id: 'admin-sctask56184526_i_courses',
        table: 'admin.sctask56184526_i_courses',
        type: 'branchNode',
        fields: [
          {
            name: 'id',
            parent_table: 'pgdb_university.courses',
            parent_field: 'id',
            child_table: 'sandbox_university.i_courses',
            child_field: 'id',
            child_edges: [
              'e-admin.sctask56184526_i_courses.id-sandbox_university.i_courses.id'
            ],
            parent_edges: [
              'e-pgdb_university.courses.id-admin.sctask56184526_i_courses.id'
            ],
            children: [
              {
                child_database: 'sandbox_university',
                child_table: 'i_courses',
                child_field: 'id',
                child_edge: 'e-admin.sctask56184526_i_courses.id-sandbox_university.i_courses.id'
              }
            ],
            selected: false
          },
          {
            name: 'name',
            parent_table: 'pgdb_university.courses',
            parent_field: 'name',
            child_table: 'sandbox_university.i_courses',
            child_field: 'name',
            child_edges: [
              'e-admin.sctask56184526_i_courses.name-sandbox_university.i_courses.name'
            ],
            parent_edges: [
              'e-pgdb_university.courses.name-admin.sctask56184526_i_courses.name'
            ],
            children: [
              {
                child_database: 'sandbox_university',
                child_table: 'i_courses',
                child_field: 'name',
                child_edge: 'e-admin.sctask56184526_i_courses.name-sandbox_university.i_courses.name'
              }
            ],
            selected: false
          },
          {
            name: 'description',
            parent_table: 'pgdb_university.courses',
            parent_field: 'description',
            child_table: 'sandbox_university.i_courses',
            child_field: 'description',
            child_edges: [
              'e-admin.sctask56184526_i_courses.description-sandbox_university.i_courses.description'
            ],
            parent_edges: [
              'e-pgdb_university.courses.description-admin.sctask56184526_i_courses.description'
            ],
            children: [
              {
                child_database: 'sandbox_university',
                child_table: 'i_courses',
                child_field: 'description',
                child_edge: 'e-admin.sctask56184526_i_courses.description-sandbox_university.i_courses.description'
              }
            ],
            selected: false
          },
          {
            name: 'teacher',
            parent_table: 'pgdb_university.courses',
            parent_field: 'teacher',
            child_table: 'sandbox_university.i_courses',
            child_field: 'teacher',
            child_edges: [
              'e-admin.sctask56184526_i_courses.teacher-sandbox_university.i_courses.teacher'
            ],
            parent_edges: [
              'e-pgdb_university.courses.teacher-admin.sctask56184526_i_courses.teacher'
            ],
            children: [
              {
                child_database: 'sandbox_university',
                child_table: 'i_courses',
                child_field: 'teacher',
                child_edge: 'e-admin.sctask56184526_i_courses.teacher-sandbox_university.i_courses.teacher'
              }
            ],
            selected: false
          },
          {
            name: 'costband',
            parent_table: 'pgdb_university.courses',
            parent_field: 'costband',
            child_table: 'sandbox_university.i_courses',
            child_field: 'costband',
            child_edges: [
              'e-admin.sctask56184526_i_courses.costband-sandbox_university.i_courses.costband'
            ],
            parent_edges: [
              'e-pgdb_university.courses.costband-admin.sctask56184526_i_courses.costband'
            ],
            children: [
              {
                child_database: 'sandbox_university',
                child_table: 'i_courses',
                child_field: 'costband',
                child_edge: 'e-admin.sctask56184526_i_courses.costband-sandbox_university.i_courses.costband'
              }
            ],
            selected: false
          }
        ],
        y_level: '1'
      },
      position: {
        x: 584,
        y: 180
      },
      width: 268,
      height: 256
    },
    {
      id: 'sandbox_university-i_courses',
      type: 'branchNode',
      data: {
        id: 'sandbox_university-i_courses',
        table: 'sandbox_university.i_courses',
        type: 'branchNode',
        fields: [
          {
            name: 'id',
            parent_table: 'admin.sctask56184526_i_courses',
            parent_field: 'id',
            child_table: 'sandbox_university.txn_courses',
            child_field: 'course_id',
            child_edges: [
              'e-sandbox_university.i_courses.id-sandbox_university.txn_courses.course_id'
            ],
            parent_edges: [
              'e-admin.sctask56184526_i_courses.id-sandbox_university.i_courses.id'
            ],
            children: [
              {
                child_database: 'sandbox_university',
                child_table: 'txn_courses',
                child_field: 'course_id',
                child_edge: 'e-sandbox_university.i_courses.id-sandbox_university.txn_courses.course_id'
              }
            ],
            selected: false
          },
          {
            name: 'name',
            parent_table: 'admin.sctask56184526_i_courses',
            parent_field: 'name',
            child_table: 'sandbox_university.txn_courses',
            child_field: 'course_name',
            child_edges: [
              'e-sandbox_university.i_courses.name-sandbox_university.txn_courses.course_name'
            ],
            parent_edges: [
              'e-admin.sctask56184526_i_courses.name-sandbox_university.i_courses.name'
            ],
            children: [
              {
                child_database: 'sandbox_university',
                child_table: 'txn_courses',
                child_field: 'course_name',
                child_edge: 'e-sandbox_university.i_courses.name-sandbox_university.txn_courses.course_name'
              }
            ],
            selected: false
          },
          {
            name: 'description',
            parent_table: 'admin.sctask56184526_i_courses',
            parent_field: 'description',
            child_table: 'sandbox_university.txn_courses',
            child_field: 'course_description',
            child_edges: [
              'e-sandbox_university.i_courses.description-sandbox_university.txn_courses.course_description'
            ],
            parent_edges: [
              'e-admin.sctask56184526_i_courses.description-sandbox_university.i_courses.description'
            ],
            children: [
              {
                child_database: 'sandbox_university',
                child_table: 'txn_courses',
                child_field: 'course_description',
                child_edge: 'e-sandbox_university.i_courses.description-sandbox_university.txn_courses.course_description'
              }
            ],
            selected: false
          },
          {
            name: 'teacher',
            parent_table: 'admin.sctask56184526_i_courses',
            parent_field: 'teacher',
            child_table: 'sandbox_university.txn_courses',
            child_field: 'teacher_id',
            child_edges: [
              'e-sandbox_university.i_courses.teacher-sandbox_university.txn_courses.teacher_id'
            ],
            parent_edges: [
              'e-admin.sctask56184526_i_courses.teacher-sandbox_university.i_courses.teacher'
            ],
            children: [
              {
                child_database: 'sandbox_university',
                child_table: 'txn_courses',
                child_field: 'teacher_id',
                child_edge: 'e-sandbox_university.i_courses.teacher-sandbox_university.txn_courses.teacher_id'
              }
            ],
            selected: false
          },
          {
            name: 'costband',
            parent_table: 'admin.sctask56184526_i_courses',
            parent_field: 'costband',
            child_table: 'sandbox_university.txn_courses',
            child_field: 'cost_band_id',
            child_edges: [
              'e-sandbox_university.i_courses.costband-sandbox_university.txn_courses.cost_band_id'
            ],
            parent_edges: [
              'e-admin.sctask56184526_i_courses.costband-sandbox_university.i_courses.costband'
            ],
            children: [
              {
                child_database: 'sandbox_university',
                child_table: 'txn_courses',
                child_field: 'cost_band_id',
                child_edge: 'e-sandbox_university.i_courses.costband-sandbox_university.txn_courses.cost_band_id'
              }
            ],
            selected: false
          }
        ],
        y_level: '1'
      },
      position: {
        x: 1074,
        y: 180
      },
      width: 243,
      height: 256
    },
    {
      id: 'sandbox_university-txn_courses',
      type: 'branchNode',
      data: {
        id: 'sandbox_university-txn_courses',
        table: 'sandbox_university.txn_courses',
        type: 'branchNode',
        fields: [
          {
            name: 'course_id',
            parent_table: 'sandbox_university.i_courses',
            parent_field: 'id',
            child_table: 'sandbox_university.j_txn_courses',
            child_field: 'course_id',
            child_edges: [
              'e-sandbox_university.txn_courses.course_id-sandbox_university.j_txn_courses.course_id',
              'e-sandbox_university.txn_courses.course_id-sandbox_university.j_txn_course_costs.course_id'
            ],
            parent_edges: [
              'e-sandbox_university.i_courses.id-sandbox_university.txn_courses.course_id'
            ],
            children: [
              {
                child_database: 'sandbox_university',
                child_table: 'j_txn_courses',
                child_field: 'course_id',
                child_edge: 'e-sandbox_university.txn_courses.course_id-sandbox_university.j_txn_courses.course_id'
              },
              {
                child_database: 'sandbox_university',
                child_table: 'j_txn_course_costs',
                child_field: 'course_id',
                child_edge: 'e-sandbox_university.txn_courses.course_id-sandbox_university.j_txn_course_costs.course_id'
              }
            ],
            selected: false
          },
          {
            name: 'course_name',
            parent_table: 'sandbox_university.i_courses',
            parent_field: 'name',
            child_table: 'sandbox_university.j_txn_course_costs',
            child_field: 'course_name',
            child_edges: [
              'e-sandbox_university.txn_courses.course_name-sandbox_university.j_txn_course_costs.course_name',
              'e-sandbox_university.txn_courses.course_name-sandbox_university.j_txn_courses.course_name'
            ],
            parent_edges: [
              'e-sandbox_university.i_courses.name-sandbox_university.txn_courses.course_name'
            ],
            children: [
              {
                child_database: 'sandbox_university',
                child_table: 'j_txn_course_costs',
                child_field: 'course_name',
                child_edge: 'e-sandbox_university.txn_courses.course_name-sandbox_university.j_txn_course_costs.course_name'
              },
              {
                child_database: 'sandbox_university',
                child_table: 'j_txn_courses',
                child_field: 'course_name',
                child_edge: 'e-sandbox_university.txn_courses.course_name-sandbox_university.j_txn_courses.course_name'
              }
            ],
            selected: false
          },
          {
            name: 'course_description',
            parent_table: 'sandbox_university.i_courses',
            parent_field: 'description',
            child_table: 'sandbox_university.j_txn_course_costs',
            child_field: 'course_description',
            child_edges: [
              'e-sandbox_university.txn_courses.course_description-sandbox_university.j_txn_course_costs.course_description',
              'e-sandbox_university.txn_courses.course_description-sandbox_university.j_txn_courses.course_description'
            ],
            parent_edges: [
              'e-sandbox_university.i_courses.description-sandbox_university.txn_courses.course_description'
            ],
            children: [
              {
                child_database: 'sandbox_university',
                child_table: 'j_txn_course_costs',
                child_field: 'course_description',
                child_edge: 'e-sandbox_university.txn_courses.course_description-sandbox_university.j_txn_course_costs.course_description'
              },
              {
                child_database: 'sandbox_university',
                child_table: 'j_txn_courses',
                child_field: 'course_description',
                child_edge: 'e-sandbox_university.txn_courses.course_description-sandbox_university.j_txn_courses.course_description'
              }
            ],
            selected: false
          },
          {
            name: 'teacher_id',
            parent_table: 'sandbox_university.i_courses',
            parent_field: 'teacher',
            child_table: 'sandbox_university.j_txn_courses',
            child_field: 'teacher_id',
            child_edges: [
              'e-sandbox_university.txn_courses.teacher_id-sandbox_university.j_txn_courses.teacher_id'
            ],
            parent_edges: [
              'e-sandbox_university.i_courses.teacher-sandbox_university.txn_courses.teacher_id'
            ],
            children: [
              {
                child_database: 'sandbox_university',
                child_table: 'j_txn_courses',
                child_field: 'teacher_id',
                child_edge: 'e-sandbox_university.txn_courses.teacher_id-sandbox_university.j_txn_courses.teacher_id'
              }
            ],
            selected: false
          },
          {
            name: 'cost_band_id',
            parent_table: 'sandbox_university.i_courses',
            parent_field: 'costband',
            child_table: 'sandbox_university.j_txn_courses',
            child_field: 'cost_band_id',
            child_edges: [
              'e-sandbox_university.txn_courses.cost_band_id-sandbox_university.j_txn_courses.cost_band_id'
            ],
            parent_edges: [
              'e-sandbox_university.i_courses.costband-sandbox_university.txn_courses.cost_band_id'
            ],
            children: [
              {
                child_database: 'sandbox_university',
                child_table: 'j_txn_courses',
                child_field: 'cost_band_id',
                child_edge: 'e-sandbox_university.txn_courses.cost_band_id-sandbox_university.j_txn_courses.cost_band_id'
              }
            ],
            selected: false
          }
        ],
        y_level: '1'
      },
      position: {
        x: 1548,
        y: 180
      },
      width: 260,
      height: 256
    }
]

I would like to recursively go through each node to find out if it has children. If it does, I would like to go through its corresponding children to see if it has children and so forth.

The code I have so far is:

for (const child of origin_node_children) {
  // Set child node and field to inspect
  let child_target_node =
    child.child_database + "-" + child.child_table;
  let child_target_field = child.child_field;
  // See if child target has children of its own
  let child_target_children = get()
    .nodes.find((node) => node.id === child_target_node)
    .data.fields.find(
      (field) => field.id === child_target_field
    ).children;
  // Style child edge
  // TODO
  if (child_target_children.length > 0) {
    for (const subchild of child_target_children) {
      let subchild_target_node =
        subchild.child_database +
        "-" +
        subchild.child_table;
      let subchild_target_field = subchild.child_field;
      let subchild_target_children = get()
        .nodes.find((node) => node.id === child_target_node)
        .data.fields.find(
          (field) => field.id === child_target_field
        ).children;
    }
    finishedReading = false;
    while (!finishedReading) {
      for (const subchild of child_target_children) {
        child_target_node =
          subchild.child_database +
          "-" +
          subchild.child_table;
        child_target_field = subchild.child_field;
        child_target_children = get()
          .nodes.find(
            (node) => node.id === child_target_node
          )
          .data.fields.find(
            (field) => field.id === child_target_field
          ).children;
      }
      finishedReading = true;
    }
  }
}

But I am getting wrapped around how to get back to the original node that has the children. If I start with sandbox_unversity-i_courses and use the field id as my entry point, I want it to go through all the children (some fields may have more than one child).

This is for react-flow. I know how to style the edges, its just going through each child of child of child etc to style each edge that follows on from this node.

A visual representation of this might help:

Image of problem

Barmar
  • 741,623
  • 53
  • 500
  • 612
DaveMcK
  • 111
  • 1
  • 1
    What kind of output are you expecting? – Andy Sep 08 '22 at 15:53
  • 1
    Put your code in a function. Then you can call it recursively at each level. – Barmar Sep 08 '22 at 15:59
  • I basically want to go through each child field and apply a function to style the edge in react flow (which I already have the function for). So in my example, id feeds into course_id, which feeds into 2 separate nodes, one of which feeds onwards. I want to style each edge as it goes along – DaveMcK Sep 08 '22 at 16:03
  • @Barmar - seems that was the answer and now I have it working. Marked as duplicate due to an answer being available as suggested. – DaveMcK Sep 09 '22 at 09:21

0 Answers0