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