@strapi/core 5.23.2 → 5.23.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/package.json.js +11 -11
- package/dist/package.json.mjs +11 -11
- package/dist/services/document-service/components.d.ts +6 -1
- package/dist/services/document-service/components.d.ts.map +1 -1
- package/dist/services/document-service/components.js +97 -0
- package/dist/services/document-service/components.js.map +1 -1
- package/dist/services/document-service/components.mjs +97 -1
- package/dist/services/document-service/components.mjs.map +1 -1
- package/dist/services/document-service/repository.d.ts.map +1 -1
- package/dist/services/document-service/repository.js +4 -0
- package/dist/services/document-service/repository.js.map +1 -1
- package/dist/services/document-service/repository.mjs +5 -1
- package/dist/services/document-service/repository.mjs.map +1 -1
- package/dist/services/document-service/utils/unidirectional-relations.d.ts +19 -2
- package/dist/services/document-service/utils/unidirectional-relations.d.ts.map +1 -1
- package/dist/services/document-service/utils/unidirectional-relations.js +21 -6
- package/dist/services/document-service/utils/unidirectional-relations.js.map +1 -1
- package/dist/services/document-service/utils/unidirectional-relations.mjs +21 -6
- package/dist/services/document-service/utils/unidirectional-relations.mjs.map +1 -1
- package/package.json +11 -11
package/dist/package.json.js
CHANGED
@@ -3,7 +3,7 @@
|
|
3
3
|
Object.defineProperty(exports, '__esModule', { value: true });
|
4
4
|
|
5
5
|
var name = "@strapi/core";
|
6
|
-
var version = "5.23.
|
6
|
+
var version = "5.23.3";
|
7
7
|
var description = "Core of Strapi";
|
8
8
|
var homepage = "https://strapi.io";
|
9
9
|
var bugs = {
|
@@ -59,14 +59,14 @@ var dependencies = {
|
|
59
59
|
"@koa/cors": "5.0.0",
|
60
60
|
"@koa/router": "12.0.2",
|
61
61
|
"@paralleldrive/cuid2": "2.2.2",
|
62
|
-
"@strapi/admin": "5.23.
|
63
|
-
"@strapi/database": "5.23.
|
64
|
-
"@strapi/generators": "5.23.
|
65
|
-
"@strapi/logger": "5.23.
|
66
|
-
"@strapi/permissions": "5.23.
|
67
|
-
"@strapi/types": "5.23.
|
68
|
-
"@strapi/typescript-utils": "5.23.
|
69
|
-
"@strapi/utils": "5.23.
|
62
|
+
"@strapi/admin": "5.23.3",
|
63
|
+
"@strapi/database": "5.23.3",
|
64
|
+
"@strapi/generators": "5.23.3",
|
65
|
+
"@strapi/logger": "5.23.3",
|
66
|
+
"@strapi/permissions": "5.23.3",
|
67
|
+
"@strapi/types": "5.23.3",
|
68
|
+
"@strapi/typescript-utils": "5.23.3",
|
69
|
+
"@strapi/utils": "5.23.3",
|
70
70
|
"@vercel/stega": "0.1.2",
|
71
71
|
bcryptjs: "2.4.3",
|
72
72
|
boxen: "5.1.2",
|
@@ -133,9 +133,9 @@ var devDependencies = {
|
|
133
133
|
"@types/node": "18.19.24",
|
134
134
|
"@types/node-schedule": "2.1.7",
|
135
135
|
"@types/statuses": "2.0.1",
|
136
|
-
"eslint-config-custom": "5.23.
|
136
|
+
"eslint-config-custom": "5.23.3",
|
137
137
|
supertest: "6.3.3",
|
138
|
-
tsconfig: "5.23.
|
138
|
+
tsconfig: "5.23.3"
|
139
139
|
};
|
140
140
|
var engines = {
|
141
141
|
node: ">=18.0.0 <=22.x.x",
|
package/dist/package.json.mjs
CHANGED
@@ -1,5 +1,5 @@
|
|
1
1
|
var name = "@strapi/core";
|
2
|
-
var version = "5.23.
|
2
|
+
var version = "5.23.3";
|
3
3
|
var description = "Core of Strapi";
|
4
4
|
var homepage = "https://strapi.io";
|
5
5
|
var bugs = {
|
@@ -55,14 +55,14 @@ var dependencies = {
|
|
55
55
|
"@koa/cors": "5.0.0",
|
56
56
|
"@koa/router": "12.0.2",
|
57
57
|
"@paralleldrive/cuid2": "2.2.2",
|
58
|
-
"@strapi/admin": "5.23.
|
59
|
-
"@strapi/database": "5.23.
|
60
|
-
"@strapi/generators": "5.23.
|
61
|
-
"@strapi/logger": "5.23.
|
62
|
-
"@strapi/permissions": "5.23.
|
63
|
-
"@strapi/types": "5.23.
|
64
|
-
"@strapi/typescript-utils": "5.23.
|
65
|
-
"@strapi/utils": "5.23.
|
58
|
+
"@strapi/admin": "5.23.3",
|
59
|
+
"@strapi/database": "5.23.3",
|
60
|
+
"@strapi/generators": "5.23.3",
|
61
|
+
"@strapi/logger": "5.23.3",
|
62
|
+
"@strapi/permissions": "5.23.3",
|
63
|
+
"@strapi/types": "5.23.3",
|
64
|
+
"@strapi/typescript-utils": "5.23.3",
|
65
|
+
"@strapi/utils": "5.23.3",
|
66
66
|
"@vercel/stega": "0.1.2",
|
67
67
|
bcryptjs: "2.4.3",
|
68
68
|
boxen: "5.1.2",
|
@@ -129,9 +129,9 @@ var devDependencies = {
|
|
129
129
|
"@types/node": "18.19.24",
|
130
130
|
"@types/node-schedule": "2.1.7",
|
131
131
|
"@types/statuses": "2.0.1",
|
132
|
-
"eslint-config-custom": "5.23.
|
132
|
+
"eslint-config-custom": "5.23.3",
|
133
133
|
supertest: "6.3.3",
|
134
|
-
tsconfig: "5.23.
|
134
|
+
tsconfig: "5.23.3"
|
135
135
|
};
|
136
136
|
var engines = {
|
137
137
|
node: ">=18.0.0 <=22.x.x",
|
@@ -22,5 +22,10 @@ declare const deleteComponents: <TUID extends UID.Schema, TEntity extends Data.E
|
|
22
22
|
}) => Promise<void>;
|
23
23
|
declare const deleteComponent: <TUID extends `${string}.${string}`>(uid: TUID, componentToDelete: Data.Component<TUID>) => Promise<void>;
|
24
24
|
declare const assignComponentData: _.CurriedFunction3<import("@strapi/types/dist/struct").ComponentSchema | import("@strapi/types/dist/struct").ContentTypeSchema, ComponentBody, Input<UID.Schema>, ComponentBody & Partial<Input<UID.Schema>>>;
|
25
|
-
|
25
|
+
/**
|
26
|
+
* Creates a filter function for component relations that can be passed to the generic
|
27
|
+
* unidirectional relations utility
|
28
|
+
*/
|
29
|
+
declare const createComponentRelationFilter: () => (relation: Record<string, any>, model: Schema.Component | Schema.ContentType, trx: any) => Promise<boolean>;
|
30
|
+
export { omitComponentData, assignComponentData, getComponents, createComponents, updateComponents, deleteComponents, deleteComponent, createComponentRelationFilter, };
|
26
31
|
//# sourceMappingURL=components.d.ts.map
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"components.d.ts","sourceRoot":"","sources":["../../../src/services/document-service/components.ts"],"names":[],"mappings":"AAAA,OAAO,CAAC,MAAM,QAAQ,CAAC;AAEvB,OAAO,KAAK,EAAS,GAAG,EAAE,MAAM,EAAE,IAAI,EAAE,OAAO,EAAE,MAAM,eAAe,CAAC;
|
1
|
+
{"version":3,"file":"components.d.ts","sourceRoot":"","sources":["../../../src/services/document-service/components.ts"],"names":[],"mappings":"AAAA,OAAO,CAAC,MAAM,QAAQ,CAAC;AAEvB,OAAO,KAAK,EAAS,GAAG,EAAE,MAAM,EAAE,IAAI,EAAE,OAAO,EAAE,MAAM,eAAe,CAAC;AAUvE,KAAK,KAAK,CAAC,CAAC,SAAS,GAAG,CAAC,MAAM,IAAI,OAAO,CAAC,SAAS,CAAC,MAAM,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC;AAE1E,KAAK,gBAAgB,CAAC,IAAI,SAAS,GAAG,CAAC,MAAM,IAAI,IAAI,CAAC,MAAM,CAC1D,IAAI,EACJ,MAAM,CAAC,oBAAoB,CAAC,IAAI,EAAE,WAAW,GAAG,aAAa,CAAC,CAC/D,CAAC;AAEF,KAAK,oBAAoB,GAAG,MAAM,CAAC,SAAS,CAAC,cAAc,CAAC,GAAG,CAAC,SAAS,EAAE,KAAK,CAAC,CAAC;AAClF,KAAK,wBAAwB,GAAG,MAAM,CAAC,SAAS,CAAC,cAAc,CAAC,GAAG,CAAC,SAAS,EAAE,IAAI,CAAC,CAAC;AAErF,KAAK,cAAc,GAAG,oBAAoB,GAAG,wBAAwB,CAAC;AAEtE,KAAK,gBAAgB,GAAG,MAAM,CAAC,SAAS,CAAC,gBAAgB,CAAC,GAAG,CAAC,SAAS,EAAE,CAAC,CAAC;AAE3E,KAAK,aAAa,GAAG;IACnB,CAAC,GAAG,EAAE,MAAM,GAAG,cAAc,GAAG,gBAAgB,CAAC;CAClD,CAAC;AAEF,QAAA,MAAM,iBAAiB,gLAStB,CAAC;AAGF,QAAA,MAAM,gBAAgB,4DACf,IAAI,QACH,KAAK,2BA6FZ,CAAC;AAEF,QAAA,MAAM,aAAa,iCACZ,IAAI,UACD;IAAE,EAAE,EAAE,QAAQ,aAAa,CAAC,MAAM,CAAC,SAAS,CAAC,EAAE,CAAA;CAAE,KACxD,QAAQ,iBAAiB,IAAI,CAAC,CAQhC,CAAC;AAMF,QAAA,MAAM,gBAAgB,qEACf,IAAI,kBACO;IAAE,EAAE,EAAE,QAAQ,aAAa,CAAC,MAAM,CAAC,SAAS,CAAC,EAAE,CAAA;CAAE,QAC3D,KAAK,2BAyEZ,CAAC;AAiGF,QAAA,MAAM,gBAAgB,oHACf,IAAI,kBACO,OAAO;;mBAqCxB,CAAC;AAyDF,QAAA,MAAM,eAAe,4CACd,IAAI,qBACU,KAAK,SAAS,CAAC,IAAI,CAAC,kBAIxC,CAAC;AAEF,QAAA,MAAM,mBAAmB,+MAIxB,CAAC;AA0HF;;;GAGG;AACH,QAAA,MAAM,6BAA6B,mBAErB,OAAO,MAAM,EAAE,GAAG,CAAC,SACtB,OAAO,SAAS,GAAG,OAAO,WAAW,OACvC,GAAG,KACP,QAAQ,OAAO,CAqBnB,CAAC;AAEF,OAAO,EACL,iBAAiB,EACjB,mBAAmB,EACnB,aAAa,EACb,gBAAgB,EAChB,gBAAgB,EAChB,gBAAgB,EAChB,eAAe,EACf,6BAA6B,GAC9B,CAAC"}
|
@@ -3,6 +3,7 @@
|
|
3
3
|
var _ = require('lodash');
|
4
4
|
var fp = require('lodash/fp');
|
5
5
|
var strapiUtils = require('@strapi/utils');
|
6
|
+
var transformContentTypesToModels = require('../../utils/transform-content-types-to-models.js');
|
6
7
|
|
7
8
|
const omitComponentData = fp.curry((schema, data)=>{
|
8
9
|
const { attributes } = schema;
|
@@ -275,8 +276,104 @@ const deleteComponent = async (uid, componentToDelete)=>{
|
|
275
276
|
const assignComponentData = fp.curry((schema, componentData, data)=>{
|
276
277
|
return fp.pipe(omitComponentData(schema), fp.assign(componentData))(data);
|
277
278
|
});
|
279
|
+
/** *************************
|
280
|
+
Component relation handling for document operations
|
281
|
+
************************** */ /**
|
282
|
+
* Find the parent entry of a component instance.
|
283
|
+
*
|
284
|
+
* Given a component model, a specific component instance id, and the list of
|
285
|
+
* possible parent content types (those that can embed this component),
|
286
|
+
* this function checks each parent's *_cmps join table to see if the component
|
287
|
+
* instance is linked to a parent entity.
|
288
|
+
*
|
289
|
+
* - Returns the parent uid, parent table name, and parent id if found.
|
290
|
+
* - Returns null if no parent relationship exists.
|
291
|
+
*/ const findComponentParent = async (componentSchema, componentId, parentSchemasForComponent, opts)=>{
|
292
|
+
if (!componentSchema?.uid) return null;
|
293
|
+
const schemaBuilder = strapi.db.getSchemaConnection(opts?.trx);
|
294
|
+
const withTrx = (qb)=>opts?.trx ? qb.transacting(opts.trx) : qb;
|
295
|
+
for (const parent of parentSchemasForComponent){
|
296
|
+
if (!parent.collectionName) continue;
|
297
|
+
// Use the exact same functions that create the tables
|
298
|
+
const identifiers = strapi.db.metadata.identifiers;
|
299
|
+
const joinTableName = transformContentTypesToModels.getComponentJoinTableName(parent.collectionName, identifiers);
|
300
|
+
try {
|
301
|
+
const tableExists = await schemaBuilder.hasTable(joinTableName);
|
302
|
+
if (!tableExists) continue;
|
303
|
+
// Use the exact same functions that create the columns
|
304
|
+
const entityIdColumn = transformContentTypesToModels.getComponentJoinColumnEntityName(identifiers);
|
305
|
+
const componentIdColumn = transformContentTypesToModels.getComponentJoinColumnInverseName(identifiers);
|
306
|
+
const componentTypeColumn = transformContentTypesToModels.getComponentTypeColumn(identifiers);
|
307
|
+
const parentRow = await withTrx(strapi.db.getConnection(joinTableName)).where({
|
308
|
+
[componentIdColumn]: componentId,
|
309
|
+
[componentTypeColumn]: componentSchema.uid
|
310
|
+
}).first(entityIdColumn);
|
311
|
+
if (parentRow) {
|
312
|
+
return {
|
313
|
+
uid: parent.uid,
|
314
|
+
table: parent.collectionName,
|
315
|
+
parentId: parentRow[entityIdColumn]
|
316
|
+
};
|
317
|
+
}
|
318
|
+
} catch {
|
319
|
+
continue;
|
320
|
+
}
|
321
|
+
}
|
322
|
+
return null;
|
323
|
+
};
|
324
|
+
/**
|
325
|
+
* Finds content types that contain the given component and have draft & publish enabled.
|
326
|
+
*/ const getParentSchemasForComponent = (componentSchema)=>{
|
327
|
+
return Object.values(strapi.contentTypes).filter((contentType)=>{
|
328
|
+
if (!contentType.options?.draftAndPublish) return false;
|
329
|
+
return Object.values(contentType.attributes).some((attr)=>{
|
330
|
+
return attr.type === 'component' && attr.component === componentSchema.uid || attr.type === 'dynamiczone' && attr.components?.includes(componentSchema.uid);
|
331
|
+
});
|
332
|
+
});
|
333
|
+
};
|
334
|
+
/**
|
335
|
+
* Determines if a component relation should be propagated to a new document version
|
336
|
+
* when a document with draft and publish is updated.
|
337
|
+
*/ const shouldPropagateComponentRelationToNewVersion = async (componentRelation, componentSchema, parentSchemasForComponent, trx)=>{
|
338
|
+
// Get the component ID column name using the actual component model name
|
339
|
+
const componentIdColumn = strapi.db.metadata.identifiers.getJoinColumnAttributeIdName(_.snakeCase(componentSchema.modelName));
|
340
|
+
const componentId = componentRelation[componentIdColumn];
|
341
|
+
const parent = await findComponentParent(componentSchema, componentId, parentSchemasForComponent, {
|
342
|
+
trx
|
343
|
+
});
|
344
|
+
// Keep relation if component has no parent entry
|
345
|
+
if (!parent?.uid) {
|
346
|
+
return true;
|
347
|
+
}
|
348
|
+
const parentContentType = strapi.contentTypes[parent.uid];
|
349
|
+
// Keep relation if parent doesn't have draft & publish enabled
|
350
|
+
if (!parentContentType?.options?.draftAndPublish) {
|
351
|
+
return true;
|
352
|
+
}
|
353
|
+
// Discard relation if parent has draft & publish enabled
|
354
|
+
return false;
|
355
|
+
};
|
356
|
+
/**
|
357
|
+
* Creates a filter function for component relations that can be passed to the generic
|
358
|
+
* unidirectional relations utility
|
359
|
+
*/ const createComponentRelationFilter = ()=>{
|
360
|
+
return async (relation, model, trx)=>{
|
361
|
+
// Only apply component-specific filtering for components
|
362
|
+
if (model.modelType !== 'component') {
|
363
|
+
return true;
|
364
|
+
}
|
365
|
+
const componentSchema = model;
|
366
|
+
const parentSchemas = getParentSchemasForComponent(componentSchema);
|
367
|
+
// Exit if no draft & publish parent types exist
|
368
|
+
if (parentSchemas.length === 0) {
|
369
|
+
return true;
|
370
|
+
}
|
371
|
+
return shouldPropagateComponentRelationToNewVersion(relation, componentSchema, parentSchemas, trx);
|
372
|
+
};
|
373
|
+
};
|
278
374
|
|
279
375
|
exports.assignComponentData = assignComponentData;
|
376
|
+
exports.createComponentRelationFilter = createComponentRelationFilter;
|
280
377
|
exports.createComponents = createComponents;
|
281
378
|
exports.deleteComponent = deleteComponent;
|
282
379
|
exports.deleteComponents = deleteComponents;
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"components.js","sources":["../../../src/services/document-service/components.ts"],"sourcesContent":["import _ from 'lodash';\nimport { has, omit, pipe, assign, curry } from 'lodash/fp';\nimport type { Utils, UID, Schema, Data, Modules } from '@strapi/types';\nimport { contentTypes as contentTypesUtils, async, errors } from '@strapi/utils';\n\n// type aliases for readability\ntype Input<T extends UID.Schema> = Modules.Documents.Params.Data.Input<T>;\n\ntype LoadedComponents<TUID extends UID.Schema> = Data.Entity<\n TUID,\n Schema.AttributeNamesByType<TUID, 'component' | 'dynamiczone'>\n>;\n\ntype SingleComponentValue = Schema.Attribute.ComponentValue<UID.Component, false>;\ntype RepeatableComponentValue = Schema.Attribute.ComponentValue<UID.Component, true>;\n\ntype ComponentValue = SingleComponentValue | RepeatableComponentValue;\n\ntype DynamicZoneValue = Schema.Attribute.DynamicZoneValue<UID.Component[]>;\n\ntype ComponentBody = {\n [key: string]: ComponentValue | DynamicZoneValue;\n};\n\nconst omitComponentData = curry(\n (schema: Schema.Schema, data: Input<UID.Schema>): Partial<Input<UID.Schema>> => {\n const { attributes } = schema;\n const componentAttributes = Object.keys(attributes).filter((attributeName) =>\n contentTypesUtils.isComponentAttribute(attributes[attributeName])\n );\n\n return omit(componentAttributes, data);\n }\n);\n\n// NOTE: we could generalize the logic to allow CRUD of relation directly in the DB layer\nconst createComponents = async <TUID extends UID.Schema, TData extends Input<TUID>>(\n uid: TUID,\n data: TData\n) => {\n const { attributes = {} } = strapi.getModel(uid);\n\n const componentBody: ComponentBody = {};\n\n const attributeNames = Object.keys(attributes);\n\n for (const attributeName of attributeNames) {\n const attribute = attributes[attributeName];\n\n if (!has(attributeName, data) || !contentTypesUtils.isComponentAttribute(attribute)) {\n continue;\n }\n\n if (attribute.type === 'component') {\n const { component: componentUID, repeatable = false } = attribute;\n\n const componentValue = data[attributeName as keyof TData];\n\n if (componentValue === null) {\n continue;\n }\n\n if (repeatable === true) {\n if (!Array.isArray(componentValue)) {\n throw new Error('Expected an array to create repeatable component');\n }\n\n const components: RepeatableComponentValue = await async.map(componentValue, (value: any) =>\n createComponent(componentUID, value)\n );\n\n componentBody[attributeName] = components.map(({ id }) => {\n return {\n id,\n __pivot: {\n field: attributeName,\n component_type: componentUID,\n },\n };\n });\n } else {\n const component = await createComponent(\n componentUID,\n componentValue as Input<UID.Component>\n );\n\n componentBody[attributeName] = {\n id: component.id,\n __pivot: {\n field: attributeName,\n component_type: componentUID,\n },\n };\n }\n\n continue;\n }\n\n if (attribute.type === 'dynamiczone') {\n const dynamiczoneValues = data[\n attributeName as keyof TData\n ] as Modules.EntityService.Params.Attribute.GetValue<Schema.Attribute.DynamicZone>;\n\n if (!Array.isArray(dynamiczoneValues)) {\n throw new Error('Expected an array to create repeatable component');\n }\n\n const createDynamicZoneComponents = async (\n value: Utils.Array.Values<typeof dynamiczoneValues>\n ) => {\n const { id } = await createComponent(value.__component, value);\n return {\n id,\n __component: value.__component,\n __pivot: {\n field: attributeName,\n },\n };\n };\n\n // MySQL/MariaDB can cause deadlocks here if concurrency higher than 1\n componentBody[attributeName] = await async.map(\n dynamiczoneValues,\n createDynamicZoneComponents\n );\n\n continue;\n }\n }\n\n return componentBody;\n};\n\nconst getComponents = async <TUID extends UID.Schema>(\n uid: TUID,\n entity: { id: Modules.EntityService.Params.Attribute.ID }\n): Promise<LoadedComponents<TUID>> => {\n const componentAttributes = contentTypesUtils.getComponentAttributes(strapi.getModel(uid));\n\n if (_.isEmpty(componentAttributes)) {\n return {} as LoadedComponents<TUID>;\n }\n\n return strapi.db.query(uid).load(entity, componentAttributes) as Promise<LoadedComponents<TUID>>;\n};\n\n/*\n delete old components\n create or update\n*/\nconst updateComponents = async <TUID extends UID.Schema, TData extends Partial<Input<TUID>>>(\n uid: TUID,\n entityToUpdate: { id: Modules.EntityService.Params.Attribute.ID },\n data: TData\n) => {\n const { attributes = {} } = strapi.getModel(uid);\n\n const componentBody: ComponentBody = {};\n\n for (const attributeName of Object.keys(attributes)) {\n const attribute = attributes[attributeName];\n\n if (!has(attributeName, data)) {\n continue;\n }\n\n if (attribute.type === 'component') {\n const { component: componentUID, repeatable = false } = attribute;\n\n const componentValue = data[attributeName as keyof TData] as ComponentValue;\n await deleteOldComponents(uid, componentUID, entityToUpdate, attributeName, componentValue);\n\n if (repeatable === true) {\n if (!Array.isArray(componentValue)) {\n throw new Error('Expected an array to create repeatable component');\n }\n\n // MySQL/MariaDB can cause deadlocks here if concurrency higher than 1\n const components: RepeatableComponentValue = await async.map(componentValue, (value: any) =>\n updateOrCreateComponent(componentUID, value)\n );\n\n componentBody[attributeName] = components.filter(_.negate(_.isNil)).map(({ id }) => {\n return {\n id,\n __pivot: {\n field: attributeName,\n component_type: componentUID,\n },\n };\n });\n } else {\n const component = await updateOrCreateComponent(componentUID, componentValue);\n componentBody[attributeName] = component && {\n id: component.id,\n __pivot: {\n field: attributeName,\n component_type: componentUID,\n },\n };\n }\n } else if (attribute.type === 'dynamiczone') {\n const dynamiczoneValues = data[attributeName as keyof TData] as DynamicZoneValue;\n\n await deleteOldDZComponents(uid, entityToUpdate, attributeName, dynamiczoneValues);\n\n if (!Array.isArray(dynamiczoneValues)) {\n throw new Error('Expected an array to create repeatable component');\n }\n\n // MySQL/MariaDB can cause deadlocks here if concurrency higher than 1\n componentBody[attributeName] = await async.map(dynamiczoneValues, async (value: any) => {\n const { id } = await updateOrCreateComponent(value.__component, value);\n\n return {\n id,\n __component: value.__component,\n __pivot: {\n field: attributeName,\n },\n };\n });\n }\n }\n\n return componentBody;\n};\n\nconst pickStringifiedId = ({\n id,\n}: {\n id: Modules.EntityService.Params.Attribute.ID;\n}): Modules.EntityService.Params.Attribute.ID & string => {\n if (typeof id === 'string') {\n return id;\n }\n\n return `${id}`;\n};\n\nconst deleteOldComponents = async <TUID extends UID.Schema>(\n uid: TUID,\n componentUID: UID.Component,\n entityToUpdate: { id: Modules.EntityService.Params.Attribute.ID },\n attributeName: string,\n componentValue: ComponentValue\n) => {\n const previousValue = (await strapi.db\n .query(uid)\n .load(entityToUpdate, attributeName)) as ComponentValue;\n const idsToKeep = _.castArray(componentValue).filter(has('id')).map(pickStringifiedId);\n const allIds = _.castArray(previousValue).filter(has('id')).map(pickStringifiedId);\n\n idsToKeep.forEach((id) => {\n if (!allIds.includes(id)) {\n throw new errors.ApplicationError(\n `Some of the provided components in ${attributeName} are not related to the entity`\n );\n }\n });\n\n const idsToDelete = _.difference(allIds, idsToKeep);\n\n if (idsToDelete.length > 0) {\n for (const idToDelete of idsToDelete) {\n await deleteComponent(componentUID, { id: idToDelete });\n }\n }\n};\n\nconst deleteOldDZComponents = async <TUID extends UID.Schema>(\n uid: TUID,\n entityToUpdate: { id: Modules.EntityService.Params.Attribute.ID },\n attributeName: string,\n dynamiczoneValues: DynamicZoneValue\n) => {\n const previousValue = (await strapi.db\n .query(uid)\n .load(entityToUpdate, attributeName)) as DynamicZoneValue;\n\n const idsToKeep = _.castArray(dynamiczoneValues)\n .filter(has('id'))\n .map((v) => ({\n id: pickStringifiedId(v),\n __component: v.__component,\n }));\n\n const allIds = _.castArray(previousValue)\n .filter(has('id'))\n .map((v) => ({\n id: pickStringifiedId(v),\n __component: v.__component,\n }));\n\n idsToKeep.forEach(({ id, __component }) => {\n if (!allIds.find((el) => el.id === id && el.__component === __component)) {\n const err = new Error(\n `Some of the provided components in ${attributeName} are not related to the entity`\n );\n\n Object.assign(err, { status: 400 });\n throw err;\n }\n });\n\n type IdsToDelete = DynamicZoneValue;\n\n const idsToDelete = allIds.reduce((acc, { id, __component }) => {\n if (!idsToKeep.find((el) => el.id === id && el.__component === __component)) {\n acc.push({ id, __component });\n }\n\n return acc;\n }, [] as IdsToDelete);\n\n if (idsToDelete.length > 0) {\n for (const idToDelete of idsToDelete) {\n const { id, __component } = idToDelete;\n await deleteComponent(__component, { id });\n }\n }\n};\n\nconst deleteComponents = async <TUID extends UID.Schema, TEntity extends Data.Entity<TUID>>(\n uid: TUID,\n entityToDelete: TEntity,\n { loadComponents = true } = {}\n) => {\n const { attributes = {} } = strapi.getModel(uid);\n\n const attributeNames = Object.keys(attributes);\n\n for (const attributeName of attributeNames) {\n const attribute = attributes[attributeName];\n\n if (attribute.type === 'component' || attribute.type === 'dynamiczone') {\n let value;\n\n if (loadComponents) {\n value = await strapi.db.query(uid).load(entityToDelete, attributeName);\n } else {\n value = entityToDelete[attributeName as keyof TEntity];\n }\n\n if (!value) {\n continue;\n }\n\n if (attribute.type === 'component') {\n const { component: componentUID } = attribute;\n await async.map(_.castArray(value), (subValue: any) =>\n deleteComponent(componentUID, subValue)\n );\n } else {\n await async.map(_.castArray(value), (subValue: any) =>\n deleteComponent(subValue.__component, subValue)\n );\n }\n\n continue;\n }\n }\n};\n\n/** *************************\n Component queries\n************************** */\n\n// components can have nested compos so this must be recursive\nconst createComponent = async <TUID extends UID.Component>(uid: TUID, data: Input<TUID>) => {\n const schema = strapi.getModel(uid);\n\n const componentData = await createComponents(uid, data);\n\n const transform = pipe(\n // Make sure we don't save the component with a pre-defined ID\n omit('id'),\n assignComponentData(schema, componentData)\n );\n\n return strapi.db.query(uid).create({ data: transform(data) });\n};\n\n// components can have nested compos so this must be recursive\nconst updateComponent = async <TUID extends UID.Component>(\n uid: TUID,\n componentToUpdate: { id: Modules.EntityService.Params.Attribute.ID },\n data: Input<TUID>\n) => {\n const schema = strapi.getModel(uid);\n\n const componentData = await updateComponents(uid, componentToUpdate, data);\n\n return strapi.db.query(uid).update({\n where: {\n id: componentToUpdate.id,\n },\n data: assignComponentData(schema, componentData, data),\n });\n};\n\nconst updateOrCreateComponent = <TUID extends UID.Component>(\n componentUID: TUID,\n value: Input<TUID>\n) => {\n if (value === null) {\n return null;\n }\n\n // update\n if ('id' in value && typeof value.id !== 'undefined') {\n // TODO: verify the compo is associated with the entity\n return updateComponent(componentUID, { id: value.id }, value);\n }\n\n // create\n return createComponent(componentUID, value);\n};\n\nconst deleteComponent = async <TUID extends UID.Component>(\n uid: TUID,\n componentToDelete: Data.Component<TUID>\n) => {\n await deleteComponents(uid, componentToDelete);\n await strapi.db.query(uid).delete({ where: { id: componentToDelete.id } });\n};\n\nconst assignComponentData = curry(\n (schema: Schema.Schema, componentData: ComponentBody, data: Input<UID.Schema>) => {\n return pipe(omitComponentData(schema), assign(componentData))(data);\n }\n);\n\nexport {\n omitComponentData,\n assignComponentData,\n getComponents,\n createComponents,\n updateComponents,\n deleteComponents,\n deleteComponent,\n};\n"],"names":["omitComponentData","curry","schema","data","attributes","componentAttributes","Object","keys","filter","attributeName","contentTypesUtils","isComponentAttribute","omit","createComponents","uid","strapi","getModel","componentBody","attributeNames","attribute","has","type","component","componentUID","repeatable","componentValue","Array","isArray","Error","components","async","map","value","createComponent","id","__pivot","field","component_type","dynamiczoneValues","createDynamicZoneComponents","__component","getComponents","entity","getComponentAttributes","_","isEmpty","db","query","load","updateComponents","entityToUpdate","deleteOldComponents","updateOrCreateComponent","negate","isNil","deleteOldDZComponents","pickStringifiedId","previousValue","idsToKeep","castArray","allIds","forEach","includes","errors","ApplicationError","idsToDelete","difference","length","idToDelete","deleteComponent","v","find","el","err","assign","status","reduce","acc","push","deleteComponents","entityToDelete","loadComponents","subValue","componentData","transform","pipe","assignComponentData","create","updateComponent","componentToUpdate","update","where","componentToDelete","delete"],"mappings":";;;;;;AAwBMA,MAAAA,iBAAAA,GAAoBC,QACxB,CAAA,CAACC,MAAuBC,EAAAA,IAAAA,GAAAA;IACtB,MAAM,EAAEC,UAAU,EAAE,GAAGF,MAAAA;AACvB,IAAA,MAAMG,mBAAsBC,GAAAA,MAAAA,CAAOC,IAAI,CAACH,YAAYI,MAAM,CAAC,CAACC,aAAAA,GAC1DC,wBAAkBC,CAAAA,oBAAoB,CAACP,UAAU,CAACK,aAAc,CAAA,CAAA,CAAA;AAGlE,IAAA,OAAOG,QAAKP,mBAAqBF,EAAAA,IAAAA,CAAAA;AACnC,CAAA;AAGF;AACMU,MAAAA,gBAAAA,GAAmB,OACvBC,GACAX,EAAAA,IAAAA,GAAAA;IAEA,MAAM,EAAEC,aAAa,EAAE,EAAE,GAAGW,MAAAA,CAAOC,QAAQ,CAACF,GAAAA,CAAAA;AAE5C,IAAA,MAAMG,gBAA+B,EAAC;IAEtC,MAAMC,cAAAA,GAAiBZ,MAAOC,CAAAA,IAAI,CAACH,UAAAA,CAAAA;IAEnC,KAAK,MAAMK,iBAAiBS,cAAgB,CAAA;QAC1C,MAAMC,SAAAA,GAAYf,UAAU,CAACK,aAAc,CAAA;QAE3C,IAAI,CAACW,OAAIX,aAAeN,EAAAA,IAAAA,CAAAA,IAAS,CAACO,wBAAkBC,CAAAA,oBAAoB,CAACQ,SAAY,CAAA,EAAA;AACnF,YAAA;AACF;QAEA,IAAIA,SAAAA,CAAUE,IAAI,KAAK,WAAa,EAAA;AAClC,YAAA,MAAM,EAAEC,SAAWC,EAAAA,YAAY,EAAEC,UAAa,GAAA,KAAK,EAAE,GAAGL,SAAAA;YAExD,MAAMM,cAAAA,GAAiBtB,IAAI,CAACM,aAA6B,CAAA;AAEzD,YAAA,IAAIgB,mBAAmB,IAAM,EAAA;AAC3B,gBAAA;AACF;AAEA,YAAA,IAAID,e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,kBAAkBvD;AACxB,SAAA;QACA/B,IAAMmF,EAAAA,mBAAAA,CAAoBpF,QAAQiF,aAAehF,EAAAA,IAAAA;AACnD,KAAA,CAAA;AACF,CAAA;AAEA,MAAMiD,uBAAAA,GAA0B,CAC9B7B,YACAS,EAAAA,KAAAA,GAAAA;AAEA,IAAA,IAAIA,UAAU,IAAM,EAAA;QAClB,OAAO,IAAA;AACT;;AAGA,IAAA,IAAI,QAAQA,KAAS,IAAA,OAAOA,KAAME,CAAAA,EAAE,KAAK,WAAa,EAAA;;AAEpD,QAAA,OAAOsD,gBAAgBjE,YAAc,EAAA;AAAEW,YAAAA,EAAAA,EAAIF,MAAME;SAAMF,EAAAA,KAAAA,CAAAA;AACzD;;AAGA,IAAA,OAAOC,gBAAgBV,YAAcS,EAAAA,KAAAA,CAAAA;AACvC,CAAA;AAEMqC,MAAAA,eAAAA,GAAkB,OACtBvD,GACA8E,EAAAA,iBAAAA,GAAAA;AAEA,IAAA,MAAMb,iBAAiBjE,GAAK8E,EAAAA,iBAAAA,CAAAA;AAC5B,IAAA,MAAM7E,OAAO+B,EAAE,CAACC,KAAK,CAACjC,GAAAA,CAAAA,CAAK+E,MAAM,CAAC;QAAEF,KAAO,EAAA;AAAEzD,YAAAA,EAAAA,EAAI0D,kBAAkB1D;AAAG;AAAE,KAAA,CAAA;AAC1E;AAEA,MAAMoD,mBAAsBrF,GAAAA,QAAAA,CAC1B,CAACC,MAAAA,EAAuBiF,aAA8BhF,EAAAA,IAAAA,GAAAA;AACpD,IAAA,OAAOkF,OAAKrF,CAAAA,iBAAAA,CAAkBE,MAASwE,CAAAA,EAAAA,SAAAA,CAAOS,aAAgBhF,CAAAA,CAAAA,CAAAA,IAAAA,CAAAA;AAChE,CAAA;;;;;;;;;;"}
|
1
|
+
{"version":3,"file":"components.js","sources":["../../../src/services/document-service/components.ts"],"sourcesContent":["import _ from 'lodash';\nimport { has, omit, pipe, assign, curry } from 'lodash/fp';\nimport type { Utils, UID, Schema, Data, Modules } from '@strapi/types';\nimport { contentTypes as contentTypesUtils, async, errors } from '@strapi/utils';\nimport {\n getComponentJoinTableName,\n getComponentJoinColumnEntityName,\n getComponentJoinColumnInverseName,\n getComponentTypeColumn,\n} from '../../utils/transform-content-types-to-models';\n\n// type aliases for readability\ntype Input<T extends UID.Schema> = Modules.Documents.Params.Data.Input<T>;\n\ntype LoadedComponents<TUID extends UID.Schema> = Data.Entity<\n TUID,\n Schema.AttributeNamesByType<TUID, 'component' | 'dynamiczone'>\n>;\n\ntype SingleComponentValue = Schema.Attribute.ComponentValue<UID.Component, false>;\ntype RepeatableComponentValue = Schema.Attribute.ComponentValue<UID.Component, true>;\n\ntype ComponentValue = SingleComponentValue | RepeatableComponentValue;\n\ntype DynamicZoneValue = Schema.Attribute.DynamicZoneValue<UID.Component[]>;\n\ntype ComponentBody = {\n [key: string]: ComponentValue | DynamicZoneValue;\n};\n\nconst omitComponentData = curry(\n (schema: Schema.Schema, data: Input<UID.Schema>): Partial<Input<UID.Schema>> => {\n const { attributes } = schema;\n const componentAttributes = Object.keys(attributes).filter((attributeName) =>\n contentTypesUtils.isComponentAttribute(attributes[attributeName])\n );\n\n return omit(componentAttributes, data);\n }\n);\n\n// NOTE: we could generalize the logic to allow CRUD of relation directly in the DB layer\nconst createComponents = async <TUID extends UID.Schema, TData extends Input<TUID>>(\n uid: TUID,\n data: TData\n) => {\n const { attributes = {} } = strapi.getModel(uid);\n\n const componentBody: ComponentBody = {};\n\n const attributeNames = Object.keys(attributes);\n\n for (const attributeName of attributeNames) {\n const attribute = attributes[attributeName];\n\n if (!has(attributeName, data) || !contentTypesUtils.isComponentAttribute(attribute)) {\n continue;\n }\n\n if (attribute.type === 'component') {\n const { component: componentUID, repeatable = false } = attribute;\n\n const componentValue = data[attributeName as keyof TData];\n\n if (componentValue === null) {\n continue;\n }\n\n if (repeatable === true) {\n if (!Array.isArray(componentValue)) {\n throw new Error('Expected an array to create repeatable component');\n }\n\n const components: RepeatableComponentValue = await async.map(componentValue, (value: any) =>\n createComponent(componentUID, value)\n );\n\n componentBody[attributeName] = components.map(({ id }) => {\n return {\n id,\n __pivot: {\n field: attributeName,\n component_type: componentUID,\n },\n };\n });\n } else {\n const component = await createComponent(\n componentUID,\n componentValue as Input<UID.Component>\n );\n\n componentBody[attributeName] = {\n id: component.id,\n __pivot: {\n field: attributeName,\n component_type: componentUID,\n },\n };\n }\n\n continue;\n }\n\n if (attribute.type === 'dynamiczone') {\n const dynamiczoneValues = data[\n attributeName as keyof TData\n ] as Modules.EntityService.Params.Attribute.GetValue<Schema.Attribute.DynamicZone>;\n\n if (!Array.isArray(dynamiczoneValues)) {\n throw new Error('Expected an array to create repeatable component');\n }\n\n const createDynamicZoneComponents = async (\n value: Utils.Array.Values<typeof dynamiczoneValues>\n ) => {\n const { id } = await createComponent(value.__component, value);\n return {\n id,\n __component: value.__component,\n __pivot: {\n field: attributeName,\n },\n };\n };\n\n // MySQL/MariaDB can cause deadlocks here if concurrency higher than 1\n componentBody[attributeName] = await async.map(\n dynamiczoneValues,\n createDynamicZoneComponents\n );\n\n continue;\n }\n }\n\n return componentBody;\n};\n\nconst getComponents = async <TUID extends UID.Schema>(\n uid: TUID,\n entity: { id: Modules.EntityService.Params.Attribute.ID }\n): Promise<LoadedComponents<TUID>> => {\n const componentAttributes = contentTypesUtils.getComponentAttributes(strapi.getModel(uid));\n\n if (_.isEmpty(componentAttributes)) {\n return {} as LoadedComponents<TUID>;\n }\n\n return strapi.db.query(uid).load(entity, componentAttributes) as Promise<LoadedComponents<TUID>>;\n};\n\n/*\n delete old components\n create or update\n*/\nconst updateComponents = async <TUID extends UID.Schema, TData extends Partial<Input<TUID>>>(\n uid: TUID,\n entityToUpdate: { id: Modules.EntityService.Params.Attribute.ID },\n data: TData\n) => {\n const { attributes = {} } = strapi.getModel(uid);\n\n const componentBody: ComponentBody = {};\n\n for (const attributeName of Object.keys(attributes)) {\n const attribute = attributes[attributeName];\n\n if (!has(attributeName, data)) {\n continue;\n }\n\n if (attribute.type === 'component') {\n const { component: componentUID, repeatable = false } = attribute;\n\n const componentValue = data[attributeName as keyof TData] as ComponentValue;\n await deleteOldComponents(uid, componentUID, entityToUpdate, attributeName, componentValue);\n\n if (repeatable === true) {\n if (!Array.isArray(componentValue)) {\n throw new Error('Expected an array to create repeatable component');\n }\n\n // MySQL/MariaDB can cause deadlocks here if concurrency higher than 1\n const components: RepeatableComponentValue = await async.map(componentValue, (value: any) =>\n updateOrCreateComponent(componentUID, value)\n );\n\n componentBody[attributeName] = components.filter(_.negate(_.isNil)).map(({ id }) => {\n return {\n id,\n __pivot: {\n field: attributeName,\n component_type: componentUID,\n },\n };\n });\n } else {\n const component = await updateOrCreateComponent(componentUID, componentValue);\n componentBody[attributeName] = component && {\n id: component.id,\n __pivot: {\n field: attributeName,\n component_type: componentUID,\n },\n };\n }\n } else if (attribute.type === 'dynamiczone') {\n const dynamiczoneValues = data[attributeName as keyof TData] as DynamicZoneValue;\n\n await deleteOldDZComponents(uid, entityToUpdate, attributeName, dynamiczoneValues);\n\n if (!Array.isArray(dynamiczoneValues)) {\n throw new Error('Expected an array to create repeatable component');\n }\n\n // MySQL/MariaDB can cause deadlocks here if concurrency higher than 1\n componentBody[attributeName] = await async.map(dynamiczoneValues, async (value: any) => {\n const { id } = await updateOrCreateComponent(value.__component, value);\n\n return {\n id,\n __component: value.__component,\n __pivot: {\n field: attributeName,\n },\n };\n });\n }\n }\n\n return componentBody;\n};\n\nconst pickStringifiedId = ({\n id,\n}: {\n id: Modules.EntityService.Params.Attribute.ID;\n}): Modules.EntityService.Params.Attribute.ID & string => {\n if (typeof id === 'string') {\n return id;\n }\n\n return `${id}`;\n};\n\nconst deleteOldComponents = async <TUID extends UID.Schema>(\n uid: TUID,\n componentUID: UID.Component,\n entityToUpdate: { id: Modules.EntityService.Params.Attribute.ID },\n attributeName: string,\n componentValue: ComponentValue\n) => {\n const previousValue = (await strapi.db\n .query(uid)\n .load(entityToUpdate, attributeName)) as ComponentValue;\n const idsToKeep = _.castArray(componentValue).filter(has('id')).map(pickStringifiedId);\n const allIds = _.castArray(previousValue).filter(has('id')).map(pickStringifiedId);\n\n idsToKeep.forEach((id) => {\n if (!allIds.includes(id)) {\n throw new errors.ApplicationError(\n `Some of the provided components in ${attributeName} are not related to the entity`\n );\n }\n });\n\n const idsToDelete = _.difference(allIds, idsToKeep);\n\n if (idsToDelete.length > 0) {\n for (const idToDelete of idsToDelete) {\n await deleteComponent(componentUID, { id: idToDelete });\n }\n }\n};\n\nconst deleteOldDZComponents = async <TUID extends UID.Schema>(\n uid: TUID,\n entityToUpdate: { id: Modules.EntityService.Params.Attribute.ID },\n attributeName: string,\n dynamiczoneValues: DynamicZoneValue\n) => {\n const previousValue = (await strapi.db\n .query(uid)\n .load(entityToUpdate, attributeName)) as DynamicZoneValue;\n\n const idsToKeep = _.castArray(dynamiczoneValues)\n .filter(has('id'))\n .map((v) => ({\n id: pickStringifiedId(v),\n __component: v.__component,\n }));\n\n const allIds = _.castArray(previousValue)\n .filter(has('id'))\n .map((v) => ({\n id: pickStringifiedId(v),\n __component: v.__component,\n }));\n\n idsToKeep.forEach(({ id, __component }) => {\n if (!allIds.find((el) => el.id === id && el.__component === __component)) {\n const err = new Error(\n `Some of the provided components in ${attributeName} are not related to the entity`\n );\n\n Object.assign(err, { status: 400 });\n throw err;\n }\n });\n\n type IdsToDelete = DynamicZoneValue;\n\n const idsToDelete = allIds.reduce((acc, { id, __component }) => {\n if (!idsToKeep.find((el) => el.id === id && el.__component === __component)) {\n acc.push({ id, __component });\n }\n\n return acc;\n }, [] as IdsToDelete);\n\n if (idsToDelete.length > 0) {\n for (const idToDelete of idsToDelete) {\n const { id, __component } = idToDelete;\n await deleteComponent(__component, { id });\n }\n }\n};\n\nconst deleteComponents = async <TUID extends UID.Schema, TEntity extends Data.Entity<TUID>>(\n uid: TUID,\n entityToDelete: TEntity,\n { loadComponents = true } = {}\n) => {\n const { attributes = {} } = strapi.getModel(uid);\n\n const attributeNames = Object.keys(attributes);\n\n for (const attributeName of attributeNames) {\n const attribute = attributes[attributeName];\n\n if (attribute.type === 'component' || attribute.type === 'dynamiczone') {\n let value;\n\n if (loadComponents) {\n value = await strapi.db.query(uid).load(entityToDelete, attributeName);\n } else {\n value = entityToDelete[attributeName as keyof TEntity];\n }\n\n if (!value) {\n continue;\n }\n\n if (attribute.type === 'component') {\n const { component: componentUID } = attribute;\n await async.map(_.castArray(value), (subValue: any) =>\n deleteComponent(componentUID, subValue)\n );\n } else {\n await async.map(_.castArray(value), (subValue: any) =>\n deleteComponent(subValue.__component, subValue)\n );\n }\n\n continue;\n }\n }\n};\n\n/** *************************\n Component queries\n************************** */\n\n// components can have nested compos so this must be recursive\nconst createComponent = async <TUID extends UID.Component>(uid: TUID, data: Input<TUID>) => {\n const schema = strapi.getModel(uid);\n\n const componentData = await createComponents(uid, data);\n\n const transform = pipe(\n // Make sure we don't save the component with a pre-defined ID\n omit('id'),\n assignComponentData(schema, componentData)\n );\n\n return strapi.db.query(uid).create({ data: transform(data) });\n};\n\n// components can have nested compos so this must be recursive\nconst updateComponent = async <TUID extends UID.Component>(\n uid: TUID,\n componentToUpdate: { id: Modules.EntityService.Params.Attribute.ID },\n data: Input<TUID>\n) => {\n const schema = strapi.getModel(uid);\n\n const componentData = await updateComponents(uid, componentToUpdate, data);\n\n return strapi.db.query(uid).update({\n where: {\n id: componentToUpdate.id,\n },\n data: assignComponentData(schema, componentData, data),\n });\n};\n\nconst updateOrCreateComponent = <TUID extends UID.Component>(\n componentUID: TUID,\n value: Input<TUID>\n) => {\n if (value === null) {\n return null;\n }\n\n // update\n if ('id' in value && typeof value.id !== 'undefined') {\n // TODO: verify the compo is associated with the entity\n return updateComponent(componentUID, { id: value.id }, value);\n }\n\n // create\n return createComponent(componentUID, value);\n};\n\nconst deleteComponent = async <TUID extends UID.Component>(\n uid: TUID,\n componentToDelete: Data.Component<TUID>\n) => {\n await deleteComponents(uid, componentToDelete);\n await strapi.db.query(uid).delete({ where: { id: componentToDelete.id } });\n};\n\nconst assignComponentData = curry(\n (schema: Schema.Schema, componentData: ComponentBody, data: Input<UID.Schema>) => {\n return pipe(omitComponentData(schema), assign(componentData))(data);\n }\n);\n\n/** *************************\n Component relation handling for document operations\n************************** */\n\n/**\n * Find the parent entry of a component instance.\n *\n * Given a component model, a specific component instance id, and the list of\n * possible parent content types (those that can embed this component),\n * this function checks each parent's *_cmps join table to see if the component\n * instance is linked to a parent entity.\n *\n * - Returns the parent uid, parent table name, and parent id if found.\n * - Returns null if no parent relationship exists.\n */\nconst findComponentParent = async (\n componentSchema: Schema.Component,\n componentId: number | string,\n parentSchemasForComponent: Schema.ContentType[],\n opts?: { trx?: any }\n): Promise<{ uid: string; table: string; parentId: number | string } | null> => {\n if (!componentSchema?.uid) return null;\n\n const schemaBuilder = strapi.db.getSchemaConnection(opts?.trx);\n const withTrx = (qb: any) => (opts?.trx ? qb.transacting(opts.trx) : qb);\n\n for (const parent of parentSchemasForComponent) {\n if (!parent.collectionName) continue;\n\n // Use the exact same functions that create the tables\n const identifiers = strapi.db.metadata.identifiers;\n const joinTableName = getComponentJoinTableName(parent.collectionName, identifiers);\n\n try {\n const tableExists = await schemaBuilder.hasTable(joinTableName);\n if (!tableExists) continue;\n\n // Use the exact same functions that create the columns\n const entityIdColumn = getComponentJoinColumnEntityName(identifiers);\n const componentIdColumn = getComponentJoinColumnInverseName(identifiers);\n const componentTypeColumn = getComponentTypeColumn(identifiers);\n\n const parentRow = await withTrx(strapi.db.getConnection(joinTableName))\n .where({\n [componentIdColumn]: componentId,\n [componentTypeColumn]: componentSchema.uid,\n })\n .first(entityIdColumn);\n\n if (parentRow) {\n return {\n uid: parent.uid,\n table: parent.collectionName,\n parentId: parentRow[entityIdColumn],\n };\n }\n } catch {\n continue;\n }\n }\n\n return null;\n};\n\n/**\n * Finds content types that contain the given component and have draft & publish enabled.\n */\nconst getParentSchemasForComponent = (componentSchema: Schema.Component): Schema.ContentType[] => {\n return Object.values(strapi.contentTypes).filter((contentType: any) => {\n if (!contentType.options?.draftAndPublish) return false;\n\n return Object.values(contentType.attributes).some((attr: any) => {\n return (\n (attr.type === 'component' && attr.component === componentSchema.uid) ||\n (attr.type === 'dynamiczone' && attr.components?.includes(componentSchema.uid))\n );\n });\n });\n};\n\n/**\n * Determines if a component relation should be propagated to a new document version\n * when a document with draft and publish is updated.\n */\nconst shouldPropagateComponentRelationToNewVersion = async (\n componentRelation: Record<string, any>,\n componentSchema: Schema.Component,\n parentSchemasForComponent: Schema.ContentType[],\n trx: any\n): Promise<boolean> => {\n // Get the component ID column name using the actual component model name\n const componentIdColumn = strapi.db.metadata.identifiers.getJoinColumnAttributeIdName(\n _.snakeCase(componentSchema.modelName)\n );\n\n const componentId = componentRelation[componentIdColumn];\n\n const parent = await findComponentParent(\n componentSchema,\n componentId,\n parentSchemasForComponent,\n { trx }\n );\n\n // Keep relation if component has no parent entry\n if (!parent?.uid) {\n return true;\n }\n\n const parentContentType = strapi.contentTypes[parent.uid as UID.ContentType];\n\n // Keep relation if parent doesn't have draft & publish enabled\n if (!parentContentType?.options?.draftAndPublish) {\n return true;\n }\n\n // Discard relation if parent has draft & publish enabled\n return false;\n};\n\n/**\n * Creates a filter function for component relations that can be passed to the generic\n * unidirectional relations utility\n */\nconst createComponentRelationFilter = () => {\n return async (\n relation: Record<string, any>,\n model: Schema.Component | Schema.ContentType,\n trx: any\n ): Promise<boolean> => {\n // Only apply component-specific filtering for components\n if (model.modelType !== 'component') {\n return true;\n }\n\n const componentSchema = model as Schema.Component;\n const parentSchemas = getParentSchemasForComponent(componentSchema);\n\n // Exit if no draft & publish parent types exist\n if (parentSchemas.length === 0) {\n return true;\n }\n\n return shouldPropagateComponentRelationToNewVersion(\n relation,\n componentSchema,\n parentSchemas,\n trx\n );\n };\n};\n\nexport {\n omitComponentData,\n assignComponentData,\n getComponents,\n createComponents,\n updateComponents,\n deleteComponents,\n deleteComponent,\n createComponentRelationFilter,\n};\n"],"names":["omitComponentData","curry","schema","data","attributes","componentAttributes","Object","keys","filter","attributeName","contentTypesUtils","isComponentAttribute","omit","createComponents","uid","strapi","getModel","componentBody","attributeNames","attribute","has","type","component","componentUID","repeatable","componentValue","Array","isArray","Error","components","async","map","value","createComponent","id","__pivot","field","component_type","dynamiczoneValues","createDynamicZoneComponents","__component","getComponents","entity","getComponentAttributes","_","isEmpty","db","query","load","updateComponents","entityToUpdate","deleteOldComponents","updateOrCreateComponent","negate","isNil","deleteOldDZComponents","pickStringifiedId","previousValue","idsToKeep","castArray","allIds","forEach","includes","errors","ApplicationError","idsToDelete","difference","length","idToDelete","deleteComponent","v","find","el","err","assign","status","reduce","acc","push","deleteComponents","entityToDelete","loadComponents","subValue","componentData","transform","pipe","assignComponentData","create","updateComponent","componentToUpdate","update","where","componentToDelete","delete","findComponentParent","componentSchema","componentId","parentSchemasForComponent","opts","schemaBuilder","getSchemaConnection","trx","withTrx","qb","transacting","parent","collectionName","identifiers","metadata","joinTableName","getComponentJoinTableName","tableExists","hasTable","entityIdColumn","getComponentJoinColumnEntityName","componentIdColumn","getComponentJoinColumnInverseName","componentTypeColumn","getComponentTypeColumn","parentRow","getConnection","first","table","parentId","getParentSchemasForComponent","values","contentTypes","contentType","options","draftAndPublish","some","attr","shouldPropagateComponentRelationToNewVersion","componentRelation","getJoinColumnAttributeIdName","snakeCase","modelName","parentContentType","createComponentRelationFilter","relation","model","modelType","parentSchemas"],"mappings":";;;;;;;AA8BMA,MAAAA,iBAAAA,GAAoBC,QACxB,CAAA,CAACC,MAAuBC,EAAAA,IAAAA,GAAAA;IACtB,MAAM,EAAEC,UAAU,EAAE,GAAGF,MAAAA;AACvB,IAAA,MAAMG,mBAAsBC,GAAAA,MAAAA,CAAOC,IAAI,CAACH,YAAYI,MAAM,CAAC,CAACC,aAAAA,GAC1DC,wBAAkBC,CAAAA,oBAAoB,CAACP,UAAU,CAACK,aAAc,CAAA,CAAA,CAAA;AAGlE,IAAA,OAAOG,QAAKP,mBAAqBF,EAAAA,IAAAA,CAAAA;A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AAAA,KAAAA;AACxB,QAAA,MAAME,gBAAgBlB,4BAA6B7B,CAAAA,eAAAA,CAAAA;;QAGnD,IAAI+C,aAAAA,CAAc3E,MAAM,KAAK,CAAG,EAAA;YAC9B,OAAO,IAAA;AACT;QAEA,OAAOiE,4CAAAA,CACLO,QACA5C,EAAAA,eAAAA,EACA+C,aACAzC,EAAAA,GAAAA,CAAAA;AAEJ,KAAA;AACF;;;;;;;;;;;"}
|
@@ -1,6 +1,7 @@
|
|
1
1
|
import _ from 'lodash';
|
2
2
|
import { curry, omit, pipe, assign, has } from 'lodash/fp';
|
3
3
|
import { contentTypes, async, errors } from '@strapi/utils';
|
4
|
+
import { getComponentJoinTableName, getComponentJoinColumnEntityName, getComponentJoinColumnInverseName, getComponentTypeColumn } from '../../utils/transform-content-types-to-models.mjs';
|
4
5
|
|
5
6
|
const omitComponentData = curry((schema, data)=>{
|
6
7
|
const { attributes } = schema;
|
@@ -273,6 +274,101 @@ const deleteComponent = async (uid, componentToDelete)=>{
|
|
273
274
|
const assignComponentData = curry((schema, componentData, data)=>{
|
274
275
|
return pipe(omitComponentData(schema), assign(componentData))(data);
|
275
276
|
});
|
277
|
+
/** *************************
|
278
|
+
Component relation handling for document operations
|
279
|
+
************************** */ /**
|
280
|
+
* Find the parent entry of a component instance.
|
281
|
+
*
|
282
|
+
* Given a component model, a specific component instance id, and the list of
|
283
|
+
* possible parent content types (those that can embed this component),
|
284
|
+
* this function checks each parent's *_cmps join table to see if the component
|
285
|
+
* instance is linked to a parent entity.
|
286
|
+
*
|
287
|
+
* - Returns the parent uid, parent table name, and parent id if found.
|
288
|
+
* - Returns null if no parent relationship exists.
|
289
|
+
*/ const findComponentParent = async (componentSchema, componentId, parentSchemasForComponent, opts)=>{
|
290
|
+
if (!componentSchema?.uid) return null;
|
291
|
+
const schemaBuilder = strapi.db.getSchemaConnection(opts?.trx);
|
292
|
+
const withTrx = (qb)=>opts?.trx ? qb.transacting(opts.trx) : qb;
|
293
|
+
for (const parent of parentSchemasForComponent){
|
294
|
+
if (!parent.collectionName) continue;
|
295
|
+
// Use the exact same functions that create the tables
|
296
|
+
const identifiers = strapi.db.metadata.identifiers;
|
297
|
+
const joinTableName = getComponentJoinTableName(parent.collectionName, identifiers);
|
298
|
+
try {
|
299
|
+
const tableExists = await schemaBuilder.hasTable(joinTableName);
|
300
|
+
if (!tableExists) continue;
|
301
|
+
// Use the exact same functions that create the columns
|
302
|
+
const entityIdColumn = getComponentJoinColumnEntityName(identifiers);
|
303
|
+
const componentIdColumn = getComponentJoinColumnInverseName(identifiers);
|
304
|
+
const componentTypeColumn = getComponentTypeColumn(identifiers);
|
305
|
+
const parentRow = await withTrx(strapi.db.getConnection(joinTableName)).where({
|
306
|
+
[componentIdColumn]: componentId,
|
307
|
+
[componentTypeColumn]: componentSchema.uid
|
308
|
+
}).first(entityIdColumn);
|
309
|
+
if (parentRow) {
|
310
|
+
return {
|
311
|
+
uid: parent.uid,
|
312
|
+
table: parent.collectionName,
|
313
|
+
parentId: parentRow[entityIdColumn]
|
314
|
+
};
|
315
|
+
}
|
316
|
+
} catch {
|
317
|
+
continue;
|
318
|
+
}
|
319
|
+
}
|
320
|
+
return null;
|
321
|
+
};
|
322
|
+
/**
|
323
|
+
* Finds content types that contain the given component and have draft & publish enabled.
|
324
|
+
*/ const getParentSchemasForComponent = (componentSchema)=>{
|
325
|
+
return Object.values(strapi.contentTypes).filter((contentType)=>{
|
326
|
+
if (!contentType.options?.draftAndPublish) return false;
|
327
|
+
return Object.values(contentType.attributes).some((attr)=>{
|
328
|
+
return attr.type === 'component' && attr.component === componentSchema.uid || attr.type === 'dynamiczone' && attr.components?.includes(componentSchema.uid);
|
329
|
+
});
|
330
|
+
});
|
331
|
+
};
|
332
|
+
/**
|
333
|
+
* Determines if a component relation should be propagated to a new document version
|
334
|
+
* when a document with draft and publish is updated.
|
335
|
+
*/ const shouldPropagateComponentRelationToNewVersion = async (componentRelation, componentSchema, parentSchemasForComponent, trx)=>{
|
336
|
+
// Get the component ID column name using the actual component model name
|
337
|
+
const componentIdColumn = strapi.db.metadata.identifiers.getJoinColumnAttributeIdName(_.snakeCase(componentSchema.modelName));
|
338
|
+
const componentId = componentRelation[componentIdColumn];
|
339
|
+
const parent = await findComponentParent(componentSchema, componentId, parentSchemasForComponent, {
|
340
|
+
trx
|
341
|
+
});
|
342
|
+
// Keep relation if component has no parent entry
|
343
|
+
if (!parent?.uid) {
|
344
|
+
return true;
|
345
|
+
}
|
346
|
+
const parentContentType = strapi.contentTypes[parent.uid];
|
347
|
+
// Keep relation if parent doesn't have draft & publish enabled
|
348
|
+
if (!parentContentType?.options?.draftAndPublish) {
|
349
|
+
return true;
|
350
|
+
}
|
351
|
+
// Discard relation if parent has draft & publish enabled
|
352
|
+
return false;
|
353
|
+
};
|
354
|
+
/**
|
355
|
+
* Creates a filter function for component relations that can be passed to the generic
|
356
|
+
* unidirectional relations utility
|
357
|
+
*/ const createComponentRelationFilter = ()=>{
|
358
|
+
return async (relation, model, trx)=>{
|
359
|
+
// Only apply component-specific filtering for components
|
360
|
+
if (model.modelType !== 'component') {
|
361
|
+
return true;
|
362
|
+
}
|
363
|
+
const componentSchema = model;
|
364
|
+
const parentSchemas = getParentSchemasForComponent(componentSchema);
|
365
|
+
// Exit if no draft & publish parent types exist
|
366
|
+
if (parentSchemas.length === 0) {
|
367
|
+
return true;
|
368
|
+
}
|
369
|
+
return shouldPropagateComponentRelationToNewVersion(relation, componentSchema, parentSchemas, trx);
|
370
|
+
};
|
371
|
+
};
|
276
372
|
|
277
|
-
export { assignComponentData, createComponents, deleteComponent, deleteComponents, getComponents, omitComponentData, updateComponents };
|
373
|
+
export { assignComponentData, createComponentRelationFilter, createComponents, deleteComponent, deleteComponents, getComponents, omitComponentData, updateComponents };
|
278
374
|
//# sourceMappingURL=components.mjs.map
|