Merge pull request #14989 from strapi/deits/fix-entities-export

This commit is contained in:
Jean-Sébastien Herbaux 2022-11-30 09:20:04 +01:00 committed by GitHub
commit c9b83b1c07
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,5 +1,6 @@
import type { ContentTypeSchema } from '@strapi/strapi'; import type { ContentTypeSchema } from '@strapi/strapi';
import { isObject, isArray, size } from 'lodash/fp';
import { Readable, PassThrough } from 'stream'; import { Readable, PassThrough } from 'stream';
/** /**
@ -14,7 +15,7 @@ export const createEntitiesStream = (strapi: Strapi.Strapi): Readable => {
// Create a query builder instance (default type is 'select') // Create a query builder instance (default type is 'select')
.queryBuilder(contentType.uid) .queryBuilder(contentType.uid)
// Apply the populate // Apply the populate
.populate(getPopulateAttributes(contentType)) .populate(getPopulateAttributes(strapi, contentType))
// Get a readable stream // Get a readable stream
.stream(); .stream();
@ -58,10 +59,52 @@ export const createEntitiesTransformStream = (): PassThrough => {
/** /**
* Get the list of attributes that needs to be populated for the entities streaming * Get the list of attributes that needs to be populated for the entities streaming
*/ */
const getPopulateAttributes = (contentType: ContentTypeSchema) => { const getPopulateAttributes = (strapi: Strapi.Strapi, contentType: ContentTypeSchema) => {
const { attributes } = contentType; const { attributes } = contentType;
return Object.keys(attributes).filter((key) => const populate: any = {};
['component', 'dynamiczone'].includes(attributes[key].type)
); const entries: [string, any][] = Object.entries(attributes);
for (const [key, attribute] of entries) {
if (attribute.type === 'component') {
const component = strapi.getModel(attribute.component);
const subPopulate = getPopulateAttributes(strapi, component);
if ((isArray(subPopulate) || isObject(subPopulate)) && size(subPopulate) > 0) {
populate[key] = { populate: subPopulate };
}
if (subPopulate === true) {
populate[key] = true;
}
}
if (attribute.type === 'dynamiczone') {
const { components: componentsUID } = attribute;
const on: any = {};
for (const componentUID of componentsUID) {
const component = strapi.getModel(componentUID);
const componentPopulate = getPopulateAttributes(strapi, component);
on[componentUID] = { populate: componentPopulate };
}
populate[key] = size(on) > 0 ? { on } : true;
}
}
const values = Object.values(populate);
if (values.length === 0) {
return true;
}
if (values.every((value) => value === true)) {
return Object.keys(populate);
}
return populate;
}; };