Skip to content

Commit 0412761

Browse files
authored
fix: infer database type from DSN protocol in API sources endpoint (#133)
When using DSN-based configuration in TOML files without explicit 'type' fields, the /api/sources endpoint was failing with "missing required type field" errors. This fix allows the endpoint to work correctly with DSN-only configurations by extracting the database type from the protocol portion of the DSN string. Changes: - Add getDatabaseTypeFromDSN() utility function to dsn-obfuscate.ts - Use centralized type inference in transformSourceConfig() - Handle both 'postgres' and 'postgresql' protocols as PostgreSQL - Maintain backward compatibility with explicit type configurations Fixes API endpoint failure for DSN-only TOML configurations.
1 parent 86d6fe2 commit 0412761

File tree

2 files changed

+33
-2
lines changed

2 files changed

+33
-2
lines changed

src/api/sources.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { Request, Response } from "express";
22
import { ConnectorManager } from "../connectors/manager.js";
3+
import { getDatabaseTypeFromDSN } from "../utils/dsn-obfuscate.js";
34
import type { SourceConfig } from "../types/config.js";
45
import type { components } from "./openapi.js";
56

@@ -15,8 +16,14 @@ function transformSourceConfig(
1516
source: SourceConfig,
1617
isDefault: boolean
1718
): DataSource {
18-
// Type is guaranteed to be present for connected sources
19-
// It's set during connection either from DSN parsing or explicit config
19+
// Determine type from explicit config or infer from DSN
20+
if (!source.type && source.dsn) {
21+
const inferredType = getDatabaseTypeFromDSN(source.dsn);
22+
if (inferredType) {
23+
source.type = inferredType;
24+
}
25+
}
26+
2027
if (!source.type) {
2128
throw new Error(`Source ${source.id} is missing required type field`);
2229
}

src/utils/dsn-obfuscate.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import type { SSHTunnelConfig } from '../types/ssh.js';
2+
import type { ConnectorType } from '../connectors/interface.js';
23

34
/**
45
* Obfuscates the password in a DSN string for logging purposes
@@ -83,4 +84,27 @@ export function obfuscateSSHConfig(config: SSHTunnelConfig): Partial<SSHTunnelCo
8384
}
8485

8586
return obfuscated;
87+
}
88+
89+
/**
90+
* Extracts the database type from a DSN string
91+
* @param dsn The DSN string to analyze
92+
* @returns The database type or undefined if cannot be determined
93+
*/
94+
export function getDatabaseTypeFromDSN(dsn: string): ConnectorType | undefined {
95+
if (!dsn) {
96+
return undefined;
97+
}
98+
99+
const protocol = dsn.split(':')[0];
100+
const protocolToType: Record<string, ConnectorType> = {
101+
'postgres': 'postgres',
102+
'postgresql': 'postgres',
103+
'mysql': 'mysql',
104+
'mariadb': 'mariadb',
105+
'sqlserver': 'sqlserver',
106+
'sqlite': 'sqlite'
107+
};
108+
109+
return protocolToType[protocol];
86110
}

0 commit comments

Comments
 (0)