mirror of
https://github.com/strapi/strapi.git
synced 2025-09-26 00:39:49 +00:00
get list of components to delete them afterwards
This commit is contained in:
parent
840391f541
commit
3577fae4e4
@ -100,6 +100,34 @@ const createComponents = async (uid, data) => {
|
|||||||
return componentBody;
|
return componentBody;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param {str} uid
|
||||||
|
* @param {*} entity
|
||||||
|
* @return {Array<{uid: string, data: *}>}
|
||||||
|
*/
|
||||||
|
const getComponents = async (uid, entity) => {
|
||||||
|
const { attributes } = strapi.getModel(uid);
|
||||||
|
const components = [];
|
||||||
|
|
||||||
|
for (const attributeName in attributes) {
|
||||||
|
const attribute = attributes[attributeName];
|
||||||
|
|
||||||
|
if (attribute.type === 'component' || attribute.type === 'dynamiczone') {
|
||||||
|
const value = await strapi.query(uid).load(entity, attributeName);
|
||||||
|
if (!value) continue;
|
||||||
|
|
||||||
|
_.castArray(value).forEach((component) => {
|
||||||
|
components.push({
|
||||||
|
uid: attribute.type === 'component' ? attribute.component : component.__component,
|
||||||
|
data: component,
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return components;
|
||||||
|
};
|
||||||
|
|
||||||
/*
|
/*
|
||||||
delete old components
|
delete old components
|
||||||
create or update
|
create or update
|
||||||
@ -352,7 +380,9 @@ const deleteComponent = async (uid, componentToDelete) => {
|
|||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
omitComponentData,
|
omitComponentData,
|
||||||
|
getComponents,
|
||||||
createComponents,
|
createComponents,
|
||||||
updateComponents,
|
updateComponents,
|
||||||
deleteComponents,
|
deleteComponents,
|
||||||
|
deleteComponent,
|
||||||
};
|
};
|
||||||
|
@ -14,9 +14,10 @@ const uploadFiles = require('../utils/upload-files');
|
|||||||
|
|
||||||
const {
|
const {
|
||||||
omitComponentData,
|
omitComponentData,
|
||||||
|
getComponents,
|
||||||
createComponents,
|
createComponents,
|
||||||
updateComponents,
|
updateComponents,
|
||||||
deleteComponents,
|
deleteComponent,
|
||||||
} = require('./components');
|
} = require('./components');
|
||||||
const { transformParamsToQuery, pickSelectionParams } = require('./params');
|
const { transformParamsToQuery, pickSelectionParams } = require('./params');
|
||||||
const { applyTransforms } = require('./attributes');
|
const { applyTransforms } = require('./attributes');
|
||||||
@ -213,8 +214,10 @@ const createDefaultImplementation = ({ strapi, db, eventHub, entityValidator })
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const componentsToDelete = await getComponents(uid, entityToDelete);
|
||||||
|
|
||||||
await db.query(uid).delete({ where: { id: entityToDelete.id } });
|
await db.query(uid).delete({ where: { id: entityToDelete.id } });
|
||||||
await deleteComponents(uid, entityToDelete);
|
await Promise.all(componentsToDelete.map((compo) => deleteComponent(compo.uid, compo.data)));
|
||||||
|
|
||||||
await this.emitEvent(uid, ENTRY_DELETE, entityToDelete);
|
await this.emitEvent(uid, ENTRY_DELETE, entityToDelete);
|
||||||
|
|
||||||
@ -234,8 +237,14 @@ const createDefaultImplementation = ({ strapi, db, eventHub, entityValidator })
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const componentsToDelete = await Promise.all(
|
||||||
|
entitiesToDelete.map((entityToDelete) => getComponents(uid, entityToDelete))
|
||||||
|
);
|
||||||
|
|
||||||
const deletedEntities = await db.query(uid).deleteMany(query);
|
const deletedEntities = await db.query(uid).deleteMany(query);
|
||||||
await Promise.all(entitiesToDelete.map((entity) => deleteComponents(uid, entity)));
|
await Promise.all(
|
||||||
|
componentsToDelete.flat().map((compo) => deleteComponent(compo.uid, compo.data))
|
||||||
|
);
|
||||||
|
|
||||||
// Trigger webhooks. One for each entity
|
// Trigger webhooks. One for each entity
|
||||||
await Promise.all(entitiesToDelete.map((entity) => this.emitEvent(uid, ENTRY_DELETE, entity)));
|
await Promise.all(entitiesToDelete.map((entity) => this.emitEvent(uid, ENTRY_DELETE, entity)));
|
||||||
|
@ -158,6 +158,15 @@ describe('Core API - Basic + compo + draftAndPublish', () => {
|
|||||||
data.productsWithCompoAndDP.shift();
|
data.productsWithCompoAndDP.shift();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe('database state', () => {
|
||||||
|
test('components have been removed from the database', async () => {
|
||||||
|
const dbComponents = await strapi.db
|
||||||
|
.query('default.compo')
|
||||||
|
.findMany({ name: 'compo name updated' });
|
||||||
|
expect(dbComponents).toHaveLength(0);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
describe('validation', () => {
|
describe('validation', () => {
|
||||||
test('Cannot create product with compo - compo required', async () => {
|
test('Cannot create product with compo - compo required', async () => {
|
||||||
const product = {
|
const product = {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user