MCPcopy
hub / github.com/mayneyao/eidos / findDependentFormulaFields

Function findDependentFormulaFields

packages/core/sqlite/sql-formula-parser.ts:305–354  ·  view source on GitHub ↗
(
  columnName: string,
  fields: IField[]
)

Source from the content-addressed store, hash-verified

303 * @returns Array of dependent field names
304 */
305export const findDependentFormulaFields = (
306 columnName: string,
307 fields: IField[]
308) => {
309 // Get the field name from column name for easier reference
310 const targetField = fields.find((f) => f.table_column_name === columnName)
311 if (!targetField) return []
312
313 // Build dependency graph
314 const { dependencyGraph } = detectCircularDependencies(fields)
315
316 // Create reverse dependency map
317 const reverseDependencies: Record<string, string[]> = {}
318 Object.entries(dependencyGraph).forEach(([field, dependencies]) => {
319 dependencies.forEach((dep) => {
320 if (!reverseDependencies[dep]) {
321 reverseDependencies[dep] = []
322 }
323 reverseDependencies[dep].push(field)
324 })
325 })
326
327 // Find all fields that depend on the target column (directly or indirectly)
328 const dependentFields: string[] = []
329 const visited: Record<string, boolean> = {}
330
331 const findAllDependents = (col: string) => {
332 if (visited[col]) return
333 visited[col] = true
334
335 const directDependents = reverseDependencies[col] || []
336 directDependents.forEach((dep) => {
337 dependentFields.push(dep)
338 findAllDependents(dep)
339 })
340 }
341
342 findAllDependents(columnName)
343
344 // Map column names back to field names for better readability
345 const reverseFieldNameMap: Record<string, string> = {}
346 fields.forEach((field) => {
347 reverseFieldNameMap[field.table_column_name] = field.name
348 })
349
350 return dependentFields.map((col) => ({
351 columnName: col,
352 fieldName: reverseFieldNameMap[col] || col,
353 }))
354}
355
356/**
357 * Gets the order in which formula fields should be deleted to respect dependencies

Callers 1

updateFormulaColumnMethod · 0.90

Calls 5

findAllDependentsFunction · 0.85
findMethod · 0.80
entriesMethod · 0.65
pushMethod · 0.45

Tested by

no test coverage detected