Skip to content

Commit

Permalink
feat(connection): Add options to mongodb connection string
Browse files Browse the repository at this point in the history
  • Loading branch information
Duy Luong committed Dec 12, 2017
1 parent 45da475 commit 95684c4
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 17 deletions.
7 changes: 6 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
"geopoint": "^1.0.1",
"lodash": "^4.17.4",
"moment": "^2.19.1",
"mongo-uri-builder": "^1.0.3",
"mongodb": "^2.2.33",
"mquery": "^2.3.2",
"pluralize": "^7.0.0",
Expand Down
10 changes: 2 additions & 8 deletions src/Database/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const mquery = require('mquery')
const CE = require('../Exceptions')
const util = require('../../lib/util')
const _ = require('lodash')
const mongoUriBuilder = require('mongo-uri-builder')
// const debug = require('debug')('mquery')

const proxyHandler = {
Expand Down Expand Up @@ -107,15 +108,8 @@ class Database {
if (config.client !== 'mongodb') {
throw new CE.RuntimeException('invalid connection type')
}
const security = (config.connection.user && config.connection.password)
? `${config.connection.user}:${config.connection.password}@`
: (config.connection.user ? `${config.connection.user}@` : '')

const authString = (config.connection.auth && config.connection.auth.source && config.connection.auth.mechanism)
? `?authSource=${config.connection.auth.source}&authMechanism=${config.connection.auth.mechanism}`
: ''

this.connectionString = `mongodb://${security}${config.connection.host}:${config.connection.port}/${config.connection.database}${authString}`
this.connectionString = config.connection.connectionString || mongoUriBuilder(config.connection)
this.connection = null
this._globalTrx = null
this.query()
Expand Down
16 changes: 12 additions & 4 deletions templates/config.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,23 @@ module.exports = {
*/
mongodb: {
client: 'mongodb',
connectionString: Env.get('DB_CONNECTION_STRING', ''),
connection: {
host: Env.get('DB_HOST', 'localhost'),
port: Env.get('DB_PORT', 27017),
user: Env.get('DB_USER', 'admin'),
username: Env.get('DB_USER', 'admin'),
password: Env.get('DB_PASSWORD', ''),
database: Env.get('DB_DATABASE', 'adonis'),
auth: {
source: Env.get('DB_AUTH_SOURCE', ''),
mechanism: Env.get('DB_AUTH_MECHANISM', '')
options: {
// replicaSet: Env.get('DB_REPLICA_SET', '')
// ssl: ('DB_SSL, '')
// connectTimeoutMS: Env.get('DB_CONNECT_TIMEOUT_MS', 15000),
// socketTimeoutMS: Env.get('DB_SOCKET_TIMEOUT_MS', 180000),
// w: ('DB_W, 0),
// readPreference: Env.get('DB_READ_PREFERENCE', 'secondary'),
// authSource: Env.get('DB_AUTH_SOURCE', ''),
// authMechanism: Env.get('DB_AUTH_MECHANISM', ''),
// other options
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion test/unit/helpers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ module.exports = {
connection: {
host: process.env.DB_HOST || '127.0.0.1',
port: process.env.DB_PORT || '27017',
user: process.env.DB_USER || 'admin',
username: process.env.DB_USER || 'admin',
password: process.env.DB_PASSWORD || '',
database: process.env.DB_NAME || 'test'
}
Expand Down
6 changes: 3 additions & 3 deletions test/unit/helpers/mongodbConnections.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ module.exports = {
default: {
client: 'mongodb',
connection: {
user: 'admin',
username: 'admin',
password: '',
database: 'default'
}
Expand All @@ -19,7 +19,7 @@ module.exports = {
alternateConnection: {
client: 'mongodb',
connection: {
user: 'admin',
username: 'admin',
password: '',
database: 'alternate'
}
Expand All @@ -28,7 +28,7 @@ module.exports = {
defaultPrefix: {
client: 'mongodb',
connection: {
user: 'admin',
username: 'admin',
password: '',
database: 'default'
},
Expand Down

0 comments on commit 95684c4

Please sign in to comment.