@sequoialabs/payload-plugin-reversia 0.1.4 → 0.1.5

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.
@@ -855,9 +855,13 @@ export function deflatePopulatedRelationships(value) {
855
855
  return out;
856
856
  }
857
857
  /**
858
- * Deflate a single value: if it's a populated doc, return its id. If it's a
859
- * polymorphic relationship `{ relationTo, value: <populated> }`, deflate the
860
- * inner value. Otherwise recurse.
858
+ * Deflate a single value: if it's a populated doc, return its id. If it's an
859
+ * object with a `relationTo` + populated `value` (polymorphic relationship OR
860
+ * Lexical upload/relationship node), deflate only the `value` property while
861
+ * preserving all other keys (`type`, `children`, `version`, `format`, …).
862
+ *
863
+ * Previous implementation returned `{ relationTo, value: id }` which stripped
864
+ * Lexical node metadata and corrupted richText trees.
861
865
  */
862
866
  function deflateValue(val) {
863
867
  if (val === null || val === undefined || typeof val !== 'object') {
@@ -866,30 +870,51 @@ function deflateValue(val) {
866
870
  if (Array.isArray(val)) {
867
871
  return val.map(deflateValue);
868
872
  }
869
- // Direct populated doc → raw id (e.g. upload field: { id, filename, ... })
870
- if (isPopulatedDoc(val)) {
871
- return val.id;
872
- }
873
- // Polymorphic relationship: { relationTo: 'x', value: <populated> }
874
873
  const obj = val;
875
- if (typeof obj.relationTo === 'string' && 'value' in obj && isPopulatedDoc(obj.value)) {
876
- return { relationTo: obj.relationTo, value: obj.value.id };
877
- }
878
- // hasMany polymorphic: { relationTo: 'x', value: [<populated>, ...] }
879
- if (typeof obj.relationTo === 'string' && Array.isArray(obj.value)) {
880
- return {
881
- relationTo: obj.relationTo,
882
- value: obj.value.map((item) => isPopulatedDoc(item) ? item.id : item),
883
- };
874
+ // Direct populated doc → raw id (e.g. upload field stored as { id, filename, … })
875
+ // Only match when the object has NO other "structural" keys that indicate
876
+ // it's a Lexical node or rich object we shouldn't collapse.
877
+ if (isPopulatedDoc(obj) && !isStructuralNode(obj)) {
878
+ return obj.id;
879
+ }
880
+ // Object with `relationTo` + populated `value` — this matches both Payload
881
+ // polymorphic relationships AND Lexical upload/relationship nodes.
882
+ // Deflate only the `value` key; preserve everything else.
883
+ if (typeof obj.relationTo === 'string' && 'value' in obj) {
884
+ if (isPopulatedDoc(obj.value)) {
885
+ const out = {};
886
+ for (const [k, v] of Object.entries(obj)) {
887
+ out[k] = k === 'value' ? obj.value.id : deflateValue(v);
888
+ }
889
+ return out;
890
+ }
891
+ if (Array.isArray(obj.value)) {
892
+ const out = {};
893
+ for (const [k, v] of Object.entries(obj)) {
894
+ out[k] =
895
+ k === 'value'
896
+ ? v.map((item) => isPopulatedDoc(item) ? item.id : item)
897
+ : deflateValue(v);
898
+ }
899
+ return out;
900
+ }
884
901
  }
885
902
  // Recurse into nested structure
886
903
  return deflatePopulatedRelationships(val);
887
904
  }
905
+ /**
906
+ * A "structural node" is an object that carries its own semantics beyond being
907
+ * a populated relationship doc — e.g. a Lexical node with `type`, `children`,
908
+ * `version`. Collapsing it to just its `id` would destroy the tree.
909
+ */
910
+ function isStructuralNode(obj) {
911
+ return typeof obj.type === 'string' || 'children' in obj || 'version' in obj;
912
+ }
888
913
  function isPopulatedDoc(val) {
889
914
  if (!val || typeof val !== 'object' || Array.isArray(val)) {
890
915
  return false;
891
916
  }
892
917
  const obj = val;
893
- return (typeof obj.id === 'string' &&
894
- ('createdAt' in obj || 'updatedAt' in obj || 'filename' in obj || 'mimeType' in obj));
918
+ const hasId = typeof obj.id === 'string' || typeof obj.id === 'number';
919
+ return (hasId && ('createdAt' in obj || 'updatedAt' in obj || 'filename' in obj || 'mimeType' in obj));
895
920
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sequoialabs/payload-plugin-reversia",
3
- "version": "0.1.4",
3
+ "version": "0.1.5",
4
4
  "author": {
5
5
  "name": "Jean Walrave",
6
6
  "email": "contact@reversia.tech",