@strapi/core 0.0.0-experimental.da85533897155e719d784f0271223c866d2f69ab → 0.0.0-experimental.dd1d47ef78ef6cfec4ed62576108500bd9f13740

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.
Files changed (111) hide show
  1. package/dist/core-api/service/collection-type.d.ts +2 -2
  2. package/dist/ee/index.d.ts.map +1 -1
  3. package/dist/ee/index.js +6 -1
  4. package/dist/ee/index.js.map +1 -1
  5. package/dist/ee/index.mjs +6 -1
  6. package/dist/ee/index.mjs.map +1 -1
  7. package/dist/factories.js.map +1 -1
  8. package/dist/factories.mjs.map +1 -1
  9. package/dist/loaders/plugins/get-enabled-plugins.d.ts.map +1 -1
  10. package/dist/loaders/plugins/get-enabled-plugins.js +6 -3
  11. package/dist/loaders/plugins/get-enabled-plugins.js.map +1 -1
  12. package/dist/loaders/plugins/get-enabled-plugins.mjs +6 -3
  13. package/dist/loaders/plugins/get-enabled-plugins.mjs.map +1 -1
  14. package/dist/loaders/plugins/index.d.ts.map +1 -1
  15. package/dist/loaders/plugins/index.js +28 -1
  16. package/dist/loaders/plugins/index.js.map +1 -1
  17. package/dist/loaders/plugins/index.mjs +9 -1
  18. package/dist/loaders/plugins/index.mjs.map +1 -1
  19. package/dist/middlewares/security.d.ts.map +1 -1
  20. package/dist/middlewares/security.js +1 -1
  21. package/dist/middlewares/security.js.map +1 -1
  22. package/dist/middlewares/security.mjs +1 -1
  23. package/dist/middlewares/security.mjs.map +1 -1
  24. package/dist/migrations/database/5.0.0-discard-drafts.d.ts +12 -9
  25. package/dist/migrations/database/5.0.0-discard-drafts.d.ts.map +1 -1
  26. package/dist/migrations/database/5.0.0-discard-drafts.js +66 -10
  27. package/dist/migrations/database/5.0.0-discard-drafts.js.map +1 -1
  28. package/dist/migrations/database/5.0.0-discard-drafts.mjs +67 -11
  29. package/dist/migrations/database/5.0.0-discard-drafts.mjs.map +1 -1
  30. package/dist/migrations/draft-publish.d.ts +1 -1
  31. package/dist/migrations/draft-publish.d.ts.map +1 -1
  32. package/dist/migrations/draft-publish.js.map +1 -1
  33. package/dist/migrations/draft-publish.mjs.map +1 -1
  34. package/dist/migrations/i18n.d.ts +5 -0
  35. package/dist/migrations/i18n.d.ts.map +1 -0
  36. package/dist/migrations/i18n.js +54 -0
  37. package/dist/migrations/i18n.js.map +1 -0
  38. package/dist/migrations/i18n.mjs +54 -0
  39. package/dist/migrations/i18n.mjs.map +1 -0
  40. package/dist/migrations/index.d.ts +5 -0
  41. package/dist/migrations/index.d.ts.map +1 -0
  42. package/dist/migrations/index.js +15 -0
  43. package/dist/migrations/index.js.map +1 -0
  44. package/dist/migrations/index.mjs +15 -0
  45. package/dist/migrations/index.mjs.map +1 -0
  46. package/dist/providers/admin.d.ts.map +1 -1
  47. package/dist/providers/admin.js.map +1 -1
  48. package/dist/providers/admin.mjs.map +1 -1
  49. package/dist/providers/registries.js +3 -3
  50. package/dist/providers/registries.js.map +1 -1
  51. package/dist/providers/registries.mjs +3 -3
  52. package/dist/providers/registries.mjs.map +1 -1
  53. package/dist/services/content-api/index.d.ts +6 -2
  54. package/dist/services/content-api/index.d.ts.map +1 -1
  55. package/dist/services/content-api/permissions/index.d.ts +6 -2
  56. package/dist/services/content-api/permissions/index.d.ts.map +1 -1
  57. package/dist/services/content-api/permissions/providers/action.d.ts +3 -1
  58. package/dist/services/content-api/permissions/providers/action.d.ts.map +1 -1
  59. package/dist/services/content-api/permissions/providers/condition.d.ts +3 -1
  60. package/dist/services/content-api/permissions/providers/condition.d.ts.map +1 -1
  61. package/dist/services/cron.d.ts +3 -3
  62. package/dist/services/cron.d.ts.map +1 -1
  63. package/dist/services/cron.js +9 -4
  64. package/dist/services/cron.js.map +1 -1
  65. package/dist/services/cron.mjs +9 -4
  66. package/dist/services/cron.mjs.map +1 -1
  67. package/dist/services/document-service/common.d.ts +1 -1
  68. package/dist/services/document-service/common.d.ts.map +1 -1
  69. package/dist/services/document-service/common.js.map +1 -1
  70. package/dist/services/document-service/common.mjs.map +1 -1
  71. package/dist/services/document-service/entries.d.ts +2 -2
  72. package/dist/services/document-service/entries.d.ts.map +1 -1
  73. package/dist/services/document-service/entries.js +6 -7
  74. package/dist/services/document-service/entries.js.map +1 -1
  75. package/dist/services/document-service/entries.mjs +1 -2
  76. package/dist/services/document-service/entries.mjs.map +1 -1
  77. package/dist/services/document-service/index.d.ts +2 -1
  78. package/dist/services/document-service/index.d.ts.map +1 -1
  79. package/dist/services/document-service/index.js +3 -2
  80. package/dist/services/document-service/index.js.map +1 -1
  81. package/dist/services/document-service/index.mjs +3 -2
  82. package/dist/services/document-service/index.mjs.map +1 -1
  83. package/dist/services/document-service/repository.d.ts.map +1 -1
  84. package/dist/services/document-service/repository.js +59 -8
  85. package/dist/services/document-service/repository.js.map +1 -1
  86. package/dist/services/document-service/repository.mjs +60 -9
  87. package/dist/services/document-service/repository.mjs.map +1 -1
  88. package/dist/services/document-service/transform/id-map.d.ts.map +1 -1
  89. package/dist/services/document-service/transform/id-map.js +13 -4
  90. package/dist/services/document-service/transform/id-map.js.map +1 -1
  91. package/dist/services/document-service/transform/id-map.mjs +14 -5
  92. package/dist/services/document-service/transform/id-map.mjs.map +1 -1
  93. package/dist/services/document-service/utils/populate.d.ts.map +1 -1
  94. package/dist/services/document-service/utils/populate.js +3 -1
  95. package/dist/services/document-service/utils/populate.js.map +1 -1
  96. package/dist/services/document-service/utils/populate.mjs +3 -1
  97. package/dist/services/document-service/utils/populate.mjs.map +1 -1
  98. package/dist/services/document-service/utils/unidirectional-relations.d.ts +36 -0
  99. package/dist/services/document-service/utils/unidirectional-relations.d.ts.map +1 -0
  100. package/dist/services/document-service/utils/unidirectional-relations.js +69 -0
  101. package/dist/services/document-service/utils/unidirectional-relations.js.map +1 -0
  102. package/dist/services/document-service/utils/unidirectional-relations.mjs +69 -0
  103. package/dist/services/document-service/utils/unidirectional-relations.mjs.map +1 -0
  104. package/dist/services/entity-validator/index.d.ts.map +1 -1
  105. package/dist/services/entity-validator/index.js +6 -2
  106. package/dist/services/entity-validator/index.js.map +1 -1
  107. package/dist/services/entity-validator/index.mjs +6 -2
  108. package/dist/services/entity-validator/index.mjs.map +1 -1
  109. package/dist/utils/transform-content-types-to-models.d.ts +325 -21
  110. package/dist/utils/transform-content-types-to-models.d.ts.map +1 -1
  111. package/package.json +19 -18
@@ -1 +1 @@
1
- {"version":3,"file":"id-map.js","sources":["../../../../src/services/document-service/transform/id-map.ts"],"sourcesContent":["import { Core, Data } from '@strapi/types';\nimport { async } from '@strapi/utils';\n\n/**\n * TODO: Find a better way to encode keys than this\n * This converts an object into a string by joining its keys and values,\n * so it can be used as a key in a Map.\n *\n * @example\n * const obj = { a: 1, b: 2 };\n * const key = encodeKey(obj);\n * ^ \"a:::1&&b:::2\"\n */\nconst encodeKey = (obj: any) => {\n // Sort keys to always keep the same order when encoding\n const keys = Object.keys(obj).sort();\n return keys.map((key) => `${key}:::${obj[key]}`).join('&&');\n};\n\ninterface KeyFields {\n uid: string;\n documentId: Data.ID;\n locale?: string | null;\n status?: 'draft' | 'published';\n}\n\nexport interface IdMap {\n loadedIds: Map<string, string>;\n toLoadIds: Map<string, KeyFields>;\n // Make the Keys type to be the params of add\n add(keys: KeyFields): void;\n load(): Promise<void>;\n get(keys: KeyFields): string | undefined;\n clear(): void;\n}\n\n/**\n * Holds a registry of document ids and their corresponding entity ids.\n */\nconst createIdMap = ({ strapi }: { strapi: Core.Strapi }): IdMap => {\n const loadedIds = new Map();\n const toLoadIds = new Map();\n\n return {\n loadedIds,\n toLoadIds,\n /**\n * Register a new document id and its corresponding entity id.\n */\n add(keyFields: KeyFields) {\n const key = encodeKey({ status: 'published', locale: null, ...keyFields });\n\n // If the id is already loaded, do nothing\n if (loadedIds.has(key)) return;\n // If the id is already in the toLoadIds, do nothing\n if (toLoadIds.has(key)) return;\n\n // Add the id to the toLoadIds\n toLoadIds.set(key, keyFields);\n },\n\n /**\n * Load all ids from the registry.\n */\n async load() {\n // Document Id to Entry Id queries are batched by its uid and locale\n // TODO: Add publication state too\n const loadIdValues = Array.from(toLoadIds.values());\n\n // 1. Group ids to query together\n const idsByUidAndLocale = loadIdValues.reduce((acc, { documentId, ...rest }) => {\n const key = encodeKey(rest);\n const ids = acc[key] || { ...rest, documentIds: [] };\n ids.documentIds.push(documentId);\n return { ...acc, [key]: ids };\n }, {});\n\n // 2. Query ids\n await async.map(\n Object.values(idsByUidAndLocale),\n async ({ uid, locale, documentIds, status }: any) => {\n const findParams = {\n select: ['id', 'documentId', 'locale', 'publishedAt'],\n where: {\n documentId: { $in: documentIds },\n locale: locale || null,\n publishedAt: status === 'draft' ? null : { $ne: null },\n },\n } as any;\n\n const result = await strapi?.db?.query(uid).findMany(findParams);\n\n // 3. Store result in loadedIds\n result?.forEach(({ documentId, id, locale, publishedAt }: any) => {\n const key = encodeKey({\n documentId,\n uid,\n locale,\n status: publishedAt ? 'published' : 'draft',\n });\n loadedIds.set(key, id);\n });\n }\n );\n\n // 4. Clear toLoadIds\n toLoadIds.clear();\n },\n\n /**\n * Get the entity id for a given document id.\n */\n get(keys: KeyFields) {\n const key = encodeKey({ status: 'published', locale: null, ...keys });\n return loadedIds.get(key);\n },\n\n /**\n * Clear the registry.\n */\n clear() {\n loadedIds.clear();\n toLoadIds.clear();\n },\n };\n};\n\nexport { createIdMap };\n"],"names":["async","locale"],"mappings":";;;AAaA,MAAM,YAAY,CAAC,QAAa;AAE9B,QAAM,OAAO,OAAO,KAAK,GAAG,EAAE,KAAK;AACnC,SAAO,KAAK,IAAI,CAAC,QAAQ,GAAG,GAAG,MAAM,IAAI,GAAG,CAAC,EAAE,EAAE,KAAK,IAAI;AAC5D;AAsBA,MAAM,cAAc,CAAC,EAAE,aAA6C;AAC5D,QAAA,gCAAgB;AAChB,QAAA,gCAAgB;AAEf,SAAA;AAAA,IACL;AAAA,IACA;AAAA;AAAA;AAAA;AAAA,IAIA,IAAI,WAAsB;AAClB,YAAA,MAAM,UAAU,EAAE,QAAQ,aAAa,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;AAAA,IAKA,IAAI,MAAiB;AACb,YAAA,MAAM,UAAU,EAAE,QAAQ,aAAa,QAAQ,MAAM,GAAG,KAAA,CAAM;AAC7D,aAAA,UAAU,IAAI,GAAG;AAAA,IAC1B;AAAA;AAAA;AAAA;AAAA,IAKA,QAAQ;AACN,gBAAU,MAAM;AAChB,gBAAU,MAAM;AAAA,IAClB;AAAA,EAAA;AAEJ;;"}
1
+ {"version":3,"file":"id-map.js","sources":["../../../../src/services/document-service/transform/id-map.ts"],"sourcesContent":["import { Core, Data, UID } from '@strapi/types';\nimport { async, contentTypes } from '@strapi/utils';\n\nconst hasDraftAndPublish = (uid: UID.CollectionType) => {\n const model = strapi.getModel(uid);\n return contentTypes.hasDraftAndPublish(model);\n};\n\n/**\n * TODO: Find a better way to encode keys than this\n * This converts an object into a string by joining its keys and values,\n * so it can be used as a key in a Map.\n *\n * @example\n * const obj = { a: 1, b: 2 };\n * const key = encodeKey(obj);\n * ^ \"a:::1&&b:::2\"\n */\nconst encodeKey = (obj: any) => {\n // Ignore status field for models without draft and publish\n if (!hasDraftAndPublish(obj.uid)) {\n delete obj.status;\n }\n\n // Sort keys to always keep the same order when encoding\n const keys = Object.keys(obj).sort();\n return keys.map((key) => `${key}:::${obj[key]}`).join('&&');\n};\n\ninterface KeyFields {\n uid: string;\n documentId: Data.ID;\n locale?: string | null;\n status?: 'draft' | 'published';\n}\n\nexport interface IdMap {\n loadedIds: Map<string, string>;\n toLoadIds: Map<string, KeyFields>;\n // Make the Keys type to be the params of add\n add(keys: KeyFields): void;\n load(): Promise<void>;\n get(keys: KeyFields): string | undefined;\n clear(): void;\n}\n\n/**\n * Holds a registry of document ids and their corresponding entity ids.\n */\nconst createIdMap = ({ strapi }: { strapi: Core.Strapi }): IdMap => {\n const loadedIds = new Map();\n const toLoadIds = new Map();\n\n return {\n loadedIds,\n toLoadIds,\n /**\n * Register a new document id and its corresponding entity id.\n */\n add(keyFields: KeyFields) {\n const key = encodeKey({ status: 'published', locale: null, ...keyFields });\n\n // If the id is already loaded, do nothing\n if (loadedIds.has(key)) return;\n // If the id is already in the toLoadIds, do nothing\n if (toLoadIds.has(key)) return;\n\n // Add the id to the toLoadIds\n toLoadIds.set(key, keyFields);\n },\n\n /**\n * Load all ids from the registry.\n */\n async load() {\n // Document Id to Entry Id queries are batched by its uid and locale\n // TODO: Add publication state too\n const loadIdValues = Array.from(toLoadIds.values());\n\n // 1. Group ids to query together\n const idsByUidAndLocale = loadIdValues.reduce((acc, { documentId, ...rest }) => {\n const key = encodeKey(rest);\n const ids = acc[key] || { ...rest, documentIds: [] };\n ids.documentIds.push(documentId);\n return { ...acc, [key]: ids };\n }, {});\n\n // 2. Query ids\n await async.map(\n Object.values(idsByUidAndLocale),\n async ({ uid, locale, documentIds, status }: any) => {\n const findParams = {\n select: ['id', 'documentId', 'locale', 'publishedAt'],\n where: {\n documentId: { $in: documentIds },\n locale: locale || null,\n },\n } as any;\n\n if (hasDraftAndPublish(uid)) {\n findParams.where.publishedAt = status === 'draft' ? null : { $ne: null };\n }\n\n const result = await strapi?.db?.query(uid).findMany(findParams);\n\n // 3. Store result in loadedIds\n result?.forEach(({ documentId, id, locale, publishedAt }: any) => {\n const key = encodeKey({\n documentId,\n uid,\n locale,\n status: publishedAt ? 'published' : 'draft',\n });\n loadedIds.set(key, id);\n });\n }\n );\n\n // 4. Clear toLoadIds\n toLoadIds.clear();\n },\n\n /**\n * Get the entity id for a given document id.\n */\n get(keys: KeyFields) {\n const key = encodeKey({ status: 'published', locale: null, ...keys });\n return loadedIds.get(key);\n },\n\n /**\n * Clear the registry.\n */\n clear() {\n loadedIds.clear();\n toLoadIds.clear();\n },\n };\n};\n\nexport { createIdMap };\n"],"names":["contentTypes","strapi","async","locale"],"mappings":";;;AAGA,MAAM,qBAAqB,CAAC,QAA4B;AAChD,QAAA,QAAQ,OAAO,SAAS,GAAG;AAC1B,SAAAA,YAAA,aAAa,mBAAmB,KAAK;AAC9C;AAYA,MAAM,YAAY,CAAC,QAA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AAAA,UAAA;AAGE,cAAA,mBAAmB,GAAG,GAAG;AAC3B,uBAAW,MAAM,cAAc,WAAW,UAAU,OAAO,EAAE,KAAK;UACpE;AAEM,gBAAA,SAAS,MAAMD,SAAQ,IAAI,MAAM,GAAG,EAAE,SAAS,UAAU;AAGvD,kBAAA,QAAQ,CAAC,EAAE,YAAY,IAAI,QAAAE,SAAQ,kBAAuB;AAChE,kBAAM,MAAM,UAAU;AAAA,cACpB;AAAA,cACA;AAAA,cACA,QAAAA;AAAAA,cACA,QAAQ,cAAc,cAAc;AAAA,YAAA,CACrC;AACS,sBAAA,IAAI,KAAK,EAAE;AAAA,UAAA,CACtB;AAAA,QACH;AAAA,MAAA;AAIF,gBAAU,MAAM;AAAA,IAClB;AAAA;AAAA;AAAA;AAAA,IAKA,IAAI,MAAiB;AACb,YAAA,MAAM,UAAU,EAAE,QAAQ,aAAa,QAAQ,MAAM,GAAG,KAAA,CAAM;AAC7D,aAAA,UAAU,IAAI,GAAG;AAAA,IAC1B;AAAA;AAAA;AAAA;AAAA,IAKA,QAAQ;AACN,gBAAU,MAAM;AAChB,gBAAU,MAAM;AAAA,IAClB;AAAA,EAAA;AAEJ;;"}
@@ -1,9 +1,16 @@
1
- import { async } from "@strapi/utils";
1
+ import { async, contentTypes } from "@strapi/utils";
2
+ const hasDraftAndPublish = (uid) => {
3
+ const model = strapi.getModel(uid);
4
+ return contentTypes.hasDraftAndPublish(model);
5
+ };
2
6
  const encodeKey = (obj) => {
7
+ if (!hasDraftAndPublish(obj.uid)) {
8
+ delete obj.status;
9
+ }
3
10
  const keys = Object.keys(obj).sort();
4
11
  return keys.map((key) => `${key}:::${obj[key]}`).join("&&");
5
12
  };
6
- const createIdMap = ({ strapi }) => {
13
+ const createIdMap = ({ strapi: strapi2 }) => {
7
14
  const loadedIds = /* @__PURE__ */ new Map();
8
15
  const toLoadIds = /* @__PURE__ */ new Map();
9
16
  return {
@@ -38,11 +45,13 @@ const createIdMap = ({ strapi }) => {
38
45
  select: ["id", "documentId", "locale", "publishedAt"],
39
46
  where: {
40
47
  documentId: { $in: documentIds },
41
- locale: locale || null,
42
- publishedAt: status === "draft" ? null : { $ne: null }
48
+ locale: locale || null
43
49
  }
44
50
  };
45
- const result = await strapi?.db?.query(uid).findMany(findParams);
51
+ if (hasDraftAndPublish(uid)) {
52
+ findParams.where.publishedAt = status === "draft" ? null : { $ne: null };
53
+ }
54
+ const result = await strapi2?.db?.query(uid).findMany(findParams);
46
55
  result?.forEach(({ documentId, id, locale: locale2, publishedAt }) => {
47
56
  const key = encodeKey({
48
57
  documentId,
@@ -1 +1 @@
1
- {"version":3,"file":"id-map.mjs","sources":["../../../../src/services/document-service/transform/id-map.ts"],"sourcesContent":["import { Core, Data } from '@strapi/types';\nimport { async } from '@strapi/utils';\n\n/**\n * TODO: Find a better way to encode keys than this\n * This converts an object into a string by joining its keys and values,\n * so it can be used as a key in a Map.\n *\n * @example\n * const obj = { a: 1, b: 2 };\n * const key = encodeKey(obj);\n * ^ \"a:::1&&b:::2\"\n */\nconst encodeKey = (obj: any) => {\n // Sort keys to always keep the same order when encoding\n const keys = Object.keys(obj).sort();\n return keys.map((key) => `${key}:::${obj[key]}`).join('&&');\n};\n\ninterface KeyFields {\n uid: string;\n documentId: Data.ID;\n locale?: string | null;\n status?: 'draft' | 'published';\n}\n\nexport interface IdMap {\n loadedIds: Map<string, string>;\n toLoadIds: Map<string, KeyFields>;\n // Make the Keys type to be the params of add\n add(keys: KeyFields): void;\n load(): Promise<void>;\n get(keys: KeyFields): string | undefined;\n clear(): void;\n}\n\n/**\n * Holds a registry of document ids and their corresponding entity ids.\n */\nconst createIdMap = ({ strapi }: { strapi: Core.Strapi }): IdMap => {\n const loadedIds = new Map();\n const toLoadIds = new Map();\n\n return {\n loadedIds,\n toLoadIds,\n /**\n * Register a new document id and its corresponding entity id.\n */\n add(keyFields: KeyFields) {\n const key = encodeKey({ status: 'published', locale: null, ...keyFields });\n\n // If the id is already loaded, do nothing\n if (loadedIds.has(key)) return;\n // If the id is already in the toLoadIds, do nothing\n if (toLoadIds.has(key)) return;\n\n // Add the id to the toLoadIds\n toLoadIds.set(key, keyFields);\n },\n\n /**\n * Load all ids from the registry.\n */\n async load() {\n // Document Id to Entry Id queries are batched by its uid and locale\n // TODO: Add publication state too\n const loadIdValues = Array.from(toLoadIds.values());\n\n // 1. Group ids to query together\n const idsByUidAndLocale = loadIdValues.reduce((acc, { documentId, ...rest }) => {\n const key = encodeKey(rest);\n const ids = acc[key] || { ...rest, documentIds: [] };\n ids.documentIds.push(documentId);\n return { ...acc, [key]: ids };\n }, {});\n\n // 2. Query ids\n await async.map(\n Object.values(idsByUidAndLocale),\n async ({ uid, locale, documentIds, status }: any) => {\n const findParams = {\n select: ['id', 'documentId', 'locale', 'publishedAt'],\n where: {\n documentId: { $in: documentIds },\n locale: locale || null,\n publishedAt: status === 'draft' ? null : { $ne: null },\n },\n } as any;\n\n const result = await strapi?.db?.query(uid).findMany(findParams);\n\n // 3. Store result in loadedIds\n result?.forEach(({ documentId, id, locale, publishedAt }: any) => {\n const key = encodeKey({\n documentId,\n uid,\n locale,\n status: publishedAt ? 'published' : 'draft',\n });\n loadedIds.set(key, id);\n });\n }\n );\n\n // 4. Clear toLoadIds\n toLoadIds.clear();\n },\n\n /**\n * Get the entity id for a given document id.\n */\n get(keys: KeyFields) {\n const key = encodeKey({ status: 'published', locale: null, ...keys });\n return loadedIds.get(key);\n },\n\n /**\n * Clear the registry.\n */\n clear() {\n loadedIds.clear();\n toLoadIds.clear();\n },\n };\n};\n\nexport { createIdMap };\n"],"names":["locale"],"mappings":";AAaA,MAAM,YAAY,CAAC,QAAa;AAE9B,QAAM,OAAO,OAAO,KAAK,GAAG,EAAE,KAAK;AACnC,SAAO,KAAK,IAAI,CAAC,QAAQ,GAAG,GAAG,MAAM,IAAI,GAAG,CAAC,EAAE,EAAE,KAAK,IAAI;AAC5D;AAsBA,MAAM,cAAc,CAAC,EAAE,aAA6C;AAC5D,QAAA,gCAAg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AAA,YAAA,CACrC;AACS,sBAAA,IAAI,KAAK,EAAE;AAAA,UAAA,CACtB;AAAA,QACH;AAAA,MAAA;AAIF,gBAAU,MAAM;AAAA,IAClB;AAAA;AAAA;AAAA;AAAA,IAKA,IAAI,MAAiB;AACb,YAAA,MAAM,UAAU,EAAE,QAAQ,aAAa,QAAQ,MAAM,GAAG,KAAA,CAAM;AAC7D,aAAA,UAAU,IAAI,GAAG;AAAA,IAC1B;AAAA;AAAA;AAAA;AAAA,IAKA,QAAQ;AACN,gBAAU,MAAM;AAChB,gBAAU,MAAM;AAAA,IAClB;AAAA,EAAA;AAEJ;"}
1
+ {"version":3,"file":"id-map.mjs","sources":["../../../../src/services/document-service/transform/id-map.ts"],"sourcesContent":["import { Core, Data, UID } from '@strapi/types';\nimport { async, contentTypes } from '@strapi/utils';\n\nconst hasDraftAndPublish = (uid: UID.CollectionType) => {\n const model = strapi.getModel(uid);\n return contentTypes.hasDraftAndPublish(model);\n};\n\n/**\n * TODO: Find a better way to encode keys than this\n * This converts an object into a string by joining its keys and values,\n * so it can be used as a key in a Map.\n *\n * @example\n * const obj = { a: 1, b: 2 };\n * const key = encodeKey(obj);\n * ^ \"a:::1&&b:::2\"\n */\nconst encodeKey = (obj: any) => {\n // Ignore status field for models without draft and publish\n if (!hasDraftAndPublish(obj.uid)) {\n delete obj.status;\n }\n\n // Sort keys to always keep the same order when encoding\n const keys = Object.keys(obj).sort();\n return keys.map((key) => `${key}:::${obj[key]}`).join('&&');\n};\n\ninterface KeyFields {\n uid: string;\n documentId: Data.ID;\n locale?: string | null;\n status?: 'draft' | 'published';\n}\n\nexport interface IdMap {\n loadedIds: Map<string, string>;\n toLoadIds: Map<string, KeyFields>;\n // Make the Keys type to be the params of add\n add(keys: KeyFields): void;\n load(): Promise<void>;\n get(keys: KeyFields): string | undefined;\n clear(): void;\n}\n\n/**\n * Holds a registry of document ids and their corresponding entity ids.\n */\nconst createIdMap = ({ strapi }: { strapi: Core.Strapi }): IdMap => {\n const loadedIds = new Map();\n const toLoadIds = new Map();\n\n return {\n loadedIds,\n toLoadIds,\n /**\n * Register a new document id and its corresponding entity id.\n */\n add(keyFields: KeyFields) {\n const key = encodeKey({ status: 'published', locale: null, ...keyFields });\n\n // If the id is already loaded, do nothing\n if (loadedIds.has(key)) return;\n // If the id is already in the toLoadIds, do nothing\n if (toLoadIds.has(key)) return;\n\n // Add the id to the toLoadIds\n toLoadIds.set(key, keyFields);\n },\n\n /**\n * Load all ids from the registry.\n */\n async load() {\n // Document Id to Entry Id queries are batched by its uid and locale\n // TODO: Add publication state too\n const loadIdValues = Array.from(toLoadIds.values());\n\n // 1. Group ids to query together\n const idsByUidAndLocale = loadIdValues.reduce((acc, { documentId, ...rest }) => {\n const key = encodeKey(rest);\n const ids = acc[key] || { ...rest, documentIds: [] };\n ids.documentIds.push(documentId);\n return { ...acc, [key]: ids };\n }, {});\n\n // 2. Query ids\n await async.map(\n Object.values(idsByUidAndLocale),\n async ({ uid, locale, documentIds, status }: any) => {\n const findParams = {\n select: ['id', 'documentId', 'locale', 'publishedAt'],\n where: {\n documentId: { $in: documentIds },\n locale: locale || null,\n },\n } as any;\n\n if (hasDraftAndPublish(uid)) {\n findParams.where.publishedAt = status === 'draft' ? null : { $ne: null };\n }\n\n const result = await strapi?.db?.query(uid).findMany(findParams);\n\n // 3. Store result in loadedIds\n result?.forEach(({ documentId, id, locale, publishedAt }: any) => {\n const key = encodeKey({\n documentId,\n uid,\n locale,\n status: publishedAt ? 'published' : 'draft',\n });\n loadedIds.set(key, id);\n });\n }\n );\n\n // 4. Clear toLoadIds\n toLoadIds.clear();\n },\n\n /**\n * Get the entity id for a given document id.\n */\n get(keys: KeyFields) {\n const key = encodeKey({ status: 'published', locale: null, ...keys });\n return loadedIds.get(key);\n },\n\n /**\n * Clear the registry.\n */\n clear() {\n loadedIds.clear();\n toLoadIds.clear();\n },\n };\n};\n\nexport { createIdMap };\n"],"names":["strapi","locale"],"mappings":";AAGA,MAAM,qBAAqB,C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,cAAc,UAAU,aAAa;AAAA,YACpD,OAAO;AAAA,cACL,YAAY,EAAE,KAAK,YAAY;AAAA,cAC/B,QAAQ,UAAU;AAAA,YACpB;AAAA,UAAA;AAGE,cAAA,mBAAmB,GAAG,GAAG;AAC3B,uBAAW,MAAM,cAAc,WAAW,UAAU,OAAO,EAAE,KAAK;UACpE;AAEM,gBAAA,SAAS,MAAMA,SAAQ,IAAI,MAAM,GAAG,EAAE,SAAS,UAAU;AAGvD,kBAAA,QAAQ,CAAC,EAAE,YAAY,IAAI,QAAAC,SAAQ,kBAAuB;AAChE,kBAAM,MAAM,UAAU;AAAA,cACpB;AAAA,cACA;AAAA,cACA,QAAAA;AAAAA,cACA,QAAQ,cAAc,cAAc;AAAA,YAAA,CACrC;AACS,sBAAA,IAAI,KAAK,EAAE;AAAA,UAAA,CACtB;AAAA,QACH;AAAA,MAAA;AAIF,gBAAU,MAAM;AAAA,IAClB;AAAA;AAAA;AAAA;AAAA,IAKA,IAAI,MAAiB;AACb,YAAA,MAAM,UAAU,EAAE,QAAQ,aAAa,QAAQ,MAAM,GAAG,KAAA,CAAM;AAC7D,aAAA,UAAU,IAAI,GAAG;AAAA,IAC1B;AAAA;AAAA;AAAA;AAAA,IAKA,QAAQ;AACN,gBAAU,MAAM;AAChB,gBAAU,MAAM;AAAA,IAClB;AAAA,EAAA;AAEJ;"}
@@ -1 +1 @@
1
- {"version":3,"file":"populate.d.ts","sourceRoot":"","sources":["../../../../src/services/document-service/utils/populate.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,GAAG,EAAE,MAAM,eAAe,CAAC;AAGpC,UAAU,OAAO;IACf;;OAEG;IACH,gBAAgB,CAAC,EAAE,MAAM,EAAE,CAAC;CAC7B;AAGD,eAAO,MAAM,eAAe,QAAS,IAAI,MAAM,SAAQ,OAAO,QAoD7D,CAAC"}
1
+ {"version":3,"file":"populate.d.ts","sourceRoot":"","sources":["../../../../src/services/document-service/utils/populate.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,GAAG,EAAE,MAAM,eAAe,CAAC;AAGpC,UAAU,OAAO;IACf;;OAEG;IACH,gBAAgB,CAAC,EAAE,MAAM,EAAE,CAAC;CAC7B;AAKD,eAAO,MAAM,eAAe,QAAS,IAAI,MAAM,SAAQ,OAAO,QAsD7D,CAAC"}
@@ -1,6 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
3
3
  const strapiUtils = require("@strapi/utils");
4
+ const { CREATED_BY_ATTRIBUTE, UPDATED_BY_ATTRIBUTE } = strapiUtils.contentTypes.constants;
4
5
  const getDeepPopulate = (uid, opts = {}) => {
5
6
  const model = strapi.getModel(uid);
6
7
  const attributes = Object.entries(model.attributes);
@@ -12,7 +13,8 @@ const getDeepPopulate = (uid, opts = {}) => {
12
13
  break;
13
14
  }
14
15
  const isVisible = strapiUtils.contentTypes.isVisibleAttribute(model, attributeName);
15
- if (isVisible) {
16
+ const isCreatorField = [CREATED_BY_ATTRIBUTE, UPDATED_BY_ATTRIBUTE].includes(attributeName);
17
+ if (isVisible || isCreatorField) {
16
18
  acc[attributeName] = { select: opts.relationalFields };
17
19
  }
18
20
  break;
@@ -1 +1 @@
1
- {"version":3,"file":"populate.js","sources":["../../../../src/services/document-service/utils/populate.ts"],"sourcesContent":["import { UID } from '@strapi/types';\nimport { contentTypes } from '@strapi/utils';\n\ninterface Options {\n /**\n * Fields to select when populating relations\n */\n relationalFields?: string[];\n}\n\n// We want to build a populate object based on the schema\nexport const getDeepPopulate = (uid: UID.Schema, opts: Options = {}) => {\n const model = strapi.getModel(uid);\n const attributes = Object.entries(model.attributes);\n\n return attributes.reduce((acc: any, [attributeName, attribute]) => {\n switch (attribute.type) {\n case 'relation': {\n // TODO: Support polymorphic relations\n const isMorphRelation = attribute.relation.toLowerCase().startsWith('morph');\n if (isMorphRelation) {\n break;\n }\n\n // TODO: Should this just be a plain list?\n // Ignore createdBy, updatedBy, ...\n const isVisible = contentTypes.isVisibleAttribute(model, attributeName);\n if (isVisible) {\n acc[attributeName] = { select: opts.relationalFields };\n }\n break;\n }\n\n case 'media': {\n acc[attributeName] = { select: ['id'] };\n break;\n }\n\n case 'component': {\n const populate = getDeepPopulate(attribute.component, opts);\n acc[attributeName] = { populate };\n break;\n }\n\n case 'dynamiczone': {\n // Use fragments to populate the dynamic zone components\n const populatedComponents = (attribute.components || []).reduce(\n (acc: any, componentUID: UID.Component) => {\n acc[componentUID] = { populate: getDeepPopulate(componentUID, opts) };\n return acc;\n },\n {}\n );\n\n acc[attributeName] = { on: populatedComponents };\n break;\n }\n default:\n break;\n }\n\n return acc;\n }, {});\n};\n"],"names":["contentTypes","acc"],"mappings":";;;AAWO,MAAM,kBAAkB,CAAC,KAAiB,OAAgB,OAAO;AAChE,QAAA,QAAQ,OAAO,SAAS,GAAG;AACjC,QAAM,aAAa,OAAO,QAAQ,MAAM,UAAU;AAElD,SAAO,WAAW,OAAO,CAAC,KAAU,CAAC,eAAe,SAAS,MAAM;AACjE,YAAQ,UAAU,MAAM;AAAA,MACtB,KAAK,YAAY;AAEf,cAAM,kBAAkB,UAAU,SAAS,YAAY,EAAE,WAAW,OAAO;AAC3E,YAAI,iBAAiB;AACnB;AAAA,QACF;AAIA,cAAM,YAAYA,YAAA,aAAa,mBAAmB,OAAO,aAAa;AACtE,YAAI,WAAW;AACb,cAAI,aAAa,IAAI,EAAE,QAAQ,KAAK,iBAAiB;AAAA,QACvD;AACA;AAAA,MACF;AAAA,MAEA,KAAK,SAAS;AACZ,YAAI,aAAa,IAAI,EAAE,QAAQ,CAAC,IAAI,EAAE;AACtC;AAAA,MACF;AAAA,MAEA,KAAK,aAAa;AAChB,cAAM,WAAW,gBAAgB,UAAU,WAAW,IAAI;AACtD,YAAA,aAAa,IAAI,EAAE;AACvB;AAAA,MACF;AAAA,MAEA,KAAK,eAAe;AAElB,cAAM,uBAAuB,UAAU,cAAc,CAAI,GAAA;AAAA,UACvD,CAACC,MAAU,iBAAgC;AACzCA,iBAAI,YAAY,IAAI,EAAE,UAAU,gBAAgB,cAAc,IAAI;AAC3DA,mBAAAA;AAAAA,UACT;AAAA,UACA,CAAC;AAAA,QAAA;AAGH,YAAI,aAAa,IAAI,EAAE,IAAI,oBAAoB;AAC/C;AAAA,MACF;AAAA,IAGF;AAEO,WAAA;AAAA,EACT,GAAG,CAAE,CAAA;AACP;;"}
1
+ {"version":3,"file":"populate.js","sources":["../../../../src/services/document-service/utils/populate.ts"],"sourcesContent":["import { UID } from '@strapi/types';\nimport { contentTypes } from '@strapi/utils';\n\ninterface Options {\n /**\n * Fields to select when populating relations\n */\n relationalFields?: string[];\n}\n\nconst { CREATED_BY_ATTRIBUTE, UPDATED_BY_ATTRIBUTE } = contentTypes.constants;\n\n// We want to build a populate object based on the schema\nexport const getDeepPopulate = (uid: UID.Schema, opts: Options = {}) => {\n const model = strapi.getModel(uid);\n const attributes = Object.entries(model.attributes);\n\n return attributes.reduce((acc: any, [attributeName, attribute]) => {\n switch (attribute.type) {\n case 'relation': {\n // TODO: Support polymorphic relations\n const isMorphRelation = attribute.relation.toLowerCase().startsWith('morph');\n if (isMorphRelation) {\n break;\n }\n\n // Ignore not visible fields other than createdBy and updatedBy\n const isVisible = contentTypes.isVisibleAttribute(model, attributeName);\n const isCreatorField = [CREATED_BY_ATTRIBUTE, UPDATED_BY_ATTRIBUTE].includes(attributeName);\n\n if (isVisible || isCreatorField) {\n acc[attributeName] = { select: opts.relationalFields };\n }\n\n break;\n }\n\n case 'media': {\n acc[attributeName] = { select: ['id'] };\n break;\n }\n\n case 'component': {\n const populate = getDeepPopulate(attribute.component, opts);\n acc[attributeName] = { populate };\n break;\n }\n\n case 'dynamiczone': {\n // Use fragments to populate the dynamic zone components\n const populatedComponents = (attribute.components || []).reduce(\n (acc: any, componentUID: UID.Component) => {\n acc[componentUID] = { populate: getDeepPopulate(componentUID, opts) };\n return acc;\n },\n {}\n );\n\n acc[attributeName] = { on: populatedComponents };\n break;\n }\n default:\n break;\n }\n\n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import { contentTypes } from "@strapi/utils";
2
+ const { CREATED_BY_ATTRIBUTE, UPDATED_BY_ATTRIBUTE } = contentTypes.constants;
2
3
  const getDeepPopulate = (uid, opts = {}) => {
3
4
  const model = strapi.getModel(uid);
4
5
  const attributes = Object.entries(model.attributes);
@@ -10,7 +11,8 @@ const getDeepPopulate = (uid, opts = {}) => {
10
11
  break;
11
12
  }
12
13
  const isVisible = contentTypes.isVisibleAttribute(model, attributeName);
13
- if (isVisible) {
14
+ const isCreatorField = [CREATED_BY_ATTRIBUTE, UPDATED_BY_ATTRIBUTE].includes(attributeName);
15
+ if (isVisible || isCreatorField) {
14
16
  acc[attributeName] = { select: opts.relationalFields };
15
17
  }
16
18
  break;
@@ -1 +1 @@
1
- {"version":3,"file":"populate.mjs","sources":["../../../../src/services/document-service/utils/populate.ts"],"sourcesContent":["import { UID } from '@strapi/types';\nimport { contentTypes } from '@strapi/utils';\n\ninterface Options {\n /**\n * Fields to select when populating relations\n */\n relationalFields?: string[];\n}\n\n// We want to build a populate object based on the schema\nexport const getDeepPopulate = (uid: UID.Schema, opts: Options = {}) => {\n const model = strapi.getModel(uid);\n const attributes = Object.entries(model.attributes);\n\n return attributes.reduce((acc: any, [attributeName, attribute]) => {\n switch (attribute.type) {\n case 'relation': {\n // TODO: Support polymorphic relations\n const isMorphRelation = attribute.relation.toLowerCase().startsWith('morph');\n if (isMorphRelation) {\n break;\n }\n\n // TODO: Should this just be a plain list?\n // Ignore createdBy, updatedBy, ...\n const isVisible = contentTypes.isVisibleAttribute(model, attributeName);\n if (isVisible) {\n acc[attributeName] = { select: opts.relationalFields };\n }\n break;\n }\n\n case 'media': {\n acc[attributeName] = { select: ['id'] };\n break;\n }\n\n case 'component': {\n const populate = getDeepPopulate(attribute.component, opts);\n acc[attributeName] = { populate };\n break;\n }\n\n case 'dynamiczone': {\n // Use fragments to populate the dynamic zone components\n const populatedComponents = (attribute.components || []).reduce(\n (acc: any, componentUID: UID.Component) => {\n acc[componentUID] = { populate: getDeepPopulate(componentUID, opts) };\n return acc;\n },\n {}\n );\n\n acc[attributeName] = { on: populatedComponents };\n break;\n }\n default:\n break;\n }\n\n return acc;\n }, {});\n};\n"],"names":["acc"],"mappings":";AAWO,MAAM,kBAAkB,CAAC,KAAiB,OAAgB,OAAO;AAChE,QAAA,QAAQ,OAAO,SAAS,GAAG;AACjC,QAAM,aAAa,OAAO,QAAQ,MAAM,UAAU;AAElD,SAAO,WAAW,OAAO,CAAC,KAAU,CAAC,eAAe,SAAS,MAAM;AACjE,YAAQ,UAAU,MAAM;AAAA,MACtB,KAAK,YAAY;AAEf,cAAM,kBAAkB,UAAU,SAAS,YAAY,EAAE,WAAW,OAAO;AAC3E,YAAI,iBAAiB;AACnB;AAAA,QACF;AAIA,cAAM,YAAY,aAAa,mBAAmB,OAAO,aAAa;AACtE,YAAI,WAAW;AACb,cAAI,aAAa,IAAI,EAAE,QAAQ,KAAK,iBAAiB;AAAA,QACvD;AACA;AAAA,MACF;AAAA,MAEA,KAAK,SAAS;AACZ,YAAI,aAAa,IAAI,EAAE,QAAQ,CAAC,IAAI,EAAE;AACtC;AAAA,MACF;AAAA,MAEA,KAAK,aAAa;AAChB,cAAM,WAAW,gBAAgB,UAAU,WAAW,IAAI;AACtD,YAAA,aAAa,IAAI,EAAE;AACvB;AAAA,MACF;AAAA,MAEA,KAAK,eAAe;AAElB,cAAM,uBAAuB,UAAU,cAAc,CAAI,GAAA;AAAA,UACvD,CAACA,MAAU,iBAAgC;AACzCA,iBAAI,YAAY,IAAI,EAAE,UAAU,gBAAgB,cAAc,IAAI;AAC3DA,mBAAAA;AAAAA,UACT;AAAA,UACA,CAAC;AAAA,QAAA;AAGH,YAAI,aAAa,IAAI,EAAE,IAAI,oBAAoB;AAC/C;AAAA,MACF;AAAA,IAGF;AAEO,WAAA;AAAA,EACT,GAAG,CAAE,CAAA;AACP;"}
1
+ {"version":3,"file":"populate.mjs","sources":["../../../../src/services/document-service/utils/populate.ts"],"sourcesContent":["import { UID } from '@strapi/types';\nimport { contentTypes } from '@strapi/utils';\n\ninterface Options {\n /**\n * Fields to select when populating relations\n */\n relationalFields?: string[];\n}\n\nconst { CREATED_BY_ATTRIBUTE, UPDATED_BY_ATTRIBUTE } = contentTypes.constants;\n\n// We want to build a populate object based on the schema\nexport const getDeepPopulate = (uid: UID.Schema, opts: Options = {}) => {\n const model = strapi.getModel(uid);\n const attributes = Object.entries(model.attributes);\n\n return attributes.reduce((acc: any, [attributeName, attribute]) => {\n switch (attribute.type) {\n case 'relation': {\n // TODO: Support polymorphic relations\n const isMorphRelation = attribute.relation.toLowerCase().startsWith('morph');\n if (isMorphRelation) {\n break;\n }\n\n // Ignore not visible fields other than createdBy and updatedBy\n const isVisible = contentTypes.isVisibleAttribute(model, attributeName);\n const isCreatorField = [CREATED_BY_ATTRIBUTE, UPDATED_BY_ATTRIBUTE].includes(attributeName);\n\n if (isVisible || isCreatorField) {\n acc[attributeName] = { select: opts.relationalFields };\n }\n\n break;\n }\n\n case 'media': {\n acc[attributeName] = { select: ['id'] };\n break;\n }\n\n case 'component': {\n const populate = getDeepPopulate(attribute.component, opts);\n acc[attributeName] = { populate };\n break;\n }\n\n case 'dynamiczone': {\n // Use fragments to populate the dynamic zone components\n const populatedComponents = (attribute.components || []).reduce(\n (acc: any, componentUID: UID.Component) => {\n acc[componentUID] = { populate: getDeepPopulate(componentUID, opts) };\n return acc;\n },\n {}\n );\n\n acc[attributeName] = { on: populatedComponents };\n break;\n }\n default:\n break;\n }\n\n return acc;\n }, {});\n};\n"],"names":["acc"],"mappings":";AAUA,MAAM,EAAE,sBAAsB,yBAAyB,aAAa;AAG7D,MAAM,kBAAkB,CAAC,KAAiB,OAAgB,OAAO;AAChE,QAAA,QAAQ,OAAO,SAAS,GAAG;AACjC,QAAM,aAAa,OAAO,QAAQ,MAAM,UAAU;AAElD,SAAO,WAAW,OAAO,CAAC,KAAU,CAAC,eAAe,SAAS,MAAM;AACjE,YAAQ,UAAU,MAAM;AAAA,MACtB,KAAK,YAAY;AAEf,cAAM,kBAAkB,UAAU,SAAS,YAAY,EAAE,WAAW,OAAO;AAC3E,YAAI,iBAAiB;AACnB;AAAA,QACF;AAGA,cAAM,YAAY,aAAa,mBAAmB,OAAO,aAAa;AACtE,cAAM,iBAAiB,CAAC,sBAAsB,oBAAoB,EAAE,SAAS,aAAa;AAE1F,YAAI,aAAa,gBAAgB;AAC/B,cAAI,aAAa,IAAI,EAAE,QAAQ,KAAK,iBAAiB;AAAA,QACvD;AAEA;AAAA,MACF;AAAA,MAEA,KAAK,SAAS;AACZ,YAAI,aAAa,IAAI,EAAE,QAAQ,CAAC,IAAI,EAAE;AACtC;AAAA,MACF;AAAA,MAEA,KAAK,aAAa;AAChB,cAAM,WAAW,gBAAgB,UAAU,WAAW,IAAI;AACtD,YAAA,aAAa,IAAI,EAAE;AACvB;AAAA,MACF;AAAA,MAEA,KAAK,eAAe;AAElB,cAAM,uBAAuB,UAAU,cAAc,CAAI,GAAA;AAAA,UACvD,CAACA,MAAU,iBAAgC;AACzCA,iBAAI,YAAY,IAAI,EAAE,UAAU,gBAAgB,cAAc,IAAI;AAC3DA,mBAAAA;AAAAA,UACT;AAAA,UACA,CAAC;AAAA,QAAA;AAGH,YAAI,aAAa,IAAI,EAAE,IAAI,oBAAoB;AAC/C;AAAA,MACF;AAAA,IAGF;AAEO,WAAA;AAAA,EACT,GAAG,CAAE,CAAA;AACP;"}
@@ -0,0 +1,36 @@
1
+ import { UID } from '@strapi/types';
2
+ interface LoadContext {
3
+ oldVersions: {
4
+ id: string;
5
+ locale: string;
6
+ }[];
7
+ newVersions: {
8
+ id: string;
9
+ locale: string;
10
+ }[];
11
+ }
12
+ /**
13
+ * Loads lingering relations that need to be updated when overriding a published or draft entry.
14
+ * This is necessary because the relations are uni-directional and the target entry is not aware of the source entry.
15
+ * This is not the case for bi-directional relations, where the target entry is also linked to the source entry.
16
+ */
17
+ declare const load: (uid: UID.ContentType, { oldVersions, newVersions }: LoadContext) => Promise<any>;
18
+ /**
19
+ * Updates uni directional relations to target the right entries when overriding published or draft entries.
20
+ *
21
+ * @param oldEntries The old entries that are being overridden
22
+ * @param newEntries The new entries that are overriding the old ones
23
+ * @param oldRelations The relations that were previously loaded with `load` @see load
24
+ */
25
+ declare const sync: (oldEntries: {
26
+ id: string;
27
+ locale: string;
28
+ }[], newEntries: {
29
+ id: string;
30
+ locale: string;
31
+ }[], oldRelations: {
32
+ joinTable: any;
33
+ relations: any[];
34
+ }[]) => Promise<void>;
35
+ export { load, sync };
36
+ //# sourceMappingURL=unidirectional-relations.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"unidirectional-relations.d.ts","sourceRoot":"","sources":["../../../../src/services/document-service/utils/unidirectional-relations.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,GAAG,EAAU,MAAM,eAAe,CAAC;AAE5C,UAAU,WAAW;IACnB,WAAW,EAAE;QAAE,EAAE,EAAE,MAAM,CAAC;QAAC,MAAM,EAAE,MAAM,CAAA;KAAE,EAAE,CAAC;IAC9C,WAAW,EAAE;QAAE,EAAE,EAAE,MAAM,CAAC;QAAC,MAAM,EAAE,MAAM,CAAA;KAAE,EAAE,CAAC;CAC/C;AAED;;;;GAIG;AACH,QAAA,MAAM,IAAI,QAAe,IAAI,WAAW,gCAAgC,WAAW,iBA8FlF,CAAC;AAEF;;;;;;GAMG;AACH,QAAA,MAAM,IAAI,eACI;IAAE,EAAE,EAAE,MAAM,CAAC;IAAC,MAAM,EAAE,MAAM,CAAA;CAAE,EAAE,cAChC;IAAE,EAAE,EAAE,MAAM,CAAC;IAAC,MAAM,EAAE,MAAM,CAAA;CAAE,EAAE,gBAC9B;IAAE,SAAS,EAAE,GAAG,CAAC;IAAC,SAAS,EAAE,GAAG,EAAE,CAAA;CAAE,EAAE,kBAiCrD,CAAC;AAEF,OAAO,EAAE,IAAI,EAAE,IAAI,EAAE,CAAC"}
@@ -0,0 +1,69 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
3
+ const fp = require("lodash/fp");
4
+ const load = async (uid, { oldVersions, newVersions }) => {
5
+ const updates = [];
6
+ await strapi.db.transaction(async ({ trx }) => {
7
+ const contentTypes = Object.values(strapi.contentTypes);
8
+ const components = Object.values(strapi.components);
9
+ for (const model of [...contentTypes, ...components]) {
10
+ const dbModel = strapi.db.metadata.get(model.uid);
11
+ for (const attribute of Object.values(dbModel.attributes)) {
12
+ if (attribute.type !== "relation" || attribute.target !== uid || attribute.inversedBy || attribute.mappedBy) {
13
+ continue;
14
+ }
15
+ const joinTable = attribute.joinTable;
16
+ if (!joinTable) {
17
+ continue;
18
+ }
19
+ const { name: sourceColumnName } = joinTable.joinColumn;
20
+ const { name: targetColumnName } = joinTable.inverseJoinColumn;
21
+ const ids = oldVersions.map((entry) => entry.id);
22
+ const oldVersionsRelations = await strapi.db.getConnection().select("*").from(joinTable.name).whereIn(targetColumnName, ids).transacting(trx);
23
+ if (oldVersionsRelations.length > 0) {
24
+ updates.push({ joinTable, relations: oldVersionsRelations });
25
+ }
26
+ if (!model.options?.draftAndPublish) {
27
+ const ids2 = newVersions.map((entry) => entry.id);
28
+ const newVersionsRelations = await strapi.db.getConnection().select("*").from(joinTable.name).whereIn(targetColumnName, ids2).transacting(trx);
29
+ if (newVersionsRelations.length > 0) {
30
+ const discardToAdd = newVersionsRelations.filter((relation) => {
31
+ const matchingOldVerion = oldVersionsRelations.find((oldRelation) => {
32
+ return oldRelation[sourceColumnName] === relation[sourceColumnName];
33
+ });
34
+ return !matchingOldVerion;
35
+ }).map(fp.omit("id"));
36
+ updates.push({ joinTable, relations: discardToAdd });
37
+ }
38
+ }
39
+ }
40
+ }
41
+ });
42
+ return updates;
43
+ };
44
+ const sync = async (oldEntries, newEntries, oldRelations) => {
45
+ const newEntryByLocale = fp.keyBy("locale", newEntries);
46
+ const oldEntriesMap = oldEntries.reduce(
47
+ (acc, entry) => {
48
+ const newEntry = newEntryByLocale[entry.locale];
49
+ if (!newEntry)
50
+ return acc;
51
+ acc[entry.id] = newEntry.id;
52
+ return acc;
53
+ },
54
+ {}
55
+ );
56
+ await strapi.db.transaction(async ({ trx }) => {
57
+ for (const { joinTable, relations } of oldRelations) {
58
+ const column = joinTable.inverseJoinColumn.name;
59
+ const newRelations = relations.map((relation) => {
60
+ const newId = oldEntriesMap[relation[column]];
61
+ return { ...relation, [column]: newId };
62
+ });
63
+ await trx.batchInsert(joinTable.name, newRelations, 1e3);
64
+ }
65
+ });
66
+ };
67
+ exports.load = load;
68
+ exports.sync = sync;
69
+ //# sourceMappingURL=unidirectional-relations.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"unidirectional-relations.js","sources":["../../../../src/services/document-service/utils/unidirectional-relations.ts"],"sourcesContent":["/* eslint-disable no-continue */\nimport { keyBy, omit } from 'lodash/fp';\n\nimport { UID, Schema } from '@strapi/types';\n\ninterface LoadContext {\n oldVersions: { id: string; locale: string }[];\n newVersions: { id: string; locale: string }[];\n}\n\n/**\n * Loads lingering relations that need to be updated when overriding a published or draft entry.\n * This is necessary because the relations are uni-directional and the target entry is not aware of the source entry.\n * This is not the case for bi-directional relations, where the target entry is also linked to the source entry.\n */\nconst load = async (uid: UID.ContentType, { oldVersions, newVersions }: LoadContext) => {\n const updates = [] as any;\n\n // Iterate all components and content types to find relations that need to be updated\n await strapi.db.transaction(async ({ trx }) => {\n const contentTypes = Object.values(strapi.contentTypes) as Schema.ContentType[];\n const components = Object.values(strapi.components) as Schema.Component[];\n\n for (const model of [...contentTypes, ...components]) {\n const dbModel = strapi.db.metadata.get(model.uid);\n\n for (const attribute of Object.values(dbModel.attributes) as any) {\n /**\n * Only consider unidirectional relations\n */\n if (\n attribute.type !== 'relation' ||\n attribute.target !== uid ||\n attribute.inversedBy ||\n attribute.mappedBy\n ) {\n continue;\n }\n\n // TODO: joinColumn relations\n const joinTable = attribute.joinTable;\n if (!joinTable) {\n continue;\n }\n\n const { name: sourceColumnName } = joinTable.joinColumn;\n const { name: targetColumnName } = joinTable.inverseJoinColumn;\n\n /**\n * Load all relations that need to be updated\n */\n // NOTE: when the model has draft and publish, we can assume relation are only draft to draft & published to published\n const ids = oldVersions.map((entry) => entry.id);\n\n const oldVersionsRelations = await strapi.db\n .getConnection()\n .select('*')\n .from(joinTable.name)\n .whereIn(targetColumnName, ids)\n .transacting(trx);\n\n if (oldVersionsRelations.length > 0) {\n updates.push({ joinTable, relations: oldVersionsRelations });\n }\n\n /**\n * if publishing\n * if published version exists\n * updated published versions links\n * else\n * create link to newly published version\n *\n * if discarding\n * if published version link exists & not draft version link\n * create link to new draft version\n */\n\n if (!model.options?.draftAndPublish) {\n const ids = newVersions.map((entry) => entry.id);\n\n const newVersionsRelations = await strapi.db\n .getConnection()\n .select('*')\n .from(joinTable.name)\n .whereIn(targetColumnName, ids)\n .transacting(trx);\n\n if (newVersionsRelations.length > 0) {\n // when publishing a draft that doesn't have a published version yet,\n // copy the links to the draft over to the published version\n // when discarding a published version, if no drafts exists\n const discardToAdd = newVersionsRelations\n .filter((relation) => {\n const matchingOldVerion = oldVersionsRelations.find((oldRelation) => {\n return oldRelation[sourceColumnName] === relation[sourceColumnName];\n });\n\n return !matchingOldVerion;\n })\n .map(omit('id'));\n\n updates.push({ joinTable, relations: discardToAdd });\n }\n }\n }\n }\n });\n\n return updates;\n};\n\n/**\n * Updates uni directional relations to target the right entries when overriding published or draft entries.\n *\n * @param oldEntries The old entries that are being overridden\n * @param newEntries The new entries that are overriding the old ones\n * @param oldRelations The relations that were previously loaded with `load` @see load\n */\nconst sync = async (\n oldEntries: { id: string; locale: string }[],\n newEntries: { id: string; locale: string }[],\n oldRelations: { joinTable: any; relations: any[] }[]\n) => {\n /**\n * Create a map of old entry ids to new entry ids\n *\n * Will be used to update the relation target ids\n */\n const newEntryByLocale = keyBy('locale', newEntries);\n const oldEntriesMap = oldEntries.reduce(\n (acc, entry) => {\n const newEntry = newEntryByLocale[entry.locale];\n if (!newEntry) return acc;\n acc[entry.id] = newEntry.id;\n return acc;\n },\n {} as Record<string, string>\n );\n\n await strapi.db.transaction(async ({ trx }) => {\n // Iterate old relations that are deleted and insert the new ones\n for (const { joinTable, relations } of oldRelations) {\n // Update old ids with the new ones\n const column = joinTable.inverseJoinColumn.name;\n\n const newRelations = relations.map((relation) => {\n const newId = oldEntriesMap[relation[column]];\n return { ...relation, [column]: newId };\n });\n\n // Insert those relations into the join table\n await trx.batchInsert(joinTable.name, newRelations, 1000);\n }\n });\n};\n\nexport { load, sync };\n"],"names":["ids","omit","keyBy"],"mappings":";;;AAeA,MAAM,OAAO,OAAO,KAAsB,EAAE,aAAa,kBAA+B;AACtF,QAAM,UAAU,CAAA;AAGhB,QAAM,OAAO,GAAG,YAAY,OAAO,EAAE,UAAU;AAC7C,UAAM,eAAe,OAAO,OAAO,OAAO,YAAY;AACtD,UAAM,aAAa,OAAO,OAAO,OAAO,UAAU;AAElD,eAAW,SAAS,CAAC,GAAG,cAAc,GAAG,UAAU,GAAG;AACpD,YAAM,UAAU,OAAO,GAAG,SAAS,IAAI,MAAM,GAAG;AAEhD,iBAAW,aAAa,OAAO,OAAO,QAAQ,UAAU,GAAU;AAK9D,YAAA,UAAU,SAAS,cACnB,UAAU,WAAW,OACrB,UAAU,cACV,UAAU,UACV;AACA;AAAA,QACF;AAGA,cAAM,YAAY,UAAU;AAC5B,YAAI,CAAC,WAAW;AACd;AAAA,QACF;AAEA,cAAM,EAAE,MAAM,qBAAqB,UAAU;AAC7C,cAAM,EAAE,MAAM,qBAAqB,UAAU;AAM7C,cAAM,MAAM,YAAY,IAAI,CAAC,UAAU,MAAM,EAAE;AAE/C,cAAM,uBAAuB,MAAM,OAAO,GACvC,gBACA,OAAO,GAAG,EACV,KAAK,UAAU,IAAI,EACnB,QAAQ,kBAAkB,GAAG,EAC7B,YAAY,GAAG;AAEd,YAAA,qBAAqB,SAAS,GAAG;AACnC,kBAAQ,KAAK,EAAE,WAAW,WAAW,qBAAsB,CAAA;AAAA,QAC7D;AAcI,YAAA,CAAC,MAA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IAC1D;AAAA,EAAA,CACD;AACH;;;"}
@@ -0,0 +1,69 @@
1
+ import { omit, keyBy } from "lodash/fp";
2
+ const load = async (uid, { oldVersions, newVersions }) => {
3
+ const updates = [];
4
+ await strapi.db.transaction(async ({ trx }) => {
5
+ const contentTypes = Object.values(strapi.contentTypes);
6
+ const components = Object.values(strapi.components);
7
+ for (const model of [...contentTypes, ...components]) {
8
+ const dbModel = strapi.db.metadata.get(model.uid);
9
+ for (const attribute of Object.values(dbModel.attributes)) {
10
+ if (attribute.type !== "relation" || attribute.target !== uid || attribute.inversedBy || attribute.mappedBy) {
11
+ continue;
12
+ }
13
+ const joinTable = attribute.joinTable;
14
+ if (!joinTable) {
15
+ continue;
16
+ }
17
+ const { name: sourceColumnName } = joinTable.joinColumn;
18
+ const { name: targetColumnName } = joinTable.inverseJoinColumn;
19
+ const ids = oldVersions.map((entry) => entry.id);
20
+ const oldVersionsRelations = await strapi.db.getConnection().select("*").from(joinTable.name).whereIn(targetColumnName, ids).transacting(trx);
21
+ if (oldVersionsRelations.length > 0) {
22
+ updates.push({ joinTable, relations: oldVersionsRelations });
23
+ }
24
+ if (!model.options?.draftAndPublish) {
25
+ const ids2 = newVersions.map((entry) => entry.id);
26
+ const newVersionsRelations = await strapi.db.getConnection().select("*").from(joinTable.name).whereIn(targetColumnName, ids2).transacting(trx);
27
+ if (newVersionsRelations.length > 0) {
28
+ const discardToAdd = newVersionsRelations.filter((relation) => {
29
+ const matchingOldVerion = oldVersionsRelations.find((oldRelation) => {
30
+ return oldRelation[sourceColumnName] === relation[sourceColumnName];
31
+ });
32
+ return !matchingOldVerion;
33
+ }).map(omit("id"));
34
+ updates.push({ joinTable, relations: discardToAdd });
35
+ }
36
+ }
37
+ }
38
+ }
39
+ });
40
+ return updates;
41
+ };
42
+ const sync = async (oldEntries, newEntries, oldRelations) => {
43
+ const newEntryByLocale = keyBy("locale", newEntries);
44
+ const oldEntriesMap = oldEntries.reduce(
45
+ (acc, entry) => {
46
+ const newEntry = newEntryByLocale[entry.locale];
47
+ if (!newEntry)
48
+ return acc;
49
+ acc[entry.id] = newEntry.id;
50
+ return acc;
51
+ },
52
+ {}
53
+ );
54
+ await strapi.db.transaction(async ({ trx }) => {
55
+ for (const { joinTable, relations } of oldRelations) {
56
+ const column = joinTable.inverseJoinColumn.name;
57
+ const newRelations = relations.map((relation) => {
58
+ const newId = oldEntriesMap[relation[column]];
59
+ return { ...relation, [column]: newId };
60
+ });
61
+ await trx.batchInsert(joinTable.name, newRelations, 1e3);
62
+ }
63
+ });
64
+ };
65
+ export {
66
+ load,
67
+ sync
68
+ };
69
+ //# sourceMappingURL=unidirectional-relations.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"unidirectional-relations.mjs","sources":["../../../../src/services/document-service/utils/unidirectional-relations.ts"],"sourcesContent":["/* eslint-disable no-continue */\nimport { keyBy, omit } from 'lodash/fp';\n\nimport { UID, Schema } from '@strapi/types';\n\ninterface LoadContext {\n oldVersions: { id: string; locale: string }[];\n newVersions: { id: string; locale: string }[];\n}\n\n/**\n * Loads lingering relations that need to be updated when overriding a published or draft entry.\n * This is necessary because the relations are uni-directional and the target entry is not aware of the source entry.\n * This is not the case for bi-directional relations, where the target entry is also linked to the source entry.\n */\nconst load = async (uid: UID.ContentType, { oldVersions, newVersions }: LoadContext) => {\n const updates = [] as any;\n\n // Iterate all components and content types to find relations that need to be updated\n await strapi.db.transaction(async ({ trx }) => {\n const contentTypes = Object.values(strapi.contentTypes) as Schema.ContentType[];\n const components = Object.values(strapi.components) as Schema.Component[];\n\n for (const model of [...contentTypes, ...components]) {\n const dbModel = strapi.db.metadata.get(model.uid);\n\n for (const attribute of Object.values(dbModel.attributes) as any) {\n /**\n * Only consider unidirectional relations\n */\n if (\n attribute.type !== 'relation' ||\n attribute.target !== uid ||\n attribute.inversedBy ||\n attribute.mappedBy\n ) {\n continue;\n }\n\n // TODO: joinColumn relations\n const joinTable = attribute.joinTable;\n if (!joinTable) {\n continue;\n }\n\n const { name: sourceColumnName } = joinTable.joinColumn;\n const { name: targetColumnName } = joinTable.inverseJoinColumn;\n\n /**\n * Load all relations that need to be updated\n */\n // NOTE: when the model has draft and publish, we can assume relation are only draft to draft & published to published\n const ids = oldVersions.map((entry) => entry.id);\n\n const oldVersionsRelations = await strapi.db\n .getConnection()\n .select('*')\n .from(joinTable.name)\n .whereIn(targetColumnName, ids)\n .transacting(trx);\n\n if (oldVersionsRelations.length > 0) {\n updates.push({ joinTable, relations: oldVersionsRelations });\n }\n\n /**\n * if publishing\n * if published version exists\n * updated published versions links\n * else\n * create link to newly published version\n *\n * if discarding\n * if published version link exists & not draft version link\n * create link to new draft version\n */\n\n if (!model.options?.draftAndPublish) {\n const ids = newVersions.map((entry) => entry.id);\n\n const newVersionsRelations = await strapi.db\n .getConnection()\n .select('*')\n .from(joinTable.name)\n .whereIn(targetColumnName, ids)\n .transacting(trx);\n\n if (newVersionsRelations.length > 0) {\n // when publishing a draft that doesn't have a published version yet,\n // copy the links to the draft over to the published version\n // when discarding a published version, if no drafts exists\n const discardToAdd = newVersionsRelations\n .filter((relation) => {\n const matchingOldVerion = oldVersionsRelations.find((oldRelation) => {\n return oldRelation[sourceColumnName] === relation[sourceColumnName];\n });\n\n return !matchingOldVerion;\n })\n .map(omit('id'));\n\n updates.push({ joinTable, relations: discardToAdd });\n }\n }\n }\n }\n });\n\n return updates;\n};\n\n/**\n * Updates uni directional relations to target the right entries when overriding published or draft entries.\n *\n * @param oldEntries The old entries that are being overridden\n * @param newEntries The new entries that are overriding the old ones\n * @param oldRelations The relations that were previously loaded with `load` @see load\n */\nconst sync = async (\n oldEntries: { id: string; locale: string }[],\n newEntries: { id: string; locale: string }[],\n oldRelations: { joinTable: any; relations: any[] }[]\n) => {\n /**\n * Create a map of old entry ids to new entry ids\n *\n * Will be used to update the relation target ids\n */\n const newEntryByLocale = keyBy('locale', newEntries);\n const oldEntriesMap = oldEntries.reduce(\n (acc, entry) => {\n const newEntry = newEntryByLocale[entry.locale];\n if (!newEntry) return acc;\n acc[entry.id] = newEntry.id;\n return acc;\n },\n {} as Record<string, string>\n );\n\n await strapi.db.transaction(async ({ trx }) => {\n // Iterate old relations that are deleted and insert the new ones\n for (const { joinTable, relations } of oldRelations) {\n // Update old ids with the new ones\n const column = joinTable.inverseJoinColumn.name;\n\n const newRelations = relations.map((relation) => {\n const newId = oldEntriesMap[relation[column]];\n return { ...relation, [column]: newId };\n });\n\n // Insert those relations into the join table\n await trx.batchInsert(joinTable.name, newRelations, 1000);\n }\n });\n};\n\nexport { load, sync };\n"],"names":["ids"],"mappings":";AAeA,MAAM,OAAO,OAAO,KAAsB,EAAE,aAAa,kBAA+B;AACtF,QAAM,UAAU,CAAA;AAGhB,QAAM,OAAO,GAAG,YAAY,OAAO,EAAE,UAAU;AAC7C,UAAM,eAAe,OAAO,OAAO,OAAO,YAAY;AACtD,UAAM,aAAa,OAAO,OAAO,OAAO,UAAU;AAElD,eAAW,SAAS,CAAC,GAAG,cAAc,GAAG,UAAU,GAAG;AACpD,YAAM,UAAU,OAAO,GAAG,SAAS,IAAI,MAAM,GAAG;AAEhD,iBAAW,aAAa,OAAO,OAAO,QAAQ,UAAU,GAAU;AAK9D,YAAA,UAAU,SAAS,cACnB,UAAU,WAAW,OACrB,UAAU,cACV,UAAU,UACV;AACA;AAAA,QACF;AAGA,cAAM,YAAY,UAAU;AAC5B,YAAI,CAAC,WAAW;AACd;AAAA,QACF;AAEA,cAAM,EAAE,MAAM,qBAAqB,UAAU;AAC7C,cAAM,EAAE,MAAM,qBAAqB,UAAU;AAM7C,cAAM,MAAM,YAAY,IAAI,CAAC,UAAU,MAAM,EAAE;AAE/C,cAAM,uBAAuB,MAAM,OAAO,GACvC,gBACA,OAAO,GAAG,EACV,KAAK,UAAU,IAAI,EACnB,QAAQ,kBAAkB,GAAG,EAC7B,YAAY,GAAG;AAEd,YAAA,qBAAqB,SAAS,GAAG;AACnC,kBAAQ,KAAK,EAAE,WAAW,WAAW,qBAAsB,CAAA;A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MAAM,cAAc,GAAI;AAAA,IAC1D;AAAA,EAAA,CACD;AACH;"}
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/services/entity-validator/index.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAKH,OAAO,EAAE,OAAO,EAAO,MAAM,EAAE,MAAM,EAAE,MAAM,eAAe,CAAC;AAa7D,MAAM,MAAM,gBAAgB,GAAG;IAC7B,aAAa,EAAE;QAEb,KAAK,EAAE,MAAM,CAAC,MAAM,CAAC;QAErB,EAAE,CAAC,EAAE,MAAM,CAAC;QAGZ,OAAO,CAAC,EAAE,gBAAgB,CAAC;KAC5B,CAAC;IAEF,eAAe,EAAE,MAAM,EAAE,CAAC;IAG1B,cAAc,EAAE,OAAO,CAAC,eAAe,CAAC,MAAM,EAAE,CAAC;IACjD,sBAAsB,CAAC,EAAE,MAAM,CAAC,SAAS,CAAC,KAAK,CAAC,MAAM,CAAC,SAAS,CAAC,WAAW,CAAC,CAAC;CAC/E,CAAC;AAWF,UAAU,gBAAgB;IACxB,OAAO,CAAC,EAAE,OAAO,CAAC;IAClB,MAAM,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;CACxB;AA+gBD,QAAA,MAAM,eAAe,EAAE,OAAO,CAAC,eAAe,CAAC,eAG9C,CAAC;AAEF,eAAe,eAAe,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/services/entity-validator/index.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAKH,OAAO,EAAE,OAAO,EAAO,MAAM,EAAE,MAAM,EAAE,MAAM,eAAe,CAAC;AAa7D,MAAM,MAAM,gBAAgB,GAAG;IAC7B,aAAa,EAAE;QAEb,KAAK,EAAE,MAAM,CAAC,MAAM,CAAC;QAErB,EAAE,CAAC,EAAE,MAAM,CAAC;QAGZ,OAAO,CAAC,EAAE,gBAAgB,CAAC;KAC5B,CAAC;IAEF,eAAe,EAAE,MAAM,EAAE,CAAC;IAG1B,cAAc,EAAE,OAAO,CAAC,eAAe,CAAC,MAAM,EAAE,CAAC;IACjD,sBAAsB,CAAC,EAAE,MAAM,CAAC,SAAS,CAAC,KAAK,CAAC,MAAM,CAAC,SAAS,CAAC,WAAW,CAAC,CAAC;CAC/E,CAAC;AAWF,UAAU,gBAAgB;IACxB,OAAO,CAAC,EAAE,OAAO,CAAC;IAClB,MAAM,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;CACxB;AAmhBD,QAAA,MAAM,eAAe,EAAE,OAAO,CAAC,eAAe,CAAC,eAG9C,CAAC;AAEF,eAAe,eAAe,CAAC"}
@@ -77,7 +77,9 @@ const createComponentValidator = (createOrUpdate) => ({
77
77
  attr: { required: true },
78
78
  updatedAttribute
79
79
  });
80
- validator2 = addMinMax(validator2, { attr, updatedAttribute });
80
+ if (!isDraft) {
81
+ validator2 = addMinMax(validator2, { attr, updatedAttribute });
82
+ }
81
83
  return validator2;
82
84
  }
83
85
  let validator = createModelValidator(createOrUpdate)(
@@ -115,7 +117,9 @@ const createDzValidator = (createOrUpdate) => ({ attr, updatedAttribute, compone
115
117
  attr: { required: true },
116
118
  updatedAttribute
117
119
  });
118
- validator = addMinMax(validator, { attr, updatedAttribute });
120
+ if (!isDraft) {
121
+ validator = addMinMax(validator, { attr, updatedAttribute });
122
+ }
119
123
  return validator;
120
124
  };
121
125
  const createRelationValidator = ({
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sources":["../../../src/services/entity-validator/index.ts"],"sourcesContent":["/**\n * Entity validator\n * Module that will validate input data for entity creation or edition\n */\n\nimport { uniqBy, castArray, isNil, isArray, mergeWith } from 'lodash';\nimport { has, prop, isObject, isEmpty } from 'lodash/fp';\nimport strapiUtils from '@strapi/utils';\nimport { Modules, UID, Struct, Schema } from '@strapi/types';\nimport { Validators, ValidatorMetas } from './validators';\n\ntype CreateOrUpdate = 'creation' | 'update';\n\nconst { yup, validateYupSchema } = strapiUtils;\nconst { isMediaAttribute, isScalarAttribute, getWritableAttributes } = strapiUtils.contentTypes;\nconst { ValidationError } = strapiUtils.errors;\n\ntype ID = { id: string | number };\n\ntype RelationSource = string | number | ID;\n\nexport type ComponentContext = {\n parentContent: {\n // The model of the parent content type that contains the current component.\n model: Struct.Schema;\n // The numeric id of the parent entity that contains the component.\n id?: number;\n // The options passed to the entity validator. From which we can extract\n // entity dimensions such as locale and publication state.\n options?: ValidatorContext;\n };\n // The path to the component within the parent content type schema.\n pathToComponent: string[];\n // If working with a repeatable component this contains the\n // full data of the repeatable component in the current entity.\n repeatableData: Modules.EntityValidator.Entity[];\n fullDynamicZoneContent?: Schema.Attribute.Value<Schema.Attribute.DynamicZone>;\n};\n\ninterface WithComponentContext {\n componentContext?: ComponentContext;\n}\n\ninterface ValidatorMeta<TAttribute = Schema.Attribute.AnyAttribute> extends WithComponentContext {\n attr: TAttribute;\n updatedAttribute: { name: string; value: any };\n}\n\ninterface ValidatorContext {\n isDraft?: boolean;\n locale?: string | null;\n}\n\ninterface ModelValidatorMetas extends WithComponentContext {\n model: Struct.Schema;\n data: Record<string, unknown>;\n entity?: Modules.EntityValidator.Entity;\n}\n\nconst isInteger = (value: unknown): value is number => Number.isInteger(value);\n\nconst addMinMax = <\n T extends {\n min(value: number): T;\n max(value: number): T;\n },\n>(\n validator: T,\n {\n attr,\n updatedAttribute,\n }: ValidatorMeta<Schema.Attribute.AnyAttribute & Schema.Attribute.MinMaxOption<string | number>>\n): T => {\n let nextValidator: T = validator;\n\n if (\n isInteger(attr.min) &&\n (('required' in attr && attr.required) ||\n (Array.isArray(updatedAttribute.value) && updatedAttribute.value.length > 0))\n ) {\n nextValidator = nextValidator.min(attr.min);\n }\n if (isInteger(attr.max)) {\n nextValidator = nextValidator.max(attr.max);\n }\n return nextValidator;\n};\n\nconst addRequiredValidation = (createOrUpdate: CreateOrUpdate) => {\n return <T extends strapiUtils.yup.AnySchema>(\n validator: T,\n {\n attr: { required },\n }: ValidatorMeta<Partial<Schema.Attribute.AnyAttribute & Schema.Attribute.RequiredOption>>\n ): T => {\n let nextValidator = validator;\n\n if (required) {\n if (createOrUpdate === 'creation') {\n nextValidator = nextValidator.notNil();\n } else if (createOrUpdate === 'update') {\n nextValidator = nextValidator.notNull();\n }\n } else {\n nextValidator = nextValidator.nullable();\n }\n return nextValidator;\n };\n};\n\nconst addDefault = (createOrUpdate: CreateOrUpdate) => {\n return (\n validator: strapiUtils.yup.BaseSchema,\n { attr }: ValidatorMeta<Schema.Attribute.AnyAttribute & Schema.Attribute.DefaultOption<unknown>>\n ) => {\n let nextValidator = validator;\n\n if (createOrUpdate === 'creation') {\n if (\n ((attr.type === 'component' && attr.repeatable) || attr.type === 'dynamiczone') &&\n !attr.required\n ) {\n nextValidator = nextValidator.default([]);\n } else {\n nextValidator = nextValidator.default(attr.default);\n }\n } else {\n nextValidator = nextValidator.default(undefined);\n }\n\n return nextValidator;\n };\n};\n\nconst preventCast = (validator: strapiUtils.yup.AnySchema) =>\n validator.transform((val, originalVal) => originalVal);\n\nconst createComponentValidator =\n (createOrUpdate: CreateOrUpdate) =>\n (\n {\n attr,\n updatedAttribute,\n componentContext,\n }: ValidatorMeta<Schema.Attribute.Component<UID.Component, boolean>>,\n { isDraft }: ValidatorContext\n ) => {\n const model = strapi.getModel(attr.component);\n if (!model) {\n throw new Error('Validation failed: Model not found');\n }\n\n if (attr?.repeatable) {\n // FIXME: yup v1\n\n let validator = yup\n .array()\n .of(\n yup.lazy((item) =>\n createModelValidator(createOrUpdate)(\n { componentContext, model, data: item },\n { isDraft }\n ).notNull()\n ) as any\n );\n\n validator = addRequiredValidation(createOrUpdate)(validator, {\n attr: { required: true },\n updatedAttribute,\n });\n\n validator = addMinMax(validator, { attr, updatedAttribute });\n\n return validator;\n }\n\n let validator = createModelValidator(createOrUpdate)(\n {\n model,\n data: updatedAttribute.value,\n componentContext,\n },\n { isDraft }\n );\n\n validator = addRequiredValidation(createOrUpdate)(validator, {\n attr: { required: !isDraft && attr.required },\n updatedAttribute,\n });\n\n return validator;\n };\n\nconst createDzValidator =\n (createOrUpdate: CreateOrUpdate) =>\n ({ attr, updatedAttribute, componentContext }: ValidatorMeta, { isDraft }: ValidatorContext) => {\n let validator;\n\n validator = yup.array().of(\n yup.lazy((item) => {\n const model = strapi.getModel(prop('__component', item));\n const schema = yup\n .object()\n .shape({\n __component: yup.string().required().oneOf(Object.keys(strapi.components)),\n })\n .notNull();\n\n return model\n ? schema.concat(\n createModelValidator(createOrUpdate)(\n { model, data: item, componentContext },\n { isDraft }\n )\n )\n : schema;\n }) as any // FIXME: yup v1\n );\n\n validator = addRequiredValidation(createOrUpdate)(validator, {\n attr: { required: true },\n updatedAttribute,\n });\n\n validator = addMinMax(validator, { attr, updatedAttribute });\n\n return validator;\n };\n\nconst createRelationValidator = ({\n updatedAttribute,\n}: ValidatorMeta<Schema.Attribute.Relation>) => {\n let validator;\n\n if (Array.isArray(updatedAttribute.value)) {\n validator = yup.array().of(yup.mixed());\n } else {\n validator = yup.mixed();\n }\n\n return validator;\n};\n\nconst createScalarAttributeValidator =\n (createOrUpdate: CreateOrUpdate) => (metas: ValidatorMeta, options: ValidatorContext) => {\n let validator;\n\n if (has(metas.attr.type, Validators)) {\n validator = (Validators as any)[metas.attr.type](metas, options);\n } else {\n // No validators specified - fall back to mixed\n validator = yup.mixed();\n }\n\n validator = addRequiredValidation(createOrUpdate)(validator, {\n attr: { required: !options.isDraft && metas.attr.required },\n updatedAttribute: metas.updatedAttribute,\n });\n\n return validator;\n };\n\nconst createAttributeValidator =\n (createOrUpdate: CreateOrUpdate) => (metas: ValidatorMetas, options: ValidatorContext) => {\n let validator = yup.mixed();\n\n if (isMediaAttribute(metas.attr)) {\n validator = yup.mixed();\n } else if (isScalarAttribute(metas.attr)) {\n validator = createScalarAttributeValidator(createOrUpdate)(metas, options);\n } else {\n if (metas.attr.type === 'component' && metas.componentContext) {\n // Build the path to the component within the parent content type schema.\n const pathToComponent = [\n ...(metas?.componentContext?.pathToComponent ?? []),\n metas.updatedAttribute.name,\n ];\n\n // If working with a repeatable component, determine the repeatable data\n // based on the component's path.\n\n // In order to validate the repeatable within this entity we need\n // access to the full repeatable data. In case we are validating a\n // nested component within a repeatable.\n // Hence why we set this up when the path to the component is only one level deep.\n const repeatableData = (\n metas.attr.repeatable && pathToComponent.length === 1\n ? metas.updatedAttribute.value\n : metas.componentContext?.repeatableData\n ) as Modules.EntityValidator.Entity[];\n\n const newComponentContext: ComponentContext = {\n ...metas.componentContext,\n pathToComponent,\n repeatableData,\n };\n\n validator = createComponentValidator(createOrUpdate)(\n {\n componentContext: newComponentContext,\n attr: metas.attr,\n updatedAttribute: metas.updatedAttribute,\n },\n options\n );\n } else if (metas.attr.type === 'dynamiczone' && metas.componentContext) {\n const newComponentContext: ComponentContext = {\n ...metas.componentContext,\n fullDynamicZoneContent: metas.updatedAttribute.value,\n pathToComponent: [...metas.componentContext.pathToComponent, metas.updatedAttribute.name],\n };\n\n Object.assign(metas, { componentContext: newComponentContext });\n\n validator = createDzValidator(createOrUpdate)(metas, options);\n } else if (metas.attr.type === 'relation') {\n validator = createRelationValidator({\n attr: metas.attr,\n updatedAttribute: metas.updatedAttribute,\n });\n }\n\n validator = preventCast(validator);\n }\n\n validator = addDefault(createOrUpdate)(validator, metas);\n\n return validator;\n };\n\nconst createModelValidator =\n (createOrUpdate: CreateOrUpdate) =>\n ({ componentContext, model, data, entity }: ModelValidatorMetas, options: ValidatorContext) => {\n const writableAttributes = model ? getWritableAttributes(model as any) : [];\n\n const schema = writableAttributes.reduce(\n (validators, attributeName) => {\n const metas = {\n attr: model.attributes[attributeName],\n updatedAttribute: { name: attributeName, value: prop(attributeName, data) },\n model,\n entity,\n componentContext,\n };\n\n const validator = createAttributeValidator(createOrUpdate)(metas, options);\n\n validators[attributeName] = validator;\n\n return validators;\n },\n {} as Record<string, strapiUtils.yup.BaseSchema>\n );\n\n return yup.object().shape(schema);\n };\n\nconst createValidateEntity = (createOrUpdate: CreateOrUpdate) => {\n return async <\n TUID extends UID.ContentType,\n TData extends Modules.EntityService.Params.Data.Input<TUID>,\n >(\n model: Schema.ContentType<TUID>,\n data: TData | Partial<TData> | undefined,\n options?: ValidatorContext,\n entity?: Modules.EntityValidator.Entity\n ): Promise<TData> => {\n if (!isObject(data)) {\n const { displayName } = model.info;\n\n throw new ValidationError(\n `Invalid payload submitted for the ${createOrUpdate} of an entity of type ${displayName}. Expected an object, but got ${typeof data}`\n );\n }\n\n const validator = createModelValidator(createOrUpdate)(\n {\n model,\n data,\n entity,\n componentContext: {\n // Set up the initial component context.\n // Keeping track of parent content type context in which a component will be used.\n // This is necessary to validate component field constraints such as uniqueness.\n parentContent: {\n id: entity?.id,\n model,\n options,\n },\n pathToComponent: [],\n repeatableData: [],\n },\n },\n {\n isDraft: options?.isDraft ?? false,\n locale: options?.locale ?? null,\n }\n )\n .test(\n 'relations-test',\n 'check that all relations exist',\n async function relationsValidation(data) {\n try {\n await checkRelationsExist(buildRelationsStore({ uid: model.uid, data }));\n } catch (e) {\n return this.createError({\n path: this.path,\n message: (e instanceof ValidationError && e.message) || 'Invalid relations',\n });\n }\n return true;\n }\n )\n .required();\n\n return validateYupSchema(validator, {\n strict: false,\n abortEarly: false,\n })(data);\n };\n};\n\n/**\n * Builds an object containing all the media and relations being associated with an entity\n */\nconst buildRelationsStore = <TUID extends UID.Schema>({\n uid,\n data,\n}: {\n uid: TUID;\n data: Record<string, unknown> | null;\n}): Record<string, ID[]> => {\n if (!uid) {\n throw new ValidationError(`Cannot build relations store: \"uid\" is undefined`);\n }\n\n if (isEmpty(data)) {\n return {};\n }\n\n const currentModel = strapi.getModel(uid);\n\n return Object.keys(currentModel.attributes).reduce(\n (result, attributeName: string) => {\n const attribute = currentModel.attributes[attributeName];\n const value = data[attributeName];\n\n if (isNil(value)) {\n return result;\n }\n\n switch (attribute.type) {\n case 'relation':\n case 'media': {\n if (\n attribute.type === 'relation' &&\n (attribute.relation === 'morphToMany' || attribute.relation === 'morphToOne')\n ) {\n // TODO: handle polymorphic relations\n break;\n }\n\n const target =\n // eslint-disable-next-line no-nested-ternary\n attribute.type === 'media' ? 'plugin::upload.file' : attribute.target;\n // As there are multiple formats supported for associating relations\n // with an entity, the value here can be an: array, object or number.\n let source: RelationSource[];\n if (Array.isArray(value)) {\n source = value;\n } else if (isObject(value)) {\n if ('connect' in value && !isNil(value.connect)) {\n source = value.connect as RelationSource[];\n } else if ('set' in value && !isNil(value.set)) {\n source = value.set as RelationSource[];\n } else {\n source = [];\n }\n } else {\n source = castArray(value as RelationSource);\n }\n const idArray = source.map((v) => ({\n id: typeof v === 'object' ? v.id : v,\n }));\n\n // Update the relationStore to keep track of all associations being made\n // with relations and media.\n result[target] = result[target] || [];\n result[target].push(...idArray);\n break;\n }\n case 'component': {\n return castArray(value).reduce((relationsStore, componentValue) => {\n if (!attribute.component) {\n throw new ValidationError(\n `Cannot build relations store from component, component identifier is undefined`\n );\n }\n\n return mergeWith(\n relationsStore,\n buildRelationsStore({\n uid: attribute.component,\n data: componentValue as Record<string, unknown>,\n }),\n (objValue, srcValue) => {\n if (isArray(objValue)) {\n return objValue.concat(srcValue);\n }\n }\n );\n }, result) as Record<string, ID[]>;\n }\n case 'dynamiczone': {\n return castArray(value).reduce((relationsStore, dzValue) => {\n const value = dzValue as Record<string, unknown>;\n if (!value.__component) {\n throw new ValidationError(\n `Cannot build relations store from dynamiczone, component identifier is undefined`\n );\n }\n\n return mergeWith(\n relationsStore,\n buildRelationsStore({\n uid: value.__component as UID.Component,\n data: value,\n }),\n (objValue, srcValue) => {\n if (isArray(objValue)) {\n return objValue.concat(srcValue);\n }\n }\n );\n }, result) as Record<string, ID[]>;\n }\n default:\n break;\n }\n\n return result;\n },\n {} as Record<string, ID[]>\n );\n};\n\n/**\n * Iterate through the relations store and validates that every relation or media\n * mentioned exists\n */\nconst checkRelationsExist = async (relationsStore: Record<string, ID[]> = {}) => {\n const promises: Promise<void>[] = [];\n\n for (const [key, value] of Object.entries(relationsStore)) {\n const evaluate = async () => {\n const uniqueValues = uniqBy(value, `id`);\n const count = await strapi.db.query(key as UID.Schema).count({\n where: {\n id: {\n $in: uniqueValues.map((v) => v.id),\n },\n },\n });\n\n if (count !== uniqueValues.length) {\n throw new ValidationError(\n `${\n uniqueValues.length - count\n } relation(s) of type ${key} associated with this entity do not exist`\n );\n }\n };\n promises.push(evaluate());\n }\n\n return Promise.all(promises);\n};\n\nconst entityValidator: Modules.EntityValidator.EntityValidator = {\n validateEntityCreation: createValidateEntity('creation'),\n validateEntityUpdate: createValidateEntity('update'),\n};\n\nexport default entityValidator;\n"],"names":["strapiUtils","validator","prop","has","Validators","validators","isObject","data","isEmpty","isNil","castArray","mergeWith","isArray","value","uniqBy"],"mappings":";;;;;;;AAaA,MAAM,EAAE,KAAK,kBAAsB,IAAAA;AACnC,MAAM,EAAE,kBAAkB,mBAAmB,sBAAA,IAA0BA,qBAAAA,QAAY;AACnF,MAAM,EAAE,gBAAgB,IAAIA,qBAAY,QAAA;AA4CxC,MAAM,YAAY,CAAC,UAAoC,OAAO,UAAU,KAAK;AAE7E,MAAM,YAAY,CAMhB,WACA;AAAA,EACE;AAAA,EACA;AACF,MACM;AACN,MAAI,gBAAmB;AAEvB,MACE,UAAU,KAAK,GAAG,MAChB,cAAc,QAAQ,KAAK,YAC1B,MAAM,QAAQ,iBAAiB,KAAK,KAAK,iBAAiB,MAAM,SAAS,IAC5E;AACgB,oBAAA,cAAc,IAAI,KAAK,GAAG;AAAA,EAC5C;AACI,MAAA,UAAU,KAAK,GAAG,GAAG;AACP,oBAAA,cAAc,IAAI,KAAK,GAAG;AAAA,EAC5C;AACO,SAAA;AACT;AAEA,MAAM,wBAAwB,CAAC,mBAAmC;AAChE,SAAO,CACL,WACA;AAAA,IACE,MAAM,EAAE,SAAS;AAAA,EAAA,MAEb;AACN,QAAI,gBAAgB;AAEpB,QAAI,UAAU;AACZ,UAAI,mBAAmB,YAAY;AACjC,wBAAgB,cAAc;MAAO,WAC5B,mBAAmB,UAAU;AACtC,wBAAgB,cAAc;MAChC;AAAA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,QAAQ;AACrD;;"}
1
+ {"version":3,"file":"index.js","sources":["../../../src/services/entity-validator/index.ts"],"sourcesContent":["/**\n * Entity validator\n * Module that will validate input data for entity creation or edition\n */\n\nimport { uniqBy, castArray, isNil, isArray, mergeWith } from 'lodash';\nimport { has, prop, isObject, isEmpty } from 'lodash/fp';\nimport strapiUtils from '@strapi/utils';\nimport { Modules, UID, Struct, Schema } from '@strapi/types';\nimport { Validators, ValidatorMetas } from './validators';\n\ntype CreateOrUpdate = 'creation' | 'update';\n\nconst { yup, validateYupSchema } = strapiUtils;\nconst { isMediaAttribute, isScalarAttribute, getWritableAttributes } = strapiUtils.contentTypes;\nconst { ValidationError } = strapiUtils.errors;\n\ntype ID = { id: string | number };\n\ntype RelationSource = string | number | ID;\n\nexport type ComponentContext = {\n parentContent: {\n // The model of the parent content type that contains the current component.\n model: Struct.Schema;\n // The numeric id of the parent entity that contains the component.\n id?: number;\n // The options passed to the entity validator. From which we can extract\n // entity dimensions such as locale and publication state.\n options?: ValidatorContext;\n };\n // The path to the component within the parent content type schema.\n pathToComponent: string[];\n // If working with a repeatable component this contains the\n // full data of the repeatable component in the current entity.\n repeatableData: Modules.EntityValidator.Entity[];\n fullDynamicZoneContent?: Schema.Attribute.Value<Schema.Attribute.DynamicZone>;\n};\n\ninterface WithComponentContext {\n componentContext?: ComponentContext;\n}\n\ninterface ValidatorMeta<TAttribute = Schema.Attribute.AnyAttribute> extends WithComponentContext {\n attr: TAttribute;\n updatedAttribute: { name: string; value: any };\n}\n\ninterface ValidatorContext {\n isDraft?: boolean;\n locale?: string | null;\n}\n\ninterface ModelValidatorMetas extends WithComponentContext {\n model: Struct.Schema;\n data: Record<string, unknown>;\n entity?: Modules.EntityValidator.Entity;\n}\n\nconst isInteger = (value: unknown): value is number => Number.isInteger(value);\n\nconst addMinMax = <\n T extends {\n min(value: number): T;\n max(value: number): T;\n },\n>(\n validator: T,\n {\n attr,\n updatedAttribute,\n }: ValidatorMeta<Schema.Attribute.AnyAttribute & Schema.Attribute.MinMaxOption<string | number>>\n): T => {\n let nextValidator: T = validator;\n\n if (\n isInteger(attr.min) &&\n (('required' in attr && attr.required) ||\n (Array.isArray(updatedAttribute.value) && updatedAttribute.value.length > 0))\n ) {\n nextValidator = nextValidator.min(attr.min);\n }\n if (isInteger(attr.max)) {\n nextValidator = nextValidator.max(attr.max);\n }\n return nextValidator;\n};\n\nconst addRequiredValidation = (createOrUpdate: CreateOrUpdate) => {\n return <T extends strapiUtils.yup.AnySchema>(\n validator: T,\n {\n attr: { required },\n }: ValidatorMeta<Partial<Schema.Attribute.AnyAttribute & Schema.Attribute.RequiredOption>>\n ): T => {\n let nextValidator = validator;\n\n if (required) {\n if (createOrUpdate === 'creation') {\n nextValidator = nextValidator.notNil();\n } else if (createOrUpdate === 'update') {\n nextValidator = nextValidator.notNull();\n }\n } else {\n nextValidator = nextValidator.nullable();\n }\n return nextValidator;\n };\n};\n\nconst addDefault = (createOrUpdate: CreateOrUpdate) => {\n return (\n validator: strapiUtils.yup.BaseSchema,\n { attr }: ValidatorMeta<Schema.Attribute.AnyAttribute & Schema.Attribute.DefaultOption<unknown>>\n ) => {\n let nextValidator = validator;\n\n if (createOrUpdate === 'creation') {\n if (\n ((attr.type === 'component' && attr.repeatable) || attr.type === 'dynamiczone') &&\n !attr.required\n ) {\n nextValidator = nextValidator.default([]);\n } else {\n nextValidator = nextValidator.default(attr.default);\n }\n } else {\n nextValidator = nextValidator.default(undefined);\n }\n\n return nextValidator;\n };\n};\n\nconst preventCast = (validator: strapiUtils.yup.AnySchema) =>\n validator.transform((val, originalVal) => originalVal);\n\nconst createComponentValidator =\n (createOrUpdate: CreateOrUpdate) =>\n (\n {\n attr,\n updatedAttribute,\n componentContext,\n }: ValidatorMeta<Schema.Attribute.Component<UID.Component, boolean>>,\n { isDraft }: ValidatorContext\n ) => {\n const model = strapi.getModel(attr.component);\n if (!model) {\n throw new Error('Validation failed: Model not found');\n }\n\n if (attr?.repeatable) {\n // FIXME: yup v1\n\n let validator = yup\n .array()\n .of(\n yup.lazy((item) =>\n createModelValidator(createOrUpdate)(\n { componentContext, model, data: item },\n { isDraft }\n ).notNull()\n ) as any\n );\n\n validator = addRequiredValidation(createOrUpdate)(validator, {\n attr: { required: true },\n updatedAttribute,\n });\n\n if (!isDraft) {\n validator = addMinMax(validator, { attr, updatedAttribute });\n }\n\n return validator;\n }\n\n let validator = createModelValidator(createOrUpdate)(\n {\n model,\n data: updatedAttribute.value,\n componentContext,\n },\n { isDraft }\n );\n\n validator = addRequiredValidation(createOrUpdate)(validator, {\n attr: { required: !isDraft && attr.required },\n updatedAttribute,\n });\n\n return validator;\n };\n\nconst createDzValidator =\n (createOrUpdate: CreateOrUpdate) =>\n ({ attr, updatedAttribute, componentContext }: ValidatorMeta, { isDraft }: ValidatorContext) => {\n let validator;\n\n validator = yup.array().of(\n yup.lazy((item) => {\n const model = strapi.getModel(prop('__component', item));\n const schema = yup\n .object()\n .shape({\n __component: yup.string().required().oneOf(Object.keys(strapi.components)),\n })\n .notNull();\n\n return model\n ? schema.concat(\n createModelValidator(createOrUpdate)(\n { model, data: item, componentContext },\n { isDraft }\n )\n )\n : schema;\n }) as any // FIXME: yup v1\n );\n\n validator = addRequiredValidation(createOrUpdate)(validator, {\n attr: { required: true },\n updatedAttribute,\n });\n\n if (!isDraft) {\n validator = addMinMax(validator, { attr, updatedAttribute });\n }\n\n return validator;\n };\n\nconst createRelationValidator = ({\n updatedAttribute,\n}: ValidatorMeta<Schema.Attribute.Relation>) => {\n let validator;\n\n if (Array.isArray(updatedAttribute.value)) {\n validator = yup.array().of(yup.mixed());\n } else {\n validator = yup.mixed();\n }\n\n return validator;\n};\n\nconst createScalarAttributeValidator =\n (createOrUpdate: CreateOrUpdate) => (metas: ValidatorMeta, options: ValidatorContext) => {\n let validator;\n\n if (has(metas.attr.type, Validators)) {\n validator = (Validators as any)[metas.attr.type](metas, options);\n } else {\n // No validators specified - fall back to mixed\n validator = yup.mixed();\n }\n\n validator = addRequiredValidation(createOrUpdate)(validator, {\n attr: { required: !options.isDraft && metas.attr.required },\n updatedAttribute: metas.updatedAttribute,\n });\n\n return validator;\n };\n\nconst createAttributeValidator =\n (createOrUpdate: CreateOrUpdate) => (metas: ValidatorMetas, options: ValidatorContext) => {\n let validator = yup.mixed();\n\n if (isMediaAttribute(metas.attr)) {\n validator = yup.mixed();\n } else if (isScalarAttribute(metas.attr)) {\n validator = createScalarAttributeValidator(createOrUpdate)(metas, options);\n } else {\n if (metas.attr.type === 'component' && metas.componentContext) {\n // Build the path to the component within the parent content type schema.\n const pathToComponent = [\n ...(metas?.componentContext?.pathToComponent ?? []),\n metas.updatedAttribute.name,\n ];\n\n // If working with a repeatable component, determine the repeatable data\n // based on the component's path.\n\n // In order to validate the repeatable within this entity we need\n // access to the full repeatable data. In case we are validating a\n // nested component within a repeatable.\n // Hence why we set this up when the path to the component is only one level deep.\n const repeatableData = (\n metas.attr.repeatable && pathToComponent.length === 1\n ? metas.updatedAttribute.value\n : metas.componentContext?.repeatableData\n ) as Modules.EntityValidator.Entity[];\n\n const newComponentContext: ComponentContext = {\n ...metas.componentContext,\n pathToComponent,\n repeatableData,\n };\n\n validator = createComponentValidator(createOrUpdate)(\n {\n componentContext: newComponentContext,\n attr: metas.attr,\n updatedAttribute: metas.updatedAttribute,\n },\n options\n );\n } else if (metas.attr.type === 'dynamiczone' && metas.componentContext) {\n const newComponentContext: ComponentContext = {\n ...metas.componentContext,\n fullDynamicZoneContent: metas.updatedAttribute.value,\n pathToComponent: [...metas.componentContext.pathToComponent, metas.updatedAttribute.name],\n };\n\n Object.assign(metas, { componentContext: newComponentContext });\n\n validator = createDzValidator(createOrUpdate)(metas, options);\n } else if (metas.attr.type === 'relation') {\n validator = createRelationValidator({\n attr: metas.attr,\n updatedAttribute: metas.updatedAttribute,\n });\n }\n\n validator = preventCast(validator);\n }\n\n validator = addDefault(createOrUpdate)(validator, metas);\n\n return validator;\n };\n\nconst createModelValidator =\n (createOrUpdate: CreateOrUpdate) =>\n ({ componentContext, model, data, entity }: ModelValidatorMetas, options: ValidatorContext) => {\n const writableAttributes = model ? getWritableAttributes(model as any) : [];\n\n const schema = writableAttributes.reduce(\n (validators, attributeName) => {\n const metas = {\n attr: model.attributes[attributeName],\n updatedAttribute: { name: attributeName, value: prop(attributeName, data) },\n model,\n entity,\n componentContext,\n };\n\n const validator = createAttributeValidator(createOrUpdate)(metas, options);\n\n validators[attributeName] = validator;\n\n return validators;\n },\n {} as Record<string, strapiUtils.yup.BaseSchema>\n );\n\n return yup.object().shape(schema);\n };\n\nconst createValidateEntity = (createOrUpdate: CreateOrUpdate) => {\n return async <\n TUID extends UID.ContentType,\n TData extends Modules.EntityService.Params.Data.Input<TUID>,\n >(\n model: Schema.ContentType<TUID>,\n data: TData | Partial<TData> | undefined,\n options?: ValidatorContext,\n entity?: Modules.EntityValidator.Entity\n ): Promise<TData> => {\n if (!isObject(data)) {\n const { displayName } = model.info;\n\n throw new ValidationError(\n `Invalid payload submitted for the ${createOrUpdate} of an entity of type ${displayName}. Expected an object, but got ${typeof data}`\n );\n }\n\n const validator = createModelValidator(createOrUpdate)(\n {\n model,\n data,\n entity,\n componentContext: {\n // Set up the initial component context.\n // Keeping track of parent content type context in which a component will be used.\n // This is necessary to validate component field constraints such as uniqueness.\n parentContent: {\n id: entity?.id,\n model,\n options,\n },\n pathToComponent: [],\n repeatableData: [],\n },\n },\n {\n isDraft: options?.isDraft ?? false,\n locale: options?.locale ?? null,\n }\n )\n .test(\n 'relations-test',\n 'check that all relations exist',\n async function relationsValidation(data) {\n try {\n await checkRelationsExist(buildRelationsStore({ uid: model.uid, data }));\n } catch (e) {\n return this.createError({\n path: this.path,\n message: (e instanceof ValidationError && e.message) || 'Invalid relations',\n });\n }\n return true;\n }\n )\n .required();\n\n return validateYupSchema(validator, {\n strict: false,\n abortEarly: false,\n })(data);\n };\n};\n\n/**\n * Builds an object containing all the media and relations being associated with an entity\n */\nconst buildRelationsStore = <TUID extends UID.Schema>({\n uid,\n data,\n}: {\n uid: TUID;\n data: Record<string, unknown> | null;\n}): Record<string, ID[]> => {\n if (!uid) {\n throw new ValidationError(`Cannot build relations store: \"uid\" is undefined`);\n }\n\n if (isEmpty(data)) {\n return {};\n }\n\n const currentModel = strapi.getModel(uid);\n\n return Object.keys(currentModel.attributes).reduce(\n (result, attributeName: string) => {\n const attribute = currentModel.attributes[attributeName];\n const value = data[attributeName];\n\n if (isNil(value)) {\n return result;\n }\n\n switch (attribute.type) {\n case 'relation':\n case 'media': {\n if (\n attribute.type === 'relation' &&\n (attribute.relation === 'morphToMany' || attribute.relation === 'morphToOne')\n ) {\n // TODO: handle polymorphic relations\n break;\n }\n\n const target =\n // eslint-disable-next-line no-nested-ternary\n attribute.type === 'media' ? 'plugin::upload.file' : attribute.target;\n // As there are multiple formats supported for associating relations\n // with an entity, the value here can be an: array, object or number.\n let source: RelationSource[];\n if (Array.isArray(value)) {\n source = value;\n } else if (isObject(value)) {\n if ('connect' in value && !isNil(value.connect)) {\n source = value.connect as RelationSource[];\n } else if ('set' in value && !isNil(value.set)) {\n source = value.set as RelationSource[];\n } else {\n source = [];\n }\n } else {\n source = castArray(value as RelationSource);\n }\n const idArray = source.map((v) => ({\n id: typeof v === 'object' ? v.id : v,\n }));\n\n // Update the relationStore to keep track of all associations being made\n // with relations and media.\n result[target] = result[target] || [];\n result[target].push(...idArray);\n break;\n }\n case 'component': {\n return castArray(value).reduce((relationsStore, componentValue) => {\n if (!attribute.component) {\n throw new ValidationError(\n `Cannot build relations store from component, component identifier is undefined`\n );\n }\n\n return mergeWith(\n relationsStore,\n buildRelationsStore({\n uid: attribute.component,\n data: componentValue as Record<string, unknown>,\n }),\n (objValue, srcValue) => {\n if (isArray(objValue)) {\n return objValue.concat(srcValue);\n }\n }\n );\n }, result) as Record<string, ID[]>;\n }\n case 'dynamiczone': {\n return castArray(value).reduce((relationsStore, dzValue) => {\n const value = dzValue as Record<string, unknown>;\n if (!value.__component) {\n throw new ValidationError(\n `Cannot build relations store from dynamiczone, component identifier is undefined`\n );\n }\n\n return mergeWith(\n relationsStore,\n buildRelationsStore({\n uid: value.__component as UID.Component,\n data: value,\n }),\n (objValue, srcValue) => {\n if (isArray(objValue)) {\n return objValue.concat(srcValue);\n }\n }\n );\n }, result) as Record<string, ID[]>;\n }\n default:\n break;\n }\n\n return result;\n },\n {} as Record<string, ID[]>\n );\n};\n\n/**\n * Iterate through the relations store and validates that every relation or media\n * mentioned exists\n */\nconst checkRelationsExist = async (relationsStore: Record<string, ID[]> = {}) => {\n const promises: Promise<void>[] = [];\n\n for (const [key, value] of Object.entries(relationsStore)) {\n const evaluate = async () => {\n const uniqueValues = uniqBy(value, `id`);\n const count = await strapi.db.query(key as UID.Schema).count({\n where: {\n id: {\n $in: uniqueValues.map((v) => v.id),\n },\n },\n });\n\n if (count !== uniqueValues.length) {\n throw new ValidationError(\n `${\n uniqueValues.length - count\n } relation(s) of type ${key} associated with this entity do not exist`\n );\n }\n };\n promises.push(evaluate());\n }\n\n return Promise.all(promises);\n};\n\nconst entityValidator: Modules.EntityValidator.EntityValidator = {\n validateEntityCreation: createValidateEntity('creation'),\n validateEntityUpdate: createValidateEntity('update'),\n};\n\nexport default entityValidator;\n"],"names":["strapiUtils","validator","prop","has","Validators","validators","isObject","data","isEmpty","isNil","castArray","mergeWith","isArray","value","uniqBy"],"mappings":";;;;;;;AAaA,MAAM,EAAE,KAAK,kBAAsB,IAAAA;AACnC,MAAM,EAAE,kBAAkB,mBAAmB,sBAAA,IAA0BA,qBAAAA,QAAY;AACnF,MAAM,EAAE,gBAAgB,IAAIA,qBAAY,QAAA;AA4CxC,MAAM,YAAY,CAAC,UAAoC,OAAO,UAAU,KAAK;AAE7E,MAAM,YAAY,CAMhB,WACA;AAAA,EACE;AAAA,EACA;AACF,MACM;AACN,MAAI,gBAAmB;AAEvB,MACE,UAAU,KAAK,GAAG,MAChB,cAAc,QAAQ,KAAK,YAC1B,MAAM,QAAQ,iBAAiB,KAAK,KAAK,iBAAiB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I;AACvC,YAAM,QAAQ,MAAM,OAAO,GAAG,MAAM,GAAiB,EAAE,MAAM;AAAA,QAC3D,OAAO;AAAA,UACL,IAAI;AAAA,YACF,KAAK,aAAa,IAAI,CAAC,MAAM,EAAE,EAAE;AAAA,UACnC;AAAA,QACF;AAAA,MAAA,CACD;AAEG,UAAA,UAAU,aAAa,QAAQ;AACjC,cAAM,IAAI;AAAA,UACR,GACE,aAAa,SAAS,KACxB,wBAAwB,GAAG;AAAA,QAAA;AAAA,MAE/B;AAAA,IAAA;AAEO,aAAA,KAAK,UAAU;AAAA,EAC1B;AAEO,SAAA,QAAQ,IAAI,QAAQ;AAC7B;AAEA,MAAM,kBAA2D;AAAA,EAC/D,wBAAwB,qBAAqB,UAAU;AAAA,EACvD,sBAAsB,qBAAqB,QAAQ;AACrD;;"}
@@ -74,7 +74,9 @@ const createComponentValidator = (createOrUpdate) => ({
74
74
  attr: { required: true },
75
75
  updatedAttribute
76
76
  });
77
- validator2 = addMinMax(validator2, { attr, updatedAttribute });
77
+ if (!isDraft) {
78
+ validator2 = addMinMax(validator2, { attr, updatedAttribute });
79
+ }
78
80
  return validator2;
79
81
  }
80
82
  let validator = createModelValidator(createOrUpdate)(
@@ -112,7 +114,9 @@ const createDzValidator = (createOrUpdate) => ({ attr, updatedAttribute, compone
112
114
  attr: { required: true },
113
115
  updatedAttribute
114
116
  });
115
- validator = addMinMax(validator, { attr, updatedAttribute });
117
+ if (!isDraft) {
118
+ validator = addMinMax(validator, { attr, updatedAttribute });
119
+ }
116
120
  return validator;
117
121
  };
118
122
  const createRelationValidator = ({