Registry / database / mongo-connection-string

mongo-connection-string

JSON →
library0.1.5jsnpmunverified

Utility library for parsing, building, and manipulating MongoDB connection strings. Current stable version is 0.1.5. It provides a ConnectionString class to decompose URIs into components (protocol, username, password, hosts, database, options) and serialize them back to URI or human-readable form. The library handles URL encoding/decoding, defaults protocol to mongodb://, and includes a heuristic for percent-encoded characters. It is actively maintained but rarely updated. Key differentiators: lightweight with TypeScript types, no dependencies, designed for both Node.js and browser, and provides a safe toString() that masks passwords.

npm install mongo-connection-string
INSTALL
IMPORT
SIG · MONGO-CONNECTION-S
M
mongo-connection-string
databasejavascriptv0.1.5
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

ConnectionString
import { ConnectionString } from 'mongo-connection-string'
import ConnectionString from 'mongo-connection-string'
TypeScript ships with named exports; default import is not available. CJS: const { ConnectionString } = require('mongo-connection-string')
parse
import { parse } from 'mongo-connection-string'
parse is undocumented in current README but may be available as an internal function. Prefer using 'new ConnectionString(input)' instead.
ConnectionStringOptions
import type { ConnectionStringOptions } from 'mongo-connection-string'
Available for TypeScript users if types are included. Check node_modules/mongo-connection-string/dist/index.d.ts for exact exports.

Parse a MongoDB connection string, access its components, and serialize back to URI or safe log string.

import { ConnectionString } from 'mongo-connection-string'; const cs = new ConnectionString('mongodb://user:password@host1:27017/mydb?w=majority'); console.log(cs.protocol); // mongodb:// console.log(cs.username); // user console.log(cs.password); // password console.log(cs.hosts); // [{ host: 'host1', port: 27017 }] console.log(cs.database); // mydb console.log(cs.options); // { w: 'majority' } console.log(cs.toURI()); // mongodb://user:password@host1:27017/mydb?w=majority console.log(cs.toString()); // mongodb://user:********/mydb?w=majority
Debug
Known issues
gotchaIf no protocol is specified, the library assumes mongodb://. This can be confusing if you pass a plain hostname like 'localhost'.
fix
Always include protocol, e.g., 'mongodb://localhost'.
affects: >=0.0.0
gotchaPercent-encoded strings in username/password are decoded; if decoding fails, the string is treated as plaintext with literal '%'.
fix
If your password contains '%' that is not part of percent-encoding, escape it properly or encode it manually.
affects: >=0.0.0
deprecatedThe parse function may be removed in future versions.
fix
Use 'new ConnectionString(input)' instead.
affects: >=0.1.0
gotchaThe ConnectionString constructor expects a string or object; passing an object with 'hosts' as array of objects works, but 'port' must be number or null.
fix
Ensure port is a number, not a string.
affects: >=0.0.0
Errors
Common errors & fixes
TypeError: ConnectionString is not a constructor
Using default import instead of named import.
fix
Use import { ConnectionString } from 'mongo-connection-string' or const { ConnectionString } = require('mongo-connection-string')
SyntaxError: Cannot find module 'mongo-connection-string'
Package not installed or project is not using Node >= 8.5.0.
fix
Run npm install mongo-connection-string and ensure Node version >= 8.5.0
Upgrade
Version history
0.1.5latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
4 hits · last 30 days
node
4
Resources
mongo-connection-string — npm install mongo-connection-string · libregistry