Some checks failed
Build & Deploy / 🔍 Prepare (push) Successful in 20s
Build & Deploy / 🧪 QA (push) Failing after 34s
Build & Deploy / 🏗️ Build (push) Has started running
Build & Deploy / 🚀 Deploy (push) Has been cancelled
Build & Deploy / 🧪 Smoke Test (push) Has been cancelled
Build & Deploy / ⚡ Lighthouse (push) Has been cancelled
Build & Deploy / 🔔 Notify (push) Has been cancelled
1 line
4.1 KiB
Plaintext
1 line
4.1 KiB
Plaintext
{"version":3,"sources":["../../../src/transform/read/relationship.ts"],"sourcesContent":["import type { RelationshipField, UploadField } from 'payload'\n\ntype Args = {\n field: RelationshipField | UploadField\n locale?: string\n ref: Record<string, unknown>\n relations: Record<string, unknown>[]\n withinArrayOrBlockLocale?: string\n}\n\nexport const transformRelationship = ({\n field,\n locale,\n ref,\n relations,\n withinArrayOrBlockLocale,\n}: Args) => {\n let result: unknown\n\n if (!('hasMany' in field) || field.hasMany === false) {\n let relation = relations[0]\n\n if (withinArrayOrBlockLocale) {\n relation = relations.find((rel) => rel.locale === withinArrayOrBlockLocale)\n }\n\n if (relation) {\n // Handle hasOne Poly\n if (Array.isArray(field.relationTo)) {\n const matchedRelation = Object.entries(relation).find(([key, val]) => {\n return val !== null && !['id', 'locale', 'order', 'parent', 'path'].includes(key)\n })\n\n if (matchedRelation) {\n const relationTo = matchedRelation[0].replace('ID', '')\n\n result = {\n relationTo,\n value: matchedRelation[1],\n }\n }\n }\n }\n } else {\n const transformedRelations = []\n\n relations.forEach((relation) => {\n let matchedLocale = true\n\n if (withinArrayOrBlockLocale) {\n matchedLocale = relation.locale === withinArrayOrBlockLocale\n }\n\n // Handle hasMany\n if (!Array.isArray(field.relationTo)) {\n const relatedData = relation[`${field.relationTo}ID`]\n\n if (relatedData && matchedLocale) {\n transformedRelations.push(relatedData)\n }\n } else {\n // Handle hasMany Poly\n const matchedRelation = Object.entries(relation).find(\n ([key, val]) =>\n val !== null &&\n !['id', 'locale', 'order', 'parent', 'path'].includes(key) &&\n matchedLocale,\n )\n\n if (matchedRelation) {\n const relationTo = matchedRelation[0].replace('ID', '')\n\n transformedRelations.push({\n relationTo,\n value: matchedRelation[1],\n })\n }\n }\n })\n\n result = transformedRelations\n }\n\n if (locale) {\n ref[field.name][locale] = result\n } else {\n ref[field.name] = result\n }\n}\n"],"names":["transformRelationship","field","locale","ref","relations","withinArrayOrBlockLocale","result","hasMany","relation","find","rel","Array","isArray","relationTo","matchedRelation","Object","entries","key","val","includes","replace","value","transformedRelations","forEach","matchedLocale","relatedData","push","name"],"mappings":"AAUA,OAAO,MAAMA,wBAAwB,CAAC,EACpCC,KAAK,EACLC,MAAM,EACNC,GAAG,EACHC,SAAS,EACTC,wBAAwB,EACnB;IACL,IAAIC;IAEJ,IAAI,CAAE,CAAA,aAAaL,KAAI,KAAMA,MAAMM,OAAO,KAAK,OAAO;QACpD,IAAIC,WAAWJ,SAAS,CAAC,EAAE;QAE3B,IAAIC,0BAA0B;YAC5BG,WAAWJ,UAAUK,IAAI,CAAC,CAACC,MAAQA,IAAIR,MAAM,KAAKG;QACpD;QAEA,IAAIG,UAAU;YACZ,qBAAqB;YACrB,IAAIG,MAAMC,OAAO,CAACX,MAAMY,UAAU,GAAG;gBACnC,MAAMC,kBAAkBC,OAAOC,OAAO,CAACR,UAAUC,IAAI,CAAC,CAAC,CAACQ,KAAKC,IAAI;oBAC/D,OAAOA,QAAQ,QAAQ,CAAC;wBAAC;wBAAM;wBAAU;wBAAS;wBAAU;qBAAO,CAACC,QAAQ,CAACF;gBAC/E;gBAEA,IAAIH,iBAAiB;oBACnB,MAAMD,aAAaC,eAAe,CAAC,EAAE,CAACM,OAAO,CAAC,MAAM;oBAEpDd,SAAS;wBACPO;wBACAQ,OAAOP,eAAe,CAAC,EAAE;oBAC3B;gBACF;YACF;QACF;IACF,OAAO;QACL,MAAMQ,uBAAuB,EAAE;QAE/BlB,UAAUmB,OAAO,CAAC,CAACf;YACjB,IAAIgB,gBAAgB;YAEpB,IAAInB,0BAA0B;gBAC5BmB,gB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