MCPcopy Create free account
hub / github.com/bytebase/dbhub / buildDSNFromSource

Function buildDSNFromSource

src/config/toml-loader.ts:831–931  ·  view source on GitHub ↗
(source: SourceConfig)

Source from the content-addressed store, hash-verified

829 * Similar to buildDSNFromEnvParams in env.ts but for TOML sources
830 */
831export function buildDSNFromSource(source: SourceConfig): string {
832 // If DSN is already provided, use it — but merge in dual-home fields
833 // (sslmode/sslrootcert/instanceName/authentication/domain) so they actually
834 // affect the connection. Conflicts between the DSN query string and these
835 // fields are rejected at validation time, so any param already present in the
836 // DSN is guaranteed to match.
837 if (source.dsn) {
838 return mergeSourceFieldsIntoDSN(source.dsn, source);
839 }
840
841 // Validate required fields
842 if (!source.type) {
843 throw new Error(
844 `Source '${source.id}': 'type' field is required when 'dsn' is not provided`
845 );
846 }
847
848 // Handle SQLite
849 if (source.type === "sqlite") {
850 if (!source.database) {
851 throw new Error(
852 `Source '${source.id}': 'database' field is required for SQLite`
853 );
854 }
855 return `sqlite:///${source.database}`;
856 }
857
858 // For other databases, require host, user, database
859 // Password is optional for Azure AD access token authentication and AWS IAM auth
860 const isAwsIamPasswordless =
861 source.aws_iam_auth === true &&
862 ["postgres", "mysql", "mariadb"].includes(source.type);
863 const passwordRequired =
864 source.authentication !== "azure-active-directory-access-token" &&
865 !isAwsIamPasswordless;
866 if (!source.host || !source.user || !source.database) {
867 throw new Error(
868 `Source '${source.id}': missing required connection parameters. ` +
869 `Required: type, host, user, database`
870 );
871 }
872 if (passwordRequired && !source.password) {
873 throw new Error(
874 `Source '${source.id}': password is required. ` +
875 `(Password is optional for azure-active-directory-access-token authentication ` +
876 `or when aws_iam_auth=true)`
877 );
878 }
879
880 // Determine default port if not specified
881 const port = source.port || getDefaultPortForType(source.type);
882
883 if (!port) {
884 throw new Error(`Source '${source.id}': unable to determine port`);
885 }
886
887 // Encode credentials
888 const encodedUser = encodeURIComponent(source.user);

Callers 3

registerLazySourceMethod · 0.85
buildConnectionDSNMethod · 0.85

Calls 3

mergeSourceFieldsIntoDSNFunction · 0.85
getDefaultPortForTypeFunction · 0.85
expandHomeDirFunction · 0.85

Tested by

no test coverage detected