Home / Function/ getGraphDataFromTables() — supabase Function Reference

getGraphDataFromTables() — supabase Function Reference

Architecture documentation for the getGraphDataFromTables() function in Schemas.utils.ts from the supabase codebase.

Entity Profile

Dependency Diagram

graph TD
  c873669a_f88c_bb16_2076_ffa1fc0fc1a7["getGraphDataFromTables()"]
  2f23e199_5215_d9cd_0558_237d0dc2652f["SchemaGraph()"]
  2f23e199_5215_d9cd_0558_237d0dc2652f -->|calls| c873669a_f88c_bb16_2076_ffa1fc0fc1a7
  9f291a05_a746_0ee5_2567_d9454cd807ec["SchemaVisualizer()"]
  9f291a05_a746_0ee5_2567_d9454cd807ec -->|calls| c873669a_f88c_bb16_2076_ffa1fc0fc1a7
  67a73498_91a3_6e06_0d6f_64c228b0844d["findTablesHandleIds()"]
  c873669a_f88c_bb16_2076_ffa1fc0fc1a7 -->|calls| 67a73498_91a3_6e06_0d6f_64c228b0844d
  1e123e3a_d836_584b_bf4a_e372fc253ab3["getLayoutedElementsViaLocalStorage()"]
  c873669a_f88c_bb16_2076_ffa1fc0fc1a7 -->|calls| 1e123e3a_d836_584b_bf4a_e372fc253ab3
  6afd133c_f4fa_d02d_b814_82224fed4bf4["getLayoutedElementsViaDagre()"]
  c873669a_f88c_bb16_2076_ffa1fc0fc1a7 -->|calls| 6afd133c_f4fa_d02d_b814_82224fed4bf4
  style c873669a_f88c_bb16_2076_ffa1fc0fc1a7 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

apps/studio/components/interfaces/Database/Schemas/Schemas.utils.ts lines 14–142

export async function getGraphDataFromTables(
  ref?: string,
  schema?: PostgresSchema,
  tables?: PostgresTable[]
): Promise<{
  nodes: Node<TableNodeData>[]
  edges: Edge[]
}> {
  if (!tables?.length) {
    return { nodes: [], edges: [] }
  }

  const nodes = tables.map((table) => {
    const columns = (table.columns || []).map((column) => {
      return {
        id: column.id,
        isPrimary: table.primary_keys.some((pk) => pk.name === column.name),
        name: column.name,
        format: column.format,
        isNullable: column.is_nullable,
        isUnique: column.is_unique,
        isUpdateable: column.is_updatable,
        isIdentity: column.is_identity,
      }
    })

    const data: TableNodeData = {
      ref,
      id: table.id,
      name: table.name,
      schema: table.schema,
      isForeign: false,
      columns,
    }

    return {
      data,
      id: `${table.id}`,
      type: 'table',
      position: { x: 0, y: 0 },
    }
  })

  const edges: Edge[] = []
  const currentSchema = tables[0].schema
  const uniqueRelationships = uniqBy(
    tables.flatMap((t) => t.relationships),
    'id'
  )

  for (const rel of uniqueRelationships) {
    // TODO: Support [external->this] relationship?
    if (rel.source_schema !== currentSchema) {
      continue
    }

    // Create additional [this->foreign] node that we can point to on the graph.
    if (rel.target_table_schema !== currentSchema) {
      const targetId = `${rel.target_table_schema}.${rel.target_table_name}.${rel.target_column_name}`

      const targetNode = nodes.find((n) => n.id === targetId)
      if (!targetNode) {
        const data: TableNodeData = {
          id: rel.id,
          ref: ref!,
          schema: rel.target_table_schema,
          name: targetId,
          isForeign: true,
          columns: [],
        }

        nodes.push({
          id: targetId,
          type: 'table',
          data: data,
          position: { x: 0, y: 0 },
        })
      }

      const [source, sourceHandle] = findTablesHandleIds(
        tables,
        rel.source_table_name,
        rel.source_column_name
      )

      if (source) {
        edges.push({
          id: String(rel.id),
          source,
          sourceHandle,
          target: targetId,
          targetHandle: targetId,
        })
      }

      continue
    }

    const [source, sourceHandle] = findTablesHandleIds(
      tables,
      rel.source_table_name,
      rel.source_column_name
    )
    const [target, targetHandle] = findTablesHandleIds(
      tables,
      rel.target_table_name,
      rel.target_column_name
    )

    // We do not support [external->this] flow currently.
    if (source && target) {
      edges.push({
        id: String(rel.id),
        source,
        sourceHandle,
        target,
        targetHandle,
      })
    }
  }

  const savedPositionsLocalStorage = localStorage.getItem(
    LOCAL_STORAGE_KEYS.SCHEMA_VISUALIZER_POSITIONS(ref ?? 'project', schema?.id ?? 0)
  )
  const savedPositions = tryParseJson(savedPositionsLocalStorage)
  return !!savedPositions
    ? getLayoutedElementsViaLocalStorage(nodes, edges, savedPositions)
    : getLayoutedElementsViaDagre(nodes, edges)
}

Subdomains

Frequently Asked Questions

What does getGraphDataFromTables() do?
getGraphDataFromTables() is a function in the supabase codebase.
What does getGraphDataFromTables() call?
getGraphDataFromTables() calls 3 function(s): findTablesHandleIds, getLayoutedElementsViaDagre, getLayoutedElementsViaLocalStorage.
What calls getGraphDataFromTables()?
getGraphDataFromTables() is called by 2 function(s): SchemaGraph, SchemaVisualizer.

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free