mirror of
https://github.com/strapi/strapi.git
synced 2025-11-10 15:19:00 +00:00
polish code again
This commit is contained in:
parent
ffc969c6f7
commit
04902d2c06
@ -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 = {};
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user