strapi-plugin-field-clearer 1.0.9 → 1.0.11
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/server/index.js
CHANGED
|
@@ -205,17 +205,34 @@ const parseFieldPath = (path) => {
|
|
|
205
205
|
const service = ({ strapi }) => ({
|
|
206
206
|
/**
|
|
207
207
|
* Fetch a document by documentId, or use findFirst() for single types when documentId is empty.
|
|
208
|
+
* Optionally specify status ('published' or 'draft') - defaults to Strapi's default (draft in admin context).
|
|
208
209
|
*/
|
|
209
|
-
async fetchDocument(contentType, documentId, populate) {
|
|
210
|
+
async fetchDocument(contentType, documentId, populate, status) {
|
|
211
|
+
const params = { populate };
|
|
212
|
+
if (status) {
|
|
213
|
+
params.status = status;
|
|
214
|
+
}
|
|
210
215
|
if (documentId) {
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
216
|
+
params.documentId = documentId;
|
|
217
|
+
return strapi.documents(contentType).findOne(params);
|
|
218
|
+
}
|
|
219
|
+
return strapi.documents(contentType).findFirst(params);
|
|
220
|
+
},
|
|
221
|
+
/**
|
|
222
|
+
* Smart fetch for reading relation data: tries published version first (which has complete
|
|
223
|
+
* relation data), then falls back to draft. This works around the Strapi v5 draft/publish
|
|
224
|
+
* issue where relation join tables can have mixed draft/published product IDs, causing the
|
|
225
|
+
* draft version to return incomplete relations.
|
|
226
|
+
*/
|
|
227
|
+
async fetchDocumentWithPublishedFallback(contentType, documentId, populate) {
|
|
228
|
+
try {
|
|
229
|
+
const publishedDoc = await this.fetchDocument(contentType, documentId, populate, "published");
|
|
230
|
+
if (publishedDoc) {
|
|
231
|
+
return publishedDoc;
|
|
232
|
+
}
|
|
233
|
+
} catch (e) {
|
|
215
234
|
}
|
|
216
|
-
return
|
|
217
|
-
populate
|
|
218
|
-
});
|
|
235
|
+
return this.fetchDocument(contentType, documentId, populate);
|
|
219
236
|
},
|
|
220
237
|
/**
|
|
221
238
|
* Preview what will be deleted (dry run - no actual deletion)
|
|
@@ -282,16 +299,27 @@ const service = ({ strapi }) => ({
|
|
|
282
299
|
* @param indices - Optional array of component indices to target (0-based). If null, targets all components.
|
|
283
300
|
*/
|
|
284
301
|
async previewNestedField(contentType, documentId, componentField, nestedField, indices = null) {
|
|
302
|
+
const populate = {
|
|
303
|
+
[componentField]: {
|
|
304
|
+
populate: {
|
|
305
|
+
[nestedField]: true
|
|
306
|
+
}
|
|
307
|
+
}
|
|
308
|
+
};
|
|
285
309
|
let document;
|
|
286
310
|
try {
|
|
287
|
-
document = await this.
|
|
288
|
-
[componentField]: {
|
|
289
|
-
populate: "*"
|
|
290
|
-
}
|
|
291
|
-
});
|
|
311
|
+
document = await this.fetchDocumentWithPublishedFallback(contentType, documentId, populate);
|
|
292
312
|
} catch (error) {
|
|
293
|
-
|
|
294
|
-
|
|
313
|
+
try {
|
|
314
|
+
document = await this.fetchDocument(contentType, documentId, {
|
|
315
|
+
[componentField]: {
|
|
316
|
+
populate: "*"
|
|
317
|
+
}
|
|
318
|
+
});
|
|
319
|
+
} catch (finalError) {
|
|
320
|
+
const message = finalError instanceof Error ? finalError.message : "Unknown error";
|
|
321
|
+
throw new Error(`Failed to fetch document: ${message}`);
|
|
322
|
+
}
|
|
295
323
|
}
|
|
296
324
|
if (!document) {
|
|
297
325
|
throw new Error("Document not found");
|
|
@@ -374,20 +402,35 @@ const service = ({ strapi }) => ({
|
|
|
374
402
|
* Used for fields inside components within dynamic zones or repeatable components
|
|
375
403
|
*/
|
|
376
404
|
async previewDeepNestedField(contentType, documentId, parentField, componentField, nestedField, indices = null) {
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
populate: "*"
|
|
405
|
+
const populate = {
|
|
406
|
+
[parentField]: {
|
|
407
|
+
populate: {
|
|
408
|
+
[componentField]: {
|
|
409
|
+
populate: {
|
|
410
|
+
[nestedField]: true
|
|
384
411
|
}
|
|
385
412
|
}
|
|
386
413
|
}
|
|
387
|
-
}
|
|
414
|
+
}
|
|
415
|
+
};
|
|
416
|
+
let document;
|
|
417
|
+
try {
|
|
418
|
+
document = await this.fetchDocumentWithPublishedFallback(contentType, documentId, populate);
|
|
388
419
|
} catch (error) {
|
|
389
|
-
|
|
390
|
-
|
|
420
|
+
try {
|
|
421
|
+
document = await this.fetchDocument(contentType, documentId, {
|
|
422
|
+
[parentField]: {
|
|
423
|
+
populate: {
|
|
424
|
+
[componentField]: {
|
|
425
|
+
populate: "*"
|
|
426
|
+
}
|
|
427
|
+
}
|
|
428
|
+
}
|
|
429
|
+
});
|
|
430
|
+
} catch (fallbackError) {
|
|
431
|
+
const message = fallbackError instanceof Error ? fallbackError.message : "Unknown error";
|
|
432
|
+
throw new Error(`Failed to fetch document: ${message}`);
|
|
433
|
+
}
|
|
391
434
|
}
|
|
392
435
|
if (!document) {
|
|
393
436
|
throw new Error("Document not found");
|
|
@@ -599,17 +642,27 @@ const service = ({ strapi }) => ({
|
|
|
599
642
|
return { message: `Field "${fieldName}" is already empty`, clearedCount: 0 };
|
|
600
643
|
}
|
|
601
644
|
const clearedCount = this.countFieldItems(fieldValue);
|
|
602
|
-
const internalId = document.id;
|
|
603
|
-
if (!internalId) {
|
|
604
|
-
throw new Error("Document internal ID not found");
|
|
605
|
-
}
|
|
606
645
|
const newValue = this.getEmptyValue(fieldValue);
|
|
646
|
+
const docId = document.documentId || documentId;
|
|
607
647
|
try {
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
648
|
+
if (docId) {
|
|
649
|
+
await strapi.documents(contentType).update({
|
|
650
|
+
documentId: docId,
|
|
651
|
+
data: {
|
|
652
|
+
[fieldName]: newValue
|
|
653
|
+
}
|
|
654
|
+
});
|
|
655
|
+
} else {
|
|
656
|
+
const internalId = document.id;
|
|
657
|
+
if (!internalId) {
|
|
658
|
+
throw new Error("Document internal ID not found");
|
|
611
659
|
}
|
|
612
|
-
|
|
660
|
+
await strapi.entityService.update(contentType, internalId, {
|
|
661
|
+
data: {
|
|
662
|
+
[fieldName]: newValue
|
|
663
|
+
}
|
|
664
|
+
});
|
|
665
|
+
}
|
|
613
666
|
} catch (error) {
|
|
614
667
|
const message = error instanceof Error ? error.message : "Unknown error";
|
|
615
668
|
throw new Error(`Failed to update document: ${message}`);
|
|
@@ -632,16 +685,27 @@ const service = ({ strapi }) => ({
|
|
|
632
685
|
if (!nestedField || typeof nestedField !== "string") {
|
|
633
686
|
throw new Error("Invalid nested field name provided");
|
|
634
687
|
}
|
|
688
|
+
const populate = {
|
|
689
|
+
[componentField]: {
|
|
690
|
+
populate: {
|
|
691
|
+
[nestedField]: true
|
|
692
|
+
}
|
|
693
|
+
}
|
|
694
|
+
};
|
|
635
695
|
let document;
|
|
636
696
|
try {
|
|
637
|
-
document = await this.fetchDocument(contentType, documentId,
|
|
638
|
-
[componentField]: {
|
|
639
|
-
populate: "*"
|
|
640
|
-
}
|
|
641
|
-
});
|
|
697
|
+
document = await this.fetchDocument(contentType, documentId, populate);
|
|
642
698
|
} catch (error) {
|
|
643
|
-
|
|
644
|
-
|
|
699
|
+
try {
|
|
700
|
+
document = await this.fetchDocument(contentType, documentId, {
|
|
701
|
+
[componentField]: {
|
|
702
|
+
populate: "*"
|
|
703
|
+
}
|
|
704
|
+
});
|
|
705
|
+
} catch (finalError) {
|
|
706
|
+
const message = finalError instanceof Error ? finalError.message : "Unknown error";
|
|
707
|
+
throw new Error(`Failed to fetch document: ${message}`);
|
|
708
|
+
}
|
|
645
709
|
}
|
|
646
710
|
if (!document) {
|
|
647
711
|
throw new Error("Document not found");
|
|
@@ -735,17 +799,27 @@ const service = ({ strapi }) => ({
|
|
|
735
799
|
}
|
|
736
800
|
return updated;
|
|
737
801
|
}).filter(Boolean);
|
|
738
|
-
const internalId = document.id;
|
|
739
|
-
if (!internalId) {
|
|
740
|
-
throw new Error("Document internal ID not found");
|
|
741
|
-
}
|
|
742
802
|
const updateData = isRepeatable ? updatedComponents : updatedComponents[0];
|
|
803
|
+
const docId = document.documentId || documentId;
|
|
743
804
|
try {
|
|
744
|
-
|
|
745
|
-
|
|
746
|
-
|
|
805
|
+
if (docId) {
|
|
806
|
+
await strapi.documents(contentType).update({
|
|
807
|
+
documentId: docId,
|
|
808
|
+
data: {
|
|
809
|
+
[componentField]: updateData
|
|
810
|
+
}
|
|
811
|
+
});
|
|
812
|
+
} else {
|
|
813
|
+
const internalId = document.id;
|
|
814
|
+
if (!internalId) {
|
|
815
|
+
throw new Error("Document internal ID not found");
|
|
747
816
|
}
|
|
748
|
-
|
|
817
|
+
await strapi.entityService.update(contentType, internalId, {
|
|
818
|
+
data: {
|
|
819
|
+
[componentField]: updateData
|
|
820
|
+
}
|
|
821
|
+
});
|
|
822
|
+
}
|
|
749
823
|
} catch (error) {
|
|
750
824
|
const message = error instanceof Error ? error.message : "Unknown error";
|
|
751
825
|
throw new Error(`Failed to update document: ${message}`);
|
|
@@ -765,20 +839,35 @@ const service = ({ strapi }) => ({
|
|
|
765
839
|
if (!parentField || !componentField || !nestedField) {
|
|
766
840
|
throw new Error("Invalid field path provided");
|
|
767
841
|
}
|
|
768
|
-
|
|
769
|
-
|
|
770
|
-
|
|
771
|
-
|
|
772
|
-
|
|
773
|
-
|
|
774
|
-
populate: "*"
|
|
842
|
+
const populate = {
|
|
843
|
+
[parentField]: {
|
|
844
|
+
populate: {
|
|
845
|
+
[componentField]: {
|
|
846
|
+
populate: {
|
|
847
|
+
[nestedField]: true
|
|
775
848
|
}
|
|
776
849
|
}
|
|
777
850
|
}
|
|
778
|
-
}
|
|
851
|
+
}
|
|
852
|
+
};
|
|
853
|
+
let document;
|
|
854
|
+
try {
|
|
855
|
+
document = await this.fetchDocument(contentType, documentId, populate);
|
|
779
856
|
} catch (error) {
|
|
780
|
-
|
|
781
|
-
|
|
857
|
+
try {
|
|
858
|
+
document = await this.fetchDocument(contentType, documentId, {
|
|
859
|
+
[parentField]: {
|
|
860
|
+
populate: {
|
|
861
|
+
[componentField]: {
|
|
862
|
+
populate: "*"
|
|
863
|
+
}
|
|
864
|
+
}
|
|
865
|
+
}
|
|
866
|
+
});
|
|
867
|
+
} catch (fallbackError) {
|
|
868
|
+
const message = fallbackError instanceof Error ? fallbackError.message : "Unknown error";
|
|
869
|
+
throw new Error(`Failed to fetch document: ${message}`);
|
|
870
|
+
}
|
|
782
871
|
}
|
|
783
872
|
if (!document) {
|
|
784
873
|
throw new Error("Document not found");
|
|
@@ -882,17 +971,27 @@ const service = ({ strapi }) => ({
|
|
|
882
971
|
}
|
|
883
972
|
return updated;
|
|
884
973
|
}).filter(Boolean);
|
|
885
|
-
const internalId = document.id;
|
|
886
|
-
if (!internalId) {
|
|
887
|
-
throw new Error("Document internal ID not found");
|
|
888
|
-
}
|
|
889
974
|
const updateData = isRepeatable ? updatedParentComponents : updatedParentComponents[0];
|
|
975
|
+
const docId = document.documentId || documentId;
|
|
890
976
|
try {
|
|
891
|
-
|
|
892
|
-
|
|
893
|
-
|
|
977
|
+
if (docId) {
|
|
978
|
+
await strapi.documents(contentType).update({
|
|
979
|
+
documentId: docId,
|
|
980
|
+
data: {
|
|
981
|
+
[parentField]: updateData
|
|
982
|
+
}
|
|
983
|
+
});
|
|
984
|
+
} else {
|
|
985
|
+
const internalId = document.id;
|
|
986
|
+
if (!internalId) {
|
|
987
|
+
throw new Error("Document internal ID not found");
|
|
894
988
|
}
|
|
895
|
-
|
|
989
|
+
await strapi.entityService.update(contentType, internalId, {
|
|
990
|
+
data: {
|
|
991
|
+
[parentField]: updateData
|
|
992
|
+
}
|
|
993
|
+
});
|
|
994
|
+
}
|
|
896
995
|
} catch (error) {
|
|
897
996
|
const message = error instanceof Error ? error.message : "Unknown error";
|
|
898
997
|
throw new Error(`Failed to update document: ${message}`);
|
package/dist/server/index.mjs
CHANGED
|
@@ -204,17 +204,34 @@ const parseFieldPath = (path) => {
|
|
|
204
204
|
const service = ({ strapi }) => ({
|
|
205
205
|
/**
|
|
206
206
|
* Fetch a document by documentId, or use findFirst() for single types when documentId is empty.
|
|
207
|
+
* Optionally specify status ('published' or 'draft') - defaults to Strapi's default (draft in admin context).
|
|
207
208
|
*/
|
|
208
|
-
async fetchDocument(contentType, documentId, populate) {
|
|
209
|
+
async fetchDocument(contentType, documentId, populate, status) {
|
|
210
|
+
const params = { populate };
|
|
211
|
+
if (status) {
|
|
212
|
+
params.status = status;
|
|
213
|
+
}
|
|
209
214
|
if (documentId) {
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
215
|
+
params.documentId = documentId;
|
|
216
|
+
return strapi.documents(contentType).findOne(params);
|
|
217
|
+
}
|
|
218
|
+
return strapi.documents(contentType).findFirst(params);
|
|
219
|
+
},
|
|
220
|
+
/**
|
|
221
|
+
* Smart fetch for reading relation data: tries published version first (which has complete
|
|
222
|
+
* relation data), then falls back to draft. This works around the Strapi v5 draft/publish
|
|
223
|
+
* issue where relation join tables can have mixed draft/published product IDs, causing the
|
|
224
|
+
* draft version to return incomplete relations.
|
|
225
|
+
*/
|
|
226
|
+
async fetchDocumentWithPublishedFallback(contentType, documentId, populate) {
|
|
227
|
+
try {
|
|
228
|
+
const publishedDoc = await this.fetchDocument(contentType, documentId, populate, "published");
|
|
229
|
+
if (publishedDoc) {
|
|
230
|
+
return publishedDoc;
|
|
231
|
+
}
|
|
232
|
+
} catch (e) {
|
|
214
233
|
}
|
|
215
|
-
return
|
|
216
|
-
populate
|
|
217
|
-
});
|
|
234
|
+
return this.fetchDocument(contentType, documentId, populate);
|
|
218
235
|
},
|
|
219
236
|
/**
|
|
220
237
|
* Preview what will be deleted (dry run - no actual deletion)
|
|
@@ -281,16 +298,27 @@ const service = ({ strapi }) => ({
|
|
|
281
298
|
* @param indices - Optional array of component indices to target (0-based). If null, targets all components.
|
|
282
299
|
*/
|
|
283
300
|
async previewNestedField(contentType, documentId, componentField, nestedField, indices = null) {
|
|
301
|
+
const populate = {
|
|
302
|
+
[componentField]: {
|
|
303
|
+
populate: {
|
|
304
|
+
[nestedField]: true
|
|
305
|
+
}
|
|
306
|
+
}
|
|
307
|
+
};
|
|
284
308
|
let document;
|
|
285
309
|
try {
|
|
286
|
-
document = await this.
|
|
287
|
-
[componentField]: {
|
|
288
|
-
populate: "*"
|
|
289
|
-
}
|
|
290
|
-
});
|
|
310
|
+
document = await this.fetchDocumentWithPublishedFallback(contentType, documentId, populate);
|
|
291
311
|
} catch (error) {
|
|
292
|
-
|
|
293
|
-
|
|
312
|
+
try {
|
|
313
|
+
document = await this.fetchDocument(contentType, documentId, {
|
|
314
|
+
[componentField]: {
|
|
315
|
+
populate: "*"
|
|
316
|
+
}
|
|
317
|
+
});
|
|
318
|
+
} catch (finalError) {
|
|
319
|
+
const message = finalError instanceof Error ? finalError.message : "Unknown error";
|
|
320
|
+
throw new Error(`Failed to fetch document: ${message}`);
|
|
321
|
+
}
|
|
294
322
|
}
|
|
295
323
|
if (!document) {
|
|
296
324
|
throw new Error("Document not found");
|
|
@@ -373,20 +401,35 @@ const service = ({ strapi }) => ({
|
|
|
373
401
|
* Used for fields inside components within dynamic zones or repeatable components
|
|
374
402
|
*/
|
|
375
403
|
async previewDeepNestedField(contentType, documentId, parentField, componentField, nestedField, indices = null) {
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
populate: "*"
|
|
404
|
+
const populate = {
|
|
405
|
+
[parentField]: {
|
|
406
|
+
populate: {
|
|
407
|
+
[componentField]: {
|
|
408
|
+
populate: {
|
|
409
|
+
[nestedField]: true
|
|
383
410
|
}
|
|
384
411
|
}
|
|
385
412
|
}
|
|
386
|
-
}
|
|
413
|
+
}
|
|
414
|
+
};
|
|
415
|
+
let document;
|
|
416
|
+
try {
|
|
417
|
+
document = await this.fetchDocumentWithPublishedFallback(contentType, documentId, populate);
|
|
387
418
|
} catch (error) {
|
|
388
|
-
|
|
389
|
-
|
|
419
|
+
try {
|
|
420
|
+
document = await this.fetchDocument(contentType, documentId, {
|
|
421
|
+
[parentField]: {
|
|
422
|
+
populate: {
|
|
423
|
+
[componentField]: {
|
|
424
|
+
populate: "*"
|
|
425
|
+
}
|
|
426
|
+
}
|
|
427
|
+
}
|
|
428
|
+
});
|
|
429
|
+
} catch (fallbackError) {
|
|
430
|
+
const message = fallbackError instanceof Error ? fallbackError.message : "Unknown error";
|
|
431
|
+
throw new Error(`Failed to fetch document: ${message}`);
|
|
432
|
+
}
|
|
390
433
|
}
|
|
391
434
|
if (!document) {
|
|
392
435
|
throw new Error("Document not found");
|
|
@@ -598,17 +641,27 @@ const service = ({ strapi }) => ({
|
|
|
598
641
|
return { message: `Field "${fieldName}" is already empty`, clearedCount: 0 };
|
|
599
642
|
}
|
|
600
643
|
const clearedCount = this.countFieldItems(fieldValue);
|
|
601
|
-
const internalId = document.id;
|
|
602
|
-
if (!internalId) {
|
|
603
|
-
throw new Error("Document internal ID not found");
|
|
604
|
-
}
|
|
605
644
|
const newValue = this.getEmptyValue(fieldValue);
|
|
645
|
+
const docId = document.documentId || documentId;
|
|
606
646
|
try {
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
647
|
+
if (docId) {
|
|
648
|
+
await strapi.documents(contentType).update({
|
|
649
|
+
documentId: docId,
|
|
650
|
+
data: {
|
|
651
|
+
[fieldName]: newValue
|
|
652
|
+
}
|
|
653
|
+
});
|
|
654
|
+
} else {
|
|
655
|
+
const internalId = document.id;
|
|
656
|
+
if (!internalId) {
|
|
657
|
+
throw new Error("Document internal ID not found");
|
|
610
658
|
}
|
|
611
|
-
|
|
659
|
+
await strapi.entityService.update(contentType, internalId, {
|
|
660
|
+
data: {
|
|
661
|
+
[fieldName]: newValue
|
|
662
|
+
}
|
|
663
|
+
});
|
|
664
|
+
}
|
|
612
665
|
} catch (error) {
|
|
613
666
|
const message = error instanceof Error ? error.message : "Unknown error";
|
|
614
667
|
throw new Error(`Failed to update document: ${message}`);
|
|
@@ -631,16 +684,27 @@ const service = ({ strapi }) => ({
|
|
|
631
684
|
if (!nestedField || typeof nestedField !== "string") {
|
|
632
685
|
throw new Error("Invalid nested field name provided");
|
|
633
686
|
}
|
|
687
|
+
const populate = {
|
|
688
|
+
[componentField]: {
|
|
689
|
+
populate: {
|
|
690
|
+
[nestedField]: true
|
|
691
|
+
}
|
|
692
|
+
}
|
|
693
|
+
};
|
|
634
694
|
let document;
|
|
635
695
|
try {
|
|
636
|
-
document = await this.fetchDocument(contentType, documentId,
|
|
637
|
-
[componentField]: {
|
|
638
|
-
populate: "*"
|
|
639
|
-
}
|
|
640
|
-
});
|
|
696
|
+
document = await this.fetchDocument(contentType, documentId, populate);
|
|
641
697
|
} catch (error) {
|
|
642
|
-
|
|
643
|
-
|
|
698
|
+
try {
|
|
699
|
+
document = await this.fetchDocument(contentType, documentId, {
|
|
700
|
+
[componentField]: {
|
|
701
|
+
populate: "*"
|
|
702
|
+
}
|
|
703
|
+
});
|
|
704
|
+
} catch (finalError) {
|
|
705
|
+
const message = finalError instanceof Error ? finalError.message : "Unknown error";
|
|
706
|
+
throw new Error(`Failed to fetch document: ${message}`);
|
|
707
|
+
}
|
|
644
708
|
}
|
|
645
709
|
if (!document) {
|
|
646
710
|
throw new Error("Document not found");
|
|
@@ -734,17 +798,27 @@ const service = ({ strapi }) => ({
|
|
|
734
798
|
}
|
|
735
799
|
return updated;
|
|
736
800
|
}).filter(Boolean);
|
|
737
|
-
const internalId = document.id;
|
|
738
|
-
if (!internalId) {
|
|
739
|
-
throw new Error("Document internal ID not found");
|
|
740
|
-
}
|
|
741
801
|
const updateData = isRepeatable ? updatedComponents : updatedComponents[0];
|
|
802
|
+
const docId = document.documentId || documentId;
|
|
742
803
|
try {
|
|
743
|
-
|
|
744
|
-
|
|
745
|
-
|
|
804
|
+
if (docId) {
|
|
805
|
+
await strapi.documents(contentType).update({
|
|
806
|
+
documentId: docId,
|
|
807
|
+
data: {
|
|
808
|
+
[componentField]: updateData
|
|
809
|
+
}
|
|
810
|
+
});
|
|
811
|
+
} else {
|
|
812
|
+
const internalId = document.id;
|
|
813
|
+
if (!internalId) {
|
|
814
|
+
throw new Error("Document internal ID not found");
|
|
746
815
|
}
|
|
747
|
-
|
|
816
|
+
await strapi.entityService.update(contentType, internalId, {
|
|
817
|
+
data: {
|
|
818
|
+
[componentField]: updateData
|
|
819
|
+
}
|
|
820
|
+
});
|
|
821
|
+
}
|
|
748
822
|
} catch (error) {
|
|
749
823
|
const message = error instanceof Error ? error.message : "Unknown error";
|
|
750
824
|
throw new Error(`Failed to update document: ${message}`);
|
|
@@ -764,20 +838,35 @@ const service = ({ strapi }) => ({
|
|
|
764
838
|
if (!parentField || !componentField || !nestedField) {
|
|
765
839
|
throw new Error("Invalid field path provided");
|
|
766
840
|
}
|
|
767
|
-
|
|
768
|
-
|
|
769
|
-
|
|
770
|
-
|
|
771
|
-
|
|
772
|
-
|
|
773
|
-
populate: "*"
|
|
841
|
+
const populate = {
|
|
842
|
+
[parentField]: {
|
|
843
|
+
populate: {
|
|
844
|
+
[componentField]: {
|
|
845
|
+
populate: {
|
|
846
|
+
[nestedField]: true
|
|
774
847
|
}
|
|
775
848
|
}
|
|
776
849
|
}
|
|
777
|
-
}
|
|
850
|
+
}
|
|
851
|
+
};
|
|
852
|
+
let document;
|
|
853
|
+
try {
|
|
854
|
+
document = await this.fetchDocument(contentType, documentId, populate);
|
|
778
855
|
} catch (error) {
|
|
779
|
-
|
|
780
|
-
|
|
856
|
+
try {
|
|
857
|
+
document = await this.fetchDocument(contentType, documentId, {
|
|
858
|
+
[parentField]: {
|
|
859
|
+
populate: {
|
|
860
|
+
[componentField]: {
|
|
861
|
+
populate: "*"
|
|
862
|
+
}
|
|
863
|
+
}
|
|
864
|
+
}
|
|
865
|
+
});
|
|
866
|
+
} catch (fallbackError) {
|
|
867
|
+
const message = fallbackError instanceof Error ? fallbackError.message : "Unknown error";
|
|
868
|
+
throw new Error(`Failed to fetch document: ${message}`);
|
|
869
|
+
}
|
|
781
870
|
}
|
|
782
871
|
if (!document) {
|
|
783
872
|
throw new Error("Document not found");
|
|
@@ -881,17 +970,27 @@ const service = ({ strapi }) => ({
|
|
|
881
970
|
}
|
|
882
971
|
return updated;
|
|
883
972
|
}).filter(Boolean);
|
|
884
|
-
const internalId = document.id;
|
|
885
|
-
if (!internalId) {
|
|
886
|
-
throw new Error("Document internal ID not found");
|
|
887
|
-
}
|
|
888
973
|
const updateData = isRepeatable ? updatedParentComponents : updatedParentComponents[0];
|
|
974
|
+
const docId = document.documentId || documentId;
|
|
889
975
|
try {
|
|
890
|
-
|
|
891
|
-
|
|
892
|
-
|
|
976
|
+
if (docId) {
|
|
977
|
+
await strapi.documents(contentType).update({
|
|
978
|
+
documentId: docId,
|
|
979
|
+
data: {
|
|
980
|
+
[parentField]: updateData
|
|
981
|
+
}
|
|
982
|
+
});
|
|
983
|
+
} else {
|
|
984
|
+
const internalId = document.id;
|
|
985
|
+
if (!internalId) {
|
|
986
|
+
throw new Error("Document internal ID not found");
|
|
893
987
|
}
|
|
894
|
-
|
|
988
|
+
await strapi.entityService.update(contentType, internalId, {
|
|
989
|
+
data: {
|
|
990
|
+
[parentField]: updateData
|
|
991
|
+
}
|
|
992
|
+
});
|
|
993
|
+
}
|
|
895
994
|
} catch (error) {
|
|
896
995
|
const message = error instanceof Error ? error.message : "Unknown error";
|
|
897
996
|
throw new Error(`Failed to update document: ${message}`);
|
|
@@ -37,7 +37,8 @@ declare const _default: {
|
|
|
37
37
|
service: ({ strapi }: {
|
|
38
38
|
strapi: import("@strapi/types/dist/core").Strapi;
|
|
39
39
|
}) => {
|
|
40
|
-
fetchDocument(contentType: string, documentId: string, populate: any): Promise<import("@strapi/types/dist/modules/documents").AnyDocument>;
|
|
40
|
+
fetchDocument(contentType: string, documentId: string, populate: any, status?: string): Promise<import("@strapi/types/dist/modules/documents").AnyDocument>;
|
|
41
|
+
fetchDocumentWithPublishedFallback(contentType: string, documentId: string, populate: any): Promise<import("@strapi/types/dist/modules/documents").AnyDocument>;
|
|
41
42
|
previewField(contentType: string, documentId: string, fieldPath: string): Promise<{
|
|
42
43
|
fieldPath: string;
|
|
43
44
|
fieldType: string;
|
|
@@ -2,7 +2,8 @@ declare const _default: {
|
|
|
2
2
|
service: ({ strapi }: {
|
|
3
3
|
strapi: import("@strapi/types/dist/core").Strapi;
|
|
4
4
|
}) => {
|
|
5
|
-
fetchDocument(contentType: string, documentId: string, populate: any): Promise<import("@strapi/types/dist/modules/documents").AnyDocument>;
|
|
5
|
+
fetchDocument(contentType: string, documentId: string, populate: any, status?: string): Promise<import("@strapi/types/dist/modules/documents").AnyDocument>;
|
|
6
|
+
fetchDocumentWithPublishedFallback(contentType: string, documentId: string, populate: any): Promise<import("@strapi/types/dist/modules/documents").AnyDocument>;
|
|
6
7
|
previewField(contentType: string, documentId: string, fieldPath: string): Promise<{
|
|
7
8
|
fieldPath: string;
|
|
8
9
|
fieldType: string;
|
|
@@ -4,8 +4,16 @@ declare const service: ({ strapi }: {
|
|
|
4
4
|
}) => {
|
|
5
5
|
/**
|
|
6
6
|
* Fetch a document by documentId, or use findFirst() for single types when documentId is empty.
|
|
7
|
+
* Optionally specify status ('published' or 'draft') - defaults to Strapi's default (draft in admin context).
|
|
7
8
|
*/
|
|
8
|
-
fetchDocument(contentType: string, documentId: string, populate: any): Promise<import("@strapi/types/dist/modules/documents").AnyDocument>;
|
|
9
|
+
fetchDocument(contentType: string, documentId: string, populate: any, status?: string): Promise<import("@strapi/types/dist/modules/documents").AnyDocument>;
|
|
10
|
+
/**
|
|
11
|
+
* Smart fetch for reading relation data: tries published version first (which has complete
|
|
12
|
+
* relation data), then falls back to draft. This works around the Strapi v5 draft/publish
|
|
13
|
+
* issue where relation join tables can have mixed draft/published product IDs, causing the
|
|
14
|
+
* draft version to return incomplete relations.
|
|
15
|
+
*/
|
|
16
|
+
fetchDocumentWithPublishedFallback(contentType: string, documentId: string, populate: any): Promise<import("@strapi/types/dist/modules/documents").AnyDocument>;
|
|
9
17
|
/**
|
|
10
18
|
* Preview what will be deleted (dry run - no actual deletion)
|
|
11
19
|
* Returns field info and items that would be deleted
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "strapi-plugin-field-clearer",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.11",
|
|
4
4
|
"description": "A Strapi v5 plugin to clear/delete field data from content types. Supports nested fields, component arrays, relations, and more.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"strapi",
|
|
@@ -82,5 +82,6 @@
|
|
|
82
82
|
"name": "field-clearer",
|
|
83
83
|
"displayName": "Field Clearer",
|
|
84
84
|
"description": "Clear/delete field data from content types with a simple UI"
|
|
85
|
-
}
|
|
85
|
+
},
|
|
86
|
+
"dependencies": {}
|
|
86
87
|
}
|