()
| 53 | * Initialize and start the DBHub server |
| 54 | */ |
| 55 | export async function main(): Promise<void> { |
| 56 | try { |
| 57 | // Resolve source configurations from TOML or fallback to single DSN |
| 58 | const sourceConfigsData = await resolveSourceConfigs(); |
| 59 | |
| 60 | if (!sourceConfigsData) { |
| 61 | const samples = ConnectorRegistry.getAllSampleDSNs(); |
| 62 | const sampleFormats = Object.entries(samples) |
| 63 | .map(([id, dsn]) => ` - ${id}: ${dsn}`) |
| 64 | .join("\n"); |
| 65 | |
| 66 | console.error(` |
| 67 | ERROR: Database connection configuration is required. |
| 68 | Please provide configuration in one of these ways (in order of priority): |
| 69 | |
| 70 | 1. Use demo mode: --demo (uses in-memory SQLite with sample employee database) |
| 71 | 2. TOML config file: --config=path/to/dbhub.toml or ./dbhub.toml |
| 72 | 3. Command line argument: --dsn="your-connection-string" |
| 73 | 4. Environment variable: export DSN="your-connection-string" |
| 74 | 5. .env file: DSN=your-connection-string |
| 75 | |
| 76 | Example DSN formats: |
| 77 | ${sampleFormats} |
| 78 | |
| 79 | Example TOML config (dbhub.toml): |
| 80 | [[sources]] |
| 81 | id = "my_db" |
| 82 | dsn = "postgres://user:pass@localhost:5432/dbname" |
| 83 | |
| 84 | See documentation for more details on configuring database connections. |
| 85 | `); |
| 86 | process.exit(1); |
| 87 | } |
| 88 | |
| 89 | // Create connector manager and connect to database(s) |
| 90 | const connectorManager = new ConnectorManager(); |
| 91 | const sources = sourceConfigsData.sources; |
| 92 | |
| 93 | console.error(`Configuration source: ${sourceConfigsData.source}`); |
| 94 | |
| 95 | // Connect to database(s) - works uniformly for all modes (demo, single DSN, multi-source TOML) |
| 96 | await connectorManager.connectWithSources(sources); |
| 97 | |
| 98 | // Initialize tool registry (manages both built-in and custom tools) |
| 99 | // This must happen AFTER ConnectorManager is initialized so source validation works |
| 100 | const { initializeToolRegistry } = await import("./tools/registry.js"); |
| 101 | initializeToolRegistry({ |
| 102 | sources: sourceConfigsData.sources, |
| 103 | tools: sourceConfigsData.tools, |
| 104 | }); |
| 105 | console.error("Tool registry initialized"); |
| 106 | |
| 107 | // Start watching TOML config file for hot reload (only when using TOML config). |
| 108 | // In STDIO mode, tool list is registered once — hot reload updates connections and |
| 109 | // tool registry, but STDIO clients won't see added/removed tools without restart. |
| 110 | // HTTP transport creates a new server per request, so tool changes apply immediately. |
| 111 | const stopConfigWatcher = startConfigWatcher({ |
| 112 | connectorManager, |
no test coverage detected