Fix mongoose destroy function & re-add strapi.stop calls

Signed-off-by: Convly <jean-sebastien.herbaux@epitech.eu>
This commit is contained in:
Convly 2020-11-27 15:58:50 +01:00
parent 987a7c1022
commit 823070df9d
4 changed files with 111 additions and 98 deletions

View File

@ -47,12 +47,13 @@ const createConnectionURL = opts => {
};
module.exports = function(strapi) {
function initialize() {
const { connections } = strapi.config;
const mongooseConnections = Object.keys(connections).filter(key =>
isMongooseConnection(connections[key])
);
const connectionsPromises = Object.keys(connections)
.filter(key => isMongooseConnection(connections[key]))
.map(async connectionName => {
function initialize() {
const connectionsPromises = mongooseConnections.map(async connectionName => {
const connection = connections[connectionName];
const instance = new Mongoose();
@ -197,10 +198,21 @@ module.exports = function(strapi) {
);
}
async function destroy() {
for (const connName of mongooseConnections) {
const connection = strapi.connections[connName];
if (connection instanceof Mongoose) {
connection.disconnect();
}
}
}
return {
defaults,
initialize,
getQueryParams,
destroy,
buildQuery,
queries,
...relations,

View File

@ -272,9 +272,9 @@ module.exports = async ({ models, target }, ctx) => {
});
};
// Only sync indexes in development env while it's not possible to create complex indexes directly from models
// In other environments it will simply create missing indexes (those defined in the models but not present in db)
if (strapi.app.env === 'development') {
// Only sync indexes when not in production env while it's not possible to create complex indexes directly from models
// In production it will simply create missing indexes (those defined in the models but not present in db)
if (strapi.app.env !== 'production') {
// Ensure indexes are synced with the model, prevent duplicate index errors
// Side-effect: Delete all the indexes not present in the model.json
Model.syncIndexes(null, handleIndexesErrors);

View File

@ -51,7 +51,7 @@ const createConnectorRegistry = ({ defaultConnection, connections }) => {
getByConnection(connection) {
if (!_.has(connections, connection)) {
throw new Error('Trying to access a connector for an unknow connection');
throw new Error('Trying to access a connector for an unknown connection');
}
const connectorKey = connections[connection].connector;

View File

@ -201,7 +201,9 @@ class Strapi {
}
async destroy() {
if (_.has(this, 'server.destroy')) {
this.server.destroy();
}
if (_.has(this, 'db')) {
await this.db.destroy();
@ -417,10 +419,9 @@ class Strapi {
// plugins bootstrap
const pluginBoostraps = Object.keys(this.plugins).map(plugin => {
return execBootstrap(_.get(this.plugins[plugin], 'config.functions.bootstrap')).catch(err => {
// console.log(err);
strapi.log.error(`Bootstrap function in plugin "${plugin}" failed`);
strapi.log.error(err);
// strapi.stop();
strapi.stop();
});
});
await Promise.all(pluginBoostraps);
@ -433,7 +434,7 @@ class Strapi {
return execBootstrap(adminBootstrap).catch(err => {
strapi.log.error(`Bootstrap function in admin failed`);
strapi.log.error(err);
// strapi.stop();
strapi.stop();
});
}