@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.
- package/dist/Strapi.d.ts.map +1 -1
- package/dist/Strapi.js +2 -0
- package/dist/Strapi.js.map +1 -1
- package/dist/Strapi.mjs +2 -0
- package/dist/Strapi.mjs.map +1 -1
- package/dist/ee/entitlements.d.ts +6 -0
- package/dist/ee/entitlements.d.ts.map +1 -1
- package/dist/ee/entitlements.js +17 -4
- package/dist/ee/entitlements.js.map +1 -1
- package/dist/ee/entitlements.mjs +17 -5
- package/dist/ee/entitlements.mjs.map +1 -1
- package/dist/ee/index.d.ts +7 -12
- package/dist/ee/index.d.ts.map +1 -1
- package/dist/ee/index.js +19 -6
- package/dist/ee/index.js.map +1 -1
- package/dist/ee/index.mjs +21 -8
- package/dist/ee/index.mjs.map +1 -1
- package/dist/factories.d.ts +1 -1
- package/dist/factories.d.ts.map +1 -1
- package/dist/factories.js.map +1 -1
- package/dist/factories.mjs.map +1 -1
- package/dist/middlewares/errors.d.ts.map +1 -1
- package/dist/middlewares/errors.js +9 -0
- package/dist/middlewares/errors.js.map +1 -1
- package/dist/middlewares/errors.mjs +9 -0
- package/dist/middlewares/errors.mjs.map +1 -1
- package/dist/package.json.js +15 -15
- package/dist/package.json.mjs +15 -15
- package/dist/providers/registries.d.ts.map +1 -1
- package/dist/providers/registries.js +1 -0
- package/dist/providers/registries.js.map +1 -1
- package/dist/providers/registries.mjs +1 -0
- package/dist/providers/registries.mjs.map +1 -1
- package/dist/registries/policies.d.ts.map +1 -1
- package/dist/registries/policies.js +20 -0
- package/dist/registries/policies.js.map +1 -1
- package/dist/registries/policies.mjs +20 -0
- package/dist/registries/policies.mjs.map +1 -1
- package/dist/services/content-api/index.d.ts.map +1 -1
- package/dist/services/document-service/common.d.ts.map +1 -1
- package/dist/services/document-service/common.js +1 -1
- package/dist/services/document-service/common.js.map +1 -1
- package/dist/services/document-service/common.mjs +1 -1
- package/dist/services/document-service/common.mjs.map +1 -1
- package/dist/services/document-service/components.d.ts.map +1 -1
- package/dist/services/document-service/components.js +3 -3
- package/dist/services/document-service/components.js.map +1 -1
- package/dist/services/document-service/components.mjs +3 -3
- package/dist/services/document-service/components.mjs.map +1 -1
- package/dist/services/document-service/entries.js +3 -3
- package/dist/services/document-service/entries.js.map +1 -1
- package/dist/services/document-service/entries.mjs +3 -3
- package/dist/services/document-service/entries.mjs.map +1 -1
- package/dist/services/document-service/internationalization.d.ts +13 -1
- package/dist/services/document-service/internationalization.d.ts.map +1 -1
- package/dist/services/document-service/internationalization.js +58 -17
- package/dist/services/document-service/internationalization.js.map +1 -1
- package/dist/services/document-service/internationalization.mjs +59 -18
- package/dist/services/document-service/internationalization.mjs.map +1 -1
- package/dist/services/document-service/middlewares/errors.d.ts.map +1 -1
- package/dist/services/document-service/middlewares/errors.js +3 -12
- package/dist/services/document-service/middlewares/errors.js.map +1 -1
- package/dist/services/document-service/middlewares/errors.mjs +4 -13
- package/dist/services/document-service/middlewares/errors.mjs.map +1 -1
- package/dist/services/document-service/middlewares/middleware-manager.js +3 -3
- package/dist/services/document-service/middlewares/middleware-manager.js.map +1 -1
- package/dist/services/document-service/middlewares/middleware-manager.mjs +3 -3
- package/dist/services/document-service/middlewares/middleware-manager.mjs.map +1 -1
- package/dist/services/document-service/repository.d.ts.map +1 -1
- package/dist/services/document-service/repository.js +46 -10
- package/dist/services/document-service/repository.js.map +1 -1
- package/dist/services/document-service/repository.mjs +47 -11
- package/dist/services/document-service/repository.mjs.map +1 -1
- package/dist/services/document-service/transform/data.d.ts +1 -1
- package/dist/services/document-service/transform/data.d.ts.map +1 -1
- package/dist/services/document-service/transform/data.js +1 -1
- package/dist/services/document-service/transform/data.js.map +1 -1
- package/dist/services/document-service/transform/data.mjs +1 -1
- package/dist/services/document-service/transform/data.mjs.map +1 -1
- package/dist/services/document-service/transform/populate.js +1 -1
- package/dist/services/document-service/transform/populate.js.map +1 -1
- package/dist/services/document-service/transform/populate.mjs +1 -1
- package/dist/services/document-service/transform/populate.mjs.map +1 -1
- package/dist/services/document-service/transform/relations/extract/data-ids.d.ts +1 -1
- package/dist/services/document-service/transform/relations/extract/data-ids.d.ts.map +1 -1
- package/dist/services/document-service/transform/relations/extract/data-ids.js +1 -1
- package/dist/services/document-service/transform/relations/extract/data-ids.js.map +1 -1
- package/dist/services/document-service/transform/relations/extract/data-ids.mjs +1 -1
- package/dist/services/document-service/transform/relations/extract/data-ids.mjs.map +1 -1
- package/dist/services/document-service/transform/relations/transform/data-ids.d.ts +1 -1
- package/dist/services/document-service/transform/relations/transform/data-ids.d.ts.map +1 -1
- package/dist/services/document-service/transform/relations/transform/default-locale.d.ts +1 -1
- package/dist/services/document-service/transform/relations/transform/default-locale.d.ts.map +1 -1
- package/dist/services/document-service/transform/relations/transform/default-locale.js +0 -1
- package/dist/services/document-service/transform/relations/transform/default-locale.js.map +1 -1
- package/dist/services/document-service/transform/relations/transform/default-locale.mjs +0 -1
- package/dist/services/document-service/transform/relations/transform/default-locale.mjs.map +1 -1
- package/dist/services/document-service/transform/relations/utils/map-relation.d.ts +1 -1
- package/dist/services/document-service/transform/relations/utils/map-relation.d.ts.map +1 -1
- package/dist/services/document-service/transform/relations/utils/map-relation.js +1 -1
- package/dist/services/document-service/transform/relations/utils/map-relation.js.map +1 -1
- package/dist/services/document-service/transform/relations/utils/map-relation.mjs +1 -1
- package/dist/services/document-service/transform/relations/utils/map-relation.mjs.map +1 -1
- package/dist/services/document-service/utils/clean-component-join-table.js +1 -1
- package/dist/services/document-service/utils/clean-component-join-table.js.map +1 -1
- package/dist/services/document-service/utils/clean-component-join-table.mjs +1 -1
- package/dist/services/document-service/utils/clean-component-join-table.mjs.map +1 -1
- package/dist/services/document-service/utils/clone-relations.d.ts.map +1 -1
- package/dist/services/document-service/utils/clone-relations.js +249 -190
- package/dist/services/document-service/utils/clone-relations.js.map +1 -1
- package/dist/services/document-service/utils/clone-relations.mjs +250 -191
- package/dist/services/document-service/utils/clone-relations.mjs.map +1 -1
- package/dist/services/entity-service/index.d.ts.map +1 -1
- package/dist/services/entity-service/index.js +3 -12
- package/dist/services/entity-service/index.js.map +1 -1
- package/dist/services/entity-service/index.mjs +4 -13
- package/dist/services/entity-service/index.mjs.map +1 -1
- package/dist/services/metrics/sender.d.ts.map +1 -1
- package/dist/services/metrics/sender.js +4 -1
- package/dist/services/metrics/sender.js.map +1 -1
- package/dist/services/metrics/sender.mjs +4 -1
- package/dist/services/metrics/sender.mjs.map +1 -1
- package/dist/services/reloader.d.ts +1 -0
- package/dist/services/reloader.d.ts.map +1 -1
- package/dist/services/reloader.js +5 -0
- package/dist/services/reloader.js.map +1 -1
- package/dist/services/reloader.mjs +5 -0
- package/dist/services/reloader.mjs.map +1 -1
- package/dist/services/server/middleware.d.ts.map +1 -1
- package/dist/services/server/middleware.js +16 -4
- package/dist/services/server/middleware.js.map +1 -1
- package/dist/services/server/middleware.mjs +16 -4
- package/dist/services/server/middleware.mjs.map +1 -1
- package/dist/utils/database-input-errors.d.ts +11 -0
- package/dist/utils/database-input-errors.d.ts.map +1 -0
- package/dist/utils/database-input-errors.js +23 -0
- package/dist/utils/database-input-errors.js.map +1 -0
- package/dist/utils/database-input-errors.mjs +20 -0
- package/dist/utils/database-input-errors.mjs.map +1 -0
- package/package.json +15 -15
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { merge, set,
|
|
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.
|
|
20
|
+
return isRecord(value) && RELATION_OPERATIONS.some((operation)=>Object.hasOwn(value, operation));
|
|
21
21
|
};
|
|
22
22
|
const hasMeaningfulRelationOperations = (value)=>{
|
|
23
|
-
if (Object.
|
|
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.
|
|
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 =
|
|
101
|
-
return traverseEntity(
|
|
100
|
+
*/ const traverseAllEntityRelations = (visitor, options, data)=>{
|
|
101
|
+
return traverseEntity((visitorOptions, utils)=>{
|
|
102
102
|
const { attribute } = visitorOptions;
|
|
103
|
-
if (
|
|
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
|
-
|
|
207
|
-
|
|
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
|
-
|
|
235
|
-
if (
|
|
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:
|
|
260
|
+
kind: plan.copyKind,
|
|
241
261
|
ownerPath: null
|
|
242
262
|
});
|
|
243
263
|
continue;
|
|
244
264
|
}
|
|
245
|
-
if (
|
|
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:
|
|
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
|
-
|
|
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.
|
|
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
|
|
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
|
|
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
|
|
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
|
|
510
|
-
|
|
511
|
-
|
|
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
|
-
|
|
563
|
-
|
|
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
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
|
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
|
-
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
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 };
|