
Security News
The Changelog Podcast: Practical Steps to Stay Safe on npm
Learn the essential steps every developer should take to stay secure on npm and reduce exposure to supply chain attacks.
mongo-client-connect
Advanced tools
It reuses the same connection objects (creating only one connection pool per connectionURI) and collection instances, avoiding the creation of many, unnecessary, connections pools to a same connectionURI. Also provides an easy way to specify N collections
Reuses the same connection objects (creating only one connection pool per connectionURI) and collection instances, avoiding the creation of many, unnecessary, connections pools to a same connectionURI.
It also provides an easy way to specify N collections from N connectionURIs to be retrieved at once, in one call (DRY!).
Mongo DB Native NodeJS Driver is the engine under the hood.
npm install mongo-client-connect
OBS: Examples are created for Nodejs v6+ (Destructuring!).
A simple connection to a MongoDB
MongoClientConnect('mongodb://localhost/dbName').then(db => {});
But in case you have more than one call in your project:
// Somewhere
MongoClientConnect('mongodb://localhost/dbName').then(db => {});
//[...]
// Somewhere else...
MongoClientConnect('mongodb://localhost/dbName').then(db => {});
by mistake, or intentionally because of by some file structure strategy, only one connection pool will be created
A simple connection to N MongoDBs
MongoClientConnect([
'mongodb://localhost/dbName1',
'mongodb://anotherHost/dbName2'
]).then(([ db1, db2 ]) => {
console.log(db1.databaseName);
console.log(db2.databaseName);
});
It will connect and return the collections that has been asked for.
MongoClientConnect('mongodb://localhost/dbName', [
'dbCollectionName1',
'dbCollectionName2'
])
.then( ([ dbCollectionName1, dbCollectionName2 ]) => {
// Example of something with one collection
dbCollectionName1.find({}).toArray().then(docs => console.log(docs.length));
// Example of something with the other collection
dbCollectionName2.find({}).toArray().then(docs => console.log(docs.length));
});
It connects to N MongoDBs and returns collections for each of them.
MongoClientConnect({
'mongodb://localhost/dbName': ['dbCollectionName1'],
'mongodb://someOtherHost/dbName': ['dbCollectionName2', 'dbCollectionName3']
})
.then(([[ dbCollectionName1 ], [ dbCollectionName2, dbCollectionName3 ]]) => {
console.log(dbCollectionName1.s.dbName, dbCollectionName2.s.dbName, dbCollectionName3.s.dbName);
})
Object)It will connect and return collections, the same as above, but the collections are specified by an Object, in case you prefer the response to be an Object. The collections can also be configured with an alias.
MongoClientConnect('mongodb://localhost/dbName', { someAlias: 'dbCollectionName' })
.then(({ someAlias }) => {
// 'someAlias' will be a reference to collection 'dbCollectionName'
someAlias.find({}).toArray().then(r => console.log(r.length))
});
Object)Same as above, it connects to N MongoDBs and returns collections for each of them, but defining collections as an Object in case you prefer the response to be an Object. The collections can also be configured with an alias.
MongoClientConnect({
'mongodb://localhost/dbName': {
alias1: 'dbCollectionName1'
},
'mongodb://someOtherHost/dbName': {
alias2: 'dbCollectionName2',
alias3: 'dbCollectionName3'
}
})
.then(([{ alias1 }, { alias2, alias3 }]) => {
console.log(alias1.s.name, alias2.s.name, alias3.s.name);
});
FAQs
It reuses the same connection objects (creating only one connection pool per connectionURI) and collection instances, avoiding the creation of many, unnecessary, connections pools to a same connectionURI. Also provides an easy way to specify N collections
The npm package mongo-client-connect receives a total of 2 weekly downloads. As such, mongo-client-connect popularity was classified as not popular.
We found that mongo-client-connect demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Security News
Learn the essential steps every developer should take to stay secure on npm and reduce exposure to supply chain attacks.

Security News
Experts push back on new claims about AI-driven ransomware, warning that hype and sponsored research are distorting how the threat is understood.

Security News
Ruby's creator Matz assumes control of RubyGems and Bundler repositories while former maintainers agree to step back and transfer all rights to end the dispute.