@strapi/core 5.59.0 → 5.61.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/core-api/routes/index.js +10 -45
- package/dist/core-api/routes/index.js.map +1 -1
- package/dist/core-api/routes/index.mjs +10 -25
- package/dist/core-api/routes/index.mjs.map +1 -1
- package/dist/core-api/routes/response.d.ts +28 -0
- package/dist/core-api/routes/response.d.ts.map +1 -0
- package/dist/core-api/routes/response.js +63 -0
- package/dist/core-api/routes/response.js.map +1 -0
- package/dist/core-api/routes/response.mjs +40 -0
- package/dist/core-api/routes/response.mjs.map +1 -0
- package/dist/package.json.js +17 -21
- package/dist/package.json.js.map +1 -1
- package/dist/package.json.mjs +17 -21
- package/dist/package.json.mjs.map +1 -1
- package/dist/providers/admin.d.ts.map +1 -1
- package/dist/providers/admin.js +8 -0
- package/dist/providers/admin.js.map +1 -1
- package/dist/providers/admin.mjs +8 -0
- package/dist/providers/admin.mjs.map +1 -1
- package/dist/providers/webhooks.d.ts.map +1 -1
- package/dist/providers/webhooks.js +1 -0
- package/dist/providers/webhooks.js.map +1 -1
- package/dist/providers/webhooks.mjs +1 -0
- package/dist/providers/webhooks.mjs.map +1 -1
- package/dist/services/config.js +1 -1
- package/dist/services/config.js.map +1 -1
- package/dist/services/config.mjs +2 -2
- package/dist/services/config.mjs.map +1 -1
- package/dist/services/document-service/components.d.ts +14 -1
- package/dist/services/document-service/components.d.ts.map +1 -1
- package/dist/services/document-service/components.js +413 -18
- package/dist/services/document-service/components.js.map +1 -1
- package/dist/services/document-service/components.mjs +414 -20
- package/dist/services/document-service/components.mjs.map +1 -1
- package/dist/services/document-service/events.d.ts.map +1 -1
- package/dist/services/document-service/events.js +8 -2
- package/dist/services/document-service/events.js.map +1 -1
- package/dist/services/document-service/events.mjs +8 -2
- package/dist/services/document-service/events.mjs.map +1 -1
- package/dist/services/document-service/middlewares/middleware-manager.d.ts +1 -1
- package/dist/services/document-service/middlewares/middleware-manager.d.ts.map +1 -1
- package/dist/services/document-service/middlewares/middleware-manager.js +6 -1
- package/dist/services/document-service/middlewares/middleware-manager.js.map +1 -1
- package/dist/services/document-service/middlewares/middleware-manager.mjs +6 -1
- package/dist/services/document-service/middlewares/middleware-manager.mjs.map +1 -1
- package/dist/services/entity-validator/index.d.ts.map +1 -1
- package/dist/services/entity-validator/index.js +41 -6
- package/dist/services/entity-validator/index.js.map +1 -1
- package/dist/services/entity-validator/index.mjs +42 -7
- package/dist/services/entity-validator/index.mjs.map +1 -1
- package/dist/services/event-hub.d.ts +1 -0
- package/dist/services/event-hub.d.ts.map +1 -1
- package/dist/services/event-hub.js +7 -0
- package/dist/services/event-hub.js.map +1 -1
- package/dist/services/event-hub.mjs +7 -0
- package/dist/services/event-hub.mjs.map +1 -1
- package/dist/services/mcp/handlers/handlePost.d.ts.map +1 -1
- package/dist/services/mcp/handlers/handlePost.js +5 -2
- package/dist/services/mcp/handlers/handlePost.js.map +1 -1
- package/dist/services/mcp/handlers/handlePost.mjs +5 -2
- package/dist/services/mcp/handlers/handlePost.mjs.map +1 -1
- package/dist/services/mcp/internal/McpConfiguration.d.ts +2 -0
- package/dist/services/mcp/internal/McpConfiguration.d.ts.map +1 -1
- package/dist/services/mcp/internal/McpConfiguration.js +17 -0
- package/dist/services/mcp/internal/McpConfiguration.js.map +1 -1
- package/dist/services/mcp/internal/McpConfiguration.mjs +17 -1
- package/dist/services/mcp/internal/McpConfiguration.mjs.map +1 -1
- package/dist/services/mcp/internal/McpServerFactory.d.ts +6 -1
- package/dist/services/mcp/internal/McpServerFactory.d.ts.map +1 -1
- package/dist/services/mcp/internal/McpServerFactory.js +11 -3
- package/dist/services/mcp/internal/McpServerFactory.js.map +1 -1
- package/dist/services/mcp/internal/McpServerFactory.mjs +12 -4
- package/dist/services/mcp/internal/McpServerFactory.mjs.map +1 -1
- package/dist/services/mcp/internal/resolveToolSchemaScope.d.ts +16 -0
- package/dist/services/mcp/internal/resolveToolSchemaScope.d.ts.map +1 -0
- package/dist/services/mcp/internal/resolveToolSchemaScope.js +40 -0
- package/dist/services/mcp/internal/resolveToolSchemaScope.js.map +1 -0
- package/dist/services/mcp/internal/resolveToolSchemaScope.mjs +38 -0
- package/dist/services/mcp/internal/resolveToolSchemaScope.mjs.map +1 -0
- package/dist/services/mcp/tool-registry.d.ts +5 -0
- package/dist/services/mcp/tool-registry.d.ts.map +1 -1
- package/dist/services/mcp/tool-registry.js +30 -1
- package/dist/services/mcp/tool-registry.js.map +1 -1
- package/dist/services/mcp/tool-registry.mjs +30 -1
- package/dist/services/mcp/tool-registry.mjs.map +1 -1
- package/dist/services/webhook-runner.d.ts +5 -6
- package/dist/services/webhook-runner.d.ts.map +1 -1
- package/dist/services/webhook-runner.js +45 -8
- package/dist/services/webhook-runner.js.map +1 -1
- package/dist/services/webhook-runner.mjs +45 -8
- package/dist/services/webhook-runner.mjs.map +1 -1
- package/dist/services/webhook-store.d.ts.map +1 -1
- package/dist/services/webhook-store.js +21 -2
- package/dist/services/webhook-store.js.map +1 -1
- package/dist/services/webhook-store.mjs +21 -2
- package/dist/services/webhook-store.mjs.map +1 -1
- package/package.json +17 -21
|
@@ -1,5 +1,6 @@
|
|
|
1
|
-
import _, { curry, omit, assign, has } from 'lodash';
|
|
1
|
+
import _, { curry, omit, assign, has, isNil, isEqual, isEmpty, isPlainObject } from 'lodash';
|
|
2
2
|
import { contentTypes, async, errors } from '@strapi/utils';
|
|
3
|
+
import { createField } from '@strapi/database';
|
|
3
4
|
import { getComponentJoinColumnEntityName, getComponentJoinColumnInverseName, getComponentTypeColumn, getComponentJoinTableName } from '../../utils/transform-content-types-to-models.mjs';
|
|
4
5
|
|
|
5
6
|
const omitComponentData = curry((schema, data)=>{
|
|
@@ -92,14 +93,26 @@ const getComponents = async (uid, entity)=>{
|
|
|
92
93
|
if (attribute.type === 'component') {
|
|
93
94
|
const { component: componentUID, repeatable = false } = attribute;
|
|
94
95
|
const componentValue = data[attributeName];
|
|
95
|
-
await deleteOldComponents(uid, componentUID, entityToUpdate, attributeName, componentValue);
|
|
96
|
+
const previousValue = await deleteOldComponents(uid, componentUID, entityToUpdate, attributeName, componentValue);
|
|
97
|
+
const previousComponents = _.castArray(previousValue ?? []).filter((component)=>!isNil(component));
|
|
98
|
+
const previousById = new Map(previousComponents.map((component)=>[
|
|
99
|
+
pickStringifiedId(component),
|
|
100
|
+
component
|
|
101
|
+
]));
|
|
102
|
+
const getPrevious = (value)=>value && has(value, 'id') && !isNil(value.id) ? previousById.get(pickStringifiedId(value)) : undefined;
|
|
96
103
|
if (repeatable === true) {
|
|
97
104
|
if (!Array.isArray(componentValue)) {
|
|
98
105
|
throw new Error('Expected an array to create repeatable component');
|
|
99
106
|
}
|
|
100
107
|
// MySQL/MariaDB can cause deadlocks here if concurrency higher than 1
|
|
101
|
-
const
|
|
102
|
-
|
|
108
|
+
const results = await async.map(componentValue, (value)=>updateOrCreateComponent(componentUID, value, getPrevious(value)));
|
|
109
|
+
const components = results.filter((result)=>!isNil(result));
|
|
110
|
+
// The join rows are already right when no component changed and the list kept its order
|
|
111
|
+
const sameList = isEqual(components.map(pickStringifiedId), previousComponents.map((component)=>pickStringifiedId(component)));
|
|
112
|
+
if (sameList && components.every((component)=>!component.changed)) {
|
|
113
|
+
continue;
|
|
114
|
+
}
|
|
115
|
+
componentBody[attributeName] = components.map(({ id })=>{
|
|
103
116
|
return {
|
|
104
117
|
id,
|
|
105
118
|
__pivot: {
|
|
@@ -109,27 +122,50 @@ const getComponents = async (uid, entity)=>{
|
|
|
109
122
|
};
|
|
110
123
|
});
|
|
111
124
|
} else {
|
|
112
|
-
const component = await updateOrCreateComponent(componentUID, componentValue);
|
|
113
|
-
|
|
125
|
+
const component = await updateOrCreateComponent(componentUID, componentValue, getPrevious(componentValue));
|
|
126
|
+
const previousId = previousComponents[0] ? pickStringifiedId(previousComponents[0]) : null;
|
|
127
|
+
const currentId = component ? pickStringifiedId(component) : null;
|
|
128
|
+
if (previousId === currentId && (!component || !component.changed)) {
|
|
129
|
+
continue;
|
|
130
|
+
}
|
|
131
|
+
componentBody[attributeName] = component ? {
|
|
114
132
|
id: component.id,
|
|
115
133
|
__pivot: {
|
|
116
134
|
field: attributeName,
|
|
117
135
|
component_type: componentUID
|
|
118
136
|
}
|
|
119
|
-
};
|
|
137
|
+
} : null;
|
|
120
138
|
}
|
|
121
139
|
} else if (attribute.type === 'dynamiczone') {
|
|
122
140
|
const dynamiczoneValues = data[attributeName];
|
|
123
|
-
await deleteOldDZComponents(uid, entityToUpdate, attributeName, dynamiczoneValues);
|
|
141
|
+
const previousValue = await deleteOldDZComponents(uid, entityToUpdate, attributeName, dynamiczoneValues);
|
|
124
142
|
if (!Array.isArray(dynamiczoneValues)) {
|
|
125
143
|
throw new Error('Expected an array to create repeatable component');
|
|
126
144
|
}
|
|
145
|
+
const toKey = (value)=>`${value.__component}:${pickStringifiedId(value)}`;
|
|
146
|
+
const previousComponents = _.castArray(previousValue ?? []).filter((component)=>!isNil(component));
|
|
147
|
+
const previousByKey = new Map(previousComponents.map((component)=>[
|
|
148
|
+
toKey(component),
|
|
149
|
+
component
|
|
150
|
+
]));
|
|
127
151
|
// MySQL/MariaDB can cause deadlocks here if concurrency higher than 1
|
|
128
|
-
|
|
129
|
-
const
|
|
152
|
+
const components = await async.map(dynamiczoneValues, async (value)=>{
|
|
153
|
+
const previous = has(value, 'id') && !isNil(value.id) ? previousByKey.get(toKey(value)) : undefined;
|
|
154
|
+
const result = await updateOrCreateComponent(value.__component, value, previous);
|
|
155
|
+
return {
|
|
156
|
+
...result,
|
|
157
|
+
__component: value.__component
|
|
158
|
+
};
|
|
159
|
+
});
|
|
160
|
+
// The join rows are already right when no component changed and the zone kept its order
|
|
161
|
+
const sameList = isEqual(components.map(toKey), previousComponents.map(toKey));
|
|
162
|
+
if (sameList && components.every((component)=>!component.changed)) {
|
|
163
|
+
continue;
|
|
164
|
+
}
|
|
165
|
+
componentBody[attributeName] = components.map(({ id, __component })=>{
|
|
130
166
|
return {
|
|
131
167
|
id,
|
|
132
|
-
__component
|
|
168
|
+
__component,
|
|
133
169
|
__pivot: {
|
|
134
170
|
field: attributeName
|
|
135
171
|
}
|
|
@@ -145,8 +181,207 @@ const pickStringifiedId = ({ id })=>{
|
|
|
145
181
|
}
|
|
146
182
|
return `${id}`;
|
|
147
183
|
};
|
|
184
|
+
/**
|
|
185
|
+
* Populate of the media attributes of a component, ids only, so that a loaded component can be
|
|
186
|
+
* compared with the data sent for it (see isComponentUnchanged).
|
|
187
|
+
*/ const getMediaPopulate = (componentUID)=>{
|
|
188
|
+
const { attributes = {} } = strapi.getModel(componentUID);
|
|
189
|
+
const populate = Object.entries(attributes).reduce((acc, [attributeName, attribute])=>{
|
|
190
|
+
if (attribute.type === 'media') {
|
|
191
|
+
acc[attributeName] = {
|
|
192
|
+
select: [
|
|
193
|
+
'id'
|
|
194
|
+
]
|
|
195
|
+
};
|
|
196
|
+
}
|
|
197
|
+
return acc;
|
|
198
|
+
}, {});
|
|
199
|
+
return isEmpty(populate) ? undefined : populate;
|
|
200
|
+
};
|
|
201
|
+
const getComponentLoadParams = (componentUID)=>{
|
|
202
|
+
const populate = getMediaPopulate(componentUID);
|
|
203
|
+
return populate ? {
|
|
204
|
+
populate
|
|
205
|
+
} : undefined;
|
|
206
|
+
};
|
|
207
|
+
/**
|
|
208
|
+
* Load params of a dynamic zone for the cleanup pass. The DB layer only returns the types listed
|
|
209
|
+
* in `on`, and a zone may still hold components of a type that was removed from it, so every
|
|
210
|
+
* registered component is listed, not only the ones the zone declares: those components have to
|
|
211
|
+
* be deleted too when they are no longer sent.
|
|
212
|
+
*/ const getDynamicZoneLoadParams = ()=>{
|
|
213
|
+
const on = {};
|
|
214
|
+
let hasMedia = false;
|
|
215
|
+
for (const componentUID of Object.keys(strapi.components)){
|
|
216
|
+
const populate = getMediaPopulate(componentUID);
|
|
217
|
+
on[componentUID] = populate ? {
|
|
218
|
+
populate
|
|
219
|
+
} : {};
|
|
220
|
+
hasMedia = hasMedia || populate !== undefined;
|
|
221
|
+
}
|
|
222
|
+
return hasMedia ? {
|
|
223
|
+
on
|
|
224
|
+
} : undefined;
|
|
225
|
+
};
|
|
226
|
+
/**
|
|
227
|
+
* A relation or media sent as `{ connect: [], disconnect: [] }` leaves the stored value as it is.
|
|
228
|
+
*/ const isRelationInputNoop = (value)=>{
|
|
229
|
+
if (!isPlainObject(value) || has(value, 'set')) {
|
|
230
|
+
return false;
|
|
231
|
+
}
|
|
232
|
+
const { connect, disconnect, ...rest } = value;
|
|
233
|
+
return isEmpty(connect) && isEmpty(disconnect) && Object.keys(rest).every((key)=>key === 'options');
|
|
234
|
+
};
|
|
235
|
+
/**
|
|
236
|
+
* Ids of a media value, as loaded (`{ id }`, `[{ id }]`) or as sent (ids, `{ id }` objects, a `set`).
|
|
237
|
+
* Returns null when they cannot be told.
|
|
238
|
+
*/ const toMediaIds = (value)=>{
|
|
239
|
+
if (isNil(value)) {
|
|
240
|
+
return [];
|
|
241
|
+
}
|
|
242
|
+
if (typeof value === 'number' || typeof value === 'string') {
|
|
243
|
+
return [
|
|
244
|
+
String(value)
|
|
245
|
+
];
|
|
246
|
+
}
|
|
247
|
+
if (Array.isArray(value)) {
|
|
248
|
+
const ids = value.map((item)=>isPlainObject(item) ? item.id : item);
|
|
249
|
+
return ids.every((id)=>typeof id === 'number' || typeof id === 'string') ? ids.map(String) : null;
|
|
250
|
+
}
|
|
251
|
+
if (isPlainObject(value)) {
|
|
252
|
+
if (has(value, 'set')) {
|
|
253
|
+
return toMediaIds(value.set);
|
|
254
|
+
}
|
|
255
|
+
if (has(value, 'id') && !has(value, 'connect') && !has(value, 'disconnect')) {
|
|
256
|
+
return toMediaIds([
|
|
257
|
+
value
|
|
258
|
+
]);
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
return null;
|
|
262
|
+
};
|
|
263
|
+
const toComparableNumber = (value)=>{
|
|
264
|
+
if (typeof value === 'number') {
|
|
265
|
+
return Number.isFinite(value) ? value : null;
|
|
266
|
+
}
|
|
267
|
+
if (typeof value === 'string' && value.trim() !== '') {
|
|
268
|
+
const parsed = Number(value);
|
|
269
|
+
return Number.isFinite(parsed) ? parsed : null;
|
|
270
|
+
}
|
|
271
|
+
return null;
|
|
272
|
+
};
|
|
273
|
+
const toComparableBigInt = (value)=>{
|
|
274
|
+
try {
|
|
275
|
+
if (typeof value === 'bigint') {
|
|
276
|
+
return value.toString();
|
|
277
|
+
}
|
|
278
|
+
if (typeof value === 'number' && Number.isInteger(value)) {
|
|
279
|
+
return BigInt(value).toString();
|
|
280
|
+
}
|
|
281
|
+
if (typeof value === 'string' && value.trim() !== '') {
|
|
282
|
+
return BigInt(value.trim()).toString();
|
|
283
|
+
}
|
|
284
|
+
} catch {
|
|
285
|
+
// not a big integer
|
|
286
|
+
}
|
|
287
|
+
return null;
|
|
288
|
+
};
|
|
289
|
+
/**
|
|
290
|
+
* What the database layer would store for `value`, or null when it would reject it. Both sides of
|
|
291
|
+
* a comparison go through it so that the comparison matches the write it replaces: the database
|
|
292
|
+
* parsers read a date without a time in local time, format a Date as a local day and reject a
|
|
293
|
+
* time without seconds, and none of that is reproduced here.
|
|
294
|
+
*/ const toStoredDateValue = (attribute, value)=>{
|
|
295
|
+
try {
|
|
296
|
+
const stored = createField({
|
|
297
|
+
type: attribute.type
|
|
298
|
+
}).toDB(value);
|
|
299
|
+
return stored instanceof Date ? stored.getTime() : String(stored);
|
|
300
|
+
} catch {
|
|
301
|
+
return null;
|
|
302
|
+
}
|
|
303
|
+
};
|
|
304
|
+
const toComparableJson = (value)=>{
|
|
305
|
+
if (typeof value === 'string') {
|
|
306
|
+
try {
|
|
307
|
+
return JSON.parse(value);
|
|
308
|
+
} catch {
|
|
309
|
+
return value;
|
|
310
|
+
}
|
|
311
|
+
}
|
|
312
|
+
return value;
|
|
313
|
+
};
|
|
314
|
+
const isScalarUnchanged = (attribute, stored, incoming)=>{
|
|
315
|
+
if (isNil(stored) || isNil(incoming)) {
|
|
316
|
+
return isNil(stored) && isNil(incoming);
|
|
317
|
+
}
|
|
318
|
+
switch(attribute.type){
|
|
319
|
+
case 'string':
|
|
320
|
+
case 'text':
|
|
321
|
+
case 'richtext':
|
|
322
|
+
case 'email':
|
|
323
|
+
case 'enumeration':
|
|
324
|
+
case 'uid':
|
|
325
|
+
return typeof incoming === 'string' && String(stored) === incoming;
|
|
326
|
+
case 'boolean':
|
|
327
|
+
return typeof incoming === 'boolean' && stored === incoming;
|
|
328
|
+
case 'integer':
|
|
329
|
+
case 'float':
|
|
330
|
+
case 'decimal':
|
|
331
|
+
{
|
|
332
|
+
const a = toComparableNumber(stored);
|
|
333
|
+
return a !== null && a === toComparableNumber(incoming);
|
|
334
|
+
}
|
|
335
|
+
case 'biginteger':
|
|
336
|
+
{
|
|
337
|
+
const a = toComparableBigInt(stored);
|
|
338
|
+
return a !== null && a === toComparableBigInt(incoming);
|
|
339
|
+
}
|
|
340
|
+
case 'date':
|
|
341
|
+
case 'time':
|
|
342
|
+
case 'datetime':
|
|
343
|
+
case 'timestamp':
|
|
344
|
+
{
|
|
345
|
+
const a = toStoredDateValue(attribute, stored);
|
|
346
|
+
return a !== null && a === toStoredDateValue(attribute, incoming);
|
|
347
|
+
}
|
|
348
|
+
case 'json':
|
|
349
|
+
case 'blocks':
|
|
350
|
+
return isEqual(toComparableJson(stored), toComparableJson(incoming));
|
|
351
|
+
default:
|
|
352
|
+
// passwords and unknown types: assume a change
|
|
353
|
+
return false;
|
|
354
|
+
}
|
|
355
|
+
};
|
|
356
|
+
/**
|
|
357
|
+
* Whether the data sent for a component leaves it as it is stored. Nested components and dynamic
|
|
358
|
+
* zones are not compared here: updateComponents only reports the ones that changed, and the caller
|
|
359
|
+
* checks that it reported none. Anything that cannot be compared with certainty counts as a change.
|
|
360
|
+
*/ const isComponentUnchanged = (schema, stored, data)=>{
|
|
361
|
+
return Object.entries(schema.attributes).every(([attributeName, attribute])=>{
|
|
362
|
+
if (!has(data, attributeName) || contentTypes.isComponentAttribute(attribute)) {
|
|
363
|
+
return true;
|
|
364
|
+
}
|
|
365
|
+
const incoming = data[attributeName];
|
|
366
|
+
if (attribute.type === 'relation') {
|
|
367
|
+
return isRelationInputNoop(incoming);
|
|
368
|
+
}
|
|
369
|
+
if (!has(stored, attributeName)) {
|
|
370
|
+
return false;
|
|
371
|
+
}
|
|
372
|
+
if (attribute.type === 'media') {
|
|
373
|
+
if (isRelationInputNoop(incoming)) {
|
|
374
|
+
return true;
|
|
375
|
+
}
|
|
376
|
+
const storedIds = toMediaIds(stored[attributeName]);
|
|
377
|
+
const incomingIds = toMediaIds(incoming);
|
|
378
|
+
return storedIds !== null && incomingIds !== null && isEqual(storedIds, incomingIds);
|
|
379
|
+
}
|
|
380
|
+
return isScalarUnchanged(attribute, stored[attributeName], incoming);
|
|
381
|
+
});
|
|
382
|
+
};
|
|
148
383
|
const deleteOldComponents = async (uid, componentUID, entityToUpdate, attributeName, componentValue)=>{
|
|
149
|
-
const previousValue = await strapi.db.query(uid).load(entityToUpdate, attributeName);
|
|
384
|
+
const previousValue = await strapi.db.query(uid).load(entityToUpdate, attributeName, getComponentLoadParams(componentUID));
|
|
150
385
|
const idsToKeep = _.castArray(componentValue).filter((value)=>has(value, 'id')).map(pickStringifiedId);
|
|
151
386
|
const allIds = _.castArray(previousValue).filter((value)=>has(value, 'id')).map(pickStringifiedId);
|
|
152
387
|
idsToKeep.forEach((id)=>{
|
|
@@ -162,9 +397,10 @@ const deleteOldComponents = async (uid, componentUID, entityToUpdate, attributeN
|
|
|
162
397
|
});
|
|
163
398
|
}
|
|
164
399
|
}
|
|
400
|
+
return previousValue;
|
|
165
401
|
};
|
|
166
402
|
const deleteOldDZComponents = async (uid, entityToUpdate, attributeName, dynamiczoneValues)=>{
|
|
167
|
-
const previousValue = await strapi.db.query(uid).load(entityToUpdate, attributeName);
|
|
403
|
+
const previousValue = await strapi.db.query(uid).load(entityToUpdate, attributeName, getDynamicZoneLoadParams());
|
|
168
404
|
const idsToKeep = _.castArray(dynamiczoneValues).filter((value)=>has(value, 'id')).map((v)=>({
|
|
169
405
|
id: pickStringifiedId(v),
|
|
170
406
|
__component: v.__component
|
|
@@ -199,8 +435,109 @@ const deleteOldDZComponents = async (uid, entityToUpdate, attributeName, dynamic
|
|
|
199
435
|
});
|
|
200
436
|
}
|
|
201
437
|
}
|
|
438
|
+
return previousValue;
|
|
439
|
+
};
|
|
440
|
+
const addComponentIds = (groups, componentUID, ids)=>{
|
|
441
|
+
if (ids.length > 0) {
|
|
442
|
+
groups.set(componentUID, [
|
|
443
|
+
...groups.get(componentUID) ?? [],
|
|
444
|
+
...ids
|
|
445
|
+
]);
|
|
446
|
+
}
|
|
447
|
+
};
|
|
448
|
+
/**
|
|
449
|
+
* Ids of the components held by the component and dynamic zone attributes of `values`, by component uid.
|
|
450
|
+
*/ const groupComponentsByUid = (schema, values)=>{
|
|
451
|
+
const groups = new Map();
|
|
452
|
+
for (const [attributeName, attribute] of Object.entries(schema.attributes)){
|
|
453
|
+
const value = values[attributeName];
|
|
454
|
+
if (isNil(value)) {
|
|
455
|
+
continue;
|
|
456
|
+
}
|
|
457
|
+
const components = _.castArray(value).filter((component)=>isPlainObject(component) && !isNil(component.id));
|
|
458
|
+
if (attribute.type === 'component') {
|
|
459
|
+
addComponentIds(groups, attribute.component, components.map((component)=>component.id));
|
|
460
|
+
} else if (attribute.type === 'dynamiczone') {
|
|
461
|
+
for (const component of components){
|
|
462
|
+
if (component.__component) {
|
|
463
|
+
addComponentIds(groups, component.__component, [
|
|
464
|
+
component.id
|
|
465
|
+
]);
|
|
466
|
+
}
|
|
467
|
+
}
|
|
468
|
+
}
|
|
469
|
+
}
|
|
470
|
+
return groups;
|
|
471
|
+
};
|
|
472
|
+
/**
|
|
473
|
+
* Ids of every component under an entry or a component, by component uid, down to the deepest
|
|
474
|
+
* level. Each level costs one query per component uid holding nested components, whatever the
|
|
475
|
+
* number of components.
|
|
476
|
+
*/ const collectComponentTree = async (uid, entity, { loadComponents = true } = {})=>{
|
|
477
|
+
const tree = new Map();
|
|
478
|
+
const schema = strapi.getModel(uid);
|
|
479
|
+
const componentAttributes = contentTypes.getComponentAttributes(schema);
|
|
480
|
+
if (componentAttributes.length === 0) {
|
|
481
|
+
return tree;
|
|
482
|
+
}
|
|
483
|
+
const values = loadComponents ? await strapi.db.query(uid).load(entity, componentAttributes) : entity;
|
|
484
|
+
let level = groupComponentsByUid(schema, values ?? {});
|
|
485
|
+
while(level.size > 0){
|
|
486
|
+
const nextLevel = new Map();
|
|
487
|
+
for (const [componentUID, ids] of level){
|
|
488
|
+
addComponentIds(tree, componentUID, ids);
|
|
489
|
+
const componentSchema = strapi.getModel(componentUID);
|
|
490
|
+
const nestedAttributes = contentTypes.getComponentAttributes(componentSchema);
|
|
491
|
+
if (nestedAttributes.length === 0) {
|
|
492
|
+
continue;
|
|
493
|
+
}
|
|
494
|
+
// Read without lifecycles: the per-row path never ran `findMany` on a component type, so no
|
|
495
|
+
// subscriber should start receiving those events here
|
|
496
|
+
const rows = await strapi.db.queryBuilder(componentUID).init({
|
|
497
|
+
where: {
|
|
498
|
+
id: {
|
|
499
|
+
$in: ids
|
|
500
|
+
}
|
|
501
|
+
},
|
|
502
|
+
select: [
|
|
503
|
+
'id'
|
|
504
|
+
],
|
|
505
|
+
populate: Object.fromEntries(nestedAttributes.map((attributeName)=>[
|
|
506
|
+
attributeName,
|
|
507
|
+
// dynamic zone items need their __component, keep every column for them
|
|
508
|
+
componentSchema.attributes[attributeName].type === 'dynamiczone' ? true : {
|
|
509
|
+
select: [
|
|
510
|
+
'id'
|
|
511
|
+
]
|
|
512
|
+
}
|
|
513
|
+
]))
|
|
514
|
+
}).execute();
|
|
515
|
+
for (const row of rows){
|
|
516
|
+
for (const [nestedUID, nestedIds] of groupComponentsByUid(componentSchema, row)){
|
|
517
|
+
addComponentIds(nextLevel, nestedUID, nestedIds);
|
|
518
|
+
}
|
|
519
|
+
}
|
|
520
|
+
}
|
|
521
|
+
level = nextLevel;
|
|
522
|
+
}
|
|
523
|
+
return tree;
|
|
202
524
|
};
|
|
203
|
-
const
|
|
525
|
+
const UPDATE_LIFECYCLES = [
|
|
526
|
+
'beforeUpdate',
|
|
527
|
+
'afterUpdate'
|
|
528
|
+
];
|
|
529
|
+
const DELETE_LIFECYCLES = [
|
|
530
|
+
'beforeDelete',
|
|
531
|
+
'afterDelete',
|
|
532
|
+
'beforeDeleteMany',
|
|
533
|
+
'afterDeleteMany'
|
|
534
|
+
];
|
|
535
|
+
/**
|
|
536
|
+
* Whether a database lifecycle subscriber would receive one of `actions` for the component type.
|
|
537
|
+
*/ const hasLifecycleSubscribers = (componentUID, actions)=>actions.some((action)=>strapi.db.lifecycles.hasSubscribers(action, componentUID));
|
|
538
|
+
/**
|
|
539
|
+
* Deletes the components one by one, each with its own delete lifecycles.
|
|
540
|
+
*/ const deleteComponentsOneByOne = async (uid, entityToDelete, { loadComponents = true } = {})=>{
|
|
204
541
|
const { attributes = {} } = strapi.getModel(uid);
|
|
205
542
|
const attributeNames = Object.keys(attributes);
|
|
206
543
|
for (const attributeName of attributeNames){
|
|
@@ -225,6 +562,46 @@ const deleteComponents = async (uid, entityToDelete, { loadComponents = true } =
|
|
|
225
562
|
}
|
|
226
563
|
}
|
|
227
564
|
};
|
|
565
|
+
// Keeps every statement under the parameter limits of the databases
|
|
566
|
+
const DELETE_BATCH_SIZE = 500;
|
|
567
|
+
/**
|
|
568
|
+
* Deletes every component under an entry or a component. The whole tree is removed level by level
|
|
569
|
+
* with one statement per component uid and per relation attribute. When a database lifecycle
|
|
570
|
+
* subscriber listens to any delete lifecycle of one of those component uids, the components are
|
|
571
|
+
* deleted one by one instead, so that each of them fires its events and no subscriber receives an
|
|
572
|
+
* event it did not receive before.
|
|
573
|
+
*/ const deleteComponents = async (uid, entityToDelete, { loadComponents = true } = {})=>{
|
|
574
|
+
const tree = await collectComponentTree(uid, entityToDelete, {
|
|
575
|
+
loadComponents
|
|
576
|
+
});
|
|
577
|
+
if (tree.size === 0) {
|
|
578
|
+
return;
|
|
579
|
+
}
|
|
580
|
+
if ([
|
|
581
|
+
...tree.keys()
|
|
582
|
+
].some((componentUID)=>hasLifecycleSubscribers(componentUID, DELETE_LIFECYCLES))) {
|
|
583
|
+
await deleteComponentsOneByOne(uid, entityToDelete, {
|
|
584
|
+
loadComponents
|
|
585
|
+
});
|
|
586
|
+
return;
|
|
587
|
+
}
|
|
588
|
+
await strapi.db.transaction(async ({ trx })=>{
|
|
589
|
+
for (const [componentUID, ids] of tree){
|
|
590
|
+
for (const batch of _.chunk(ids, DELETE_BATCH_SIZE)){
|
|
591
|
+
await strapi.db.entityManager.deleteManyRelations(componentUID, batch, {
|
|
592
|
+
transaction: trx
|
|
593
|
+
});
|
|
594
|
+
await strapi.db.query(componentUID).deleteMany({
|
|
595
|
+
where: {
|
|
596
|
+
id: {
|
|
597
|
+
$in: batch
|
|
598
|
+
}
|
|
599
|
+
}
|
|
600
|
+
});
|
|
601
|
+
}
|
|
602
|
+
}
|
|
603
|
+
});
|
|
604
|
+
};
|
|
228
605
|
/** *************************
|
|
229
606
|
Component queries
|
|
230
607
|
************************** */ // components can have nested compos so this must be recursive
|
|
@@ -238,17 +615,30 @@ const createComponent = async (uid, data)=>{
|
|
|
238
615
|
});
|
|
239
616
|
};
|
|
240
617
|
// components can have nested compos so this must be recursive
|
|
241
|
-
const updateComponent = async (uid, componentToUpdate, data)=>{
|
|
618
|
+
const updateComponent = async (uid, componentToUpdate, data, previous)=>{
|
|
242
619
|
const schema = strapi.getModel(uid);
|
|
243
620
|
const componentData = await updateComponents(uid, componentToUpdate, data);
|
|
244
|
-
|
|
621
|
+
// Nothing to write when the component is stored as sent. updateComponents only reports the
|
|
622
|
+
// nested attributes that changed, so an empty componentData means none of them did. The write
|
|
623
|
+
// is kept when a lifecycle subscriber listens to it, so that it fires as before.
|
|
624
|
+
if (previous && isEmpty(componentData) && !hasLifecycleSubscribers(uid, UPDATE_LIFECYCLES) && isComponentUnchanged(schema, previous, data)) {
|
|
625
|
+
return {
|
|
626
|
+
id: componentToUpdate.id,
|
|
627
|
+
changed: false
|
|
628
|
+
};
|
|
629
|
+
}
|
|
630
|
+
const component = await strapi.db.query(uid).update({
|
|
245
631
|
where: {
|
|
246
632
|
id: componentToUpdate.id
|
|
247
633
|
},
|
|
248
634
|
data: assignComponentData(schema, componentData, data)
|
|
249
635
|
});
|
|
636
|
+
return component ? {
|
|
637
|
+
id: component.id,
|
|
638
|
+
changed: true
|
|
639
|
+
} : null;
|
|
250
640
|
};
|
|
251
|
-
const updateOrCreateComponent = (componentUID, value)=>{
|
|
641
|
+
const updateOrCreateComponent = async (componentUID, value, previous)=>{
|
|
252
642
|
if (value === null) {
|
|
253
643
|
return null;
|
|
254
644
|
}
|
|
@@ -257,10 +647,14 @@ const updateOrCreateComponent = (componentUID, value)=>{
|
|
|
257
647
|
// TODO: verify the compo is associated with the entity
|
|
258
648
|
return updateComponent(componentUID, {
|
|
259
649
|
id: value.id
|
|
260
|
-
}, value);
|
|
650
|
+
}, value, previous);
|
|
261
651
|
}
|
|
262
652
|
// create
|
|
263
|
-
|
|
653
|
+
const component = await createComponent(componentUID, value);
|
|
654
|
+
return {
|
|
655
|
+
id: component.id,
|
|
656
|
+
changed: true
|
|
657
|
+
};
|
|
264
658
|
};
|
|
265
659
|
const deleteComponent = async (uid, componentToDelete)=>{
|
|
266
660
|
await deleteComponents(uid, componentToDelete);
|
|
@@ -428,5 +822,5 @@ const PARENT_LOOKUP_BATCH_SIZE = 500;
|
|
|
428
822
|
};
|
|
429
823
|
};
|
|
430
824
|
|
|
431
|
-
export { assignComponentData, createComponentRelationFilter, createComponents, deleteComponent, deleteComponents, findComponentParent, getComponents, getParentSchemasForComponent, omitComponentData, updateComponents };
|
|
825
|
+
export { assignComponentData, createComponentRelationFilter, createComponents, deleteComponent, deleteComponents, findComponentParent, getComponents, getParentSchemasForComponent, isComponentUnchanged, omitComponentData, updateComponents };
|
|
432
826
|
//# sourceMappingURL=components.mjs.map
|