Add authSource & ssl options to MongoDB connector fix

This commit is contained in:
Minh Tri Nguyen 2018-03-04 23:02:29 +08:00
parent eab59ad395
commit a18ba8805e
5 changed files with 19 additions and 5 deletions

View File

@ -10,7 +10,8 @@
"port": "${process.env.DATABASE_PORT || 27017}",
"database": "${process.env.DATABASE_NAME || 'production'}",
"username": "${process.env.DATABASE_USERNAME || ''}",
"password": "${process.env.DATABASE_PASSWORD || ''}"
"password": "${process.env.DATABASE_PASSWORD || ''}",
"ssl": false
},
"options": {}
}

View File

@ -9,7 +9,8 @@
"port": 27017,
"database": "test",
"username": "",
"password": ""
"password": "",
"ssl": false
},
"options": {}
}

View File

@ -193,6 +193,14 @@ module.exports = (scope, cb) => {
message: 'Password:',
mask: '*',
default: _.get(scope.database, 'password', undefined)
},
{
when: !hasDatabaseConfig,
type: 'boolean',
prefix: '',
name: 'ssl',
message: 'Enable SSL connection:',
default: _.get(scope.database, 'ssl', false)
}
])
.then(answers => {
@ -206,6 +214,7 @@ module.exports = (scope, cb) => {
scope.database.settings.database = answers.database;
scope.database.settings.username = answers.username;
scope.database.settings.password = answers.password;
scope.database.settings.ssl = answers.ssl;
logger.info('Testing database connection...');

View File

@ -30,7 +30,8 @@ module.exports = function (strapi) {
defaultConnection: 'default',
host: 'localhost',
port: 27017,
database: 'strapi'
database: 'strapi',
ssl: false
},
/**
@ -40,7 +41,7 @@ module.exports = function (strapi) {
initialize: cb => {
_.forEach(_.pickBy(strapi.config.connections, {connector: 'strapi-mongoose'}), (connection, connectionName) => {
const instance = new Mongoose();
const { uri, host, port, username, password, database } = _.defaults(connection.settings, strapi.config.hook.settings.mongoose);
const { uri, host, port, username, password, database, ssl } = _.defaults(connection.settings, strapi.config.hook.settings.mongoose);
// Connect to mongo database
const connectOptions = {}
@ -50,6 +51,7 @@ module.exports = function (strapi) {
connectOptions.pass = password
}
}
connectOptions.ssl = ssl ? true : false;
instance.connect(uri || `mongodb://${host}:${port}/${database}`, connectOptions);

View File

@ -10,7 +10,7 @@ const logger = require('strapi-utils').logger;
module.exports = (scope, success, error) => {
const Mongoose = require(path.resolve(`${scope.tmpPath}/node_modules/mongoose`));
const { username, password } = scope.database.settings
const { username, password, ssl } = scope.database.settings
const connectOptions = {}
if (username) {
connectOptions.user = username
@ -18,6 +18,7 @@ module.exports = (scope, success, error) => {
connectOptions.pass = password
}
}
connectOptions.ssl = ssl ? true : false
Mongoose.connect(`mongodb://${scope.database.settings.host}:${scope.database.settings.port}/${scope.database.settings.database}`, connectOptions, function (err) {
if (err) {
logger.warn('Database connection has failed! Make sure your database is running.');