* Returns `schema` with `columns` sorted by `metadata.columnOrder` (the user- * editable visible order). Columns missing from `columnOrder` are appended at * the end in their original (schema-creation) order — covers tables created * before `columnOrder` existed and any drift from out-of-band col
( schema: TableSchema, metadata: TableMetadata | null )
| 86 | * consumer (grid, sidebar, copilot, mothership) gets the same ordered list. |
| 87 | */ |
| 88 | function applyColumnOrderToSchema( |
| 89 | schema: TableSchema, |
| 90 | metadata: TableMetadata | null |
| 91 | ): TableSchema { |
| 92 | const order = metadata?.columnOrder |
| 93 | if (!order || order.length === 0) return schema |
| 94 | // `columnOrder` holds stable column ids (legacy entries equal the name == id). |
| 95 | const byId = new Map<string, TableSchema['columns'][number]>() |
| 96 | for (const c of schema.columns) byId.set(getColumnId(c), c) |
| 97 | const ordered: TableSchema['columns'] = [] |
| 98 | for (const id of order) { |
| 99 | const c = byId.get(id) |
| 100 | if (c) { |
| 101 | ordered.push(c) |
| 102 | byId.delete(id) |
| 103 | } |
| 104 | } |
| 105 | for (const c of byId.values()) ordered.push(c) |
| 106 | return { ...schema, columns: ordered } |
| 107 | } |
| 108 | |
| 109 | /** |
| 110 | * Gets a table by ID with full details. |
no test coverage detected