ts-parse-database-url is a lightweight TypeScript-first utility designed to parse database connection URLs into their distinct components, such as driver, user, host, port, and database name. It provides a structured object output, simplifying the extraction and utilization of individual connection parameters. As of version 1.0.3, the package is stable and maintains a focused scope, primarily supporting common database URL formats. Due to its specific utility nature, major releases are infrequent, with updates generally consisting of bug fixes or enhancements to driver support. Its key differentiator is its native TypeScript support, which ensures type safety for the parsed output, making it highly suitable for modern TypeScript applications that retrieve database configurations from environment variables or other dynamic sources. The library draws inspiration from `node-parse-database-url` but is rebuilt with an emphasis on modern TypeScript practices and explicit type definitions.
npm install ts-parse-database-urlVerified import paths — ran on the pinned version, not inferred.
Demonstrates parsing both MySQL and PostgreSQL database URLs, showing how to extract individual connection parameters into a structured object.
Always verify that your database URL format and driver type are supported. Consult the package's test suite or source code for the most accurate list of currently supported drivers and their expected URL structures.
Ensure that all database URLs strictly adhere to standard URI syntax (e.g., `driver://user:pass@host:port/db`) and include all expected components. Validate input strings before passing them to the parser.
If default ports are required, implement a fallback mechanism in your application code, e.g., `parsed.port ?? getDefaultPort(parsed.driver)`.
Ensure the input to `parseDatabaseUrl` is a valid string. Add a check for the parser's return value before accessing its properties, e.g., `const parsed = parseDatabaseUrl(url); if (!parsed) { throw new Error('Invalid URL'); } console.log(parsed.driver);`Access optional properties with caution using optional chaining (`parsed?.password`) or by explicitly checking for their presence ( `if (parsed.password) { ... }`). The `ParseResult` type should correctly mark `password` as optional.No dependency data recorded yet.