Registry / database / mongodb-connection-model

mongodb-connection-model

JSON →
library22.4.1jsnpmunverified

A JavaScript library for modeling MongoDB connection URIs and options. Version 22.4.1 provides a structured way to parse, validate, and serialize connection strings for the MongoDB Node.js driver. Maintained as part of the MongoDB Compass project, it aligns with driver version 5.x and validates options like TLS, authentication, and read preferences. The model converts between URI strings and objects, offering detailed validation error messages. It is released sporadically alongside Compass releases, with a focus on correctness and compatibility with the official driver.

npm install mongodb-connection-model
INSTALL
IMPORT
SIG · MONGODB-CONNECTION
M
mongodb-connection-model
databasejavascriptv22.4.1
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.

ConnectionModel
import { ConnectionModel } from 'mongodb-connection-model';
import ConnectionModel from 'mongodb-connection-model'; or require('mongodb-connection-model')
ESM only; named export in all versions.
parseConnectionModel
import { parseConnectionModel } from 'mongodb-connection-model';
Utility function to parse a URI into a ConnectionModel object.
toConnectionString
import { toConnectionString } from 'mongodb-connection-model';
Converts a ConnectionModel instance or plain object to a MongoDB URI string.

Parses a MongoDB URI into a ConnectionModel, modifies options, validates, and serializes back to a URI.

import { ConnectionModel, parseConnectionModel, toConnectionString } from 'mongodb-connection-model'; // Parse a MongoDB URI const uri = 'mongodb://user:pass@localhost:27017/db?authSource=admin&ssl=true'; const model = parseConnectionModel(uri); // Modify options model.ssl = false; model.auth.user = 'newuser'; // Validate const validation = model.validate(); if (validation.isValid) { const newUri = toConnectionString(model); console.log('Generated URI:', newUri); } else { console.error('Validation errors:', validation.errors); } // Create from object const customModel = new ConnectionModel({ hostname: 'cluster0.example.mongodb.net', port: 27017, ssl: true, auth: { user: 'admin', password: 'password' }, ns: 'test', mongodbUsername: 'admin', mongodbPassword: 'password', mongodbDatabaseName: 'admin', extraOptions: { authSource: 'admin' } }); console.log(toConnectionString(customModel));
Debug
Known issues
breakingESM-only: require() will not work. Must use import or dynamic import().
fix
Use import syntax or switch to CommonJS version (21.x or earlier).
affects: >=22.0.0
breakingMongoDB driver peer dependency changed to ^5.5.0. Incompatible with driver v4.
fix
Update mongodb to ^5.5.0 or later.
affects: >=22.0.0
deprecatedThe property 'mongodbUsername' and 'mongodbPassword' are replaced by 'auth.user' and 'auth.password'.
fix
Use model.auth = { user: '...', password: '...' } instead.
affects: >=22.0.0
gotchaWhen parsing a URI, the model may convert certain options to internal formats; always validate after modification.
fix
Call model.validate() before toConnectionString() to ensure consistency.
affects: >=0.0.0
gotchaThe 'ns' property (namespace) is not validated — ensure it is in 'database.collection' format.
fix
Manually verify ns format or use dedicated validation.
affects: >=0.0.0
Errors
Common errors & fixes
TypeError: ConnectionModel is not a constructor
Using require() or wrong import style (default import instead of named).
fix
Use import { ConnectionModel } from 'mongodb-connection-model';
Error: Cannot find module 'mongodb'
Missing peer dependency mongodb.
fix
Run npm install mongodb@^5.5.0
Upgrade
Version history
22.4.1latest on npm
Audit
Dependencies
mongodbrequiredPeer dependency for type compatibility with MongoDB Node.js driver v5
Agent activity
8 hits · last 30 days
node
8
Resources
mongodb-connection-model — npm install mongodb-connection-model · libregistry