Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: skip PolymorphicRelationsType during export #173

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/server/services/export/export-v2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import {
isComponentAttribute,
isDynamicZoneAttribute,
isMediaAttribute,
isRelationAttribute,
isBasicRelationsTypeAttribute,
getModel,
deleteEntryProp,
setEntryProp,
Expand Down Expand Up @@ -164,7 +164,7 @@ async function findEntriesForHierarchy(
}));
} else if (isMediaAttribute(propAttribute)) {
return flattenEntryCommon(propEntries);
} else if (isRelationAttribute(propAttribute)) {
} else if (isBasicRelationsTypeAttribute(propAttribute)) {
return flattenEntryCommon(propEntries);
}
return propEntries;
Expand Down Expand Up @@ -373,7 +373,7 @@ function getPopulateFromSchema(slug: string, deepness = 5): Populate | true | un
return compPopulate === true ? zonePopulate : merge(zonePopulate, compPopulate);
}, {});
populate[attributeName] = isEmpty(dynamicPopulate) ? true : dynamicPopulate;
} else if (isRelationAttribute(attribute)) {
} else if (isBasicRelationsTypeAttribute(attribute)) {
const relationPopulate = getPopulateFromSchema(attribute.target, deepness - 1);
if (relationPopulate) {
populate[attributeName] = relationPopulate;
Expand Down Expand Up @@ -413,7 +413,7 @@ function buildSlugHierarchy(slug: SchemaUID, deepness = 5): Hierarchy {
hierarchy[attributeName] = buildSlugHierarchy(attribute.component, deepness - 1);
} else if (isDynamicZoneAttribute(attribute)) {
hierarchy[attributeName] = Object.fromEntries(attribute.components.map((componentSlug) => [componentSlug, buildSlugHierarchy(componentSlug, deepness - 1)]));
} else if (isRelationAttribute(attribute)) {
} else if (isBasicRelationsTypeAttribute(attribute)) {
const relationHierarchy = buildSlugHierarchy(attribute.target, deepness - 1);
if (relationHierarchy) {
hierarchy[attributeName] = relationHierarchy;
Expand Down
4 changes: 2 additions & 2 deletions src/server/services/import/import-v2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import pick from 'lodash/pick';
import castArray from 'lodash/castArray';
import { extract, toArray } from '../../../libs/arrays';
import { ObjectBuilder } from '../../../libs/objects';
import { getModel, getModelAttributes, isComponentAttribute, isDynamicZoneAttribute, isMediaAttribute, isRelationAttribute } from '../../utils/models';
import { getModel, getModelAttributes, isComponentAttribute, isDynamicZoneAttribute, isMediaAttribute, isBasicRelationsTypeAttribute } from '../../utils/models';
import { Entry, EntryId, Schema, SchemaUID, User } from '../../types';
import { head, toPairs } from 'lodash';
import { FileEntry, FileEntryDynamicZone, FileId } from './types';
Expand Down Expand Up @@ -359,7 +359,7 @@ function getComponentData(
} else {
store[attributeName] = fileIdToDbId.getMapping('plugin::upload.file', attributeValue as number | string);
}
} else if (isRelationAttribute(attribute)) {
} else if (isBasicRelationsTypeAttribute(attribute)) {
if (attribute.relation.endsWith('Many')) {
store[attributeName] = (attributeValue as (number | string)[]).map((id) => fileIdToDbId.getMapping(attribute.target, id));
} else {
Expand Down
11 changes: 7 additions & 4 deletions src/server/utils/models.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { BasicRelationsType } from '@strapi/strapi';
import { toArray } from '../../libs/arrays';
import { Attribute, AttributeType, ComponentAttribute, DynamicZoneAttribute, Entry, MediaAttribute, RelationAttribute, Schema, SchemaUID } from '../types';

Expand All @@ -9,7 +10,7 @@ export {
isComponentAttribute,
isDynamicZoneAttribute,
isMediaAttribute,
isRelationAttribute,
isBasicRelationsTypeAttribute,
getEntryProp,
setEntryProp,
deleteEntryProp,
Expand All @@ -23,7 +24,7 @@ module.exports = {
isComponentAttribute,
isDynamicZoneAttribute,
isMediaAttribute,
isRelationAttribute,
isBasicRelationsTypeAttribute,
getEntryProp,
setEntryProp,
deleteEntryProp,
Expand Down Expand Up @@ -101,8 +102,10 @@ function isMediaAttribute(attribute: any): attribute is MediaAttribute {
return (attribute as { type: AttributeType }).type === 'media';
}

function isRelationAttribute(attribute: any): attribute is RelationAttribute {
return (attribute as { type: AttributeType }).type === 'relation';
function isBasicRelationsTypeAttribute(attribute: any): attribute is RelationAttribute {
const relation = (attribute as { relation: BasicRelationsType }).relation;

return (attribute as { type: AttributeType }).type === 'relation' && (relation === 'oneToOne' || relation === 'oneToMany' || relation === 'manyToOne' || relation === 'manyToMany');
}

function getEntryProp(entry: Entry, prop: string): any {
Expand Down