Registry / database / ts-parse-database-url

ts-parse-database-url

JSON →
library1.0.3jsnpmunverified

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-url
INSTALL
IMPORT
SIG · TS-PARSE-DATABASE-
T
ts-parse-database-url
databasejavascriptv1.0.3
Install
—
Import
—
Disk
—
Pass rate
0/ 6
Env Coverage0 / 6
glibc
18–22
musl
18–22
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18–226 runs
build_error
glibc
node 18–226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

parseDatabaseUrl
✓ import parseDatabaseUrl from 'ts-parse-database-url';
✗ const parseDatabaseUrl = require('ts-parse-database-url');
The package is primarily designed for ESM consumption, though CommonJS usage is supported by most bundlers and Node.js versions.
ParseResult
✓ import type { ParseResult } from 'ts-parse-database-url';
Import the `ParseResult` type for explicit type annotation of the object returned by `parseDatabaseUrl`.

Demonstrates parsing both MySQL and PostgreSQL database URLs, showing how to extract individual connection parameters into a structured object.

import parseDatabaseUrl from 'ts-parse-database-url'; // Example with a common MySQL URL const mysqlUrl = 'mysql://someuser:password@server.heroku.com:1337/herokudb'; const parsedMysql = parseDatabaseUrl(mysqlUrl); console.log('Parsed MySQL URL:', parsedMysql); /* Expected output: { driver: 'mysql', user: 'someuser', password: 'password', host: 'server.heroku.com', port: '1337', database: 'herokudb' }*/ // Example with a PostgreSQL URL (often from process.env) const postgresUrl = process.env.DATABASE_URL ?? 'postgresql://admin:secret@localhost:5432/my_app_db'; const parsedPostgres = parseDatabaseUrl(postgresUrl); console.log('Parsed PostgreSQL URL:', parsedPostgres); /* Expected output (example): { driver: 'postgresql', user: 'admin', password: 'secret', host: 'localhost', port: '5432', database: 'my_app_db' }*/
Debug
Known issues
gotchaThe package supports a limited set of common database drivers (e.g., MySQL, PostgreSQL). Attempting to parse URLs for unsupported or highly specialized drivers may lead to incorrect or incomplete parsing results.
fix
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.
affects: >=1.0.0
gotchaMalformed or non-standard database URLs can result in partial parsing, missing properties (e.g., `null` or `undefined`), or unexpected values in the parsed output object. The parser relies on standard URI components.
fix
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.
affects: >=1.0.0
gotchaWhen a port is not explicitly specified in the database URL, the parser will *not* automatically infer a default port based on the driver. The `port` property will be `undefined` or `null`.
fix
If default ports are required, implement a fallback mechanism in your application code, e.g., `parsed.port ?? getDefaultPort(parsed.driver)`.
affects: >=1.0.0
Errors
Common errors & fixes
TypeError: Cannot read properties of undefined (reading 'driver')
The input database URL was malformed or `null`/`undefined`, causing the `parseDatabaseUrl` function to return an unexpected value (e.g., `null`) which then had properties accessed.
fix
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);`
Property 'password' does not exist on type 'ParseResult'.
This TypeScript error typically occurs when the database URL did not include a password component, and you're trying to access `parsed.password` without checking for its existence, or if the `ParseResult` type doesn't fully account for optional properties.
fix
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.
Upgrade
Version history
1.0.3latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
15 hits · last 30 days
node
12
OpenAI (training)
1
Resources
ts-parse-database-url — npm install ts-parse-database-url · libregistry