From 42304aa680c63bd9ffbf0eb343fe71c3487d6efa Mon Sep 17 00:00:00 2001 From: Convly Date: Thu, 24 Nov 2022 10:19:40 +0100 Subject: [PATCH 1/4] Fix the nested populate for entities export --- .../local-strapi-source-provider/entities.ts | 61 +++++++++++++++++-- 1 file changed, 55 insertions(+), 6 deletions(-) diff --git a/packages/core/data-transfer/lib/providers/local-strapi-source-provider/entities.ts b/packages/core/data-transfer/lib/providers/local-strapi-source-provider/entities.ts index b54293570f..9546eae4fb 100644 --- a/packages/core/data-transfer/lib/providers/local-strapi-source-provider/entities.ts +++ b/packages/core/data-transfer/lib/providers/local-strapi-source-provider/entities.ts @@ -1,5 +1,6 @@ -import type { ContentTypeSchema } from '@strapi/strapi'; +import type { ComponentAttribute, ContentTypeSchema } from '@strapi/strapi'; +import { isObject, isArray, size } from 'lodash/fp'; 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') .queryBuilder(contentType.uid) // Apply the populate - .populate(getPopulateAttributes(contentType)) + .populate(getPopulateAttributes(strapi, contentType)) // Get a readable stream .stream(); @@ -58,10 +59,58 @@ export const createEntitiesTransformStream = (): PassThrough => { /** * 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; - return Object.keys(attributes).filter((key) => - ['component', 'dynamiczone'].includes(attributes[key].type) - ); + const populate: any = {}; + + 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)) { + populate[key] = subPopulate; + } + + if (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] = componentPopulate; + } + + populate[key] = size(on) > 0 ? { on } : true; + } + } + + const values = Object.values(populate); + + // console.log(contentType.uid, JSON.stringify(populate, null, 2)); + + if (values.length === 0) { + return true; + } + + if (values.every((value) => value === true)) { + return Object.keys(populate); + } + + return populate; }; From a7aac3e78188a9cff1519e9fb4663fe5c5e18e56 Mon Sep 17 00:00:00 2001 From: Convly Date: Thu, 24 Nov 2022 11:21:35 +0100 Subject: [PATCH 2/4] Remove useless import --- .../lib/providers/local-strapi-source-provider/entities.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/core/data-transfer/lib/providers/local-strapi-source-provider/entities.ts b/packages/core/data-transfer/lib/providers/local-strapi-source-provider/entities.ts index 9546eae4fb..4649e2fdf0 100644 --- a/packages/core/data-transfer/lib/providers/local-strapi-source-provider/entities.ts +++ b/packages/core/data-transfer/lib/providers/local-strapi-source-provider/entities.ts @@ -1,4 +1,4 @@ -import type { ComponentAttribute, ContentTypeSchema } from '@strapi/strapi'; +import type { ContentTypeSchema } from '@strapi/strapi'; import { isObject, isArray, size } from 'lodash/fp'; import { Readable, PassThrough } from 'stream'; From 659a55ae991f9027030951b222b3282198cabc16 Mon Sep 17 00:00:00 2001 From: Convly Date: Tue, 29 Nov 2022 17:05:20 +0100 Subject: [PATCH 3/4] Remove console.log --- .../lib/providers/local-strapi-source-provider/entities.ts | 2 -- 1 file changed, 2 deletions(-) diff --git a/packages/core/data-transfer/lib/providers/local-strapi-source-provider/entities.ts b/packages/core/data-transfer/lib/providers/local-strapi-source-provider/entities.ts index 4649e2fdf0..97866c23a4 100644 --- a/packages/core/data-transfer/lib/providers/local-strapi-source-provider/entities.ts +++ b/packages/core/data-transfer/lib/providers/local-strapi-source-provider/entities.ts @@ -102,8 +102,6 @@ const getPopulateAttributes = (strapi: Strapi.Strapi, contentType: ContentTypeSc const values = Object.values(populate); - // console.log(contentType.uid, JSON.stringify(populate, null, 2)); - if (values.length === 0) { return true; } From afea3145b88b1264b36da99e8d667d61077b02d4 Mon Sep 17 00:00:00 2001 From: Convly Date: Tue, 29 Nov 2022 17:35:17 +0100 Subject: [PATCH 4/4] Fix populate structure --- .../providers/local-strapi-source-provider/entities.ts | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/packages/core/data-transfer/lib/providers/local-strapi-source-provider/entities.ts b/packages/core/data-transfer/lib/providers/local-strapi-source-provider/entities.ts index 97866c23a4..0aaba4b213 100644 --- a/packages/core/data-transfer/lib/providers/local-strapi-source-provider/entities.ts +++ b/packages/core/data-transfer/lib/providers/local-strapi-source-provider/entities.ts @@ -71,11 +71,7 @@ const getPopulateAttributes = (strapi: Strapi.Strapi, contentType: ContentTypeSc const component = strapi.getModel(attribute.component); const subPopulate = getPopulateAttributes(strapi, component); - if (isArray(subPopulate)) { - populate[key] = subPopulate; - } - - if (isObject(subPopulate) && size(subPopulate) > 0) { + if ((isArray(subPopulate) || isObject(subPopulate)) && size(subPopulate) > 0) { populate[key] = { populate: subPopulate }; } @@ -93,7 +89,7 @@ const getPopulateAttributes = (strapi: Strapi.Strapi, contentType: ContentTypeSc const component = strapi.getModel(componentUID); const componentPopulate = getPopulateAttributes(strapi, component); - on[componentUID] = componentPopulate; + on[componentUID] = { populate: componentPopulate }; } populate[key] = size(on) > 0 ? { on } : true;