mirror of
https://github.com/strapi/strapi.git
synced 2025-09-27 01:09:49 +00:00
Implement delete many content-manager with new queries
This commit is contained in:
parent
9612ff0f14
commit
93bf13a77c
@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
const pluralize = require('pluralize');
|
const pluralize = require('pluralize');
|
||||||
|
|
||||||
module.exports = async ({ model, definition, ORM, GLOBALS }) => {
|
const createGroupModels = async ({ model, definition, ORM, GLOBALS }) => {
|
||||||
const { collectionName, primaryKey } = definition;
|
const { collectionName, primaryKey } = definition;
|
||||||
|
|
||||||
const groupAttributes = Object.keys(definition.attributes).filter(
|
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;
|
if (await ORM.knex.schema.hasTable(joinTable)) return;
|
||||||
|
|
||||||
@ -58,3 +71,8 @@ module.exports = async ({ model, definition, ORM, GLOBALS }) => {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
module.exports = {
|
||||||
|
createGroupModels,
|
||||||
|
createGroupJoinTables,
|
||||||
|
};
|
||||||
|
@ -5,7 +5,10 @@ const { singular } = require('pluralize');
|
|||||||
const utilsModels = require('strapi-utils').models;
|
const utilsModels = require('strapi-utils').models;
|
||||||
const relations = require('./relations');
|
const relations = require('./relations');
|
||||||
const buildDatabaseSchema = require('./buildDatabaseSchema');
|
const buildDatabaseSchema = require('./buildDatabaseSchema');
|
||||||
const genGroupRelatons = require('./generate-group-relations');
|
const {
|
||||||
|
createGroupJoinTables,
|
||||||
|
createGroupModels,
|
||||||
|
} = require('./generate-group-relations');
|
||||||
|
|
||||||
const PIVOT_PREFIX = '_pivot_';
|
const PIVOT_PREFIX = '_pivot_';
|
||||||
|
|
||||||
@ -114,6 +117,8 @@ module.exports = ({ models, target, plugin = false }, ctx) => {
|
|||||||
global[definition.globalName] = {};
|
global[definition.globalName] = {};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
await createGroupModels({ model: loadedModel, definition, ORM, GLOBALS });
|
||||||
|
|
||||||
// Add every relationships to the loaded model for Bookshelf.
|
// Add every relationships to the loaded model for Bookshelf.
|
||||||
// Basic attributes don't need this-- only relations.
|
// Basic attributes don't need this-- only relations.
|
||||||
Object.keys(definition.attributes).forEach(name => {
|
Object.keys(definition.attributes).forEach(name => {
|
||||||
@ -734,7 +739,7 @@ module.exports = ({ models, target, plugin = false }, ctx) => {
|
|||||||
model: target[model],
|
model: target[model],
|
||||||
});
|
});
|
||||||
|
|
||||||
await genGroupRelatons({ model: loadedModel, definition, ORM, GLOBALS });
|
await createGroupJoinTables({ definition, ORM });
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
strapi.log.error(`Impossible to register the '${model}' model.`);
|
strapi.log.error(`Impossible to register the '${model}' model.`);
|
||||||
strapi.log.error(err);
|
strapi.log.error(err);
|
||||||
|
@ -120,50 +120,16 @@ module.exports = {
|
|||||||
const { source } = query;
|
const { source } = query;
|
||||||
const { model } = params;
|
const { model } = params;
|
||||||
|
|
||||||
const primaryKey = strapi.query(model, source).primaryKey;
|
const { primaryKey } = strapi.query(model, source);
|
||||||
const toRemove = Object.keys(query).reduce((acc, curr) => {
|
const toRemove = Object.values(_.omit(query, 'source'));
|
||||||
if (curr !== 'source') {
|
|
||||||
return acc.concat([query[curr]]);
|
|
||||||
}
|
|
||||||
|
|
||||||
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 };
|
return Promise.all(
|
||||||
const entries = await strapi.query(model, source).find(filter, null, true);
|
entries.map(entry => strapi.query(model, source).delete({ id: entry.id }))
|
||||||
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,
|
|
||||||
});
|
|
||||||
},
|
},
|
||||||
|
|
||||||
search(params, query) {
|
search(params, query) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user