@payloadcms/db-mongodb 3.0.0-canary.f6e77b8 → 3.0.0-canary.fb04843

Sign up to get free protection for your applications and to get access to all the features.
Files changed (220) hide show
  1. package/README.md +1 -1
  2. package/dist/connect.d.ts +1 -1
  3. package/dist/connect.d.ts.map +1 -1
  4. package/dist/connect.js +18 -4
  5. package/dist/connect.js.map +1 -1
  6. package/dist/count.d.ts +1 -1
  7. package/dist/count.d.ts.map +1 -1
  8. package/dist/count.js +10 -3
  9. package/dist/count.js.map +1 -1
  10. package/dist/countGlobalVersions.d.ts +3 -0
  11. package/dist/countGlobalVersions.d.ts.map +1 -0
  12. package/dist/countGlobalVersions.js +40 -0
  13. package/dist/countGlobalVersions.js.map +1 -0
  14. package/dist/countVersions.d.ts +3 -0
  15. package/dist/countVersions.d.ts.map +1 -0
  16. package/dist/countVersions.js +40 -0
  17. package/dist/countVersions.js.map +1 -0
  18. package/dist/create.d.ts +1 -1
  19. package/dist/create.d.ts.map +1 -1
  20. package/dist/create.js +17 -4
  21. package/dist/create.js.map +1 -1
  22. package/dist/createGlobal.d.ts +1 -1
  23. package/dist/createGlobal.d.ts.map +1 -1
  24. package/dist/createGlobal.js +11 -6
  25. package/dist/createGlobal.js.map +1 -1
  26. package/dist/createGlobalVersion.d.ts +1 -1
  27. package/dist/createGlobalVersion.d.ts.map +1 -1
  28. package/dist/createGlobalVersion.js +14 -5
  29. package/dist/createGlobalVersion.js.map +1 -1
  30. package/dist/createMigration.d.ts +1 -1
  31. package/dist/createMigration.d.ts.map +1 -1
  32. package/dist/createMigration.js +20 -32
  33. package/dist/createMigration.js.map +1 -1
  34. package/dist/createVersion.d.ts +1 -1
  35. package/dist/createVersion.d.ts.map +1 -1
  36. package/dist/createVersion.js +32 -10
  37. package/dist/createVersion.js.map +1 -1
  38. package/dist/deleteMany.d.ts +1 -1
  39. package/dist/deleteMany.d.ts.map +1 -1
  40. package/dist/deleteMany.js +1 -1
  41. package/dist/deleteMany.js.map +1 -1
  42. package/dist/deleteOne.d.ts +1 -1
  43. package/dist/deleteOne.d.ts.map +1 -1
  44. package/dist/deleteOne.js +12 -4
  45. package/dist/deleteOne.js.map +1 -1
  46. package/dist/deleteVersions.d.ts +1 -1
  47. package/dist/deleteVersions.d.ts.map +1 -1
  48. package/dist/deleteVersions.js +1 -1
  49. package/dist/deleteVersions.js.map +1 -1
  50. package/dist/destroy.d.ts +1 -1
  51. package/dist/destroy.d.ts.map +1 -1
  52. package/dist/destroy.js.map +1 -1
  53. package/dist/exports/migration-utils.d.ts +3 -0
  54. package/dist/exports/migration-utils.d.ts.map +1 -0
  55. package/dist/exports/migration-utils.js +4 -0
  56. package/dist/exports/migration-utils.js.map +1 -0
  57. package/dist/find.d.ts +1 -1
  58. package/dist/find.d.ts.map +1 -1
  59. package/dist/find.js +36 -7
  60. package/dist/find.js.map +1 -1
  61. package/dist/findGlobal.d.ts +1 -1
  62. package/dist/findGlobal.d.ts.map +1 -1
  63. package/dist/findGlobal.js +11 -5
  64. package/dist/findGlobal.js.map +1 -1
  65. package/dist/findGlobalVersions.d.ts +1 -1
  66. package/dist/findGlobalVersions.d.ts.map +1 -1
  67. package/dist/findGlobalVersions.js +19 -9
  68. package/dist/findGlobalVersions.js.map +1 -1
  69. package/dist/findOne.d.ts +1 -1
  70. package/dist/findOne.d.ts.map +1 -1
  71. package/dist/findOne.js +28 -4
  72. package/dist/findOne.js.map +1 -1
  73. package/dist/findVersions.d.ts +1 -1
  74. package/dist/findVersions.d.ts.map +1 -1
  75. package/dist/findVersions.js +17 -6
  76. package/dist/findVersions.js.map +1 -1
  77. package/dist/index.d.ts +59 -35
  78. package/dist/index.d.ts.map +1 -1
  79. package/dist/index.js +18 -7
  80. package/dist/index.js.map +1 -1
  81. package/dist/init.d.ts +1 -1
  82. package/dist/init.d.ts.map +1 -1
  83. package/dist/init.js +19 -17
  84. package/dist/init.js.map +1 -1
  85. package/dist/migrateFresh.d.ts.map +1 -1
  86. package/dist/migrateFresh.js +1 -1
  87. package/dist/migrateFresh.js.map +1 -1
  88. package/dist/models/buildCollectionSchema.d.ts +2 -29
  89. package/dist/models/buildCollectionSchema.d.ts.map +1 -1
  90. package/dist/models/buildCollectionSchema.js +16 -4
  91. package/dist/models/buildCollectionSchema.js.map +1 -1
  92. package/dist/models/buildGlobalModel.d.ts +1 -1
  93. package/dist/models/buildGlobalModel.d.ts.map +1 -1
  94. package/dist/models/buildGlobalModel.js +2 -2
  95. package/dist/models/buildGlobalModel.js.map +1 -1
  96. package/dist/models/buildSchema.d.ts +2 -29
  97. package/dist/models/buildSchema.d.ts.map +1 -1
  98. package/dist/models/buildSchema.js +116 -23
  99. package/dist/models/buildSchema.js.map +1 -1
  100. package/dist/predefinedMigrations/migrateRelationshipsV2_V3.d.ts +6 -0
  101. package/dist/predefinedMigrations/migrateRelationshipsV2_V3.d.ts.map +1 -0
  102. package/dist/predefinedMigrations/migrateRelationshipsV2_V3.js +141 -0
  103. package/dist/predefinedMigrations/migrateRelationshipsV2_V3.js.map +1 -0
  104. package/dist/predefinedMigrations/migrateVersionsV1_V2.d.ts +5 -0
  105. package/dist/predefinedMigrations/migrateVersionsV1_V2.d.ts.map +1 -0
  106. package/dist/predefinedMigrations/migrateVersionsV1_V2.js +107 -0
  107. package/dist/predefinedMigrations/migrateVersionsV1_V2.js.map +1 -0
  108. package/dist/predefinedMigrations/relationships-v2-v3.d.ts +4 -0
  109. package/dist/predefinedMigrations/relationships-v2-v3.d.ts.map +1 -0
  110. package/dist/predefinedMigrations/relationships-v2-v3.js +9 -0
  111. package/dist/predefinedMigrations/relationships-v2-v3.js.map +1 -0
  112. package/dist/predefinedMigrations/versions-v1-v2.d.ts +4 -0
  113. package/dist/predefinedMigrations/versions-v1-v2.d.ts.map +1 -0
  114. package/dist/predefinedMigrations/versions-v1-v2.js +8 -0
  115. package/dist/predefinedMigrations/versions-v1-v2.js.map +1 -0
  116. package/dist/queries/buildAndOrConditions.d.ts +1 -2
  117. package/dist/queries/buildAndOrConditions.d.ts.map +1 -1
  118. package/dist/queries/buildAndOrConditions.js +0 -2
  119. package/dist/queries/buildAndOrConditions.js.map +1 -1
  120. package/dist/queries/buildQuery.d.ts +3 -4
  121. package/dist/queries/buildQuery.d.ts.map +1 -1
  122. package/dist/queries/buildQuery.js +2 -3
  123. package/dist/queries/buildQuery.js.map +1 -1
  124. package/dist/queries/buildSearchParams.d.ts +1 -2
  125. package/dist/queries/buildSearchParams.d.ts.map +1 -1
  126. package/dist/queries/buildSearchParams.js +34 -22
  127. package/dist/queries/buildSearchParams.js.map +1 -1
  128. package/dist/queries/buildSortParam.d.ts +3 -29
  129. package/dist/queries/buildSortParam.d.ts.map +1 -1
  130. package/dist/queries/buildSortParam.js +26 -17
  131. package/dist/queries/buildSortParam.js.map +1 -1
  132. package/dist/queries/getLocalizedSortProperty.d.ts +1 -2
  133. package/dist/queries/getLocalizedSortProperty.d.ts.map +1 -1
  134. package/dist/queries/getLocalizedSortProperty.js +2 -2
  135. package/dist/queries/getLocalizedSortProperty.js.map +1 -1
  136. package/dist/queries/getLocalizedSortProperty.spec.js +44 -44
  137. package/dist/queries/getLocalizedSortProperty.spec.js.map +1 -1
  138. package/dist/queries/mock.js.map +1 -1
  139. package/dist/queries/operatorMap.js.map +1 -1
  140. package/dist/queries/parseParams.d.ts +1 -3
  141. package/dist/queries/parseParams.d.ts.map +1 -1
  142. package/dist/queries/parseParams.js +8 -6
  143. package/dist/queries/parseParams.js.map +1 -1
  144. package/dist/queries/sanitizeQueryValue.d.ts +1 -1
  145. package/dist/queries/sanitizeQueryValue.d.ts.map +1 -1
  146. package/dist/queries/sanitizeQueryValue.js +145 -28
  147. package/dist/queries/sanitizeQueryValue.js.map +1 -1
  148. package/dist/queryDrafts.d.ts +1 -1
  149. package/dist/queryDrafts.d.ts.map +1 -1
  150. package/dist/queryDrafts.js +37 -10
  151. package/dist/queryDrafts.js.map +1 -1
  152. package/dist/testCredentials.js.map +1 -1
  153. package/dist/transactions/beginTransaction.d.ts +1 -1
  154. package/dist/transactions/beginTransaction.d.ts.map +1 -1
  155. package/dist/transactions/beginTransaction.js +3 -1
  156. package/dist/transactions/beginTransaction.js.map +1 -1
  157. package/dist/transactions/commitTransaction.d.ts +1 -1
  158. package/dist/transactions/commitTransaction.d.ts.map +1 -1
  159. package/dist/transactions/commitTransaction.js +3 -0
  160. package/dist/transactions/commitTransaction.js.map +1 -1
  161. package/dist/transactions/rollbackTransaction.d.ts +1 -1
  162. package/dist/transactions/rollbackTransaction.d.ts.map +1 -1
  163. package/dist/transactions/rollbackTransaction.js +13 -7
  164. package/dist/transactions/rollbackTransaction.js.map +1 -1
  165. package/dist/types.d.ts +7 -31
  166. package/dist/types.d.ts.map +1 -1
  167. package/dist/types.js.map +1 -1
  168. package/dist/updateGlobal.d.ts +1 -1
  169. package/dist/updateGlobal.d.ts.map +1 -1
  170. package/dist/updateGlobal.js +18 -5
  171. package/dist/updateGlobal.js.map +1 -1
  172. package/dist/updateGlobalVersion.d.ts +2 -3
  173. package/dist/updateGlobalVersion.d.ts.map +1 -1
  174. package/dist/updateGlobalVersion.js +19 -5
  175. package/dist/updateGlobalVersion.js.map +1 -1
  176. package/dist/updateOne.d.ts +1 -1
  177. package/dist/updateOne.d.ts.map +1 -1
  178. package/dist/updateOne.js +25 -7
  179. package/dist/updateOne.js.map +1 -1
  180. package/dist/updateVersion.d.ts +1 -1
  181. package/dist/updateVersion.d.ts.map +1 -1
  182. package/dist/updateVersion.js +18 -4
  183. package/dist/updateVersion.js.map +1 -1
  184. package/dist/upsert.d.ts +3 -0
  185. package/dist/upsert.d.ts.map +1 -0
  186. package/dist/upsert.js +15 -0
  187. package/dist/upsert.js.map +1 -0
  188. package/dist/utilities/buildJoinAggregation.d.ts +18 -0
  189. package/dist/utilities/buildJoinAggregation.d.ts.map +1 -0
  190. package/dist/utilities/buildJoinAggregation.js +159 -0
  191. package/dist/utilities/buildJoinAggregation.js.map +1 -0
  192. package/dist/utilities/buildProjectionFromSelect.d.ts +8 -0
  193. package/dist/utilities/buildProjectionFromSelect.d.ts.map +1 -0
  194. package/dist/utilities/buildProjectionFromSelect.js +171 -0
  195. package/dist/utilities/buildProjectionFromSelect.js.map +1 -0
  196. package/dist/utilities/getDBName.d.ts +1 -1
  197. package/dist/utilities/getDBName.d.ts.map +1 -1
  198. package/dist/utilities/getDBName.js +3 -1
  199. package/dist/utilities/getDBName.js.map +1 -1
  200. package/dist/utilities/handleError.d.ts +6 -2
  201. package/dist/utilities/handleError.d.ts.map +1 -1
  202. package/dist/utilities/handleError.js +12 -9
  203. package/dist/utilities/handleError.js.map +1 -1
  204. package/dist/utilities/sanitizeInternalFields.d.ts +1 -2
  205. package/dist/utilities/sanitizeInternalFields.d.ts.map +1 -1
  206. package/dist/utilities/sanitizeInternalFields.js +1 -2
  207. package/dist/utilities/sanitizeInternalFields.js.map +1 -1
  208. package/dist/utilities/sanitizeRelationshipIDs.d.ts +9 -0
  209. package/dist/utilities/sanitizeRelationshipIDs.d.ts.map +1 -0
  210. package/dist/utilities/sanitizeRelationshipIDs.js +115 -0
  211. package/dist/utilities/sanitizeRelationshipIDs.js.map +1 -0
  212. package/dist/utilities/sanitizeRelationshipIDs.spec.js +383 -0
  213. package/dist/utilities/sanitizeRelationshipIDs.spec.js.map +1 -0
  214. package/dist/withSession.d.ts +3 -27
  215. package/dist/withSession.d.ts.map +1 -1
  216. package/dist/withSession.js +10 -4
  217. package/dist/withSession.js.map +1 -1
  218. package/license.md +22 -0
  219. package/package.json +29 -17
  220. package/predefinedMigrations/versions-v1-v2.js +0 -96
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../src/utilities/buildJoinAggregation.ts"],"sourcesContent":["import type { PipelineStage } from 'mongoose'\nimport type { CollectionSlug, JoinQuery, SanitizedCollectionConfig, Where } from 'payload'\n\nimport type { MongooseAdapter } from '../index.js'\n\nimport { buildSortParam } from '../queries/buildSortParam.js'\n\ntype BuildJoinAggregationArgs = {\n adapter: MongooseAdapter\n collection: CollectionSlug\n collectionConfig: SanitizedCollectionConfig\n joins: JoinQuery\n // the number of docs to get at the top collection level\n limit?: number\n locale: string\n projection?: Record<string, true>\n // the where clause for the top collection\n query?: Where\n /** whether the query is from drafts */\n versions?: boolean\n}\n\nexport const buildJoinAggregation = async ({\n adapter,\n collection,\n collectionConfig,\n joins,\n limit,\n locale,\n projection,\n query,\n versions,\n}: BuildJoinAggregationArgs): Promise<PipelineStage[] | undefined> => {\n if (Object.keys(collectionConfig.joins).length === 0 || joins === false) {\n return\n }\n\n const joinConfig = adapter.payload.collections[collection].config.joins\n const aggregate: PipelineStage[] = [\n {\n $sort: { createdAt: -1 },\n },\n ]\n\n if (query) {\n aggregate.push({\n $match: query,\n })\n }\n\n if (limit) {\n aggregate.push({\n $limit: limit,\n })\n }\n\n for (const slug of Object.keys(joinConfig)) {\n for (const join of joinConfig[slug]) {\n const joinModel = adapter.collections[join.field.collection]\n\n if (projection && !projection[join.joinPath]) {\n continue\n }\n\n if (joins?.[join.joinPath] === false) {\n continue\n }\n\n const {\n limit: limitJoin = join.field.defaultLimit ?? 10,\n sort: sortJoin = join.field.defaultSort || collectionConfig.defaultSort,\n where: whereJoin,\n } = joins?.[join.joinPath] || {}\n\n const sort = buildSortParam({\n config: adapter.payload.config,\n fields: adapter.payload.collections[slug].config.fields,\n locale,\n sort: sortJoin,\n timestamps: true,\n })\n const sortProperty = Object.keys(sort)[0]\n const sortDirection = sort[sortProperty] === 'asc' ? 1 : -1\n\n const $match = await joinModel.buildQuery({\n locale,\n payload: adapter.payload,\n where: whereJoin,\n })\n\n const pipeline: Exclude<PipelineStage, PipelineStage.Merge | PipelineStage.Out>[] = [\n { $match },\n {\n $sort: { [sortProperty]: sortDirection },\n },\n ]\n\n if (limitJoin > 0) {\n pipeline.push({\n $limit: limitJoin + 1,\n })\n }\n\n if (adapter.payload.config.localization && locale === 'all') {\n adapter.payload.config.localization.localeCodes.forEach((code) => {\n const as = `${versions ? `version.${join.joinPath}` : join.joinPath}${code}`\n\n aggregate.push(\n {\n $lookup: {\n as: `${as}.docs`,\n foreignField: `${join.field.on}${code}`,\n from: adapter.collections[slug].collection.name,\n localField: versions ? 'parent' : '_id',\n pipeline,\n },\n },\n {\n $addFields: {\n [`${as}.docs`]: {\n $map: {\n as: 'doc',\n in: '$$doc._id',\n input: `$${as}.docs`,\n },\n }, // Slicing the docs to match the limit\n [`${as}.hasNextPage`]: limitJoin\n ? { $gt: [{ $size: `$${as}.docs` }, limitJoin] }\n : false,\n // Boolean indicating if more docs than limit\n },\n },\n )\n if (limitJoin > 0) {\n aggregate.push({\n $addFields: {\n [`${as}.docs`]: {\n $slice: [`$${as}.docs`, limitJoin],\n },\n },\n })\n }\n })\n } else {\n const localeSuffix =\n join.field.localized && adapter.payload.config.localization && locale ? `.${locale}` : ''\n const as = `${versions ? `version.${join.joinPath}` : join.joinPath}${localeSuffix}`\n\n aggregate.push(\n {\n $lookup: {\n as: `${as}.docs`,\n foreignField: `${join.field.on}${localeSuffix}`,\n from: adapter.collections[slug].collection.name,\n localField: versions ? 'parent' : '_id',\n pipeline,\n },\n },\n {\n $addFields: {\n [`${as}.docs`]: {\n $map: {\n as: 'doc',\n in: '$$doc._id',\n input: `$${as}.docs`,\n },\n }, // Slicing the docs to match the limit\n [`${as}.hasNextPage`]: {\n $gt: [{ $size: `$${as}.docs` }, limitJoin || Number.MAX_VALUE],\n }, // Boolean indicating if more docs than limit\n },\n },\n )\n if (limitJoin > 0) {\n aggregate.push({\n $addFields: {\n [`${as}.docs`]: {\n $slice: [`$${as}.docs`, limitJoin],\n },\n },\n })\n }\n }\n }\n }\n\n if (projection) {\n aggregate.push({ $project: projection })\n }\n\n return aggregate\n}\n"],"names":["buildSortParam","buildJoinAggregation","adapter","collection","collectionConfig","joins","limit","locale","projection","query","versions","Object","keys","length","joinConfig","payload","collections","config","aggregate","$sort","createdAt","push","$match","$limit","slug","join","joinModel","field","joinPath","limitJoin","defaultLimit","sort","sortJoin","defaultSort","where","whereJoin","fields","timestamps","sortProperty","sortDirection","buildQuery","pipeline","localization","localeCodes","forEach","code","as","$lookup","foreignField","on","from","name","localField","$addFields","$map","in","input","$gt","$size","$slice","localeSuffix","localized","Number","MAX_VALUE","$project"],"mappings":"AAKA,SAASA,cAAc,QAAQ,+BAA8B;AAiB7D,OAAO,MAAMC,uBAAuB,OAAO,EACzCC,OAAO,EACPC,UAAU,EACVC,gBAAgB,EAChBC,KAAK,EACLC,KAAK,EACLC,MAAM,EACNC,UAAU,EACVC,KAAK,EACLC,QAAQ,EACiB;IACzB,IAAIC,OAAOC,IAAI,CAACR,iBAAiBC,KAAK,EAAEQ,MAAM,KAAK,KAAKR,UAAU,OAAO;QACvE;IACF;IAEA,MAAMS,aAAaZ,QAAQa,OAAO,CAACC,WAAW,CAACb,WAAW,CAACc,MAAM,CAACZ,KAAK;IACvE,MAAMa,YAA6B;QACjC;YACEC,OAAO;gBAAEC,WAAW,CAAC;YAAE;QACzB;KACD;IAED,IAAIX,OAAO;QACTS,UAAUG,IAAI,CAAC;YACbC,QAAQb;QACV;IACF;IAEA,IAAIH,OAAO;QACTY,UAAUG,IAAI,CAAC;YACbE,QAAQjB;QACV;IACF;IAEA,KAAK,MAAMkB,QAAQb,OAAOC,IAAI,CAACE,YAAa;QAC1C,KAAK,MAAMW,QAAQX,UAAU,CAACU,KAAK,CAAE;YACnC,MAAME,YAAYxB,QAAQc,WAAW,CAACS,KAAKE,KAAK,CAACxB,UAAU,CAAC;YAE5D,IAAIK,cAAc,CAACA,UAAU,CAACiB,KAAKG,QAAQ,CAAC,EAAE;gBAC5C;YACF;YAEA,IAAIvB,OAAO,CAACoB,KAAKG,QAAQ,CAAC,KAAK,OAAO;gBACpC;YACF;YAEA,MAAM,EACJtB,OAAOuB,YAAYJ,KAAKE,KAAK,CAACG,YAAY,IAAI,EAAE,EAChDC,MAAMC,WAAWP,KAAKE,KAAK,CAACM,WAAW,IAAI7B,iBAAiB6B,WAAW,EACvEC,OAAOC,SAAS,EACjB,GAAG9B,OAAO,CAACoB,KAAKG,QAAQ,CAAC,IAAI,CAA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C,EAAE;4BACdQ,MAAM;gCACJR,IAAI;gCACJS,IAAI;gCACJC,OAAO,CAAC,CAAC,EAAEV,GAAG,KAAK,CAAC;4BACtB;wBACF;wBACA,CAAC,CAAC,EAAEA,GAAG,YAAY,CAAC,CAAC,EAAE;4BACrBW,KAAK;gCAAC;oCAAEC,OAAO,CAAC,CAAC,EAAEZ,GAAG,KAAK,CAAC;gCAAC;gCAAGjB,aAAaiC,OAAOC,SAAS;6BAAC;wBAChE;oBACF;gBACF;gBAEF,IAAIlC,YAAY,GAAG;oBACjBX,UAAUG,IAAI,CAAC;wBACbgC,YAAY;4BACV,CAAC,CAAC,EAAEP,GAAG,KAAK,CAAC,CAAC,EAAE;gCACda,QAAQ;oCAAC,CAAC,CAAC,EAAEb,GAAG,KAAK,CAAC;oCAAEjB;iCAAU;4BACpC;wBACF;oBACF;gBACF;YACF;QACF;IACF;IAEA,IAAIrB,YAAY;QACdU,UAAUG,IAAI,CAAC;YAAE2C,UAAUxD;QAAW;IACxC;IAEA,OAAOU;AACT,EAAC"}
@@ -0,0 +1,8 @@
1
+ import { type Field, type SelectType } from 'payload';
2
+ import type { MongooseAdapter } from '../index.js';
3
+ export declare const buildProjectionFromSelect: ({ adapter, fields, select, }: {
4
+ adapter: MongooseAdapter;
5
+ fields: Field[];
6
+ select?: SelectType;
7
+ }) => Record<string, true> | undefined;
8
+ //# sourceMappingURL=buildProjectionFromSelect.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"buildProjectionFromSelect.d.ts","sourceRoot":"","sources":["../../src/utilities/buildProjectionFromSelect.ts"],"names":[],"mappings":"AAAA,OAAO,EAEL,KAAK,KAAK,EAGV,KAAK,UAAU,EAEhB,MAAM,SAAS,CAAA;AAGhB,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,aAAa,CAAA;AA6MlD,eAAO,MAAM,yBAAyB,iCAInC;IACD,OAAO,EAAE,eAAe,CAAA;IACxB,MAAM,EAAE,KAAK,EAAE,CAAA;IACf,MAAM,CAAC,EAAE,UAAU,CAAA;CACpB,KAAG,MAAM,CAAC,MAAM,EAAE,IAAI,CAAC,GAAG,SAmB1B,CAAA"}
@@ -0,0 +1,171 @@
1
+ import { deepCopyObjectSimple } from 'payload';
2
+ import { fieldAffectsData, getSelectMode, tabHasName } from 'payload/shared';
3
+ const addFieldToProjection = ({ adapter, databaseSchemaPath, field, projection, withinLocalizedField })=>{
4
+ const { config } = adapter.payload;
5
+ if (withinLocalizedField && config.localization) {
6
+ for (const locale of config.localization.localeCodes){
7
+ const localeDatabaseSchemaPath = databaseSchemaPath.replace('<locale>', locale);
8
+ projection[`${localeDatabaseSchemaPath}${field.name}`] = true;
9
+ }
10
+ } else {
11
+ projection[`${databaseSchemaPath}${field.name}`] = true;
12
+ }
13
+ };
14
+ const traverseFields = ({ adapter, databaseSchemaPath = '', fields, projection, select, selectAllOnCurrentLevel = false, selectMode, withinLocalizedField = false })=>{
15
+ for (const field of fields){
16
+ if (fieldAffectsData(field)) {
17
+ if (selectMode === 'include') {
18
+ if (select[field.name] === true || selectAllOnCurrentLevel) {
19
+ addFieldToProjection({
20
+ adapter,
21
+ databaseSchemaPath,
22
+ field,
23
+ projection,
24
+ withinLocalizedField
25
+ });
26
+ continue;
27
+ }
28
+ if (!select[field.name]) {
29
+ continue;
30
+ }
31
+ }
32
+ if (selectMode === 'exclude') {
33
+ if (typeof select[field.name] === 'undefined') {
34
+ addFieldToProjection({
35
+ adapter,
36
+ databaseSchemaPath,
37
+ field,
38
+ projection,
39
+ withinLocalizedField
40
+ });
41
+ continue;
42
+ }
43
+ if (select[field.name] === false) {
44
+ continue;
45
+ }
46
+ }
47
+ }
48
+ let fieldDatabaseSchemaPath = databaseSchemaPath;
49
+ let fieldWithinLocalizedField = withinLocalizedField;
50
+ if (fieldAffectsData(field)) {
51
+ fieldDatabaseSchemaPath = `${databaseSchemaPath}${field.name}.`;
52
+ if (field.localized) {
53
+ fieldDatabaseSchemaPath = `${fieldDatabaseSchemaPath}<locale>.`;
54
+ fieldWithinLocalizedField = true;
55
+ }
56
+ }
57
+ switch(field.type){
58
+ case 'array':
59
+ case 'group':
60
+ case 'tab':
61
+ {
62
+ let fieldSelect;
63
+ if (field.type === 'tab' && !tabHasName(field)) {
64
+ fieldSelect = select;
65
+ } else {
66
+ fieldSelect = select[field.name];
67
+ }
68
+ if (field.type === 'array' && selectMode === 'include') {
69
+ fieldSelect['id'] = true;
70
+ }
71
+ traverseFields({
72
+ adapter,
73
+ databaseSchemaPath: fieldDatabaseSchemaPath,
74
+ fields: field.fields,
75
+ projection,
76
+ select: fieldSelect,
77
+ selectMode,
78
+ withinLocalizedField: fieldWithinLocalizedField
79
+ });
80
+ break;
81
+ }
82
+ case 'blocks':
83
+ {
84
+ const blocksSelect = select[field.name];
85
+ for (const block of field.blocks){
86
+ if (selectMode === 'include' && blocksSelect[block.slug] === true || selectMode === 'exclude' && typeof blocksSelect[block.slug] === 'undefined') {
87
+ traverseFields({
88
+ adapter,
89
+ databaseSchemaPath: fieldDatabaseSchemaPath,
90
+ fields: block.fields,
91
+ projection,
92
+ select: {},
93
+ selectAllOnCurrentLevel: true,
94
+ selectMode: 'include',
95
+ withinLocalizedField: fieldWithinLocalizedField
96
+ });
97
+ continue;
98
+ }
99
+ let blockSelectMode = selectMode;
100
+ if (selectMode === 'exclude' && blocksSelect[block.slug] === false) {
101
+ blockSelectMode = 'include';
102
+ }
103
+ if (typeof blocksSelect[block.slug] !== 'object') {
104
+ blocksSelect[block.slug] = {};
105
+ }
106
+ if (blockSelectMode === 'include') {
107
+ blocksSelect[block.slug]['id'] = true;
108
+ blocksSelect[block.slug]['blockType'] = true;
109
+ }
110
+ traverseFields({
111
+ adapter,
112
+ databaseSchemaPath: fieldDatabaseSchemaPath,
113
+ fields: block.fields,
114
+ projection,
115
+ select: blocksSelect[block.slug],
116
+ selectMode: blockSelectMode,
117
+ withinLocalizedField: fieldWithinLocalizedField
118
+ });
119
+ }
120
+ break;
121
+ }
122
+ case 'collapsible':
123
+ case 'row':
124
+ traverseFields({
125
+ adapter,
126
+ databaseSchemaPath,
127
+ fields: field.fields,
128
+ projection,
129
+ select,
130
+ selectMode,
131
+ withinLocalizedField
132
+ });
133
+ break;
134
+ case 'tabs':
135
+ traverseFields({
136
+ adapter,
137
+ databaseSchemaPath,
138
+ fields: field.tabs.map((tab)=>({
139
+ ...tab,
140
+ type: 'tab'
141
+ })),
142
+ projection,
143
+ select,
144
+ selectMode,
145
+ withinLocalizedField
146
+ });
147
+ break;
148
+ default:
149
+ break;
150
+ }
151
+ }
152
+ };
153
+ export const buildProjectionFromSelect = ({ adapter, fields, select })=>{
154
+ if (!select) {
155
+ return;
156
+ }
157
+ const projection = {
158
+ _id: true
159
+ };
160
+ traverseFields({
161
+ adapter,
162
+ fields,
163
+ projection,
164
+ // Clone to safely mutate it later
165
+ select: deepCopyObjectSimple(select),
166
+ selectMode: getSelectMode(select)
167
+ });
168
+ return projection;
169
+ };
170
+
171
+ //# sourceMappingURL=buildProjectionFromSelect.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../src/utilities/buildProjectionFromSelect.ts"],"sourcesContent":["import {\n deepCopyObjectSimple,\n type Field,\n type FieldAffectingData,\n type SelectMode,\n type SelectType,\n type TabAsField,\n} from 'payload'\nimport { fieldAffectsData, getSelectMode, tabHasName } from 'payload/shared'\n\nimport type { MongooseAdapter } from '../index.js'\n\nconst addFieldToProjection = ({\n adapter,\n databaseSchemaPath,\n field,\n projection,\n withinLocalizedField,\n}: {\n adapter: MongooseAdapter\n databaseSchemaPath: string\n field: FieldAffectingData\n projection: Record<string, true>\n withinLocalizedField: boolean\n}) => {\n const { config } = adapter.payload\n\n if (withinLocalizedField && config.localization) {\n for (const locale of config.localization.localeCodes) {\n const localeDatabaseSchemaPath = databaseSchemaPath.replace('<locale>', locale)\n projection[`${localeDatabaseSchemaPath}${field.name}`] = true\n }\n } else {\n projection[`${databaseSchemaPath}${field.name}`] = true\n }\n}\n\nconst traverseFields = ({\n adapter,\n databaseSchemaPath = '',\n fields,\n projection,\n select,\n selectAllOnCurrentLevel = false,\n selectMode,\n withinLocalizedField = false,\n}: {\n adapter: MongooseAdapter\n databaseSchemaPath?: string\n fields: (Field | TabAsField)[]\n projection: Record<string, true>\n select: SelectType\n selectAllOnCurrentLevel?: boolean\n selectMode: SelectMode\n withinLocalizedField?: boolean\n}) => {\n for (const field of fields) {\n if (fieldAffectsData(field)) {\n if (selectMode === 'include') {\n if (select[field.name] === true || selectAllOnCurrentLevel) {\n addFieldToProjection({\n adapter,\n databaseSchemaPath,\n field,\n projection,\n withinLocalizedField,\n })\n continue\n }\n\n if (!select[field.name]) {\n continue\n }\n }\n\n if (selectMode === 'exclude') {\n if (typeof select[field.name] === 'undefined') {\n addFieldToProjection({\n adapter,\n databaseSchemaPath,\n field,\n projection,\n withinLocalizedField,\n })\n continue\n }\n\n if (select[field.name] === false) {\n continue\n }\n }\n }\n\n let fieldDatabaseSchemaPath = databaseSchemaPath\n let fieldWithinLocalizedField = withinLocalizedField\n\n if (fieldAffectsData(field)) {\n fieldDatabaseSchemaPath = `${databaseSchemaPath}${field.name}.`\n\n if (field.localized) {\n fieldDatabaseSchemaPath = `${fieldDatabaseSchemaPath}<locale>.`\n fieldWithinLocalizedField = true\n }\n }\n\n switch (field.type) {\n case 'array':\n case 'group':\n\n case 'tab': {\n let fieldSelect: SelectType\n\n if (field.type === 'tab' && !tabHasName(field)) {\n fieldSelect = select\n } else {\n fieldSelect = select[field.name] as SelectType\n }\n\n if (field.type === 'array' && selectMode === 'include') {\n fieldSelect['id'] = true\n }\n\n traverseFields({\n adapter,\n databaseSchemaPath: fieldDatabaseSchemaPath,\n fields: field.fields,\n projection,\n select: fieldSelect,\n selectMode,\n withinLocalizedField: fieldWithinLocalizedField,\n })\n\n break\n }\n\n case 'blocks': {\n const blocksSelect = select[field.name] as SelectType\n\n for (const block of field.blocks) {\n if (\n (selectMode === 'include' && blocksSelect[block.slug] === true) ||\n (selectMode === 'exclude' && typeof blocksSelect[block.slug] === 'undefined')\n ) {\n traverseFields({\n adapter,\n databaseSchemaPath: fieldDatabaseSchemaPath,\n fields: block.fields,\n projection,\n select: {},\n selectAllOnCurrentLevel: true,\n selectMode: 'include',\n withinLocalizedField: fieldWithinLocalizedField,\n })\n continue\n }\n\n let blockSelectMode = selectMode\n\n if (selectMode === 'exclude' && blocksSelect[block.slug] === false) {\n blockSelectMode = 'include'\n }\n\n if (typeof blocksSelect[block.slug] !== 'object') {\n blocksSelect[block.slug] = {}\n }\n\n if (blockSelectMode === 'include') {\n blocksSelect[block.slug]['id'] = true\n blocksSelect[block.slug]['blockType'] = true\n }\n\n traverseFields({\n adapter,\n databaseSchemaPath: fieldDatabaseSchemaPath,\n fields: block.fields,\n projection,\n select: blocksSelect[block.slug] as SelectType,\n selectMode: blockSelectMode,\n withinLocalizedField: fieldWithinLocalizedField,\n })\n }\n\n break\n }\n case 'collapsible':\n case 'row':\n traverseFields({\n adapter,\n databaseSchemaPath,\n fields: field.fields,\n projection,\n select,\n selectMode,\n withinLocalizedField,\n })\n break\n\n case 'tabs':\n traverseFields({\n adapter,\n databaseSchemaPath,\n fields: field.tabs.map((tab) => ({ ...tab, type: 'tab' })),\n projection,\n select,\n selectMode,\n withinLocalizedField,\n })\n break\n\n default:\n break\n }\n }\n}\n\nexport const buildProjectionFromSelect = ({\n adapter,\n fields,\n select,\n}: {\n adapter: MongooseAdapter\n fields: Field[]\n select?: SelectType\n}): Record<string, true> | undefined => {\n if (!select) {\n return\n }\n\n const projection: Record<string, true> = {\n _id: true,\n }\n\n traverseFields({\n adapter,\n fields,\n projection,\n // Clone to safely mutate it later\n select: deepCopyObjectSimple(select),\n selectMode: getSelectMode(select),\n })\n\n return projection\n}\n"],"names":["deepCopyObjectSimple","fieldAffectsData","getSelectMode","tabHasName","addFieldToProjection","adapter","databaseSchemaPath","field","projection","withinLocalizedField","config","payload","localization","locale","localeCodes","localeDatabaseSchemaPath","replace","name","traverseFields","fields","select","selectAllOnCurrentLevel","selectMode","fieldDatabaseSchemaPath","fieldWithinLocalizedField","localized","type","fieldSelect","blocksSelect","block","blocks","slug","blockSelectMode","tabs","map","tab","buildProjectionFromSelect","_id"],"mappings":"AAAA,SACEA,oBAAoB,QAMf,UAAS;AAChB,SAASC,gBAAgB,EAAEC,aAAa,EAAEC,UAAU,QAAQ,iBAAgB;AAI5E,MAAMC,uBAAuB,CAAC,EAC5BC,OAAO,EACPC,kBAAkB,EAClBC,KAAK,EACLC,UAAU,EACVC,oBAAoB,EAOrB;IACC,MAAM,EAAEC,MAAM,EAAE,GAAGL,QAAQM,OAAO;IAElC,IAAIF,wBAAwBC,OAAOE,YAAY,EAAE;QAC/C,KAAK,MAAMC,UAAUH,OAAOE,YAAY,CAACE,WAAW,CAAE;YACpD,MAAMC,2BAA2BT,mBAAmBU,OAAO,CAAC,YAAYH;YACxEL,UAAU,CAAC,CAAC,EAAEO,yBAAyB,EAAER,MAAMU,IAAI,CAAC,CAAC,CAAC,GAAG;QAC3D;IACF,OAAO;QACLT,UAAU,CAAC,CAAC,EAAEF,mBAAmB,EAAEC,MAAMU,IAAI,CAAC,CAAC,CAAC,GAAG;IACrD;AACF;AAEA,MAAMC,iBAAiB,CAAC,EACtBb,OAAO,EACPC,qBAAqB,EAAE,EACvBa,MAAM,EACNX,UAAU,EACVY,MAAM,EACNC,0BAA0B,KAAK,EAC/BC,UAAU,EACVb,uBAAuB,KAAK,EAU7B;IACC,KAAK,MAAMF,SAASY,OAAQ;QAC1B,IAAIlB,iBAAiBM,QAAQ;YAC3B,IAAIe,eAAe,WAAW;gBAC5B,IAAIF,MAAM,CAACb,MAAMU,IAAI,CAAC,KAAK,QAAQI,yBAAyB;oBAC1DjB,qBAAqB;wBACnBC;wBACAC;wBACAC;wBACAC;wBACAC;oBACF;oBACA;gBACF;gBAEA,IAAI,CAACW,MAAM,CAACb,MAAMU,IAAI,CAAC,EAAE;oBACvB;gBACF;YACF;YAEA,IAAIK,eAAe,WAAW;gBAC5B,IAAI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bb;QACAc;QACAX;QACA,kCAAkC;QAClCY,QAAQpB,qBAAqBoB;QAC7BE,YAAYpB,cAAckB;IAC5B;IAEA,OAAOZ;AACT,EAAC"}
@@ -1,4 +1,4 @@
1
- import type { DBIdentifierName } from 'payload/database';
1
+ import type { DBIdentifierName } from 'payload';
2
2
  type Args = {
3
3
  config: {
4
4
  dbName?: DBIdentifierName;
@@ -1 +1 @@
1
- {"version":3,"file":"getDBName.d.ts","sourceRoot":"","sources":["../../src/utilities/getDBName.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,kBAAkB,CAAA;AAExD,KAAK,IAAI,GAAG;IACV,MAAM,EAAE;QACN,MAAM,CAAC,EAAE,gBAAgB,CAAA;QACzB,QAAQ,CAAC,EAAE,gBAAgB,CAAA;QAC3B,IAAI,CAAC,EAAE,MAAM,CAAA;QACb,IAAI,CAAC,EAAE,MAAM,CAAA;KACd,CAAA;IACD,OAAO,CAAC,EAAE,OAAO,CAAA;IACjB,MAAM,CAAC,EAAE,QAAQ,GAAG,UAAU,CAAA;IAC9B,QAAQ,CAAC,EAAE,OAAO,CAAA;CACnB,CAAA;AAED;;;GAGG;AACH,eAAO,MAAM,SAAS,0DAKnB,IAAI,KAAG,MAiBT,CAAA"}
1
+ {"version":3,"file":"getDBName.d.ts","sourceRoot":"","sources":["../../src/utilities/getDBName.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,SAAS,CAAA;AAE/C,KAAK,IAAI,GAAG;IACV,MAAM,EAAE;QACN,MAAM,CAAC,EAAE,gBAAgB,CAAA;QACzB,QAAQ,CAAC,EAAE,gBAAgB,CAAA;QAC3B,IAAI,CAAC,EAAE,MAAM,CAAA;QACb,IAAI,CAAC,EAAE,MAAM,CAAA;KACd,CAAA;IACD,OAAO,CAAC,EAAE,OAAO,CAAA;IACjB,MAAM,CAAC,EAAE,QAAQ,GAAG,UAAU,CAAA;IAC9B,QAAQ,CAAC,EAAE,OAAO,CAAA;CACnB,CAAA;AAED;;;GAGG;AACH,eAAO,MAAM,SAAS,0DAKnB,IAAI,KAAG,MAmBT,CAAA"}
@@ -12,7 +12,9 @@
12
12
  } else {
13
13
  result = name ?? slug;
14
14
  }
15
- if (versions) result = `_${result}_versions`;
15
+ if (versions) {
16
+ result = `_${result}_versions`;
17
+ }
16
18
  return result;
17
19
  };
18
20
 
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/utilities/getDBName.ts"],"sourcesContent":["import type { DBIdentifierName } from 'payload/database'\n\ntype Args = {\n config: {\n dbName?: DBIdentifierName\n enumName?: DBIdentifierName\n name?: string\n slug?: string\n }\n locales?: boolean\n target?: 'dbName' | 'enumName'\n versions?: boolean\n}\n\n/**\n * Used to name database enums and collections\n * Returns the collection or enum name for a given entity\n */\nexport const getDBName = ({\n config: { name, slug },\n config,\n target = 'dbName',\n versions = false,\n}: Args): string => {\n let result: string\n let custom = config[target]\n\n if (!custom && target === 'enumName') {\n custom = config['dbName']\n }\n\n if (custom) {\n result = typeof custom === 'function' ? custom({}) : custom\n } else {\n result = name ?? slug\n }\n\n if (versions) result = `_${result}_versions`\n\n return result\n}\n"],"names":["getDBName","config","name","slug","target","versions","result","custom"],"rangeMappings":";;;;;;;;;;;;;;;;","mappings":"AAcA;;;CAGC,GACD,OAAO,MAAMA,YAAY,CAAC,EACxBC,QAAQ,EAAEC,IAAI,EAAEC,IAAI,EAAE,EACtBF,MAAM,EACNG,SAAS,QAAQ,EACjBC,WAAW,KAAK,EACX;IACL,IAAIC;IACJ,IAAIC,SAASN,MAAM,CAACG,OAAO;IAE3B,IAAI,CAACG,UAAUH,WAAW,YAAY;QACpCG,SAASN,MAAM,CAAC,SAAS;IAC3B;IAEA,IAAIM,QAAQ;QACVD,SAAS,OAAOC,WAAW,aAAaA,OAAO,CAAC,KAAKA;IACvD,OAAO;QACLD,SAASJ,QAAQC;IACnB;IAEA,IAAIE,UAAUC,SAAS,CAAC,CAAC,EAAEA,OAAO,SAAS,CAAC;IAE5C,OAAOA;AACT,EAAC"}
1
+ {"version":3,"sources":["../../src/utilities/getDBName.ts"],"sourcesContent":["import type { DBIdentifierName } from 'payload'\n\ntype Args = {\n config: {\n dbName?: DBIdentifierName\n enumName?: DBIdentifierName\n name?: string\n slug?: string\n }\n locales?: boolean\n target?: 'dbName' | 'enumName'\n versions?: boolean\n}\n\n/**\n * Used to name database enums and collections\n * Returns the collection or enum name for a given entity\n */\nexport const getDBName = ({\n config: { name, slug },\n config,\n target = 'dbName',\n versions = false,\n}: Args): string => {\n let result: string\n let custom = config[target]\n\n if (!custom && target === 'enumName') {\n custom = config['dbName']\n }\n\n if (custom) {\n result = typeof custom === 'function' ? custom({}) : custom\n } else {\n result = name ?? slug\n }\n\n if (versions) {\n result = `_${result}_versions`\n }\n\n return result\n}\n"],"names":["getDBName","config","name","slug","target","versions","result","custom"],"mappings":"AAcA;;;CAGC,GACD,OAAO,MAAMA,YAAY,CAAC,EACxBC,QAAQ,EAAEC,IAAI,EAAEC,IAAI,EAAE,EACtBF,MAAM,EACNG,SAAS,QAAQ,EACjBC,WAAW,KAAK,EACX;IACL,IAAIC;IACJ,IAAIC,SAASN,MAAM,CAACG,OAAO;IAE3B,IAAI,CAACG,UAAUH,WAAW,YAAY;QACpCG,SAASN,MAAM,CAAC,SAAS;IAC3B;IAEA,IAAIM,QAAQ;QACVD,SAAS,OAAOC,WAAW,aAAaA,OAAO,CAAC,KAAKA;IACvD,OAAO;QACLD,SAASJ,QAAQC;IACnB;IAEA,IAAIE,UAAU;QACZC,SAAS,CAAC,CAAC,EAAEA,OAAO,SAAS,CAAC;IAChC;IAEA,OAAOA;AACT,EAAC"}
@@ -1,3 +1,7 @@
1
- declare const handleError: (error: any, req: any) => never;
2
- export default handleError;
1
+ export declare const handleError: ({ collection, error, global, req, }: {
2
+ collection?: string;
3
+ error: any;
4
+ global?: string;
5
+ req: any;
6
+ }) => never;
3
7
  //# sourceMappingURL=handleError.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"handleError.d.ts","sourceRoot":"","sources":["../../src/utilities/handleError.ts"],"names":[],"mappings":"AAGA,QAAA,MAAM,WAAW,iCAiBhB,CAAA;AAED,eAAe,WAAW,CAAA"}
1
+ {"version":3,"file":"handleError.d.ts","sourceRoot":"","sources":["../../src/utilities/handleError.ts"],"names":[],"mappings":"AAGA,eAAO,MAAM,WAAW,wCAKrB;IACD,UAAU,CAAC,EAAE,MAAM,CAAA;IACnB,KAAK,MAAA;IACL,MAAM,CAAC,EAAE,MAAM,CAAA;IACf,GAAG,MAAA;CACJ,UAqBA,CAAA"}
@@ -1,20 +1,23 @@
1
1
  import httpStatus from 'http-status';
2
- import { APIError, ValidationError } from 'payload/errors';
3
- const handleError = (error, req)=>{
2
+ import { APIError, ValidationError } from 'payload';
3
+ export const handleError = ({ collection, error, global, req })=>{
4
4
  // Handle uniqueness error from MongoDB
5
5
  if (error.code === 11000 && error.keyValue) {
6
- throw new ValidationError([
7
- {
8
- field: Object.keys(error.keyValue)[0],
9
- message: req.t('error:valueMustBeUnique')
10
- }
11
- ], req.t);
6
+ throw new ValidationError({
7
+ collection,
8
+ errors: [
9
+ {
10
+ message: req.t('error:valueMustBeUnique'),
11
+ path: Object.keys(error.keyValue)[0]
12
+ }
13
+ ],
14
+ global
15
+ }, req.t);
12
16
  } else if (error.code === 11000) {
13
17
  throw new APIError(req.t('error:valueMustBeUnique'), httpStatus.BAD_REQUEST);
14
18
  } else {
15
19
  throw error;
16
20
  }
17
21
  };
18
- export default handleError;
19
22
 
20
23
  //# sourceMappingURL=handleError.js.map
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/utilities/handleError.ts"],"sourcesContent":["import httpStatus from 'http-status'\nimport { APIError, ValidationError } from 'payload/errors'\n\nconst handleError = (error, req) => {\n // Handle uniqueness error from MongoDB\n if (error.code === 11000 && error.keyValue) {\n throw new ValidationError(\n [\n {\n field: Object.keys(error.keyValue)[0],\n message: req.t('error:valueMustBeUnique'),\n },\n ],\n req.t,\n )\n } else if (error.code === 11000) {\n throw new APIError(req.t('error:valueMustBeUnique'), httpStatus.BAD_REQUEST)\n } else {\n throw error\n }\n}\n\nexport default handleError\n"],"names":["httpStatus","APIError","ValidationError","handleError","error","req","code","keyValue","field","Object","keys","message","t","BAD_REQUEST"],"rangeMappings":";;;;;;;;;;;;;;;;;","mappings":"AAAA,OAAOA,gBAAgB,cAAa;AACpC,SAASC,QAAQ,EAAEC,eAAe,QAAQ,iBAAgB;AAE1D,MAAMC,cAAc,CAACC,OAAOC;IAC1B,uCAAuC;IACvC,IAAID,MAAME,IAAI,KAAK,SAASF,MAAMG,QAAQ,EAAE;QAC1C,MAAM,IAAIL,gBACR;YACE;gBACEM,OAAOC,OAAOC,IAAI,CAACN,MAAMG,QAAQ,CAAC,CAAC,EAAE;gBACrCI,SAASN,IAAIO,CAAC,CAAC;YACjB;SACD,EACDP,IAAIO,CAAC;IAET,OAAO,IAAIR,MAAME,IAAI,KAAK,OAAO;QAC/B,MAAM,IAAIL,SAASI,IAAIO,CAAC,CAAC,4BAA4BZ,WAAWa,WAAW;IAC7E,OAAO;QACL,MAAMT;IACR;AACF;AAEA,eAAeD,YAAW"}
1
+ {"version":3,"sources":["../../src/utilities/handleError.ts"],"sourcesContent":["import httpStatus from 'http-status'\nimport { APIError, ValidationError } from 'payload'\n\nexport const handleError = ({\n collection,\n error,\n global,\n req,\n}: {\n collection?: string\n error\n global?: string\n req\n}) => {\n // Handle uniqueness error from MongoDB\n if (error.code === 11000 && error.keyValue) {\n throw new ValidationError(\n {\n collection,\n errors: [\n {\n message: req.t('error:valueMustBeUnique'),\n path: Object.keys(error.keyValue)[0],\n },\n ],\n global,\n },\n req.t,\n )\n } else if (error.code === 11000) {\n throw new APIError(req.t('error:valueMustBeUnique'), httpStatus.BAD_REQUEST)\n } else {\n throw error\n }\n}\n"],"names":["httpStatus","APIError","ValidationError","handleError","collection","error","global","req","code","keyValue","errors","message","t","path","Object","keys","BAD_REQUEST"],"mappings":"AAAA,OAAOA,gBAAgB,cAAa;AACpC,SAASC,QAAQ,EAAEC,eAAe,QAAQ,UAAS;AAEnD,OAAO,MAAMC,cAAc,CAAC,EAC1BC,UAAU,EACVC,KAAK,EACLC,MAAM,EACNC,GAAG,EAMJ;IACC,uCAAuC;IACvC,IAAIF,MAAMG,IAAI,KAAK,SAASH,MAAMI,QAAQ,EAAE;QAC1C,MAAM,IAAIP,gBACR;YACEE;YACAM,QAAQ;gBACN;oBACEC,SAASJ,IAAIK,CAAC,CAAC;oBACfC,MAAMC,OAAOC,IAAI,CAACV,MAAMI,QAAQ,CAAC,CAAC,EAAE;gBACtC;aACD;YACDH;QACF,GACAC,IAAIK,CAAC;IAET,OAAO,IAAIP,MAAMG,IAAI,KAAK,OAAO;QAC/B,MAAM,IAAIP,SAASM,IAAIK,CAAC,CAAC,4BAA4BZ,WAAWgB,WAAW;IAC7E,OAAO;QACL,MAAMX;IACR;AACF,EAAC"}
@@ -1,3 +1,2 @@
1
- declare const sanitizeInternalFields: <T extends Record<string, unknown>>(incomingDoc: T) => T;
2
- export default sanitizeInternalFields;
1
+ export declare const sanitizeInternalFields: <T extends Record<string, unknown>>(incomingDoc: T) => T;
3
2
  //# sourceMappingURL=sanitizeInternalFields.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"sanitizeInternalFields.d.ts","sourceRoot":"","sources":["../../src/utilities/sanitizeInternalFields.ts"],"names":[],"mappings":"AAEA,QAAA,MAAM,sBAAsB,mDAAoD,CAAC,KAAG,CAiBvE,CAAA;AAEb,eAAe,sBAAsB,CAAA"}
1
+ {"version":3,"file":"sanitizeInternalFields.d.ts","sourceRoot":"","sources":["../../src/utilities/sanitizeInternalFields.ts"],"names":[],"mappings":"AAEA,eAAO,MAAM,sBAAsB,GAAI,CAAC,SAAS,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,eAAe,CAAC,KAAG,CAiB9E,CAAA"}
@@ -1,7 +1,7 @@
1
1
  const internalFields = [
2
2
  '__v'
3
3
  ];
4
- const sanitizeInternalFields = (incomingDoc)=>Object.entries(incomingDoc).reduce((newDoc, [key, val])=>{
4
+ export const sanitizeInternalFields = (incomingDoc)=>Object.entries(incomingDoc).reduce((newDoc, [key, val])=>{
5
5
  if (key === '_id') {
6
6
  return {
7
7
  ...newDoc,
@@ -16,6 +16,5 @@ const sanitizeInternalFields = (incomingDoc)=>Object.entries(incomingDoc).reduce
16
16
  [key]: val
17
17
  };
18
18
  }, {});
19
- export default sanitizeInternalFields;
20
19
 
21
20
  //# sourceMappingURL=sanitizeInternalFields.js.map
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/utilities/sanitizeInternalFields.ts"],"sourcesContent":["const internalFields = ['__v']\n\nconst sanitizeInternalFields = <T extends Record<string, unknown>>(incomingDoc: T): T =>\n Object.entries(incomingDoc).reduce((newDoc, [key, val]): T => {\n if (key === '_id') {\n return {\n ...newDoc,\n id: val,\n }\n }\n\n if (internalFields.indexOf(key) > -1) {\n return newDoc\n }\n\n return {\n ...newDoc,\n [key]: val,\n }\n }, {} as T)\n\nexport default sanitizeInternalFields\n"],"names":["internalFields","sanitizeInternalFields","incomingDoc","Object","entries","reduce","newDoc","key","val","id","indexOf"],"rangeMappings":";;;;;;;;;;;;;;;;;;","mappings":"AAAA,MAAMA,iBAAiB;IAAC;CAAM;AAE9B,MAAMC,yBAAyB,CAAoCC,cACjEC,OAAOC,OAAO,CAACF,aAAaG,MAAM,CAAC,CAACC,QAAQ,CAACC,KAAKC,IAAI;QACpD,IAAID,QAAQ,OAAO;YACjB,OAAO;gBACL,GAAGD,MAAM;gBACTG,IAAID;YACN;QACF;QAEA,IAAIR,eAAeU,OAAO,CAACH,OAAO,CAAC,GAAG;YACpC,OAAOD;QACT;QAEA,OAAO;YACL,GAAGA,MAAM;YACT,CAACC,IAAI,EAAEC;QACT;IACF,GAAG,CAAC;AAEN,eAAeP,uBAAsB"}
1
+ {"version":3,"sources":["../../src/utilities/sanitizeInternalFields.ts"],"sourcesContent":["const internalFields = ['__v']\n\nexport const sanitizeInternalFields = <T extends Record<string, unknown>>(incomingDoc: T): T =>\n Object.entries(incomingDoc).reduce((newDoc, [key, val]): T => {\n if (key === '_id') {\n return {\n ...newDoc,\n id: val,\n }\n }\n\n if (internalFields.indexOf(key) > -1) {\n return newDoc\n }\n\n return {\n ...newDoc,\n [key]: val,\n }\n }, {} as T)\n"],"names":["internalFields","sanitizeInternalFields","incomingDoc","Object","entries","reduce","newDoc","key","val","id","indexOf"],"mappings":"AAAA,MAAMA,iBAAiB;IAAC;CAAM;AAE9B,OAAO,MAAMC,yBAAyB,CAAoCC,cACxEC,OAAOC,OAAO,CAACF,aAAaG,MAAM,CAAC,CAACC,QAAQ,CAACC,KAAKC,IAAI;QACpD,IAAID,QAAQ,OAAO;YACjB,OAAO;gBACL,GAAGD,MAAM;gBACTG,IAAID;YACN;QACF;QAEA,IAAIR,eAAeU,OAAO,CAACH,OAAO,CAAC,GAAG;YACpC,OAAOD;QACT;QAEA,OAAO;YACL,GAAGA,MAAM;YACT,CAACC,IAAI,EAAEC;QACT;IACF,GAAG,CAAC,GAAO"}
@@ -0,0 +1,9 @@
1
+ import type { Field, SanitizedConfig } from 'payload';
2
+ type Args = {
3
+ config: SanitizedConfig;
4
+ data: Record<string, unknown>;
5
+ fields: Field[];
6
+ };
7
+ export declare const sanitizeRelationshipIDs: ({ config, data, fields, }: Args) => Record<string, unknown>;
8
+ export {};
9
+ //# sourceMappingURL=sanitizeRelationshipIDs.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"sanitizeRelationshipIDs.d.ts","sourceRoot":"","sources":["../../src/utilities/sanitizeRelationshipIDs.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAoB,KAAK,EAAE,eAAe,EAA0B,MAAM,SAAS,CAAA;AAM/F,KAAK,IAAI,GAAG;IACV,MAAM,EAAE,eAAe,CAAA;IACvB,IAAI,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAA;IAC7B,MAAM,EAAE,KAAK,EAAE,CAAA;CAChB,CAAA;AAuGD,eAAO,MAAM,uBAAuB,8BAIjC,IAAI,KAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CA6B/B,CAAA"}
@@ -0,0 +1,115 @@
1
+ import { Types } from 'mongoose';
2
+ import { APIError, traverseFields } from 'payload';
3
+ import { fieldAffectsData } from 'payload/shared';
4
+ function isValidRelationObject(value) {
5
+ return typeof value === 'object' && value !== null && 'relationTo' in value && 'value' in value;
6
+ }
7
+ const convertValue = ({ relatedCollection, value })=>{
8
+ const customIDField = relatedCollection.fields.find((field)=>fieldAffectsData(field) && field.name === 'id');
9
+ if (!customIDField) {
10
+ try {
11
+ return new Types.ObjectId(value);
12
+ } catch (error) {
13
+ throw new APIError(`Failed to create ObjectId from value: ${value}. Error: ${error.message}`, 400);
14
+ }
15
+ }
16
+ return value;
17
+ };
18
+ const sanitizeRelationship = ({ config, field, locale, ref, value })=>{
19
+ let relatedCollection;
20
+ let result = value;
21
+ const hasManyRelations = typeof field.relationTo !== 'string';
22
+ if (!hasManyRelations) {
23
+ relatedCollection = config.collections?.find(({ slug })=>slug === field.relationTo);
24
+ }
25
+ if (Array.isArray(value)) {
26
+ result = value.map((val)=>{
27
+ // Handle has many
28
+ if (relatedCollection && val && (typeof val === 'string' || typeof val === 'number')) {
29
+ return convertValue({
30
+ relatedCollection,
31
+ value: val
32
+ });
33
+ }
34
+ // Handle has many - polymorphic
35
+ if (isValidRelationObject(val)) {
36
+ const relatedCollectionForSingleValue = config.collections?.find(({ slug })=>slug === val.relationTo);
37
+ if (relatedCollectionForSingleValue) {
38
+ return {
39
+ relationTo: val.relationTo,
40
+ value: convertValue({
41
+ relatedCollection: relatedCollectionForSingleValue,
42
+ value: val.value
43
+ })
44
+ };
45
+ }
46
+ }
47
+ return val;
48
+ });
49
+ }
50
+ // Handle has one - polymorphic
51
+ if (isValidRelationObject(value)) {
52
+ relatedCollection = config.collections?.find(({ slug })=>slug === value.relationTo);
53
+ if (relatedCollection) {
54
+ result = {
55
+ relationTo: value.relationTo,
56
+ value: convertValue({
57
+ relatedCollection,
58
+ value: value.value
59
+ })
60
+ };
61
+ }
62
+ }
63
+ // Handle has one
64
+ if (relatedCollection && value && (typeof value === 'string' || typeof value === 'number')) {
65
+ result = convertValue({
66
+ relatedCollection,
67
+ value
68
+ });
69
+ }
70
+ if (locale) {
71
+ ref[locale] = result;
72
+ } else {
73
+ ref[field.name] = result;
74
+ }
75
+ };
76
+ export const sanitizeRelationshipIDs = ({ config, data, fields })=>{
77
+ const sanitize = ({ field, ref })=>{
78
+ if (field.type === 'relationship' || field.type === 'upload') {
79
+ // handle localized relationships
80
+ if (config.localization && field.localized) {
81
+ const locales = config.localization.locales;
82
+ const fieldRef = ref[field.name];
83
+ for (const { code } of locales){
84
+ if (ref[field.name]?.[code]) {
85
+ const value = ref[field.name][code];
86
+ sanitizeRelationship({
87
+ config,
88
+ field,
89
+ locale: code,
90
+ ref: fieldRef,
91
+ value
92
+ });
93
+ }
94
+ }
95
+ } else {
96
+ // handle non-localized relationships
97
+ sanitizeRelationship({
98
+ config,
99
+ field,
100
+ locale: undefined,
101
+ ref,
102
+ value: ref[field.name]
103
+ });
104
+ }
105
+ }
106
+ };
107
+ traverseFields({
108
+ callback: sanitize,
109
+ fields,
110
+ ref: data
111
+ });
112
+ return data;
113
+ };
114
+
115
+ //# sourceMappingURL=sanitizeRelationshipIDs.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../src/utilities/sanitizeRelationshipIDs.ts"],"sourcesContent":["import type { CollectionConfig, Field, SanitizedConfig, TraverseFieldsCallback } from 'payload'\n\nimport { Types } from 'mongoose'\nimport { APIError, traverseFields } from 'payload'\nimport { fieldAffectsData } from 'payload/shared'\n\ntype Args = {\n config: SanitizedConfig\n data: Record<string, unknown>\n fields: Field[]\n}\n\ninterface RelationObject {\n relationTo: string\n value: number | string\n}\n\nfunction isValidRelationObject(value: unknown): value is RelationObject {\n return typeof value === 'object' && value !== null && 'relationTo' in value && 'value' in value\n}\n\nconst convertValue = ({\n relatedCollection,\n value,\n}: {\n relatedCollection: CollectionConfig\n value: number | string\n}): number | string | Types.ObjectId => {\n const customIDField = relatedCollection.fields.find(\n (field) => fieldAffectsData(field) && field.name === 'id',\n )\n\n if (!customIDField) {\n try {\n return new Types.ObjectId(value)\n } catch (error) {\n throw new APIError(\n `Failed to create ObjectId from value: ${value}. Error: ${error.message}`,\n 400,\n )\n }\n }\n\n return value\n}\n\nconst sanitizeRelationship = ({ config, field, locale, ref, value }) => {\n let relatedCollection: CollectionConfig | undefined\n let result = value\n\n const hasManyRelations = typeof field.relationTo !== 'string'\n\n if (!hasManyRelations) {\n relatedCollection = config.collections?.find(({ slug }) => slug === field.relationTo)\n }\n\n if (Array.isArray(value)) {\n result = value.map((val) => {\n // Handle has many\n if (relatedCollection && val && (typeof val === 'string' || typeof val === 'number')) {\n return convertValue({\n relatedCollection,\n value: val,\n })\n }\n\n // Handle has many - polymorphic\n if (isValidRelationObject(val)) {\n const relatedCollectionForSingleValue = config.collections?.find(\n ({ slug }) => slug === val.relationTo,\n )\n\n if (relatedCollectionForSingleValue) {\n return {\n relationTo: val.relationTo,\n value: convertValue({\n relatedCollection: relatedCollectionForSingleValue,\n value: val.value,\n }),\n }\n }\n }\n\n return val\n })\n }\n\n // Handle has one - polymorphic\n if (isValidRelationObject(value)) {\n relatedCollection = config.collections?.find(({ slug }) => slug === value.relationTo)\n\n if (relatedCollection) {\n result = {\n relationTo: value.relationTo,\n value: convertValue({ relatedCollection, value: value.value }),\n }\n }\n }\n\n // Handle has one\n if (relatedCollection && value && (typeof value === 'string' || typeof value === 'number')) {\n result = convertValue({\n relatedCollection,\n value,\n })\n }\n if (locale) {\n ref[locale] = result\n } else {\n ref[field.name] = result\n }\n}\n\nexport const sanitizeRelationshipIDs = ({\n config,\n data,\n fields,\n}: Args): Record<string, unknown> => {\n const sanitize: TraverseFieldsCallback = ({ field, ref }) => {\n if (field.type === 'relationship' || field.type === 'upload') {\n // handle localized relationships\n if (config.localization && field.localized) {\n const locales = config.localization.locales\n const fieldRef = ref[field.name]\n for (const { code } of locales) {\n if (ref[field.name]?.[code]) {\n const value = ref[field.name][code]\n sanitizeRelationship({ config, field, locale: code, ref: fieldRef, value })\n }\n }\n } else {\n // handle non-localized relationships\n sanitizeRelationship({\n config,\n field,\n locale: undefined,\n ref,\n value: ref[field.name],\n })\n }\n }\n }\n\n traverseFields({ callback: sanitize, fields, ref: data })\n\n return data\n}\n"],"names":["Types","APIError","traverseFields","fieldAffectsData","isValidRelationObject","value","convertValue","relatedCollection","customIDField","fields","find","field","name","ObjectId","error","message","sanitizeRelationship","config","locale","ref","result","hasManyRelations","relationTo","collections","slug","Array","isArray","map","val","relatedCollectionForSingleValue","sanitizeRelationshipIDs","data","sanitize","type","localization","localized","locales","fieldRef","code","undefined","callback"],"mappings":"AAEA,SAASA,KAAK,QAAQ,WAAU;AAChC,SAASC,QAAQ,EAAEC,cAAc,QAAQ,UAAS;AAClD,SAASC,gBAAgB,QAAQ,iBAAgB;AAajD,SAASC,sBAAsBC,KAAc;IAC3C,OAAO,OAAOA,UAAU,YAAYA,UAAU,QAAQ,gBAAgBA,SAAS,WAAWA;AAC5F;AAEA,MAAMC,eAAe,CAAC,EACpBC,iBAAiB,EACjBF,KAAK,EAIN;IACC,MAAMG,gBAAgBD,kBAAkBE,MAAM,CAACC,IAAI,CACjD,CAACC,QAAUR,iBAAiBQ,UAAUA,MAAMC,IAAI,KAAK;IAGvD,IAAI,CAACJ,eAAe;QAClB,IAAI;YACF,OAAO,IAAIR,MAAMa,QAAQ,CAACR;QAC5B,EAAE,OAAOS,OAAO;YACd,MAAM,IAAIb,SACR,CAAC,sCAAsC,EAAEI,MAAM,SAAS,EAAES,MAAMC,OAAO,CAAC,CAAC,EACzE;QAEJ;IACF;IAEA,OAAOV;AACT;AAEA,MAAMW,uBAAuB,CAAC,EAAEC,MAAM,EAAEN,KAAK,EAAEO,MAAM,E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