Parse Database URIs

2.1.2 · active · verified Wed Apr 22

parse-db-uri is a utility library designed to parse database connection URIs into a structured JavaScript object. It extends the output of the 'parse-url' package with database-specific fields such as `database`, `user`, `host`, `password`, and `dialect`. The library is currently stable at version 2.1.2, with recent updates including automatic decoding of passwords and usernames in the URI and added support for SQLite URIs. It provides a straightforward, function-based API for extracting components from common database URI formats, making it easier to configure database connections in applications. The release cadence is irregular but has seen active maintenance recently within the 2.x major version.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates parsing MySQL, SQLite, and PostgreSQL database URIs into structured objects, including database-specific fields and environment variable usage for sensitive credentials.

const parseDbUri = require("parse-db-uri");

const mysqlUri = "mysql://root:secretpass@localhost:3306/my_database";
const sqliteUri = "sqlite://data/db.sqlite3";
const postgresUri = `postgres://${process.env.DB_USER ?? 'admin'}:${process.env.DB_PASSWORD ?? 'password'}@db.example.com:5432/my_app_db?sslmode=require`;

console.log("MySQL URI parsed:", parseDbUri(mysqlUri));
// Expected output for mysqlUri:
// { protocols: [ 'mysql' ], protocol: 'mysql', port: 3306, resource: 'localhost', host: 'localhost', user: 'root', password: 'secretpass', pathname: '/my_database', hash: '', search: '', href: 'mysql://root:secretpass@localhost:3306/my_database', query: {}, parse_failed: false, uri: 'mysql://root:secretpass@localhost:3306/my_database', database: 'my_database', dialect: 'mysql' }

console.log("SQLite URI parsed:", parseDbUri(sqliteUri));
// Expected output for sqliteUri:
// { protocols: [ 'sqlite' ], protocol: 'sqlite', port: 0, resource: 'data', host: 'data', user: '', password: '', pathname: 'data/db.sqlite3', hash: '', search: '', href: 'sqlite://data/db.sqlite3', query: {}, parse_failed: false, uri: 'sqlite://data/db.sqlite3', dialect: 'sqlite' }

console.log("PostgreSQL URI parsed:", parseDbUri(postgresUri));

view raw JSON →