Connection String Parser

1.0.4 · active · verified Tue Apr 21

The `connection-string-parser` library provides a generic, cross-environment solution for converting URI-based connection strings into structured JSON objects and vice-versa. It supports both Node.js and browser environments, addressing a common problem where applications need to consume compact URI strings (e.g., `mongodb://user:pass@host/db?options`) but underlying connection tools require a JSON configuration object. The current stable version is 1.0.4. The library is written in TypeScript and ships with type definitions. It differentiates itself by offering a flexible, scheme-agnostic parsing mechanism, allowing developers to define custom URI patterns beyond standard database connection strings, making it suitable for various project-specific configurations. Its release cadence appears to be stable, focusing on robust utility.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates how to initialize the parser with a scheme and parse a URI-encoded connection string into a structured connection object, showing key components like scheme, hosts, credentials, and options.

import { ConnectionStringParser, IConnectionString } from "connection-string-parser";

const connectionStringParser = new ConnectionStringParser({
	scheme: "mongodb",
	hosts: []
});

const connectionString = "mongodb://s%23perus%24r:unbr%23k%40bl%24@ho%24t:1234/%24my-db?replicaSet=%24super%40";
const connectionObject: IConnectionString = connectionStringParser.parse(connectionString);

console.log("Parsed Connection Object:", connectionObject);
// Expected output:
// {
//   scheme: 'mongodb',
//   hosts: [ { host: 'ho$t', port: 1234 } ],
//   username: 's#perus$r',
//   password: 'unbr#k@bl$',
//   database: '$my-db',
//   options: { replicaSet: '$super@' }
// }

view raw JSON →