🚀 DAY 5 OF LAUNCH WEEK: Introducing Socket Firewall Enterprise.Learn more →
Socket
Book a DemoInstallSign in
Socket

mongo-client-connect

Package Overview
Dependencies
Maintainers
1
Versions
7
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

mongo-client-connect

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

latest
Source
npmnpm
Version
1.0.6
Version published
Weekly downloads
2
-94.44%
Maintainers
1
Weekly downloads
 
Created
Source

Mongo Client Connect

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.

Installation

npm install mongo-client-connect

Usage

OBS: Examples are created for Nodejs v6+ (Destructuring!).

Connect to a MongoDB

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

Connect to many MongoDBs

A simple connection to N MongoDBs

MongoClientConnect([
	'mongodb://localhost/dbName1',
	'mongodb://anotherHost/dbName2'
]).then(([ db1, db2 ]) => {
	console.log(db1.databaseName);
	console.log(db2.databaseName);
});

Connect and gets Collections

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));
});

Connect to many DBs and get Collections

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);
})

Connect and gets Collections (by 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))
	});

Connect to many DBs and get Collections (by 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);
});

Keywords

mongodb

FAQs

Package last updated on 03 Sep 2016

Did you know?

Socket

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.

Install

Related posts