mongo-singleton
A singleton instance for MongoDB with fallbacks for unspecified Mongo URL (great for testing)
Last updated 2 years ago by jmargaglione .
ISC · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install mongo-singleton 
SYNC missed versions from official npm registry.

mongo-singleton

A singleton instance for MongoDB with fallbacks for unspecified Mongo URL (great for testing)

Provides a fairly simple way to manage multiple database connections in one spot, essentially pooling them.

The implementation uses Promises, and you will need ES6 support (this was written for NodeJS, so that should not be an issue).

There are a couple of ways you can specify the URL to connect to:

  • Just send the URL over as a parameter when you require the module:
(MongoSingleton)('mongodb://localhost:27017/test')
				.then(([db, url]) => {
					// The url gives the URL that was connected to, in case you are 
          // using an environment variable and want to log it
          console.log('Connected to ' + url);
          
          db.doSomethingInteresting();
				})
				.catch(err => {
					console.error('Dammit');
				});
  • Pass nothing, and let it pick up the MongoDB URL from the environment (process.env.MONGO_URL):
(MongoSingleton)()
				.then(([db, url]) => {
          console.log('Connected to ' + url);
          
          db.doSomethingInteresting();
				})
				.catch(err => {
					console.error('Dammit');
				});

Bear in mind that this is implemented using promises, so you might want to add an await in front of the call if you intend to put any code afterwards that requires the database connection.

await (MongoSingleton)()
  .then(([db,url]) => {
    ...
  });
  .catch(err => {
    ...
  });

Current Tags

  • 1.0.0                                ...           latest (2 years ago)

1 Versions

  • 1.0.0                                ...           2 years ago
Maintainers (1)
Downloads
Today 0
This Week 0
This Month 1
Last Day 0
Last Week 1
Last Month 0
Dependencies (1)
Dev Dependencies (2)
Dependents (0)
None

Copyright 2014 - 2016 © taobao.org |