polish code again

This commit is contained in:
sundaycrafts 2018-09-12 14:12:09 +09:00
parent ffc969c6f7
commit 04902d2c06

View File

@ -48,8 +48,8 @@ module.exports = function (strapi) {
* Initialize the hook * Initialize the hook
*/ */
initialize: cb => { initialize: cb =>
_.forEach(_.pickBy(strapi.config.connections, {connector: 'strapi-hook-mongoose'}), (connection, connectionName) => { _.forEach(_.pickBy(strapi.config.connections, {connector: 'strapi-hook-mongoose'}), async (connection, connectionName) => {
const instance = new Mongoose(); const instance = new Mongoose();
const { uri, host, port, username, password, database, srv } = _.defaults(connection.settings, strapi.config.hook.settings.mongoose); const { uri, host, port, username, password, database, srv } = _.defaults(connection.settings, strapi.config.hook.settings.mongoose);
const uriOptions = uri ? url.parse(uri, true).query : {}; const uriOptions = uri ? url.parse(uri, true).query : {};
@ -78,28 +78,21 @@ module.exports = function (strapi) {
options.debug = debug === true || debug === 'true'; options.debug = debug === true || debug === 'true';
try {
/* FIXME: for now, mongoose doesn't support srv auth except the way including user/pass in URI. /* FIXME: for now, mongoose doesn't support srv auth except the way including user/pass in URI.
* https://github.com/Automattic/mongoose/issues/6881 */ * https://github.com/Automattic/mongoose/issues/6881 */
instance.connect(uri || await instance.connect(uri ||
`mongodb${isSrv ? '+srv' : ''}://${username}:${password}@${host}${ !isSrv ? ':' + port : '' }/`, `mongodb${isSrv ? '+srv' : ''}://${username}:${password}@${host}${ !isSrv ? ':' + port : '' }/`,
connectOptions connectOptions
); );
} catch ({message}) {
for (let key in options) { const errMsg = message.includes(`:${port}`)
instance.set(key, options[key]); ? 'Make sure your MongoDB database is running...' : message
return cb(errMsg);
} }
// Handle error Object.keys(options, key => instance.set(key, options[key]));
instance.connection.on('error', error => {
if (error.message.indexOf(`:${port}`)) {
return cb('Make sure your MongoDB database is running...');
}
cb(error);
});
// Handle success
instance.connection.on('open', () => {
const mountModels = (models, target, plugin = false) => { const mountModels = (models, target, plugin = false) => {
if (!target) return; if (!target) return;
@ -464,9 +457,7 @@ module.exports = function (strapi) {
}); });
cb(); cb();
}); }),
});
},
getQueryParams: (value, type, key) => { getQueryParams: (value, type, key) => {
const result = {}; const result = {};