Parse Database URIs
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
-
TypeError: parseDbUri is not a function
cause This error typically occurs when the `parseDbUri` function is not correctly imported or referenced, often due to an incorrect import syntax for a default export (e.g., using named destructuring for a CommonJS default).fixEnsure the import statement is `import parseDbUri from 'parse-db-uri';` for ESM, or `const parseDbUri = require('parse-db-uri');` for CommonJS. -
ReferenceError: parseDbUri is not defined
cause The variable `parseDbUri` was used before it was declared or properly imported into the current scope, or there's a typo in the variable name.fixVerify that `import parseDbUri from 'parse-db-uri';` or `const parseDbUri = require('parse-db-uri');` is present at the top of your module and that `parseDbUri` is spelled correctly wherever it's used.
Warnings
- breaking Version 2.0.0 introduced a major version bump, implying potential breaking changes. While specific details were not provided in the changelog, users upgrading from 1.x should exercise caution and thoroughly test their implementations, as underlying parsing logic or result object structure may have changed.
- gotcha The package is primarily a CommonJS module that exports a single function as its default. In modern ESM environments, attempting to use named imports (`import { parseDbUri } from 'parse-db-uri';`) will fail, returning `undefined` for `parseDbUri`.
- gotcha As of version 2.1.2, the library automatically decodes URL-encoded characters within usernames and passwords. While this is generally desired for usability, developers should be aware of this behavior, especially if their application relies on the raw, encoded form of these URI components.
Install
-
npm install parse-db-uri -
yarn add parse-db-uri -
pnpm add parse-db-uri
Imports
- parseDbUri
import { parseDbUri } from 'parse-db-uri';import parseDbUri from 'parse-db-uri';
- parseDbUri
const { parseDbUri } = require('parse-db-uri');const parseDbUri = require('parse-db-uri');
Quickstart
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));