Connection String Parser
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
-
URIError: URI malformed
cause The connection string or one of its components contains invalid URI sequences, likely due to improper or missing `encodeURIComponent` calls before parsing, or it's simply malformed.fixManually apply `encodeURIComponent()` to each non-literal component (username, password, database, options values) of your connection string before passing the complete string to the parser. Ensure the overall string follows the expected URI format. -
TypeError: Cannot read properties of undefined (reading 'property')
cause Attempting to access properties of the parsed connection object when the parsing failed (e.g., due to a scheme mismatch) and the object is `null`, `undefined`, or incomplete.fixVerify that the `ConnectionStringParser` instance was configured with the correct `scheme` matching the input string. Always check the returned object for existence and expected structure before accessing its properties.
Warnings
- gotcha All components of an input connection string (username, password, host, database, options values) *must be manually URI-encoded* before being passed to the `parse` method. The library automatically decodes them, but will not encode them on input, leading to incorrect parsing if not pre-encoded.
- gotcha The `ConnectionStringParser` instance is initialized with a specific `scheme` (e.g., `{ scheme: 'mongodb' }`). If the input connection string's scheme does not match this configured scheme, the parser may return an empty or incomplete connection object without throwing an error, leading to silent failures.
- gotcha While the library is described as both a parser and formatter, the provided documentation excerpt primarily focuses on parsing. Users attempting to convert a connection object back to a string might need to consult the full API documentation for the `format` method, which is not directly demonstrated in the quickstart.
Install
-
npm install connection-string-parser -
yarn add connection-string-parser -
pnpm add connection-string-parser
Imports
- ConnectionStringParser
const ConnectionStringParser = require('connection-string-parser');import { ConnectionStringParser } from 'connection-string-parser'; - ConnectionStringParserOptions
import { ConnectionStringParserOptions } from 'connection-string-parser';import type { ConnectionStringParserOptions } from 'connection-string-parser'; - IConnectionString
import { IConnectionString } from 'connection-string-parser';import type { IConnectionString } from 'connection-string-parser';
Quickstart
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@' }
// }