@sequoialabs/payload-plugin-reversia 0.1.1 → 0.1.2
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/utils/fields.js +37 -19
- package/package.json +1 -1
package/dist/utils/fields.js
CHANGED
|
@@ -786,7 +786,7 @@ export function deflatePopulatedRelationships(value) {
|
|
|
786
786
|
return value.map(deflatePopulatedRelationships);
|
|
787
787
|
}
|
|
788
788
|
const obj = value;
|
|
789
|
-
// Lexical block node — deflate populated fields
|
|
789
|
+
// Lexical block node — deflate populated fields inside `fields`.
|
|
790
790
|
if (obj.type === 'block' && obj.fields && typeof obj.fields === 'object') {
|
|
791
791
|
const fields = obj.fields;
|
|
792
792
|
const deflated = {};
|
|
@@ -794,14 +794,8 @@ export function deflatePopulatedRelationships(value) {
|
|
|
794
794
|
if (key === 'id' || key === 'blockType' || key === 'blockName') {
|
|
795
795
|
deflated[key] = val;
|
|
796
796
|
}
|
|
797
|
-
else if (Array.isArray(val)) {
|
|
798
|
-
deflated[key] = val.map((item) => isPopulatedDoc(item) ? item.id : item);
|
|
799
|
-
}
|
|
800
|
-
else if (isPopulatedDoc(val)) {
|
|
801
|
-
deflated[key] = val.id;
|
|
802
|
-
}
|
|
803
797
|
else {
|
|
804
|
-
deflated[key] = val;
|
|
798
|
+
deflated[key] = deflateValue(val);
|
|
805
799
|
}
|
|
806
800
|
}
|
|
807
801
|
return {
|
|
@@ -812,22 +806,46 @@ export function deflatePopulatedRelationships(value) {
|
|
|
812
806
|
: {}),
|
|
813
807
|
};
|
|
814
808
|
}
|
|
815
|
-
//
|
|
816
|
-
//
|
|
817
|
-
|
|
818
|
-
return {
|
|
819
|
-
...obj,
|
|
820
|
-
children: obj.children.map(deflatePopulatedRelationships),
|
|
821
|
-
};
|
|
822
|
-
}
|
|
823
|
-
// Plain object (group value, etc.) — recurse all keys in case there are
|
|
824
|
-
// nested Lexical trees or arrays.
|
|
809
|
+
// Any other object — recurse all keys, deflating populated docs wherever
|
|
810
|
+
// they appear. This catches relationship / upload fields inside Payload
|
|
811
|
+
// array items, group subfields, etc. — not just Lexical blocks.
|
|
825
812
|
const out = {};
|
|
826
813
|
for (const [k, v] of Object.entries(obj)) {
|
|
827
|
-
out[k] =
|
|
814
|
+
out[k] = deflateValue(v);
|
|
828
815
|
}
|
|
829
816
|
return out;
|
|
830
817
|
}
|
|
818
|
+
/**
|
|
819
|
+
* Deflate a single value: if it's a populated doc, return its id. If it's a
|
|
820
|
+
* polymorphic relationship `{ relationTo, value: <populated> }`, deflate the
|
|
821
|
+
* inner value. Otherwise recurse.
|
|
822
|
+
*/
|
|
823
|
+
function deflateValue(val) {
|
|
824
|
+
if (val === null || val === undefined || typeof val !== 'object') {
|
|
825
|
+
return val;
|
|
826
|
+
}
|
|
827
|
+
if (Array.isArray(val)) {
|
|
828
|
+
return val.map(deflateValue);
|
|
829
|
+
}
|
|
830
|
+
// Direct populated doc → raw id (e.g. upload field: { id, filename, ... })
|
|
831
|
+
if (isPopulatedDoc(val)) {
|
|
832
|
+
return val.id;
|
|
833
|
+
}
|
|
834
|
+
// Polymorphic relationship: { relationTo: 'x', value: <populated> }
|
|
835
|
+
const obj = val;
|
|
836
|
+
if (typeof obj.relationTo === 'string' && 'value' in obj && isPopulatedDoc(obj.value)) {
|
|
837
|
+
return { relationTo: obj.relationTo, value: obj.value.id };
|
|
838
|
+
}
|
|
839
|
+
// hasMany polymorphic: { relationTo: 'x', value: [<populated>, ...] }
|
|
840
|
+
if (typeof obj.relationTo === 'string' && Array.isArray(obj.value)) {
|
|
841
|
+
return {
|
|
842
|
+
relationTo: obj.relationTo,
|
|
843
|
+
value: obj.value.map((item) => isPopulatedDoc(item) ? item.id : item),
|
|
844
|
+
};
|
|
845
|
+
}
|
|
846
|
+
// Recurse into nested structure
|
|
847
|
+
return deflatePopulatedRelationships(val);
|
|
848
|
+
}
|
|
831
849
|
function isPopulatedDoc(val) {
|
|
832
850
|
if (!val || typeof val !== 'object' || Array.isArray(val)) {
|
|
833
851
|
return false;
|