native-mongo-util
Advanced tools
Comparing version 2.3.2 to 3.0.0
22
index.js
@@ -8,3 +8,3 @@ const Log = require('logger3000').getLogger(__filename); | ||
const MONGO_URL = process.env.MONGO_URL || 'mongodb://localhost/test'; | ||
const { get } = require('./tunnel'); | ||
/** | ||
@@ -36,3 +36,3 @@ * Class to create native Mongo connection | ||
*/ | ||
this.getCollection = memoize(collectionName => { | ||
this.getCollection = memoize((collectionName) => { | ||
this.checkAndThrowDBNotConnectedError(); | ||
@@ -56,2 +56,3 @@ return this._db.collection(collectionName); | ||
* Connects to mongodb for the provided `mongoURL` for this connection instance | ||
* @param {Object} [options] - MongoDB connection options | ||
* @public | ||
@@ -61,3 +62,3 @@ * @returns {Promise<DB>} - Mongodb DB instance. | ||
*/ | ||
async connect() { | ||
async connect(options = {}) { | ||
if (this.isConnecting) { | ||
@@ -73,11 +74,10 @@ Log.debug({ msg: 'connecting to DB' }); | ||
loggerLevel: 'info', | ||
useNewUrlParser: true | ||
useNewUrlParser: true, | ||
}; | ||
this.isConnecting = true; | ||
this._client = await MongoClient.connect(this._mongoURL, { | ||
...this._options, | ||
...fixOpts | ||
}); | ||
await get(); | ||
this._client = await MongoClient.connect(this._mongoURL, { ...this._options, ...fixOpts, ...options }); | ||
this._db = this._client.db(); | ||
@@ -170,3 +170,3 @@ | ||
*/ | ||
exports.connect = options => { | ||
exports.connect = (options) => { | ||
if (_defaultConnection) { | ||
@@ -187,3 +187,3 @@ throw new Error(`Already connected to ${_defaultConnection.getDBName()}, Try \`newConnection()\` instead.`); | ||
*/ | ||
exports.close = async force => { | ||
exports.close = async (force) => { | ||
const result = await _defaultConnection.close(force); | ||
@@ -219,3 +219,3 @@ | ||
*/ | ||
exports.getCollection = collectionName => _defaultConnection.getCollection(collectionName); | ||
exports.getCollection = (collectionName) => _defaultConnection.getCollection(collectionName); | ||
@@ -222,0 +222,0 @@ /** |
{ | ||
"name": "native-mongo-util", | ||
"version": "2.3.2", | ||
"version": "3.0.0", | ||
"description": "", | ||
@@ -8,4 +8,4 @@ "main": "index.js", | ||
"test": "ava test.js", | ||
"lint": "eslint index.js", | ||
"format": "prettier --write 'index.js'" | ||
"lint": "eslint index.js tunnel.js", | ||
"format": "prettier --write '{index.js,tunnel.js}'" | ||
}, | ||
@@ -24,20 +24,21 @@ "repository": { | ||
"dependencies": { | ||
"bluebird": "^3.5.5", | ||
"bluebird": "^3.7.2", | ||
"tunnel-ssh": "^4.1.4", | ||
"logger3000": "0.1.1", | ||
"memoizee": "^0.4.14", | ||
"mongodb": "^3.2.7", | ||
"mongodb": "^3.5.7", | ||
"then-sleep": "^1.0.1" | ||
}, | ||
"devDependencies": { | ||
"ava": "^2.1.0", | ||
"babel-eslint": "^10.0.2", | ||
"eslint": "^6.0.1", | ||
"ava": "^3.6.0", | ||
"babel-eslint": "^10.1.0", | ||
"eslint": "^6.8.0", | ||
"eslint-config-lil": "^1.0.6", | ||
"eslint-config-prettier": "^6.0.0", | ||
"eslint-config-xo-space": "^0.21.0", | ||
"eslint-plugin-prettier": "^3.1.0", | ||
"eslint-config-prettier": "^6.10.1", | ||
"eslint-config-xo-space": "^0.24.0", | ||
"eslint-plugin-prettier": "^3.1.2", | ||
"event-stream": ">=4.0.1", | ||
"nodemon": "^1.19.1", | ||
"prettier": "^1.18.2" | ||
"nodemon": "^2.0.2", | ||
"prettier": "^2.0.4" | ||
} | ||
} |
# native-mongo-util | ||
Utility package to connect multiple mongo databases. | ||
Utility package to connect multiple mongo databases. Supports SSH tunneling. | ||
@@ -69,2 +69,10 @@ ## Usage | ||
## Environment Variables | ||
- `MONGO_URL` Mongo connection url | ||
- `SSH_HOST` ssh hostname or ip | ||
- `SSH_USER` ssh user for remote connection | ||
- `SSH_PASSWORD` ssh connection password | ||
- `SSH_KEY_PATH` ssh private key path like `~/.ssh/id_rsa`. Either of `SSH_PASSWORD` or `SSH_KEY_PATH` must be provided depending on remote server auth mode. | ||
[![Build Status](https://travis-ci.org/saggiyogesh/native-mongo-util.svg?branch=master)](https://travis-ci.org/saggiyogesh/native-mongo-util) |
13
test.js
process.env.MONGO_URL = 'mongodb://localhost/testDB'; | ||
process.env.NODE_ENV = 'production'; | ||
import test from 'ava'; | ||
const test = require('ava'); | ||
const { getDBName, connect, getCollection, newConnection, getClient } = require('./'); | ||
const opts = { poolSize: 20 }; | ||
test.serial('check connection with mongodb', async t => { | ||
test.serial('check connection with mongodb', async (t) => { | ||
await connect(opts); | ||
@@ -17,3 +18,3 @@ const dbName = getDBName(); | ||
test.serial('fetch all records from a collection', async t => { | ||
test.serial('fetch all records from a collection', async (t) => { | ||
const d = Date.now(); | ||
@@ -25,3 +26,3 @@ await getCollection('testCol').insert({ d }); | ||
test.serial('another mongo connection', async t => { | ||
test.serial('another mongo connection', async (t) => { | ||
const mongoURL = 'mongodb://localhost/someDB'; | ||
@@ -40,3 +41,3 @@ const connection = newConnection(mongoURL, opts); | ||
test.serial('validate options passed to mongo client for default connection', async t => { | ||
test.serial('validate options passed to mongo client for default connection', async (t) => { | ||
const client = await getClient(); | ||
@@ -46,5 +47,5 @@ t.is(opts.poolSize, client.s.options.poolSize); | ||
test.serial('validate options passed to mongo client for other connection', async t => { | ||
test.serial('validate options passed to mongo client for other connection', async (t) => { | ||
const client = await getClient(); | ||
t.is(opts.poolSize, client.s.options.poolSize); | ||
}); |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 2 instances in 1 package
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
13233
9
274
78
6
6
+ Addedtunnel-ssh@^4.1.4
+ Addedasn1@0.2.6(transitive)
+ Addedbcrypt-pbkdf@1.0.2(transitive)
+ Addedcpu-features@0.0.2(transitive)
+ Addeddebug@2.6.9(transitive)
+ Addedlodash.defaults@4.2.0(transitive)
+ Addedms@2.0.0(transitive)
+ Addednan@2.22.0(transitive)
+ Addedsafer-buffer@2.1.2(transitive)
+ Addedssh2@1.4.0(transitive)
+ Addedtunnel-ssh@4.1.6(transitive)
+ Addedtweetnacl@0.14.5(transitive)
Updatedbluebird@^3.7.2
Updatedmongodb@^3.5.7