@strapi/core 5.61.1 → 5.62.0

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.
Files changed (140) hide show
  1. package/dist/Strapi.d.ts.map +1 -1
  2. package/dist/Strapi.js +2 -0
  3. package/dist/Strapi.js.map +1 -1
  4. package/dist/Strapi.mjs +2 -0
  5. package/dist/Strapi.mjs.map +1 -1
  6. package/dist/ee/entitlements.d.ts +6 -0
  7. package/dist/ee/entitlements.d.ts.map +1 -1
  8. package/dist/ee/entitlements.js +17 -4
  9. package/dist/ee/entitlements.js.map +1 -1
  10. package/dist/ee/entitlements.mjs +17 -5
  11. package/dist/ee/entitlements.mjs.map +1 -1
  12. package/dist/ee/index.d.ts +7 -12
  13. package/dist/ee/index.d.ts.map +1 -1
  14. package/dist/ee/index.js +19 -6
  15. package/dist/ee/index.js.map +1 -1
  16. package/dist/ee/index.mjs +21 -8
  17. package/dist/ee/index.mjs.map +1 -1
  18. package/dist/factories.d.ts +1 -1
  19. package/dist/factories.d.ts.map +1 -1
  20. package/dist/factories.js.map +1 -1
  21. package/dist/factories.mjs.map +1 -1
  22. package/dist/middlewares/errors.d.ts.map +1 -1
  23. package/dist/middlewares/errors.js +9 -0
  24. package/dist/middlewares/errors.js.map +1 -1
  25. package/dist/middlewares/errors.mjs +9 -0
  26. package/dist/middlewares/errors.mjs.map +1 -1
  27. package/dist/package.json.js +15 -15
  28. package/dist/package.json.mjs +15 -15
  29. package/dist/providers/registries.d.ts.map +1 -1
  30. package/dist/providers/registries.js +1 -0
  31. package/dist/providers/registries.js.map +1 -1
  32. package/dist/providers/registries.mjs +1 -0
  33. package/dist/providers/registries.mjs.map +1 -1
  34. package/dist/registries/policies.d.ts.map +1 -1
  35. package/dist/registries/policies.js +20 -0
  36. package/dist/registries/policies.js.map +1 -1
  37. package/dist/registries/policies.mjs +20 -0
  38. package/dist/registries/policies.mjs.map +1 -1
  39. package/dist/services/content-api/index.d.ts.map +1 -1
  40. package/dist/services/document-service/common.d.ts.map +1 -1
  41. package/dist/services/document-service/common.js +1 -1
  42. package/dist/services/document-service/common.js.map +1 -1
  43. package/dist/services/document-service/common.mjs +1 -1
  44. package/dist/services/document-service/common.mjs.map +1 -1
  45. package/dist/services/document-service/components.d.ts.map +1 -1
  46. package/dist/services/document-service/components.js +3 -3
  47. package/dist/services/document-service/components.js.map +1 -1
  48. package/dist/services/document-service/components.mjs +3 -3
  49. package/dist/services/document-service/components.mjs.map +1 -1
  50. package/dist/services/document-service/entries.js +3 -3
  51. package/dist/services/document-service/entries.js.map +1 -1
  52. package/dist/services/document-service/entries.mjs +3 -3
  53. package/dist/services/document-service/entries.mjs.map +1 -1
  54. package/dist/services/document-service/internationalization.d.ts +13 -1
  55. package/dist/services/document-service/internationalization.d.ts.map +1 -1
  56. package/dist/services/document-service/internationalization.js +58 -17
  57. package/dist/services/document-service/internationalization.js.map +1 -1
  58. package/dist/services/document-service/internationalization.mjs +59 -18
  59. package/dist/services/document-service/internationalization.mjs.map +1 -1
  60. package/dist/services/document-service/middlewares/errors.d.ts.map +1 -1
  61. package/dist/services/document-service/middlewares/errors.js +3 -12
  62. package/dist/services/document-service/middlewares/errors.js.map +1 -1
  63. package/dist/services/document-service/middlewares/errors.mjs +4 -13
  64. package/dist/services/document-service/middlewares/errors.mjs.map +1 -1
  65. package/dist/services/document-service/middlewares/middleware-manager.js +3 -3
  66. package/dist/services/document-service/middlewares/middleware-manager.js.map +1 -1
  67. package/dist/services/document-service/middlewares/middleware-manager.mjs +3 -3
  68. package/dist/services/document-service/middlewares/middleware-manager.mjs.map +1 -1
  69. package/dist/services/document-service/repository.d.ts.map +1 -1
  70. package/dist/services/document-service/repository.js +46 -10
  71. package/dist/services/document-service/repository.js.map +1 -1
  72. package/dist/services/document-service/repository.mjs +47 -11
  73. package/dist/services/document-service/repository.mjs.map +1 -1
  74. package/dist/services/document-service/transform/data.d.ts +1 -1
  75. package/dist/services/document-service/transform/data.d.ts.map +1 -1
  76. package/dist/services/document-service/transform/data.js +1 -1
  77. package/dist/services/document-service/transform/data.js.map +1 -1
  78. package/dist/services/document-service/transform/data.mjs +1 -1
  79. package/dist/services/document-service/transform/data.mjs.map +1 -1
  80. package/dist/services/document-service/transform/populate.js +1 -1
  81. package/dist/services/document-service/transform/populate.js.map +1 -1
  82. package/dist/services/document-service/transform/populate.mjs +1 -1
  83. package/dist/services/document-service/transform/populate.mjs.map +1 -1
  84. package/dist/services/document-service/transform/relations/extract/data-ids.d.ts +1 -1
  85. package/dist/services/document-service/transform/relations/extract/data-ids.d.ts.map +1 -1
  86. package/dist/services/document-service/transform/relations/extract/data-ids.js +1 -1
  87. package/dist/services/document-service/transform/relations/extract/data-ids.js.map +1 -1
  88. package/dist/services/document-service/transform/relations/extract/data-ids.mjs +1 -1
  89. package/dist/services/document-service/transform/relations/extract/data-ids.mjs.map +1 -1
  90. package/dist/services/document-service/transform/relations/transform/data-ids.d.ts +1 -1
  91. package/dist/services/document-service/transform/relations/transform/data-ids.d.ts.map +1 -1
  92. package/dist/services/document-service/transform/relations/transform/default-locale.d.ts +1 -1
  93. package/dist/services/document-service/transform/relations/transform/default-locale.d.ts.map +1 -1
  94. package/dist/services/document-service/transform/relations/transform/default-locale.js +0 -1
  95. package/dist/services/document-service/transform/relations/transform/default-locale.js.map +1 -1
  96. package/dist/services/document-service/transform/relations/transform/default-locale.mjs +0 -1
  97. package/dist/services/document-service/transform/relations/transform/default-locale.mjs.map +1 -1
  98. package/dist/services/document-service/transform/relations/utils/map-relation.d.ts +1 -1
  99. package/dist/services/document-service/transform/relations/utils/map-relation.d.ts.map +1 -1
  100. package/dist/services/document-service/transform/relations/utils/map-relation.js +1 -1
  101. package/dist/services/document-service/transform/relations/utils/map-relation.js.map +1 -1
  102. package/dist/services/document-service/transform/relations/utils/map-relation.mjs +1 -1
  103. package/dist/services/document-service/transform/relations/utils/map-relation.mjs.map +1 -1
  104. package/dist/services/document-service/utils/clean-component-join-table.js +1 -1
  105. package/dist/services/document-service/utils/clean-component-join-table.js.map +1 -1
  106. package/dist/services/document-service/utils/clean-component-join-table.mjs +1 -1
  107. package/dist/services/document-service/utils/clean-component-join-table.mjs.map +1 -1
  108. package/dist/services/document-service/utils/clone-relations.d.ts.map +1 -1
  109. package/dist/services/document-service/utils/clone-relations.js +249 -190
  110. package/dist/services/document-service/utils/clone-relations.js.map +1 -1
  111. package/dist/services/document-service/utils/clone-relations.mjs +250 -191
  112. package/dist/services/document-service/utils/clone-relations.mjs.map +1 -1
  113. package/dist/services/entity-service/index.d.ts.map +1 -1
  114. package/dist/services/entity-service/index.js +3 -12
  115. package/dist/services/entity-service/index.js.map +1 -1
  116. package/dist/services/entity-service/index.mjs +4 -13
  117. package/dist/services/entity-service/index.mjs.map +1 -1
  118. package/dist/services/metrics/sender.d.ts.map +1 -1
  119. package/dist/services/metrics/sender.js +4 -1
  120. package/dist/services/metrics/sender.js.map +1 -1
  121. package/dist/services/metrics/sender.mjs +4 -1
  122. package/dist/services/metrics/sender.mjs.map +1 -1
  123. package/dist/services/reloader.d.ts +1 -0
  124. package/dist/services/reloader.d.ts.map +1 -1
  125. package/dist/services/reloader.js +5 -0
  126. package/dist/services/reloader.js.map +1 -1
  127. package/dist/services/reloader.mjs +5 -0
  128. package/dist/services/reloader.mjs.map +1 -1
  129. package/dist/services/server/middleware.d.ts.map +1 -1
  130. package/dist/services/server/middleware.js +16 -4
  131. package/dist/services/server/middleware.js.map +1 -1
  132. package/dist/services/server/middleware.mjs +16 -4
  133. package/dist/services/server/middleware.mjs.map +1 -1
  134. package/dist/utils/database-input-errors.d.ts +11 -0
  135. package/dist/utils/database-input-errors.d.ts.map +1 -0
  136. package/dist/utils/database-input-errors.js +23 -0
  137. package/dist/utils/database-input-errors.js.map +1 -0
  138. package/dist/utils/database-input-errors.mjs +20 -0
  139. package/dist/utils/database-input-errors.mjs.map +1 -0
  140. package/package.json +15 -15
@@ -1,4 +1,4 @@
1
- import { merge, set, get, unset, has } from 'lodash';
1
+ import { merge, set, unset, get, has } from 'lodash';
2
2
  import { traverseEntity, contentTypes } from '@strapi/utils';
3
3
  import { traverseEntityRelations as traverseEntityRelationsCurried } from '../transform/relations/utils/map-relation.mjs';
4
4
  import { isLocalizedContentType, getDefaultLocale, getRelationTargetLocale } from '../transform/relations/utils/i18n.mjs';
@@ -17,10 +17,10 @@ const isRelationAttribute = (attribute)=>{
17
17
  return isRecord(attribute) && attribute.type === 'relation' && typeof attribute.relation === 'string';
18
18
  };
19
19
  const isRelationOperationPayload = (value)=>{
20
- return isRecord(value) && RELATION_OPERATIONS.some((operation)=>Object.prototype.hasOwnProperty.call(value, operation));
20
+ return isRecord(value) && RELATION_OPERATIONS.some((operation)=>Object.hasOwn(value, operation));
21
21
  };
22
22
  const hasMeaningfulRelationOperations = (value)=>{
23
- if (Object.prototype.hasOwnProperty.call(value, 'set')) {
23
+ if (Object.hasOwn(value, 'set')) {
24
24
  return true;
25
25
  }
26
26
  return [
@@ -82,7 +82,7 @@ const isRelationUnchangedAtPath = (submitted, path, submittedValue)=>{
82
82
  return false;
83
83
  }
84
84
  current = current[index];
85
- } else if (isRecord(current) && Object.prototype.hasOwnProperty.call(current, segment)) {
85
+ } else if (isRecord(current) && Object.hasOwn(current, segment)) {
86
86
  current = current[segment];
87
87
  } else {
88
88
  return false;
@@ -97,10 +97,10 @@ const isRelationUnchangedAtPath = (submitted, path, submittedValue)=>{
97
97
  * Like `traverseEntityRelations`, but also visits inline storage (`useJoinTable: false`
98
98
  * and `morphToOne`). Clone follow-up must see those relations; the shared transform
99
99
  * helper intentionally skips them because they are handled in processData.
100
- */ const traverseAllEntityRelations = async (visitor, options, data)=>{
101
- return traverseEntity(async (visitorOptions, utils)=>{
100
+ */ const traverseAllEntityRelations = (visitor, options, data)=>{
101
+ return traverseEntity((visitorOptions, utils)=>{
102
102
  const { attribute } = visitorOptions;
103
- if (!attribute || attribute.type !== 'relation') {
103
+ if (attribute?.type !== 'relation') {
104
104
  return;
105
105
  }
106
106
  return visitor(visitorOptions, utils);
@@ -196,6 +196,51 @@ const collectNestedCloneRelationAdjustments = async (originalData, submitted, co
196
196
  postCloneUpdates
197
197
  };
198
198
  };
199
+ const planRootCloneRelation = (attributeName, attribute, submitted, originalData)=>{
200
+ if (!isRelationAttribute(attribute)) {
201
+ return {
202
+ kind: 'keep'
203
+ };
204
+ }
205
+ const submittedValue = submitted[attributeName];
206
+ const relationWasSubmitted = Object.hasOwn(submitted, attributeName);
207
+ const isOperationPayload = isRelationOperationPayload(submittedValue);
208
+ const hasMeaningfulOperations = isOperationPayload && hasMeaningfulRelationOperations(submittedValue);
209
+ const relationIsUnchanged = !relationWasSubmitted || isOperationPayload && !hasMeaningfulOperations;
210
+ if (relationIsUnchanged && isBidirectionalOneToOne(attribute) && usesJoinTable(attribute) && hasPopulatedRelation(originalData[attributeName])) {
211
+ return {
212
+ kind: 'defer',
213
+ copyKind: 'joinTable'
214
+ };
215
+ }
216
+ if (relationIsUnchanged && isBidirectionalOneToOne(attribute) && !usesJoinTable(attribute) && hasPopulatedRelation(originalData[attributeName])) {
217
+ return {
218
+ kind: 'defer',
219
+ copyKind: 'fkColumn'
220
+ };
221
+ }
222
+ // Always defer morphToOne: populate can be null even when morph columns are set.
223
+ if (relationIsUnchanged && isMorphToOneAttribute(attribute)) {
224
+ return {
225
+ kind: 'defer',
226
+ copyKind: 'morphToOne'
227
+ };
228
+ }
229
+ if (relationIsUnchanged && isBidirectionalOneToManyMappedBy(attribute) && hasPopulatedRelationList(originalData[attributeName])) {
230
+ return {
231
+ kind: 'omit'
232
+ };
233
+ }
234
+ if (!relationIsUnchanged && isOperationPayload && hasMeaningfulOperations && (!usesJoinTable(attribute) || isMorphToOneAttribute(attribute))) {
235
+ return {
236
+ kind: 'postClone',
237
+ value: submittedValue
238
+ };
239
+ }
240
+ return {
241
+ kind: 'keep'
242
+ };
243
+ };
199
244
  const prepareCloneData = async (originalData, submittedData, contentType, getModel)=>{
200
245
  const submitted = submittedData ?? {};
201
246
  const relationOperationOverrides = await collectRelationOperationOverrides(submitted, contentType, getModel);
@@ -203,54 +248,24 @@ const prepareCloneData = async (originalData, submittedData, contentType, getMod
203
248
  const deferredCopies = [];
204
249
  const postCloneUpdates = [];
205
250
  for (const [attributeName, attribute] of Object.entries(contentType.attributes)){
206
- if (!isRelationAttribute(attribute)) {
207
- continue;
208
- }
209
- const submittedValue = submitted[attributeName];
210
- const relationWasSubmitted = Object.prototype.hasOwnProperty.call(submitted, attributeName);
211
- const isOperationPayload = isRelationOperationPayload(submittedValue);
212
- const hasMeaningfulOperations = isOperationPayload && hasMeaningfulRelationOperations(submittedValue);
213
- const relationIsUnchanged = !relationWasSubmitted || isOperationPayload && !hasMeaningfulOperations;
214
- if (relationIsUnchanged && isBidirectionalOneToOne(attribute) && usesJoinTable(attribute) && hasPopulatedRelation(originalData[attributeName])) {
215
- delete data[attributeName];
216
- deferredCopies.push({
217
- schemaUid: contentType.uid,
218
- attributeName,
219
- kind: 'joinTable',
220
- ownerPath: null
221
- });
222
- continue;
223
- }
224
- if (relationIsUnchanged && isBidirectionalOneToOne(attribute) && !usesJoinTable(attribute) && hasPopulatedRelation(originalData[attributeName])) {
225
- delete data[attributeName];
226
- deferredCopies.push({
227
- schemaUid: contentType.uid,
228
- attributeName,
229
- kind: 'fkColumn',
230
- ownerPath: null
231
- });
251
+ const plan = planRootCloneRelation(attributeName, attribute, submitted, originalData);
252
+ if (plan.kind === 'keep') {
232
253
  continue;
233
254
  }
234
- // Always defer morphToOne: populate can be null even when morph columns are set.
235
- if (relationIsUnchanged && isMorphToOneAttribute(attribute)) {
236
- delete data[attributeName];
255
+ delete data[attributeName];
256
+ if (plan.kind === 'defer') {
237
257
  deferredCopies.push({
238
258
  schemaUid: contentType.uid,
239
259
  attributeName,
240
- kind: 'morphToOne',
260
+ kind: plan.copyKind,
241
261
  ownerPath: null
242
262
  });
243
263
  continue;
244
264
  }
245
- if (relationIsUnchanged && isBidirectionalOneToManyMappedBy(attribute) && hasPopulatedRelationList(originalData[attributeName])) {
246
- delete data[attributeName];
247
- continue;
248
- }
249
- if (!relationIsUnchanged && isRelationOperationPayload(submittedValue) && hasMeaningfulOperations && (!usesJoinTable(attribute) || isMorphToOneAttribute(attribute))) {
250
- delete data[attributeName];
265
+ if (plan.kind === 'postClone') {
251
266
  postCloneUpdates.push({
252
267
  dataPath: attributeName,
253
- value: submittedValue,
268
+ value: plan.value,
254
269
  schemaUid: contentType.uid,
255
270
  ownerPath: null
256
271
  });
@@ -394,33 +409,41 @@ const copyMorphToOneRelation = async (strapi, targetUid, attributeName, sourceEn
394
409
  });
395
410
  });
396
411
  };
412
+ const runInOrder = async (items, run)=>{
413
+ await items.reduce((previous, item)=>previous.then(()=>run(item)), Promise.resolve());
414
+ };
415
+ const applyOneDeferredCloneRelationCopy = async (strapi, originalData, clonedData, sourceRootId, targetRootId, task)=>{
416
+ // Root owners (ownerPath === null) resolve via the fallback root IDs.
417
+ // Nested owners must resolve explicitly — falling back to the root ID would
418
+ // copy component-schema relation rows using an unrelated document ID.
419
+ const sourceEntryId = resolveOwnerEntryId(originalData, task.ownerPath, sourceRootId);
420
+ const targetEntryId = resolveOwnerEntryId(clonedData, task.ownerPath, targetRootId);
421
+ if (sourceEntryId == null || targetEntryId == null) {
422
+ throw new Error(`Unable to resolve clone relation owner for "${task.attributeName}" at path "${task.ownerPath ?? '<root>'}"`);
423
+ }
424
+ if (ownerComponentWasReplaced(originalData, clonedData, task.ownerPath)) {
425
+ return;
426
+ }
427
+ if (sourceEntryId === targetEntryId) {
428
+ return;
429
+ }
430
+ if (task.kind === 'joinTable') {
431
+ await copyCloneRelationRows(strapi, task.schemaUid, task.attributeName, sourceEntryId, targetEntryId);
432
+ return;
433
+ }
434
+ if (task.kind === 'fkColumn') {
435
+ await copyFkColumnRelation(strapi, task.schemaUid, task.attributeName, sourceEntryId, targetEntryId);
436
+ return;
437
+ }
438
+ if (task.kind === 'morphToOne') {
439
+ await copyMorphToOneRelation(strapi, task.schemaUid, task.attributeName, sourceEntryId, targetEntryId);
440
+ }
441
+ };
397
442
  const applyDeferredCloneRelationCopies = async (strapi, rootUid, sourceRootId, targetRootId, originalData, clonedData, deferredCopies)=>{
398
443
  if (deferredCopies.length === 0) {
399
444
  return;
400
445
  }
401
- for (const task of deferredCopies){
402
- // Root owners (ownerPath === null) resolve via the fallback root IDs.
403
- // Nested owners must resolve explicitly — falling back to the root ID would
404
- // copy component-schema relation rows using an unrelated document ID.
405
- const sourceEntryId = resolveOwnerEntryId(originalData, task.ownerPath, sourceRootId);
406
- const targetEntryId = resolveOwnerEntryId(clonedData, task.ownerPath, targetRootId);
407
- if (sourceEntryId == null || targetEntryId == null) {
408
- throw new Error(`Unable to resolve clone relation owner for "${task.attributeName}" at path "${task.ownerPath ?? '<root>'}"`);
409
- }
410
- if (ownerComponentWasReplaced(originalData, clonedData, task.ownerPath)) {
411
- continue;
412
- }
413
- if (sourceEntryId === targetEntryId) {
414
- continue;
415
- }
416
- if (task.kind === 'joinTable') {
417
- await copyCloneRelationRows(strapi, task.schemaUid, task.attributeName, sourceEntryId, targetEntryId);
418
- } else if (task.kind === 'fkColumn') {
419
- await copyFkColumnRelation(strapi, task.schemaUid, task.attributeName, sourceEntryId, targetEntryId);
420
- } else if (task.kind === 'morphToOne') {
421
- await copyMorphToOneRelation(strapi, task.schemaUid, task.attributeName, sourceEntryId, targetEntryId);
422
- }
423
- }
446
+ await runInOrder(deferredCopies, (task)=>applyOneDeferredCloneRelationCopy(strapi, originalData, clonedData, sourceRootId, targetRootId, task));
424
447
  };
425
448
  const toRelationRef = (value)=>{
426
449
  if (isRecord(value)) {
@@ -451,7 +474,7 @@ const relationOperationList = (value)=>{
451
474
  ];
452
475
  };
453
476
  const pickInlineRelationTargetRef = (value)=>{
454
- if (Object.prototype.hasOwnProperty.call(value, 'set')) {
477
+ if (Object.hasOwn(value, 'set')) {
455
478
  const setValue = value.set;
456
479
  if (setValue == null) {
457
480
  return null;
@@ -460,7 +483,7 @@ const pickInlineRelationTargetRef = (value)=>{
460
483
  if (list.length === 0) {
461
484
  return null;
462
485
  }
463
- return toRelationRef(list[list.length - 1]);
486
+ return toRelationRef(list.at(-1));
464
487
  }
465
488
  const connect = value.connect;
466
489
  if (connect == null) {
@@ -470,11 +493,31 @@ const pickInlineRelationTargetRef = (value)=>{
470
493
  if (list.length === 0) {
471
494
  return undefined;
472
495
  }
473
- return toRelationRef(list[list.length - 1]);
496
+ return toRelationRef(list.at(-1));
474
497
  };
475
498
  const toRelationStatus = (value)=>{
476
499
  return value === 'draft' || value === 'published' ? value : undefined;
477
500
  };
501
+ const relationEntryIdForStatus = async (strapi, targetUid, documentId, status, targetHasDP, targetLocale)=>{
502
+ const where = {
503
+ documentId
504
+ };
505
+ if (targetHasDP) {
506
+ where.publishedAt = status === 'draft' ? null : {
507
+ $ne: null
508
+ };
509
+ }
510
+ if (targetLocale) {
511
+ where.locale = targetLocale;
512
+ }
513
+ const row = await strapi.db.query(targetUid).findOne({
514
+ where,
515
+ select: [
516
+ 'id'
517
+ ]
518
+ });
519
+ return typeof row?.id === 'number' ? row.id : undefined;
520
+ };
478
521
  const resolveEntryIdsByRef = async (strapi, sourceUid, targetUid, ref, opts)=>{
479
522
  if (typeof ref.id === 'number') {
480
523
  return [
@@ -484,8 +527,9 @@ const resolveEntryIdsByRef = async (strapi, sourceUid, targetUid, ref, opts)=>{
484
527
  if (typeof ref.documentId !== 'string') {
485
528
  return [];
486
529
  }
530
+ const documentId = ref.documentId;
487
531
  let relation = {
488
- documentId: ref.documentId,
532
+ documentId,
489
533
  locale: typeof ref.locale === 'string' ? ref.locale : undefined,
490
534
  status: toRelationStatus(ref.status)
491
535
  };
@@ -505,27 +549,14 @@ const resolveEntryIdsByRef = async (strapi, sourceUid, targetUid, ref, opts)=>{
505
549
  });
506
550
  const model = strapi.getModel(targetUid);
507
551
  const targetHasDP = contentTypes.hasDraftAndPublish(model);
552
+ const ids = [];
553
+ await statuses.reduce((previous, status)=>previous.then(async ()=>{
554
+ ids.push(await relationEntryIdForStatus(strapi, targetUid, documentId, status, targetHasDP, targetLocale));
555
+ }), Promise.resolve());
508
556
  const resolvedIds = [];
509
- for (const status of statuses){
510
- const where = {
511
- documentId: ref.documentId
512
- };
513
- if (targetHasDP) {
514
- where.publishedAt = status === 'draft' ? null : {
515
- $ne: null
516
- };
517
- }
518
- if (targetLocale) {
519
- where.locale = targetLocale;
520
- }
521
- const row = await strapi.db.query(targetUid).findOne({
522
- where,
523
- select: [
524
- 'id'
525
- ]
526
- });
527
- if (typeof row?.id === 'number' && !resolvedIds.includes(row.id)) {
528
- resolvedIds.push(row.id);
557
+ for (const id of ids){
558
+ if (typeof id === 'number' && !resolvedIds.includes(id)) {
559
+ resolvedIds.push(id);
529
560
  }
530
561
  }
531
562
  return resolvedIds;
@@ -552,6 +583,40 @@ const currentAssociationId = (current)=>{
552
583
  }
553
584
  return undefined;
554
585
  };
586
+ const disconnectItemRef = (item)=>{
587
+ if (isRecord(item)) {
588
+ return item;
589
+ }
590
+ if (typeof item === 'number' || typeof item === 'string') {
591
+ return {
592
+ id: item
593
+ };
594
+ }
595
+ return undefined;
596
+ };
597
+ const disconnectItemMatchesCurrent = async (strapi, sourceUid, attribute, item, match)=>{
598
+ const { currentId, currentType, typeField, opts } = match;
599
+ const ref = disconnectItemRef(item);
600
+ if (!ref) {
601
+ return false;
602
+ }
603
+ if (isMorphToOneAttribute(attribute)) {
604
+ const targetUid = ref[typeField];
605
+ if (typeof targetUid !== 'string') {
606
+ return false;
607
+ }
608
+ if (currentType != null && currentType !== targetUid) {
609
+ return false;
610
+ }
611
+ const ids = await resolveEntryIdsByRef(strapi, sourceUid, targetUid, ref, opts);
612
+ return ids.includes(currentId);
613
+ }
614
+ if (!hasRelationTarget(attribute)) {
615
+ return false;
616
+ }
617
+ const ids = await resolveEntryIdsByRef(strapi, sourceUid, attribute.target, ref, opts);
618
+ return ids.includes(currentId);
619
+ };
555
620
  const disconnectMatchesCurrent = async (strapi, sourceUid, attribute, disconnect, current, opts)=>{
556
621
  const currentId = currentAssociationId(current);
557
622
  if (currentId == null) {
@@ -559,67 +624,46 @@ const disconnectMatchesCurrent = async (strapi, sourceUid, attribute, disconnect
559
624
  }
560
625
  const typeField = getMorphTypeField(attribute);
561
626
  const currentType = isMorphToOneAttribute(attribute) && isRecord(current) && typeof current[typeField] === 'string' ? current[typeField] : undefined;
562
- for (const item of disconnect){
563
- let ref = null;
564
- if (isRecord(item)) {
565
- ref = item;
566
- } else if (typeof item === 'number' || typeof item === 'string') {
567
- ref = {
568
- id: item
569
- };
570
- }
571
- if (!ref) {
572
- continue;
573
- }
574
- if (isMorphToOneAttribute(attribute)) {
575
- const targetUid = ref[typeField];
576
- if (typeof targetUid !== 'string') {
577
- continue;
578
- }
579
- if (currentType != null && currentType !== targetUid) {
580
- continue;
581
- }
582
- const ids = await resolveEntryIdsByRef(strapi, sourceUid, targetUid, ref, opts);
583
- if (ids.includes(currentId)) {
627
+ return await disconnect.reduce((previous, item)=>previous.then((alreadyMatched)=>{
628
+ if (alreadyMatched) {
584
629
  return true;
585
630
  }
586
- continue;
587
- }
588
- if (!hasRelationTarget(attribute)) {
589
- continue;
590
- }
591
- const ids = await resolveEntryIdsByRef(strapi, sourceUid, attribute.target, ref, opts);
592
- if (ids.includes(currentId)) {
593
- return true;
594
- }
631
+ return disconnectItemMatchesCurrent(strapi, sourceUid, attribute, item, {
632
+ currentId,
633
+ currentType,
634
+ typeField,
635
+ opts
636
+ });
637
+ }), Promise.resolve(false));
638
+ };
639
+ const restoreUnmatchedInlineRelation = async (strapi, attribute, value, opts)=>{
640
+ const disconnect = relationOperationList(value.disconnect);
641
+ if (disconnect.length === 0) {
642
+ return undefined;
595
643
  }
596
- return false;
644
+ const matches = await disconnectMatchesCurrent(strapi, opts.sourceUid, attribute, disconnect, opts.originalValue, {
645
+ locale: opts.locale
646
+ });
647
+ if (matches) {
648
+ return null;
649
+ }
650
+ // Unmatched disconnect must keep the original association. The clone was
651
+ // created without the relation (stripped from create data), so copy it now.
652
+ if (opts.sourceOwnerId == null) {
653
+ throw new Error(`Unable to restore clone relation "${opts.attributeName}" after unmatched disconnect`);
654
+ }
655
+ const readUid = opts.sourceSchemaUid ?? opts.ownerUid;
656
+ if (isMorphToOneAttribute(attribute)) {
657
+ await copyMorphToOneRelation(strapi, opts.ownerUid, opts.attributeName, opts.sourceOwnerId, opts.targetOwnerId, readUid);
658
+ return undefined;
659
+ }
660
+ await copyFkColumnRelation(strapi, opts.ownerUid, opts.attributeName, opts.sourceOwnerId, opts.targetOwnerId, readUid);
661
+ return undefined;
597
662
  };
598
663
  const resolveInlineRelationAssignment = async (strapi, attribute, value, opts)=>{
599
664
  const targetRef = pickInlineRelationTargetRef(value);
600
665
  if (targetRef === undefined) {
601
- const disconnect = relationOperationList(value.disconnect);
602
- if (disconnect.length === 0) {
603
- return undefined;
604
- }
605
- const matches = await disconnectMatchesCurrent(strapi, opts.sourceUid, attribute, disconnect, opts.originalValue, {
606
- locale: opts.locale
607
- });
608
- if (matches) {
609
- return null;
610
- }
611
- // Unmatched disconnect must keep the original association. The clone was
612
- // created without the relation (stripped from create data), so copy it now.
613
- if (opts.sourceOwnerId == null) {
614
- throw new Error(`Unable to restore clone relation "${opts.attributeName}" after unmatched disconnect`);
615
- }
616
- const readUid = opts.sourceSchemaUid ?? opts.ownerUid;
617
- if (isMorphToOneAttribute(attribute)) {
618
- await copyMorphToOneRelation(strapi, opts.ownerUid, opts.attributeName, opts.sourceOwnerId, opts.targetOwnerId, readUid);
619
- } else {
620
- await copyFkColumnRelation(strapi, opts.ownerUid, opts.attributeName, opts.sourceOwnerId, opts.targetOwnerId, readUid);
621
- }
622
- return undefined;
666
+ return await restoreUnmatchedInlineRelation(strapi, attribute, value, opts);
623
667
  }
624
668
  if (targetRef === null) {
625
669
  return null;
@@ -628,7 +672,7 @@ const resolveInlineRelationAssignment = async (strapi, attribute, value, opts)=>
628
672
  const typeField = getMorphTypeField(attribute);
629
673
  const targetUid = targetRef[typeField];
630
674
  if (typeof targetUid !== 'string') {
631
- throw new Error(`Inline morphToOne clone update requires ${typeField}`);
675
+ throw new TypeError(`Inline morphToOne clone update requires ${typeField}`);
632
676
  }
633
677
  const id = await resolveEntryIdByRef(strapi, opts.sourceUid, targetUid, targetRef, opts);
634
678
  if (id == null) {
@@ -648,56 +692,71 @@ const resolveInlineRelationAssignment = async (strapi, attribute, value, opts)=>
648
692
  }
649
693
  return id;
650
694
  };
695
+ const schemaUidForPostCloneUpdate = (strapi, originalData, clonedData, update, attributeName)=>{
696
+ if (!ownerComponentWasReplaced(originalData, clonedData, update.ownerPath)) {
697
+ return update.schemaUid;
698
+ }
699
+ const cloneComponent = ownerComponentUid(get(clonedData, update.ownerPath));
700
+ const cloneAttribute = cloneComponent ? strapi.db.metadata.get(cloneComponent).attributes[attributeName] : undefined;
701
+ // The submitted operation belongs to the new block. Do not apply it with the
702
+ // source component UID, or the new row id will match an unrelated row.
703
+ if (!cloneComponent || cloneAttribute?.type !== 'relation') {
704
+ return undefined;
705
+ }
706
+ return cloneComponent;
707
+ };
708
+ const applyOnePostCloneRelationUpdate = async (strapi, update, context)=>{
709
+ const { rootUid, sourceRootId, clonedEntryId, clonedData, originalData, locale } = context;
710
+ const attributeName = update.dataPath.split('.').pop();
711
+ const ownerEntryId = resolveOwnerEntryId(clonedData, update.ownerPath, clonedEntryId);
712
+ const sourceOwnerId = resolveOwnerEntryId(originalData, update.ownerPath, sourceRootId);
713
+ if (ownerEntryId == null) {
714
+ throw new Error(`Unable to resolve clone relation owner for "${attributeName}" at path "${update.ownerPath ?? '<root>'}"`);
715
+ }
716
+ const schemaUid = schemaUidForPostCloneUpdate(strapi, originalData, clonedData, update, attributeName);
717
+ if (schemaUid === undefined) {
718
+ return;
719
+ }
720
+ const attribute = strapi.db.metadata.get(schemaUid).attributes[attributeName];
721
+ if (attribute?.type !== 'relation') {
722
+ throw new Error(`Unable to resolve clone relation attribute "${attributeName}" on "${schemaUid}"`);
723
+ }
724
+ const assignment = await resolveInlineRelationAssignment(strapi, attribute, update.value, {
725
+ locale,
726
+ ownerUid: schemaUid,
727
+ sourceSchemaUid: update.schemaUid,
728
+ sourceUid: rootUid,
729
+ originalValue: get(originalData, update.dataPath),
730
+ sourceOwnerId,
731
+ targetOwnerId: ownerEntryId,
732
+ attributeName
733
+ });
734
+ if (assignment === undefined) {
735
+ return;
736
+ }
737
+ // processData accepts attribute names (FK id / morph { id, __type }), not operation payloads.
738
+ await strapi.db.query(schemaUid).update({
739
+ where: {
740
+ id: ownerEntryId
741
+ },
742
+ data: {
743
+ [attributeName]: assignment
744
+ }
745
+ });
746
+ };
651
747
  const applyPostCloneRelationUpdates = async (strapi, rootUid, sourceRootId, clonedEntryId, clonedData, originalData, postCloneUpdates)=>{
652
748
  if (postCloneUpdates.length === 0) {
653
749
  return;
654
750
  }
655
751
  const locale = clonedData.locale;
656
- for (const update of postCloneUpdates){
657
- const attributeName = update.dataPath.split('.').pop();
658
- const ownerEntryId = resolveOwnerEntryId(clonedData, update.ownerPath, clonedEntryId);
659
- const sourceOwnerId = resolveOwnerEntryId(originalData, update.ownerPath, sourceRootId);
660
- if (ownerEntryId == null) {
661
- throw new Error(`Unable to resolve clone relation owner for "${attributeName}" at path "${update.ownerPath ?? '<root>'}"`);
662
- }
663
- let schemaUid = update.schemaUid;
664
- if (ownerComponentWasReplaced(originalData, clonedData, update.ownerPath)) {
665
- const cloneComponent = ownerComponentUid(get(clonedData, update.ownerPath));
666
- const cloneAttribute = cloneComponent ? strapi.db.metadata.get(cloneComponent).attributes[attributeName] : undefined;
667
- // The submitted operation belongs to the new block. Do not apply it with the
668
- // source component UID, or the new row id will match an unrelated row.
669
- if (!cloneComponent || cloneAttribute?.type !== 'relation') {
670
- continue;
671
- }
672
- schemaUid = cloneComponent;
673
- }
674
- const attribute = strapi.db.metadata.get(schemaUid).attributes[attributeName];
675
- if (!attribute || attribute.type !== 'relation') {
676
- throw new Error(`Unable to resolve clone relation attribute "${attributeName}" on "${schemaUid}"`);
677
- }
678
- const assignment = await resolveInlineRelationAssignment(strapi, attribute, update.value, {
679
- locale,
680
- ownerUid: schemaUid,
681
- sourceSchemaUid: update.schemaUid,
682
- sourceUid: rootUid,
683
- originalValue: get(originalData, update.dataPath),
684
- sourceOwnerId,
685
- targetOwnerId: ownerEntryId,
686
- attributeName
687
- });
688
- if (assignment === undefined) {
689
- continue;
690
- }
691
- // processData accepts attribute names (FK id / morph { id, __type }), not operation payloads.
692
- await strapi.db.query(schemaUid).update({
693
- where: {
694
- id: ownerEntryId
695
- },
696
- data: {
697
- [attributeName]: assignment
698
- }
699
- });
700
- }
752
+ await runInOrder(postCloneUpdates, (update)=>applyOnePostCloneRelationUpdate(strapi, update, {
753
+ rootUid,
754
+ sourceRootId,
755
+ clonedEntryId,
756
+ clonedData,
757
+ originalData,
758
+ locale
759
+ }));
701
760
  };
702
761
 
703
762
  export { applyDeferredCloneRelationCopies, applyPostCloneRelationUpdates, copyCloneRelationRows, prepareCloneData };