Database URL Parser

1.0.3 · active · verified Tue Apr 21

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.

Common errors

Warnings

Install

Imports

Quickstart

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'
}*/

view raw JSON →