-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathconnection-props.ts
More file actions
41 lines (38 loc) · 1.37 KB
/
Copy pathconnection-props.ts
File metadata and controls
41 lines (38 loc) · 1.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import { DatabaseConnection } from '../types.js';
/**
* Read a connection property, looking through both levels the workspace uses.
*
* The JSON workspace format keeps engine/driver properties nested under a
* `properties` key inside the connection configuration, while the legacy XML
* format keeps everything flat. Nested values win, since that is where
* driver-specific configuration lives.
*
* Names are matched case-insensitively because casing varies by driver
* (Postgres uses `sslmode`, MySQL uses `sslMode`). Nested objects are skipped
* so a container never masks a scalar of the same name.
*/
export function readConnectionProp(
connection: DatabaseConnection,
...names: string[]
): string | undefined {
const props = (connection.properties ?? {}) as Record<string, unknown>;
const nested = (props['properties'] as Record<string, unknown> | undefined) ?? {};
for (const name of names) {
const wanted = name.toLowerCase();
for (const source of [nested, props]) {
const key = Object.keys(source).find((k) => k.toLowerCase() === wanted);
if (key === undefined) {
continue;
}
const value = source[key];
if (value === undefined || value === null || typeof value === 'object') {
continue;
}
const str = String(value);
if (str.length > 0) {
return str;
}
}
}
return undefined;
}