|
| 1 | +/** |
| 2 | + * Fetch Node Type Registry |
| 3 | + * |
| 4 | + * Queries the node_type_registry table from the database at schema build time. |
| 5 | + * Used to populate the BlueprintTypesPlugin with real node type entries |
| 6 | + * so that @oneOf types reflect the actual registered node types. |
| 7 | + * |
| 8 | + * The query uses the metaschema_public schema and selects all columns |
| 9 | + * needed by the plugin: name, slug, category, display_name, description, |
| 10 | + * parameter_schema, and tags. |
| 11 | + */ |
| 12 | + |
| 13 | +import { Pool } from 'pg'; |
| 14 | +import type { NodeTypeRegistryEntry } from './plugin'; |
| 15 | + |
| 16 | +/** |
| 17 | + * Fetch all node_type_registry entries from the database. |
| 18 | + * |
| 19 | + * Connects using the provided connection string, queries |
| 20 | + * metaschema_public.node_type_registry, and returns the rows |
| 21 | + * as NodeTypeRegistryEntry[]. |
| 22 | + * |
| 23 | + * If the table doesn't exist or the query fails (e.g., the database |
| 24 | + * hasn't been migrated yet), returns an empty array and logs a warning. |
| 25 | + * |
| 26 | + * @param connectionString - PostgreSQL connection string |
| 27 | + * @returns Array of node type registry entries |
| 28 | + */ |
| 29 | +export async function fetchNodeTypeRegistry( |
| 30 | + connectionString: string, |
| 31 | +): Promise<NodeTypeRegistryEntry[]> { |
| 32 | + const pool = new Pool({ connectionString, max: 1 }); |
| 33 | + |
| 34 | + try { |
| 35 | + const result = await pool.query<NodeTypeRegistryEntry>(` |
| 36 | + SELECT |
| 37 | + name, |
| 38 | + slug, |
| 39 | + category, |
| 40 | + COALESCE(display_name, name) as display_name, |
| 41 | + COALESCE(description, '') as description, |
| 42 | + COALESCE(parameter_schema, '{}'::jsonb) as parameter_schema, |
| 43 | + COALESCE(tags, '{}') as tags |
| 44 | + FROM metaschema_public.node_type_registry |
| 45 | + ORDER BY category, name |
| 46 | + `); |
| 47 | + |
| 48 | + return result.rows; |
| 49 | + } catch (error: unknown) { |
| 50 | + // If the table doesn't exist yet (not migrated), return empty gracefully |
| 51 | + const pgError = error as { code?: string; message?: string }; |
| 52 | + if (pgError.code === '42P01') { |
| 53 | + // 42P01 = undefined_table |
| 54 | + // This is expected when the database hasn't been migrated with the metaschema package |
| 55 | + return []; |
| 56 | + } |
| 57 | + |
| 58 | + // For other errors, log a warning and return empty |
| 59 | + // This ensures the schema build doesn't fail just because |
| 60 | + // node_type_registry isn't accessible |
| 61 | + console.warn( |
| 62 | + '[BlueprintTypesPlugin] Failed to fetch node_type_registry:', |
| 63 | + pgError.message ?? String(error), |
| 64 | + ); |
| 65 | + return []; |
| 66 | + } finally { |
| 67 | + await pool.end(); |
| 68 | + } |
| 69 | +} |
0 commit comments