function merge()

in packages/graph-explorer/src/core/StateProvider/schema.ts [98:146]


function merge<T extends VertexTypeConfig | EdgeTypeConfig>(
  existing: T[],
  newConfigs: T[]
): T[] {
  // Update existing nodes with new attributes
  const updated = existing.map(config => {
    const newConfig = newConfigs.find(vt => vt.type === config.type);

    // No attributes to update
    if (!newConfig) {
      return config;
    }

    // Find missing attributes
    const existingAttributes = new Set(
      config.attributes.map(attr => attr.name)
    );
    const missingAttributes = newConfig.attributes.filter(
      newAttr => !existingAttributes.has(newAttr.name)
    );

    if (missingAttributes.length === 0) {
      return config;
    }

    logger.debug(
      `Adding missing attributes to ${config.type}:`,
      missingAttributes
    );

    return {
      ...config,
      attributes: [...config.attributes, ...missingAttributes],
    };
  });

  // Find missing vertex type configs
  const existingTypes = new Set(updated.map(vt => vt.type));
  const missing = newConfigs.filter(vt => !existingTypes.has(vt.type));

  if (missing.length === 0) {
    return updated;
  }

  logger.debug(`Adding missing types:`, missing);

  // Combine all together
  return [...updated, ...missing];
}