diff --git a/packages/strapi-hook-bookshelf/lib/generate-group-relations.js b/packages/strapi-hook-bookshelf/lib/generate-group-relations.js index aad3d1ac8e..3517446cdf 100644 --- a/packages/strapi-hook-bookshelf/lib/generate-group-relations.js +++ b/packages/strapi-hook-bookshelf/lib/generate-group-relations.js @@ -2,7 +2,7 @@ const pluralize = require('pluralize'); -module.exports = async ({ model, definition, ORM, GLOBALS }) => { +const createGroupModels = async ({ model, definition, ORM, GLOBALS }) => { const { collectionName, primaryKey } = definition; const groupAttributes = Object.keys(definition.attributes).filter( @@ -36,6 +36,19 @@ module.exports = async ({ model, definition, ORM, GLOBALS }) => { }); }; }); + } +}; + +const createGroupJoinTables = async ({ definition, ORM }) => { + const { collectionName, primaryKey } = definition; + + const groupAttributes = Object.keys(definition.attributes).filter( + key => definition.attributes[key].type === 'group' + ); + + if (groupAttributes.length > 0) { + const joinTable = `${collectionName}_groups`; + const joinColumn = `${pluralize.singular(collectionName)}_${primaryKey}`; if (await ORM.knex.schema.hasTable(joinTable)) return; @@ -58,3 +71,8 @@ module.exports = async ({ model, definition, ORM, GLOBALS }) => { }); } }; + +module.exports = { + createGroupModels, + createGroupJoinTables, +}; diff --git a/packages/strapi-hook-bookshelf/lib/mount-models.js b/packages/strapi-hook-bookshelf/lib/mount-models.js index 2b67e7b332..dc42dc2686 100644 --- a/packages/strapi-hook-bookshelf/lib/mount-models.js +++ b/packages/strapi-hook-bookshelf/lib/mount-models.js @@ -5,7 +5,10 @@ const { singular } = require('pluralize'); const utilsModels = require('strapi-utils').models; const relations = require('./relations'); const buildDatabaseSchema = require('./buildDatabaseSchema'); -const genGroupRelatons = require('./generate-group-relations'); +const { + createGroupJoinTables, + createGroupModels, +} = require('./generate-group-relations'); const PIVOT_PREFIX = '_pivot_'; @@ -114,6 +117,8 @@ module.exports = ({ models, target, plugin = false }, ctx) => { global[definition.globalName] = {}; } + await createGroupModels({ model: loadedModel, definition, ORM, GLOBALS }); + // Add every relationships to the loaded model for Bookshelf. // Basic attributes don't need this-- only relations. Object.keys(definition.attributes).forEach(name => { @@ -734,7 +739,7 @@ module.exports = ({ models, target, plugin = false }, ctx) => { model: target[model], }); - await genGroupRelatons({ model: loadedModel, definition, ORM, GLOBALS }); + await createGroupJoinTables({ definition, ORM }); } catch (err) { strapi.log.error(`Impossible to register the '${model}' model.`); strapi.log.error(err); diff --git a/packages/strapi-plugin-content-manager/services/ContentManager.js b/packages/strapi-plugin-content-manager/services/ContentManager.js index d9383afafe..72066401c9 100644 --- a/packages/strapi-plugin-content-manager/services/ContentManager.js +++ b/packages/strapi-plugin-content-manager/services/ContentManager.js @@ -120,50 +120,16 @@ module.exports = { const { source } = query; const { model } = params; - const primaryKey = strapi.query(model, source).primaryKey; - const toRemove = Object.keys(query).reduce((acc, curr) => { - if (curr !== 'source') { - return acc.concat([query[curr]]); - } + const { primaryKey } = strapi.query(model, source); + const toRemove = Object.values(_.omit(query, 'source')); - return acc; - }, []); + // do not allow deleting more than 100 items at once + const filter = { [`${primaryKey}_in`]: toRemove, _limit: 100 }; + const entries = await strapi.query(model, source).find(filter, []); - const filter = { [`${primaryKey}_in`]: toRemove }; - const entries = await strapi.query(model, source).find(filter, null, true); - const associations = strapi.query(model, source).associations; - - for (let i = 0; i < entries.length; ++i) { - const entry = entries[i]; - - associations.forEach(association => { - if (entry[association.alias]) { - switch (association.nature) { - case 'oneWay': - case 'oneToOne': - case 'manyToOne': - case 'oneToManyMorph': - entry[association.alias] = null; - break; - case 'oneToMany': - case 'manyToMany': - case 'manyToManyMorph': - entry[association.alias] = []; - break; - default: - } - } - }); - - await strapi.query(model, source).update({ - [primaryKey]: entry[primaryKey], - values: _.pick(entry, associations.map(a => a.alias)), - }); - } - - return strapi.query(model, source).deleteMany({ - [primaryKey]: toRemove, - }); + return Promise.all( + entries.map(entry => strapi.query(model, source).delete({ id: entry.id })) + ); }, search(params, query) {