Skip to content

Commit

Permalink
Protocol configurable
Browse files Browse the repository at this point in the history
  • Loading branch information
Nataniel López committed Jul 12, 2019
1 parent d801d54 commit c3c6b90
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 deletions lib/mongodb.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ const MongoDBError = require('./mongodb-error');

const DEFAULT_LIMIT = 500;

const MONGODB_URL_PREFIX = 'mongodb://';
const MONGODB_DEFAULT_PROTOCOL = 'mongodb://';

const MONGODB_DEFAULT_HOST = 'localhost';

/**
* @class MongoDB
Expand All @@ -23,13 +25,16 @@ class MongoDB {
throw new MongoDBError('Invalid config', MongoDBError.codes.INVALID_CONFIG);

this.config = {
host: config.host ? config.host.replace(MONGODB_URL_PREFIX, '') : '',
protocol: config.protocol || MONGODB_DEFAULT_PROTOCOL,
host: config.host || MONGODB_DEFAULT_HOST,
port: config.port || 27017,
user: config.user || '',
password: config.password || '',
database: config.database || '',
limit: config.limit || DEFAULT_LIMIT
};
// Avoid protocol duplication
this.config.host = this.config.host.replace(this.config.protocol, '');
}

/**
Expand All @@ -42,7 +47,7 @@ class MongoDB {

this.client = await MongoClient.connect(

`${MONGODB_URL_PREFIX}${this.config.user ?
`${this.config.protocol}${this.config.user ?
`${this.config.user}:${this.config.password}@` : ''}${this.config.host}:${this.config.port}/${this.config.database}`,
{
useNewUrlParser: true
Expand Down

0 comments on commit c3c6b90

Please sign in to comment.