@strapi/core 0.0.0-experimental.e9303c99ae3c28b4b8421ab6971efae0748dd599 → 0.0.0-experimental.e99215433ba1d6e5497350812df274f426dbdba3

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.

Potentially problematic release.


This version of @strapi/core might be problematic. Click here for more details.

Files changed (269) hide show
  1. package/dist/Strapi.d.ts +1 -0
  2. package/dist/Strapi.d.ts.map +1 -1
  3. package/dist/Strapi.js +23 -2
  4. package/dist/Strapi.js.map +1 -1
  5. package/dist/Strapi.mjs +23 -2
  6. package/dist/Strapi.mjs.map +1 -1
  7. package/dist/configuration/index.d.ts +1 -0
  8. package/dist/configuration/index.d.ts.map +1 -1
  9. package/dist/configuration/index.js +1 -0
  10. package/dist/configuration/index.js.map +1 -1
  11. package/dist/configuration/index.mjs +1 -0
  12. package/dist/configuration/index.mjs.map +1 -1
  13. package/dist/constants.d.ts +3 -0
  14. package/dist/constants.d.ts.map +1 -0
  15. package/dist/constants.js +6 -0
  16. package/dist/constants.js.map +1 -0
  17. package/dist/constants.mjs +4 -0
  18. package/dist/constants.mjs.map +1 -0
  19. package/dist/core-api/controller/index.d.ts.map +1 -1
  20. package/dist/core-api/controller/index.js +2 -1
  21. package/dist/core-api/controller/index.js.map +1 -1
  22. package/dist/core-api/controller/index.mjs +2 -1
  23. package/dist/core-api/controller/index.mjs.map +1 -1
  24. package/dist/core-api/controller/transform.d.ts +3 -2
  25. package/dist/core-api/controller/transform.d.ts.map +1 -1
  26. package/dist/core-api/controller/transform.js +13 -3
  27. package/dist/core-api/controller/transform.js.map +1 -1
  28. package/dist/core-api/controller/transform.mjs +13 -3
  29. package/dist/core-api/controller/transform.mjs.map +1 -1
  30. package/dist/core-api/routes/index.d.ts +4 -22
  31. package/dist/core-api/routes/index.d.ts.map +1 -1
  32. package/dist/core-api/routes/index.js +150 -8
  33. package/dist/core-api/routes/index.js.map +1 -1
  34. package/dist/core-api/routes/index.mjs +131 -8
  35. package/dist/core-api/routes/index.mjs.map +1 -1
  36. package/dist/core-api/routes/validation/attributes.d.ts +244 -0
  37. package/dist/core-api/routes/validation/attributes.d.ts.map +1 -0
  38. package/dist/core-api/routes/validation/attributes.js +560 -0
  39. package/dist/core-api/routes/validation/attributes.js.map +1 -0
  40. package/dist/core-api/routes/validation/attributes.mjs +521 -0
  41. package/dist/core-api/routes/validation/attributes.mjs.map +1 -0
  42. package/dist/core-api/routes/validation/common.d.ts +105 -0
  43. package/dist/core-api/routes/validation/common.d.ts.map +1 -0
  44. package/dist/core-api/routes/validation/common.js +116 -0
  45. package/dist/core-api/routes/validation/common.js.map +1 -0
  46. package/dist/core-api/routes/validation/common.mjs +95 -0
  47. package/dist/core-api/routes/validation/common.mjs.map +1 -0
  48. package/dist/core-api/routes/validation/component.d.ts +34 -0
  49. package/dist/core-api/routes/validation/component.d.ts.map +1 -0
  50. package/dist/core-api/routes/validation/component.js +45 -0
  51. package/dist/core-api/routes/validation/component.js.map +1 -0
  52. package/dist/core-api/routes/validation/component.mjs +43 -0
  53. package/dist/core-api/routes/validation/component.mjs.map +1 -0
  54. package/dist/core-api/routes/validation/constants.d.ts +8 -0
  55. package/dist/core-api/routes/validation/constants.d.ts.map +1 -0
  56. package/dist/core-api/routes/validation/constants.js +18 -0
  57. package/dist/core-api/routes/validation/constants.js.map +1 -0
  58. package/dist/core-api/routes/validation/constants.mjs +16 -0
  59. package/dist/core-api/routes/validation/constants.mjs.map +1 -0
  60. package/dist/core-api/routes/validation/content-type.d.ts +128 -0
  61. package/dist/core-api/routes/validation/content-type.d.ts.map +1 -0
  62. package/dist/core-api/routes/validation/content-type.js +201 -0
  63. package/dist/core-api/routes/validation/content-type.js.map +1 -0
  64. package/dist/core-api/routes/validation/content-type.mjs +180 -0
  65. package/dist/core-api/routes/validation/content-type.mjs.map +1 -0
  66. package/dist/core-api/routes/validation/index.d.ts +5 -0
  67. package/dist/core-api/routes/validation/index.d.ts.map +1 -0
  68. package/dist/core-api/routes/validation/mappers.d.ts +105 -0
  69. package/dist/core-api/routes/validation/mappers.d.ts.map +1 -0
  70. package/dist/core-api/routes/validation/mappers.js +273 -0
  71. package/dist/core-api/routes/validation/mappers.js.map +1 -0
  72. package/dist/core-api/routes/validation/mappers.mjs +249 -0
  73. package/dist/core-api/routes/validation/mappers.mjs.map +1 -0
  74. package/dist/core-api/routes/validation/utils.d.ts +47 -0
  75. package/dist/core-api/routes/validation/utils.d.ts.map +1 -0
  76. package/dist/core-api/routes/validation/utils.js +128 -0
  77. package/dist/core-api/routes/validation/utils.js.map +1 -0
  78. package/dist/core-api/routes/validation/utils.mjs +106 -0
  79. package/dist/core-api/routes/validation/utils.mjs.map +1 -0
  80. package/dist/domain/content-type/index.d.ts.map +1 -1
  81. package/dist/domain/content-type/index.js +17 -1
  82. package/dist/domain/content-type/index.js.map +1 -1
  83. package/dist/domain/content-type/index.mjs +17 -1
  84. package/dist/domain/content-type/index.mjs.map +1 -1
  85. package/dist/domain/module/index.d.ts.map +1 -1
  86. package/dist/domain/module/index.js +3 -0
  87. package/dist/domain/module/index.js.map +1 -1
  88. package/dist/domain/module/index.mjs +3 -0
  89. package/dist/domain/module/index.mjs.map +1 -1
  90. package/dist/ee/index.d.ts +6 -0
  91. package/dist/ee/index.d.ts.map +1 -1
  92. package/dist/ee/index.js +29 -3
  93. package/dist/ee/index.js.map +1 -1
  94. package/dist/ee/index.mjs +30 -4
  95. package/dist/ee/index.mjs.map +1 -1
  96. package/dist/ee/license.d.ts +3 -1
  97. package/dist/ee/license.d.ts.map +1 -1
  98. package/dist/ee/license.js +8 -2
  99. package/dist/ee/license.js.map +1 -1
  100. package/dist/ee/license.mjs +9 -4
  101. package/dist/ee/license.mjs.map +1 -1
  102. package/dist/factories.d.ts +3 -1
  103. package/dist/factories.d.ts.map +1 -1
  104. package/dist/factories.js +10 -2
  105. package/dist/factories.js.map +1 -1
  106. package/dist/factories.mjs +10 -3
  107. package/dist/factories.mjs.map +1 -1
  108. package/dist/index.js +1 -1
  109. package/dist/index.mjs +1 -1
  110. package/dist/loaders/plugins/index.js +1 -1
  111. package/dist/loaders/plugins/index.js.map +1 -1
  112. package/dist/loaders/plugins/index.mjs +1 -1
  113. package/dist/loaders/plugins/index.mjs.map +1 -1
  114. package/dist/middlewares/cors.d.ts +9 -1
  115. package/dist/middlewares/cors.d.ts.map +1 -1
  116. package/dist/middlewares/cors.js +39 -17
  117. package/dist/middlewares/cors.js.map +1 -1
  118. package/dist/middlewares/cors.mjs +39 -18
  119. package/dist/middlewares/cors.mjs.map +1 -1
  120. package/dist/middlewares/security.d.ts.map +1 -1
  121. package/dist/middlewares/security.js +2 -15
  122. package/dist/middlewares/security.js.map +1 -1
  123. package/dist/middlewares/security.mjs +2 -15
  124. package/dist/middlewares/security.mjs.map +1 -1
  125. package/dist/migrations/database/5.0.0-discard-drafts.d.ts.map +1 -1
  126. package/dist/migrations/database/5.0.0-discard-drafts.js +168 -59
  127. package/dist/migrations/database/5.0.0-discard-drafts.js.map +1 -1
  128. package/dist/migrations/database/5.0.0-discard-drafts.mjs +169 -60
  129. package/dist/migrations/database/5.0.0-discard-drafts.mjs.map +1 -1
  130. package/dist/migrations/first-published-at.d.ts +4 -0
  131. package/dist/migrations/first-published-at.d.ts.map +1 -0
  132. package/dist/migrations/first-published-at.js +51 -0
  133. package/dist/migrations/first-published-at.js.map +1 -0
  134. package/dist/migrations/first-published-at.mjs +49 -0
  135. package/dist/migrations/first-published-at.mjs.map +1 -0
  136. package/dist/migrations/index.d.ts.map +1 -1
  137. package/dist/migrations/index.js +5 -0
  138. package/dist/migrations/index.js.map +1 -1
  139. package/dist/migrations/index.mjs +5 -0
  140. package/dist/migrations/index.mjs.map +1 -1
  141. package/dist/package.json.js +18 -13
  142. package/dist/package.json.js.map +1 -1
  143. package/dist/package.json.mjs +18 -13
  144. package/dist/package.json.mjs.map +1 -1
  145. package/dist/providers/index.d.ts.map +1 -1
  146. package/dist/providers/index.js +2 -0
  147. package/dist/providers/index.js.map +1 -1
  148. package/dist/providers/index.mjs +2 -0
  149. package/dist/providers/index.mjs.map +1 -1
  150. package/dist/providers/session-manager.d.ts +3 -0
  151. package/dist/providers/session-manager.d.ts.map +1 -0
  152. package/dist/providers/session-manager.js +23 -0
  153. package/dist/providers/session-manager.js.map +1 -0
  154. package/dist/providers/session-manager.mjs +21 -0
  155. package/dist/providers/session-manager.mjs.map +1 -0
  156. package/dist/services/content-api/index.d.ts +1 -1
  157. package/dist/services/content-api/index.d.ts.map +1 -1
  158. package/dist/services/content-api/index.js +1 -1
  159. package/dist/services/content-api/index.js.map +1 -1
  160. package/dist/services/content-api/index.mjs +2 -2
  161. package/dist/services/content-api/index.mjs.map +1 -1
  162. package/dist/services/content-source-maps.d.ts +13 -0
  163. package/dist/services/content-source-maps.d.ts.map +1 -0
  164. package/dist/services/content-source-maps.js +108 -0
  165. package/dist/services/content-source-maps.js.map +1 -0
  166. package/dist/services/content-source-maps.mjs +106 -0
  167. package/dist/services/content-source-maps.mjs.map +1 -0
  168. package/dist/services/core-store.d.ts +2 -2
  169. package/dist/services/core-store.d.ts.map +1 -1
  170. package/dist/services/core-store.js.map +1 -1
  171. package/dist/services/core-store.mjs.map +1 -1
  172. package/dist/services/document-service/components.d.ts +31 -1
  173. package/dist/services/document-service/components.d.ts.map +1 -1
  174. package/dist/services/document-service/components.js +109 -0
  175. package/dist/services/document-service/components.js.map +1 -1
  176. package/dist/services/document-service/components.mjs +107 -1
  177. package/dist/services/document-service/components.mjs.map +1 -1
  178. package/dist/services/document-service/entries.d.ts.map +1 -1
  179. package/dist/services/document-service/entries.js +42 -0
  180. package/dist/services/document-service/entries.js.map +1 -1
  181. package/dist/services/document-service/entries.mjs +43 -1
  182. package/dist/services/document-service/entries.mjs.map +1 -1
  183. package/dist/services/document-service/first-published-at.d.ts +7 -0
  184. package/dist/services/document-service/first-published-at.d.ts.map +1 -0
  185. package/dist/services/document-service/first-published-at.js +31 -0
  186. package/dist/services/document-service/first-published-at.js.map +1 -0
  187. package/dist/services/document-service/first-published-at.mjs +28 -0
  188. package/dist/services/document-service/first-published-at.mjs.map +1 -0
  189. package/dist/services/document-service/internationalization.d.ts +6 -1
  190. package/dist/services/document-service/internationalization.d.ts.map +1 -1
  191. package/dist/services/document-service/internationalization.js +32 -0
  192. package/dist/services/document-service/internationalization.js.map +1 -1
  193. package/dist/services/document-service/internationalization.mjs +32 -1
  194. package/dist/services/document-service/internationalization.mjs.map +1 -1
  195. package/dist/services/document-service/repository.d.ts.map +1 -1
  196. package/dist/services/document-service/repository.js +16 -8
  197. package/dist/services/document-service/repository.js.map +1 -1
  198. package/dist/services/document-service/repository.mjs +18 -10
  199. package/dist/services/document-service/repository.mjs.map +1 -1
  200. package/dist/services/document-service/utils/clean-component-join-table.d.ts +7 -0
  201. package/dist/services/document-service/utils/clean-component-join-table.d.ts.map +1 -0
  202. package/dist/services/document-service/utils/unidirectional-relations.d.ts +19 -2
  203. package/dist/services/document-service/utils/unidirectional-relations.d.ts.map +1 -1
  204. package/dist/services/document-service/utils/unidirectional-relations.js +21 -6
  205. package/dist/services/document-service/utils/unidirectional-relations.js.map +1 -1
  206. package/dist/services/document-service/utils/unidirectional-relations.mjs +21 -6
  207. package/dist/services/document-service/utils/unidirectional-relations.mjs.map +1 -1
  208. package/dist/services/entity-validator/index.d.ts.map +1 -1
  209. package/dist/services/entity-validator/index.js +9 -0
  210. package/dist/services/entity-validator/index.js.map +1 -1
  211. package/dist/services/entity-validator/index.mjs +9 -0
  212. package/dist/services/entity-validator/index.mjs.map +1 -1
  213. package/dist/services/entity-validator/validators.d.ts +1 -0
  214. package/dist/services/entity-validator/validators.d.ts.map +1 -1
  215. package/dist/services/entity-validator/validators.js +3 -0
  216. package/dist/services/entity-validator/validators.js.map +1 -1
  217. package/dist/services/entity-validator/validators.mjs +3 -0
  218. package/dist/services/entity-validator/validators.mjs.map +1 -1
  219. package/dist/services/metrics/admin-user-hash.d.ts.map +1 -1
  220. package/dist/services/metrics/admin-user-hash.js.map +1 -1
  221. package/dist/services/metrics/admin-user-hash.mjs.map +1 -1
  222. package/dist/services/metrics/index.d.ts +1 -1
  223. package/dist/services/metrics/index.d.ts.map +1 -1
  224. package/dist/services/metrics/index.js +11 -9
  225. package/dist/services/metrics/index.js.map +1 -1
  226. package/dist/services/metrics/index.mjs +11 -9
  227. package/dist/services/metrics/index.mjs.map +1 -1
  228. package/dist/services/metrics/middleware.d.ts +2 -1
  229. package/dist/services/metrics/middleware.d.ts.map +1 -1
  230. package/dist/services/metrics/middleware.js +2 -2
  231. package/dist/services/metrics/middleware.js.map +1 -1
  232. package/dist/services/metrics/middleware.mjs +2 -2
  233. package/dist/services/metrics/middleware.mjs.map +1 -1
  234. package/dist/services/metrics/sender.d.ts.map +1 -1
  235. package/dist/services/metrics/sender.js +7 -6
  236. package/dist/services/metrics/sender.js.map +1 -1
  237. package/dist/services/metrics/sender.mjs +8 -7
  238. package/dist/services/metrics/sender.mjs.map +1 -1
  239. package/dist/services/server/register-routes.js +22 -2
  240. package/dist/services/server/register-routes.js.map +1 -1
  241. package/dist/services/server/register-routes.mjs +22 -2
  242. package/dist/services/server/register-routes.mjs.map +1 -1
  243. package/dist/services/server/routing.d.ts +10 -0
  244. package/dist/services/server/routing.d.ts.map +1 -1
  245. package/dist/services/server/routing.js +7 -1
  246. package/dist/services/server/routing.js.map +1 -1
  247. package/dist/services/server/routing.mjs +7 -1
  248. package/dist/services/server/routing.mjs.map +1 -1
  249. package/dist/services/session-manager.d.ts +167 -0
  250. package/dist/services/session-manager.d.ts.map +1 -0
  251. package/dist/services/session-manager.js +529 -0
  252. package/dist/services/session-manager.js.map +1 -0
  253. package/dist/services/session-manager.mjs +526 -0
  254. package/dist/services/session-manager.mjs.map +1 -0
  255. package/dist/services/utils/conditional-fields.d.ts +3 -0
  256. package/dist/services/utils/conditional-fields.d.ts.map +1 -0
  257. package/dist/services/utils/conditional-fields.js +22 -0
  258. package/dist/services/utils/conditional-fields.js.map +1 -0
  259. package/dist/services/utils/conditional-fields.mjs +20 -0
  260. package/dist/services/utils/conditional-fields.mjs.map +1 -0
  261. package/dist/utils/fetch.d.ts +5 -1
  262. package/dist/utils/fetch.d.ts.map +1 -1
  263. package/dist/utils/fetch.js +8 -4
  264. package/dist/utils/fetch.js.map +1 -1
  265. package/dist/utils/fetch.mjs +8 -4
  266. package/dist/utils/fetch.mjs.map +1 -1
  267. package/dist/utils/transform-content-types-to-models.d.ts +197 -0
  268. package/dist/utils/transform-content-types-to-models.d.ts.map +1 -1
  269. package/package.json +18 -13
@@ -2,6 +2,7 @@
2
2
 
3
3
  var fp = require('lodash/fp');
4
4
  var helmet = require('koa-helmet');
5
+ var strapiUtils = require('@strapi/utils');
5
6
 
6
7
  const defaults = {
7
8
  crossOriginEmbedderPolicy: false,
@@ -11,21 +12,7 @@ const defaults = {
11
12
  contentSecurityPolicy: {
12
13
  useDefaults: true,
13
14
  directives: {
14
- 'connect-src': [
15
- "'self'",
16
- 'https:'
17
- ],
18
- 'img-src': [
19
- "'self'",
20
- 'data:',
21
- 'blob:',
22
- 'https://market-assets.strapi.io'
23
- ],
24
- 'media-src': [
25
- "'self'",
26
- 'data:',
27
- 'blob:'
28
- ],
15
+ ...strapiUtils.CSP_DEFAULTS,
29
16
  upgradeInsecureRequests: null
30
17
  }
31
18
  },
@@ -1 +1 @@
1
- {"version":3,"file":"security.js","sources":["../../src/middlewares/security.ts"],"sourcesContent":["import { defaultsDeep, mergeWith } from 'lodash/fp';\nimport helmet, { KoaHelmet } from 'koa-helmet';\n\nimport type { Core } from '@strapi/types';\n\nexport type Config = NonNullable<Parameters<KoaHelmet>[0]>;\n\nconst defaults: Config = {\n crossOriginEmbedderPolicy: false,\n crossOriginOpenerPolicy: false,\n crossOriginResourcePolicy: false,\n originAgentCluster: false,\n contentSecurityPolicy: {\n useDefaults: true,\n directives: {\n 'connect-src': [\"'self'\", 'https:'],\n 'img-src': [\"'self'\", 'data:', 'blob:', 'https://market-assets.strapi.io'],\n 'media-src': [\"'self'\", 'data:', 'blob:'],\n upgradeInsecureRequests: null,\n },\n },\n xssFilter: false,\n hsts: {\n maxAge: 31536000,\n includeSubDomains: true,\n },\n frameguard: {\n action: 'sameorigin',\n },\n};\n\nconst mergeConfig = (existingConfig: Config, newConfig: Config) => {\n return mergeWith(\n (obj, src) => (Array.isArray(obj) && Array.isArray(src) ? obj.concat(src) : undefined),\n existingConfig,\n newConfig\n );\n};\n\nexport const security: Core.MiddlewareFactory<Config> =\n (config, { strapi }) =>\n (ctx, next) => {\n let helmetConfig: Config = defaultsDeep(defaults, config);\n\n const specialPaths = ['/documentation'];\n\n const directives: {\n 'script-src': string[];\n 'img-src': string[];\n 'manifest-src': string[];\n 'frame-src': string[];\n } = {\n 'script-src': [\"'self'\", \"'unsafe-inline'\", 'cdn.jsdelivr.net'],\n 'img-src': [\"'self'\", 'data:', 'cdn.jsdelivr.net', 'strapi.io'],\n 'manifest-src': [],\n 'frame-src': [],\n };\n\n // if apollo graphql playground is enabled, add exceptions for it\n if (strapi.plugin('graphql')?.service('utils').playground.isEnabled()) {\n const { config: gqlConfig } = strapi.plugin('graphql');\n specialPaths.push(gqlConfig('endpoint'));\n\n directives['script-src'].push(`https: 'unsafe-inline'`);\n directives['img-src'].push(`'apollo-server-landing-page.cdn.apollographql.com'`);\n directives['manifest-src'].push(`'self'`);\n directives['manifest-src'].push('apollo-server-landing-page.cdn.apollographql.com');\n directives['frame-src'].push(`'self'`);\n directives['frame-src'].push('sandbox.embed.apollographql.com');\n }\n\n // TODO: we shouldn't combine playground exceptions with documentation for all routes, we should first check the path and then return exceptions specific to that\n if (ctx.method === 'GET' && specialPaths.some((str) => ctx.path.startsWith(str))) {\n helmetConfig = mergeConfig(helmetConfig, {\n crossOriginEmbedderPolicy: false, // TODO: only use this for graphql playground\n contentSecurityPolicy: {\n directives,\n },\n });\n }\n\n /**\n * These are for vite's watch mode so it can accurately\n * connect to the HMR websocket & reconnect on failure\n * or when the server restarts.\n *\n * It only applies in development, and only on GET requests\n * that are part of the admin route.\n */\n\n if (\n ['development', 'test'].includes(process.env.NODE_ENV ?? '') &&\n ctx.method === 'GET' &&\n ctx.path.startsWith(strapi.config.get('admin.path'))\n ) {\n helmetConfig = mergeConfig(helmetConfig, {\n contentSecurityPolicy: {\n directives: {\n 'script-src': [\"'self'\", \"'unsafe-inline'\"],\n 'connect-src': [\"'self'\", 'http:', 'https:', 'ws:'],\n },\n },\n });\n }\n\n return helmet(helmetConfig)(ctx, next);\n };\n"],"names":["defaults","crossOriginEmbedderPolicy","crossOriginOpenerPolicy","crossOriginResourcePolicy","originAgentCluster","contentSecurityPolicy","useDefaults","directives","upgradeInsecureRequests","xssFilter","hsts","maxAge","includeSubDomains","frameguard","action","mergeConfig","existingConfig","newConfig","mergeWith","obj","src","Array","isArray","concat","undefined","security","config","strapi","ctx","next","helmetConfig","defaultsDeep","specialPaths","plugin","service","playground","isEnabled","gqlConfig","push","method","some","str","path","startsWith","includes","process","env","NODE_ENV","get","helmet"],"mappings":";;;;;AAOA,MAAMA,QAAmB,GAAA;IACvBC,yBAA2B,EAAA,KAAA;IAC3BC,uBAAyB,EAAA,KAAA;IACzBC,yBAA2B,EAAA,KAAA;IAC3BC,kBAAoB,EAAA,KAAA;IACpBC,qBAAuB,EAAA;QACrBC,WAAa,EAAA,IAAA;QACbC,UAAY,EAAA;YACV,aAAe,EAAA;AAAC,gBAAA,QAAA;AAAU,gBAAA;AAAS,aAAA;YACnC,SAAW,EAAA;AAAC,gBAAA,QAAA;AAAU,gBAAA,OAAA;AAAS,gBAAA,OAAA;AAAS,gBAAA;AAAkC,aAAA;YAC1E,WAAa,EAAA;AAAC,gBAAA,QAAA;AAAU,gBAAA,OAAA;AAAS,gBAAA;AAAQ,aAAA;YACzCC,uBAAyB,EAAA;AAC3B;AACF,KAAA;IACAC,SAAW,EAAA,KAAA;IACXC,IAAM,EAAA;QACJC,MAAQ,EAAA,QAAA;QACRC,iBAAmB,EAAA;AACrB,KAAA;IACAC,UAAY,EAAA;QACVC,MAAQ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;AACF,aAAA,CAAA;AACF;QAEA,OAAO0C,MAAAA,CAAOnB,cAAcF,GAAKC,EAAAA,IAAAA,CAAAA;;;;;"}
1
+ {"version":3,"file":"security.js","sources":["../../src/middlewares/security.ts"],"sourcesContent":["import { defaultsDeep, mergeWith } from 'lodash/fp';\nimport helmet, { KoaHelmet } from 'koa-helmet';\nimport { CSP_DEFAULTS } from '@strapi/utils';\n\nimport type { Core } from '@strapi/types';\n\nexport type Config = NonNullable<Parameters<KoaHelmet>[0]>;\n\nconst defaults: Config = {\n crossOriginEmbedderPolicy: false,\n crossOriginOpenerPolicy: false,\n crossOriginResourcePolicy: false,\n originAgentCluster: false,\n contentSecurityPolicy: {\n useDefaults: true,\n directives: {\n ...CSP_DEFAULTS,\n upgradeInsecureRequests: null,\n },\n },\n xssFilter: false,\n hsts: {\n maxAge: 31536000,\n includeSubDomains: true,\n },\n frameguard: {\n action: 'sameorigin',\n },\n};\n\nconst mergeConfig = (existingConfig: Config, newConfig: Config) => {\n return mergeWith(\n (obj, src) => (Array.isArray(obj) && Array.isArray(src) ? obj.concat(src) : undefined),\n existingConfig,\n newConfig\n );\n};\n\nexport const security: Core.MiddlewareFactory<Config> =\n (config, { strapi }) =>\n (ctx, next) => {\n let helmetConfig: Config = defaultsDeep(defaults, config);\n const specialPaths = ['/documentation'];\n\n const directives: {\n 'script-src': string[];\n 'img-src': string[];\n 'manifest-src': string[];\n 'frame-src': string[];\n } = {\n 'script-src': [\"'self'\", \"'unsafe-inline'\", 'cdn.jsdelivr.net'],\n 'img-src': [\"'self'\", 'data:', 'cdn.jsdelivr.net', 'strapi.io'],\n 'manifest-src': [],\n 'frame-src': [],\n };\n\n // if apollo graphql playground is enabled, add exceptions for it\n if (strapi.plugin('graphql')?.service('utils').playground.isEnabled()) {\n const { config: gqlConfig } = strapi.plugin('graphql');\n specialPaths.push(gqlConfig('endpoint'));\n\n directives['script-src'].push(`https: 'unsafe-inline'`);\n directives['img-src'].push(`'apollo-server-landing-page.cdn.apollographql.com'`);\n directives['manifest-src'].push(`'self'`);\n directives['manifest-src'].push('apollo-server-landing-page.cdn.apollographql.com');\n directives['frame-src'].push(`'self'`);\n directives['frame-src'].push('sandbox.embed.apollographql.com');\n }\n\n // TODO: we shouldn't combine playground exceptions with documentation for all routes, we should first check the path and then return exceptions specific to that\n if (ctx.method === 'GET' && specialPaths.some((str) => ctx.path.startsWith(str))) {\n helmetConfig = mergeConfig(helmetConfig, {\n crossOriginEmbedderPolicy: false, // TODO: only use this for graphql playground\n contentSecurityPolicy: {\n directives,\n },\n });\n }\n\n /**\n * These are for vite's watch mode so it can accurately\n * connect to the HMR websocket & reconnect on failure\n * or when the server restarts.\n *\n * It only applies in development, and only on GET requests\n * that are part of the admin route.\n */\n\n if (\n ['development', 'test'].includes(process.env.NODE_ENV ?? '') &&\n ctx.method === 'GET' &&\n ctx.path.startsWith(strapi.config.get('admin.path'))\n ) {\n helmetConfig = mergeConfig(helmetConfig, {\n contentSecurityPolicy: {\n directives: {\n 'script-src': [\"'self'\", \"'unsafe-inline'\"],\n 'connect-src': [\"'self'\", 'http:', 'https:', 'ws:'],\n },\n },\n });\n }\n\n return helmet(helmetConfig)(ctx, next);\n };\n"],"names":["defaults","crossOriginEmbedderPolicy","crossOriginOpenerPolicy","crossOriginResourcePolicy","originAgentCluster","contentSecurityPolicy","useDefaults","directives","CSP_DEFAULTS","upgradeInsecureRequests","xssFilter","hsts","maxAge","includeSubDomains","frameguard","action","mergeConfig","existingConfig","newConfig","mergeWith","obj","src","Array","isArray","concat","undefined","security","config","strapi","ctx","next","helmetConfig","defaultsDeep","specialPaths","plugin","service","playground","isEnabled","gqlConfig","push","method","some","str","path","startsWith","includes","process","env","NODE_ENV","get","helmet"],"mappings":";;;;;;AAQA,MAAMA,QAAmB,GAAA;IACvBC,yBAA2B,EAAA,KAAA;IAC3BC,uBAAyB,EAAA,KAAA;IACzBC,yBAA2B,EAAA,KAAA;IAC3BC,kBAAoB,EAAA,KAAA;IACpBC,qBAA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AYe,YAAc,EAAA;gBACvC1B,qBAAuB,EAAA;oBACrBE,UAAY,EAAA;wBACV,YAAc,EAAA;AAAC,4BAAA,QAAA;AAAU,4BAAA;AAAkB,yBAAA;wBAC3C,aAAe,EAAA;AAAC,4BAAA,QAAA;AAAU,4BAAA,OAAA;AAAS,4BAAA,QAAA;AAAU,4BAAA;AAAM;AACrD;AACF;AACF,aAAA,CAAA;AACF;QAEA,OAAO2C,MAAAA,CAAOnB,cAAcF,GAAKC,EAAAA,IAAAA,CAAAA;;;;;"}
@@ -1,5 +1,6 @@
1
1
  import { defaultsDeep, mergeWith } from 'lodash/fp';
2
2
  import helmet from 'koa-helmet';
3
+ import { CSP_DEFAULTS } from '@strapi/utils';
3
4
 
4
5
  const defaults = {
5
6
  crossOriginEmbedderPolicy: false,
@@ -9,21 +10,7 @@ const defaults = {
9
10
  contentSecurityPolicy: {
10
11
  useDefaults: true,
11
12
  directives: {
12
- 'connect-src': [
13
- "'self'",
14
- 'https:'
15
- ],
16
- 'img-src': [
17
- "'self'",
18
- 'data:',
19
- 'blob:',
20
- 'https://market-assets.strapi.io'
21
- ],
22
- 'media-src': [
23
- "'self'",
24
- 'data:',
25
- 'blob:'
26
- ],
13
+ ...CSP_DEFAULTS,
27
14
  upgradeInsecureRequests: null
28
15
  }
29
16
  },
@@ -1 +1 @@
1
- {"version":3,"file":"security.mjs","sources":["../../src/middlewares/security.ts"],"sourcesContent":["import { defaultsDeep, mergeWith } from 'lodash/fp';\nimport helmet, { KoaHelmet } from 'koa-helmet';\n\nimport type { Core } from '@strapi/types';\n\nexport type Config = NonNullable<Parameters<KoaHelmet>[0]>;\n\nconst defaults: Config = {\n crossOriginEmbedderPolicy: false,\n crossOriginOpenerPolicy: false,\n crossOriginResourcePolicy: false,\n originAgentCluster: false,\n contentSecurityPolicy: {\n useDefaults: true,\n directives: {\n 'connect-src': [\"'self'\", 'https:'],\n 'img-src': [\"'self'\", 'data:', 'blob:', 'https://market-assets.strapi.io'],\n 'media-src': [\"'self'\", 'data:', 'blob:'],\n upgradeInsecureRequests: null,\n },\n },\n xssFilter: false,\n hsts: {\n maxAge: 31536000,\n includeSubDomains: true,\n },\n frameguard: {\n action: 'sameorigin',\n },\n};\n\nconst mergeConfig = (existingConfig: Config, newConfig: Config) => {\n return mergeWith(\n (obj, src) => (Array.isArray(obj) && Array.isArray(src) ? obj.concat(src) : undefined),\n existingConfig,\n newConfig\n );\n};\n\nexport const security: Core.MiddlewareFactory<Config> =\n (config, { strapi }) =>\n (ctx, next) => {\n let helmetConfig: Config = defaultsDeep(defaults, config);\n\n const specialPaths = ['/documentation'];\n\n const directives: {\n 'script-src': string[];\n 'img-src': string[];\n 'manifest-src': string[];\n 'frame-src': string[];\n } = {\n 'script-src': [\"'self'\", \"'unsafe-inline'\", 'cdn.jsdelivr.net'],\n 'img-src': [\"'self'\", 'data:', 'cdn.jsdelivr.net', 'strapi.io'],\n 'manifest-src': [],\n 'frame-src': [],\n };\n\n // if apollo graphql playground is enabled, add exceptions for it\n if (strapi.plugin('graphql')?.service('utils').playground.isEnabled()) {\n const { config: gqlConfig } = strapi.plugin('graphql');\n specialPaths.push(gqlConfig('endpoint'));\n\n directives['script-src'].push(`https: 'unsafe-inline'`);\n directives['img-src'].push(`'apollo-server-landing-page.cdn.apollographql.com'`);\n directives['manifest-src'].push(`'self'`);\n directives['manifest-src'].push('apollo-server-landing-page.cdn.apollographql.com');\n directives['frame-src'].push(`'self'`);\n directives['frame-src'].push('sandbox.embed.apollographql.com');\n }\n\n // TODO: we shouldn't combine playground exceptions with documentation for all routes, we should first check the path and then return exceptions specific to that\n if (ctx.method === 'GET' && specialPaths.some((str) => ctx.path.startsWith(str))) {\n helmetConfig = mergeConfig(helmetConfig, {\n crossOriginEmbedderPolicy: false, // TODO: only use this for graphql playground\n contentSecurityPolicy: {\n directives,\n },\n });\n }\n\n /**\n * These are for vite's watch mode so it can accurately\n * connect to the HMR websocket & reconnect on failure\n * or when the server restarts.\n *\n * It only applies in development, and only on GET requests\n * that are part of the admin route.\n */\n\n if (\n ['development', 'test'].includes(process.env.NODE_ENV ?? '') &&\n ctx.method === 'GET' &&\n ctx.path.startsWith(strapi.config.get('admin.path'))\n ) {\n helmetConfig = mergeConfig(helmetConfig, {\n contentSecurityPolicy: {\n directives: {\n 'script-src': [\"'self'\", \"'unsafe-inline'\"],\n 'connect-src': [\"'self'\", 'http:', 'https:', 'ws:'],\n },\n },\n });\n }\n\n return helmet(helmetConfig)(ctx, next);\n };\n"],"names":["defaults","crossOriginEmbedderPolicy","crossOriginOpenerPolicy","crossOriginResourcePolicy","originAgentCluster","contentSecurityPolicy","useDefaults","directives","upgradeInsecureRequests","xssFilter","hsts","maxAge","includeSubDomains","frameguard","action","mergeConfig","existingConfig","newConfig","mergeWith","obj","src","Array","isArray","concat","undefined","security","config","strapi","ctx","next","helmetConfig","defaultsDeep","specialPaths","plugin","service","playground","isEnabled","gqlConfig","push","method","some","str","path","startsWith","includes","process","env","NODE_ENV","get","helmet"],"mappings":";;;AAOA,MAAMA,QAAmB,GAAA;IACvBC,yBAA2B,EAAA,KAAA;IAC3BC,uBAAyB,EAAA,KAAA;IACzBC,yBAA2B,EAAA,KAAA;IAC3BC,kBAAoB,EAAA,KAAA;IACpBC,qBAAuB,EAAA;QACrBC,WAAa,EAAA,IAAA;QACbC,UAAY,EAAA;YACV,aAAe,EAAA;AAAC,gBAAA,QAAA;AAAU,gBAAA;AAAS,aAAA;YACnC,SAAW,EAAA;AAAC,gBAAA,QAAA;AAAU,gBAAA,OAAA;AAAS,gBAAA,OAAA;AAAS,gBAAA;AAAkC,aAAA;YAC1E,WAAa,EAAA;AAAC,gBAAA,QAAA;AAAU,gBAAA,OAAA;AAAS,gBAAA;AAAQ,aAAA;YACzCC,uBAAyB,EAAA;AAC3B;AACF,KAAA;IACAC,SAAW,EAAA,KAAA;IACXC,IAAM,EAAA;QACJC,MAAQ,EAAA,QAAA;QACRC,iBAAmB,EAAA;AACrB,KAAA;IACAC,UAAY,EAA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M;AACrD;AACF;AACF,aAAA,CAAA;AACF;QAEA,OAAO0C,MAAAA,CAAOnB,cAAcF,GAAKC,EAAAA,IAAAA,CAAAA;;;;;"}
1
+ {"version":3,"file":"security.mjs","sources":["../../src/middlewares/security.ts"],"sourcesContent":["import { defaultsDeep, mergeWith } from 'lodash/fp';\nimport helmet, { KoaHelmet } from 'koa-helmet';\nimport { CSP_DEFAULTS } from '@strapi/utils';\n\nimport type { Core } from '@strapi/types';\n\nexport type Config = NonNullable<Parameters<KoaHelmet>[0]>;\n\nconst defaults: Config = {\n crossOriginEmbedderPolicy: false,\n crossOriginOpenerPolicy: false,\n crossOriginResourcePolicy: false,\n originAgentCluster: false,\n contentSecurityPolicy: {\n useDefaults: true,\n directives: {\n ...CSP_DEFAULTS,\n upgradeInsecureRequests: null,\n },\n },\n xssFilter: false,\n hsts: {\n maxAge: 31536000,\n includeSubDomains: true,\n },\n frameguard: {\n action: 'sameorigin',\n },\n};\n\nconst mergeConfig = (existingConfig: Config, newConfig: Config) => {\n return mergeWith(\n (obj, src) => (Array.isArray(obj) && Array.isArray(src) ? obj.concat(src) : undefined),\n existingConfig,\n newConfig\n );\n};\n\nexport const security: Core.MiddlewareFactory<Config> =\n (config, { strapi }) =>\n (ctx, next) => {\n let helmetConfig: Config = defaultsDeep(defaults, config);\n const specialPaths = ['/documentation'];\n\n const directives: {\n 'script-src': string[];\n 'img-src': string[];\n 'manifest-src': string[];\n 'frame-src': string[];\n } = {\n 'script-src': [\"'self'\", \"'unsafe-inline'\", 'cdn.jsdelivr.net'],\n 'img-src': [\"'self'\", 'data:', 'cdn.jsdelivr.net', 'strapi.io'],\n 'manifest-src': [],\n 'frame-src': [],\n };\n\n // if apollo graphql playground is enabled, add exceptions for it\n if (strapi.plugin('graphql')?.service('utils').playground.isEnabled()) {\n const { config: gqlConfig } = strapi.plugin('graphql');\n specialPaths.push(gqlConfig('endpoint'));\n\n directives['script-src'].push(`https: 'unsafe-inline'`);\n directives['img-src'].push(`'apollo-server-landing-page.cdn.apollographql.com'`);\n directives['manifest-src'].push(`'self'`);\n directives['manifest-src'].push('apollo-server-landing-page.cdn.apollographql.com');\n directives['frame-src'].push(`'self'`);\n directives['frame-src'].push('sandbox.embed.apollographql.com');\n }\n\n // TODO: we shouldn't combine playground exceptions with documentation for all routes, we should first check the path and then return exceptions specific to that\n if (ctx.method === 'GET' && specialPaths.some((str) => ctx.path.startsWith(str))) {\n helmetConfig = mergeConfig(helmetConfig, {\n crossOriginEmbedderPolicy: false, // TODO: only use this for graphql playground\n contentSecurityPolicy: {\n directives,\n },\n });\n }\n\n /**\n * These are for vite's watch mode so it can accurately\n * connect to the HMR websocket & reconnect on failure\n * or when the server restarts.\n *\n * It only applies in development, and only on GET requests\n * that are part of the admin route.\n */\n\n if (\n ['development', 'test'].includes(process.env.NODE_ENV ?? '') &&\n ctx.method === 'GET' &&\n ctx.path.startsWith(strapi.config.get('admin.path'))\n ) {\n helmetConfig = mergeConfig(helmetConfig, {\n contentSecurityPolicy: {\n directives: {\n 'script-src': [\"'self'\", \"'unsafe-inline'\"],\n 'connect-src': [\"'self'\", 'http:', 'https:', 'ws:'],\n },\n },\n });\n }\n\n return helmet(helmetConfig)(ctx, next);\n };\n"],"names":["defaults","crossOriginEmbedderPolicy","crossOriginOpenerPolicy","crossOriginResourcePolicy","originAgentCluster","contentSecurityPolicy","useDefaults","directives","CSP_DEFAULTS","upgradeInsecureRequests","xssFilter","hsts","maxAge","includeSubDomains","frameguard","action","mergeConfig","existingConfig","newConfig","mergeWith","obj","src","Array","isArray","concat","undefined","security","config","strapi","ctx","next","helmetConfig","defaultsDeep","specialPaths","plugin","service","playground","isEnabled","gqlConfig","push","method","some","str","path","startsWith","includes","process","env","NODE_ENV","get","helmet"],"mappings":";;;;AAQA,MAAMA,QAAmB,GAAA;IACvBC,yBAA2B,EAAA,KAAA;IAC3BC,uBAAyB,EAAA,KAAA;IACzBC,yBAA2B,EAAA,KAAA;IAC3BC,kBAAoB,EAAA,KAAA;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,GAAef,YAAYe,YAAc,EAAA;gBACvC1B,qBAAuB,EAAA;oBACrBE,UAAY,EAAA;wBACV,YAAc,EAAA;AAAC,4BAAA,QAAA;AAAU,4BAAA;AAAkB,yBAAA;wBAC3C,aAAe,EAAA;AAAC,4BAAA,QAAA;AAAU,4BAAA,OAAA;AAAS,4BAAA,QAAA;AAAU,4BAAA;AAAM;AACrD;AACF;AACF,aAAA,CAAA;AACF;QAEA,OAAO2C,MAAAA,CAAOnB,cAAcF,GAAKC,EAAAA,IAAAA,CAAAA;;;;;"}
@@ -1 +1 @@
1
- {"version":3,"file":"5.0.0-discard-drafts.d.ts","sourceRoot":"","sources":["../../../src/migrations/database/5.0.0-discard-drafts.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AAIH,OAAO,KAAK,EAAE,QAAQ,EAAE,SAAS,EAAE,MAAM,kBAAkB,CAAC;AAI5D,KAAK,eAAe,GAAG;IAAE,UAAU,EAAE,MAAM,CAAC;IAAC,MAAM,EAAE,MAAM,CAAA;CAAE,CAAC;AAC9D,KAAK,IAAI,GAAG,UAAU,CAAC,SAAS,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;AAqF3C;;;;;GAKG;AACH,wBAAuB,iBAAiB,CAAC,EACvC,EAAE,EACF,GAAG,EACH,GAAG,EACH,gBAAuB,GACxB,EAAE;IACD,EAAE,EAAE,QAAQ,CAAC;IACb,GAAG,EAAE,IAAI,CAAC;IACV,GAAG,EAAE,MAAM,CAAC;IACZ,gBAAgB,CAAC,EAAE,MAAM,CAAC;CAC3B,oDAgCA;AA2DD,eAAO,MAAM,qBAAqB,EAAE,SAQnC,CAAC"}
1
+ {"version":3,"file":"5.0.0-discard-drafts.d.ts","sourceRoot":"","sources":["../../../src/migrations/database/5.0.0-discard-drafts.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AAIH,OAAO,KAAK,EAAE,QAAQ,EAAE,SAAS,EAAE,MAAM,kBAAkB,CAAC;AAG5D,KAAK,eAAe,GAAG;IAAE,UAAU,EAAE,MAAM,CAAC;IAAC,MAAM,EAAE,MAAM,CAAA;CAAE,CAAC;AAC9D,KAAK,IAAI,GAAG,UAAU,CAAC,SAAS,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;AA6S3C;;;;;GAKG;AACH,wBAAuB,iBAAiB,CAAC,EACvC,EAAE,EACF,GAAG,EACH,GAAG,EACH,gBAAuB,GACxB,EAAE;IACD,EAAE,EAAE,QAAQ,CAAC;IACb,GAAG,EAAE,IAAI,CAAC;IACV,GAAG,EAAE,MAAM,CAAC;IACZ,gBAAgB,CAAC,EAAE,MAAM,CAAC;CAC3B,oDAgCA;AAED,eAAO,MAAM,qBAAqB,EAAE,SAQnC,CAAC"}
@@ -1,7 +1,6 @@
1
1
  'use strict';
2
2
 
3
3
  var strapiUtils = require('@strapi/utils');
4
- var index = require('../../services/document-service/index.js');
5
4
 
6
5
  /**
7
6
  * Check if the model has draft and publish enabled.
@@ -58,6 +57,174 @@ var index = require('../../services/document-service/index.js');
58
57
  .whereNotNull('published_at');
59
58
  });
60
59
  }
60
+ /**
61
+ * Copy relations from published entries to draft entries using direct database queries.
62
+ * This replaces the need to call discardDraft for each entry.
63
+ */ async function copyRelationsToDrafts({ db, trx, uid }) {
64
+ const meta = db.metadata.get(uid);
65
+ if (!meta) return;
66
+ // Get all published entries for this content type
67
+ const publishedEntries = await trx(meta.tableName).select([
68
+ 'id',
69
+ 'documentId',
70
+ 'locale'
71
+ ]).whereNotNull('published_at');
72
+ // Get all draft entries for this content type
73
+ const draftEntries = await trx(meta.tableName).select([
74
+ 'id',
75
+ 'documentId',
76
+ 'locale'
77
+ ]).whereNull('published_at');
78
+ if (publishedEntries.length === 0 || draftEntries.length === 0) {
79
+ return;
80
+ }
81
+ // Create mapping from documentId to draft entry ID (only for drafts created by migration)
82
+ const draftByDocumentId = new Map();
83
+ for (const draft of draftEntries){
84
+ if (draft.documentId) {
85
+ draftByDocumentId.set(draft.documentId, draft);
86
+ }
87
+ }
88
+ // Create mapping from published entry ID to draft entry ID
89
+ const publishedToDraftMap = new Map();
90
+ for (const published of publishedEntries){
91
+ const draft = draftByDocumentId.get(published.documentId);
92
+ if (draft) {
93
+ publishedToDraftMap.set(published.id, draft.id);
94
+ }
95
+ }
96
+ if (publishedToDraftMap.size === 0) {
97
+ return;
98
+ }
99
+ // Copy relations for this content type
100
+ await copyRelationsForContentType({
101
+ trx,
102
+ uid,
103
+ publishedToDraftMap,
104
+ publishedEntries
105
+ });
106
+ // Copy relations from other content types that target this content type
107
+ await copyRelationsFromOtherContentTypes({
108
+ trx,
109
+ uid,
110
+ publishedToDraftMap,
111
+ publishedEntries
112
+ });
113
+ }
114
+ /**
115
+ * Copy relations within the same content type (self-referential relations)
116
+ */ async function copyRelationsForContentType({ trx, uid, publishedToDraftMap }) {
117
+ const meta = strapi.db.metadata.get(uid);
118
+ if (!meta) return;
119
+ const publishedIds = Array.from(publishedToDraftMap.keys());
120
+ for (const attribute of Object.values(meta.attributes)){
121
+ if (attribute.type !== 'relation' || attribute.target !== uid) {
122
+ continue;
123
+ }
124
+ const joinTable = attribute.joinTable;
125
+ if (!joinTable) {
126
+ continue;
127
+ }
128
+ const { name: sourceColumnName } = joinTable.joinColumn;
129
+ const { name: targetColumnName } = joinTable.inverseJoinColumn;
130
+ // Get all relations where the source is a published entry
131
+ const relations = await trx(joinTable.name).select('*').whereIn(sourceColumnName, publishedIds);
132
+ if (relations.length === 0) {
133
+ continue;
134
+ }
135
+ // Create new relations pointing to draft entries
136
+ const newRelations = relations.map((relation)=>{
137
+ const newSourceId = publishedToDraftMap.get(relation[sourceColumnName]);
138
+ const newTargetId = publishedToDraftMap.get(relation[targetColumnName]);
139
+ if (!newSourceId || !newTargetId) {
140
+ return null;
141
+ }
142
+ return {
143
+ ...relation,
144
+ [sourceColumnName]: newSourceId,
145
+ [targetColumnName]: newTargetId
146
+ };
147
+ }).filter(Boolean);
148
+ if (newRelations.length > 0) {
149
+ await trx.batchInsert(joinTable.name, newRelations, 1000);
150
+ }
151
+ }
152
+ }
153
+ /**
154
+ * Copy relations from other content types that target this content type
155
+ */ async function copyRelationsFromOtherContentTypes({ trx, uid, publishedToDraftMap }) {
156
+ const targetIds = Array.from(publishedToDraftMap.keys());
157
+ // Iterate through all content types and components to find relations targeting our content type
158
+ const contentTypes = Object.values(strapi.contentTypes);
159
+ const components = Object.values(strapi.components);
160
+ for (const model of [
161
+ ...contentTypes,
162
+ ...components
163
+ ]){
164
+ const dbModel = strapi.db.metadata.get(model.uid);
165
+ if (!dbModel) continue;
166
+ for (const attribute of Object.values(dbModel.attributes)){
167
+ if (attribute.type !== 'relation' || attribute.target !== uid) {
168
+ continue;
169
+ }
170
+ const joinTable = attribute.joinTable;
171
+ if (!joinTable) {
172
+ continue;
173
+ }
174
+ const { name: targetColumnName } = joinTable.inverseJoinColumn;
175
+ // Get all relations where the target is a published entry of our content type
176
+ const relations = await trx(joinTable.name).select('*').whereIn(targetColumnName, targetIds);
177
+ if (relations.length === 0) {
178
+ continue;
179
+ }
180
+ // Create new relations pointing to draft entries
181
+ const newRelations = relations.map((relation)=>{
182
+ const newTargetId = publishedToDraftMap.get(relation[targetColumnName]);
183
+ if (!newTargetId) {
184
+ return null;
185
+ }
186
+ return {
187
+ ...relation,
188
+ [targetColumnName]: newTargetId
189
+ };
190
+ }).filter(Boolean);
191
+ if (newRelations.length > 0) {
192
+ await trx.batchInsert(joinTable.name, newRelations, 1000);
193
+ }
194
+ }
195
+ }
196
+ }
197
+ /**
198
+ * 2 pass migration to create the draft entries for all the published entries.
199
+ * And then copy relations directly using database queries.
200
+ */ const migrateUp = async (trx, db)=>{
201
+ const dpModels = [];
202
+ for (const meta of db.metadata.values()){
203
+ const hasDP = await hasDraftAndPublish(trx, meta);
204
+ if (hasDP) {
205
+ dpModels.push(meta);
206
+ }
207
+ }
208
+ /**
209
+ * Create plain draft entries for all the entries that were published.
210
+ */ for (const model of dpModels){
211
+ await copyPublishedEntriesToDraft({
212
+ db,
213
+ trx,
214
+ uid: model.uid
215
+ });
216
+ }
217
+ /**
218
+ * Copy relations from published entries to draft entries using direct database queries.
219
+ * This is much more efficient than calling discardDraft for each entry.
220
+ */ for (const model of dpModels){
221
+ await copyRelationsToDrafts({
222
+ db,
223
+ trx,
224
+ uid: model.uid
225
+ });
226
+ }
227
+ };
61
228
  /**
62
229
  * Load a batch of versions to discard.
63
230
  *
@@ -95,64 +262,6 @@ var index = require('../../services/document-service/index.js');
95
262
  yield batch;
96
263
  }
97
264
  }
98
- /**
99
- * 2 pass migration to create the draft entries for all the published entries.
100
- * And then discard the drafts to copy the relations.
101
- */ const migrateUp = async (trx, db)=>{
102
- const dpModels = [];
103
- for (const meta of db.metadata.values()){
104
- const hasDP = await hasDraftAndPublish(trx, meta);
105
- if (hasDP) {
106
- dpModels.push(meta);
107
- }
108
- }
109
- /**
110
- * Create plain draft entries for all the entries that were published.
111
- */ for (const model of dpModels){
112
- await copyPublishedEntriesToDraft({
113
- db,
114
- trx,
115
- uid: model.uid
116
- });
117
- }
118
- /**
119
- * Discard the drafts will copy the relations from the published entries to the newly created drafts.
120
- *
121
- * Load a batch of entries (batched to prevent loading millions of rows at once ),
122
- * and discard them using the document service.
123
- *
124
- * NOTE: This is using a custom document service without any validations,
125
- * to prevent the migration from failing if users already had invalid data in V4.
126
- * E.g. @see https://github.com/strapi/strapi/issues/21583
127
- */ const documentService = index.createDocumentService(strapi, {
128
- async validateEntityCreation (_, data) {
129
- return data;
130
- },
131
- async validateEntityUpdate (_, data) {
132
- // Data can be partially empty on partial updates
133
- // This migration doesn't trigger any update (or partial update),
134
- // so it's safe to return the data as is.
135
- return data;
136
- }
137
- });
138
- for (const model of dpModels){
139
- const discardDraft = async (entry)=>documentService(model.uid).discardDraft({
140
- documentId: entry.documentId,
141
- locale: entry.locale
142
- });
143
- for await (const batch of getBatchToDiscard({
144
- db,
145
- trx,
146
- uid: model.uid
147
- })){
148
- // NOTE: concurrency had to be disabled to prevent a race condition with self-references
149
- // TODO: improve performance in a safe way
150
- await strapiUtils.async.map(batch, discardDraft, {
151
- concurrency: 1
152
- });
153
- }
154
- }
155
- };
156
265
  const discardDocumentDrafts = {
157
266
  name: 'core::5.0.0-discard-drafts',
158
267
  async up (trx, db) {
@@ -1 +1 @@
1
- {"version":3,"file":"5.0.0-discard-drafts.js","sources":["../../../src/migrations/database/5.0.0-discard-drafts.ts"],"sourcesContent":["/**\n * This migration is responsible for creating the draft counterpart for all the entries that were in a published state.\n *\n * In v4, entries could either be in a draft or published state, but not both at the same time.\n * In v5, we introduced the concept of document, and an entry can be in a draft or published state.\n *\n * This means the migration needs to create the draft counterpart if an entry was published.\n *\n * This migration performs the following steps:\n * 1. Creates draft entries for all published entries, without it's components, dynamic zones or relations.\n * 2. Using the document service, discard those same drafts to copy its relations.\n */\n\n/* eslint-disable no-continue */\nimport type { UID } from '@strapi/types';\nimport type { Database, Migration } from '@strapi/database';\nimport { async, contentTypes } from '@strapi/utils';\nimport { createDocumentService } from '../../services/document-service';\n\ntype DocumentVersion = { documentId: string; locale: string };\ntype Knex = Parameters<Migration['up']>[0];\n\n/**\n * Check if the model has draft and publish enabled.\n */\nconst hasDraftAndPublish = async (trx: Knex, meta: any) => {\n const hasTable = await trx.schema.hasTable(meta.tableName);\n\n if (!hasTable) {\n return false;\n }\n\n const uid = meta.uid as UID.ContentType;\n const model = strapi.getModel(uid);\n const hasDP = contentTypes.hasDraftAndPublish(model);\n if (!hasDP) {\n return false;\n }\n\n return true;\n};\n\n/**\n * Copy all the published entries to draft entries, without it's components, dynamic zones or relations.\n * This ensures all necessary draft's exist before copying it's relations.\n */\nasync function copyPublishedEntriesToDraft({\n db,\n trx,\n uid,\n}: {\n db: Database;\n trx: Knex;\n uid: string;\n}) {\n // Extract all scalar attributes to use in the insert query\n const meta = db.metadata.get(uid);\n\n // Get scalar attributes that will be copied over the new draft\n const scalarAttributes = Object.values(meta.attributes).reduce((acc, attribute: any) => {\n if (['id'].includes(attribute.columnName)) {\n return acc;\n }\n\n if (contentTypes.isScalarAttribute(attribute)) {\n acc.push(attribute.columnName);\n }\n\n return acc;\n }, [] as string[]);\n\n /**\n * Query to copy the published entries into draft entries.\n *\n * INSERT INTO tableName (columnName1, columnName2, columnName3, ...)\n * SELECT columnName1, columnName2, columnName3, ...\n * FROM tableName\n */\n await trx\n // INSERT INTO tableName (columnName1, columnName2, columnName3, ...)\n .into(\n trx.raw(`?? (${scalarAttributes.map(() => `??`).join(', ')})`, [\n meta.tableName,\n ...scalarAttributes,\n ])\n )\n .insert((subQb: typeof trx) => {\n // SELECT columnName1, columnName2, columnName3, ...\n subQb\n .select(\n ...scalarAttributes.map((att: string) => {\n // Override 'publishedAt' and 'updatedAt' attributes\n if (att === 'published_at') {\n return trx.raw('NULL as ??', 'published_at');\n }\n\n return att;\n })\n )\n .from(meta.tableName)\n // Only select entries that were published\n .whereNotNull('published_at');\n });\n}\n\n/**\n * Load a batch of versions to discard.\n *\n * Versions with only a draft version will be ignored.\n * Only versions with a published version (which always have a draft version) will be discarded.\n */\nexport async function* getBatchToDiscard({\n db,\n trx,\n uid,\n defaultBatchSize = 1000,\n}: {\n db: Database;\n trx: Knex;\n uid: string;\n defaultBatchSize?: number;\n}) {\n const client = db.config.connection.client;\n const isSQLite =\n typeof client === 'string' && ['sqlite', 'sqlite3', 'better-sqlite3'].includes(client);\n\n // The SQLite documentation states that the maximum number of terms in a\n // compound SELECT statement is 500 by default.\n // See: https://www.sqlite.org/limits.html\n // To ensure a successful migration, we limit the batch size to 500 for SQLite.\n const batchSize = isSQLite ? Math.min(defaultBatchSize, 500) : defaultBatchSize;\n let offset = 0;\n let hasMore = true;\n\n while (hasMore) {\n // Look for the published entries to discard\n const batch: DocumentVersion[] = await db\n .queryBuilder(uid)\n .select(['id', 'documentId', 'locale'])\n .where({ publishedAt: { $ne: null } })\n .limit(batchSize)\n .offset(offset)\n .orderBy('id')\n .transacting(trx)\n .execute();\n\n if (batch.length < batchSize) {\n hasMore = false;\n }\n\n offset += batchSize;\n yield batch;\n }\n}\n\n/**\n * 2 pass migration to create the draft entries for all the published entries.\n * And then discard the drafts to copy the relations.\n */\nconst migrateUp = async (trx: Knex, db: Database) => {\n const dpModels = [];\n for (const meta of db.metadata.values()) {\n const hasDP = await hasDraftAndPublish(trx, meta);\n if (hasDP) {\n dpModels.push(meta);\n }\n }\n\n /**\n * Create plain draft entries for all the entries that were published.\n */\n for (const model of dpModels) {\n await copyPublishedEntriesToDraft({ db, trx, uid: model.uid });\n }\n\n /**\n * Discard the drafts will copy the relations from the published entries to the newly created drafts.\n *\n * Load a batch of entries (batched to prevent loading millions of rows at once ),\n * and discard them using the document service.\n *\n * NOTE: This is using a custom document service without any validations,\n * to prevent the migration from failing if users already had invalid data in V4.\n * E.g. @see https://github.com/strapi/strapi/issues/21583\n */\n const documentService = createDocumentService(strapi, {\n async validateEntityCreation(_, data) {\n return data;\n },\n async validateEntityUpdate(_, data) {\n // Data can be partially empty on partial updates\n // This migration doesn't trigger any update (or partial update),\n // so it's safe to return the data as is.\n return data as any;\n },\n });\n\n for (const model of dpModels) {\n const discardDraft = async (entry: DocumentVersion) =>\n documentService(model.uid as UID.ContentType).discardDraft({\n documentId: entry.documentId,\n locale: entry.locale,\n });\n\n for await (const batch of getBatchToDiscard({ db, trx, uid: model.uid })) {\n // NOTE: concurrency had to be disabled to prevent a race condition with self-references\n // TODO: improve performance in a safe way\n await async.map(batch, discardDraft, { concurrency: 1 });\n }\n }\n};\n\nexport const discardDocumentDrafts: Migration = {\n name: 'core::5.0.0-discard-drafts',\n async up(trx, db) {\n await migrateUp(trx, db);\n },\n async down() {\n throw new Error('not implemented');\n },\n};\n"],"names":["hasDraftAndPublish","trx","meta","hasTable","schema","tableName","uid","model","strapi","getModel","hasDP","contentTypes","copyPublishedEntriesToDraft","db","metadata","get","scalarAttributes","Object","values","attributes","reduce","acc","attribute","includes","columnName","isScalarAttribute","push","into","raw","map","join","insert","subQb","select","att","from","whereNotNull","getBatchToDiscard","defaultBatchSize","client","config","connection","isSQLite","batchSize","Math","min","offset","hasMore","batch","queryBuilder","where","publishedAt","$ne","limit","orderBy","transacting","execute","length","migrateUp","dpModels","documentService","createDocumentService","validateEntityCreation","_","data","validateEntityUpdate","discardDraft","entry","documentId","locale","async","concurrency","discardDocumentDrafts","name","up","down","Error"],"mappings":";;;;;AAsBA;;IAGA,MAAMA,kBAAqB,GAAA,OAAOC,GAAWC,EAAAA,IAAAA,GAAAA;IAC3C,MAAMC,QAAAA,GAAW,MAAMF,GAAIG,CAAAA,MAAM,CAACD,QAAQ,CAACD,KAAKG,SAAS,CAAA;AAEzD,IAAA,IAAI,CAACF,QAAU,EAAA;QACb,OAAO,KAAA;AACT;IAEA,MAAMG,GAAAA,GAAMJ,KAAKI,GAAG;IACpB,MAAMC,KAAAA,GAAQC,MAAOC,CAAAA,QAAQ,CAACH,GAAAA,CAAAA;IAC9B,MAAMI,KAAAA,GAAQC,wBAAaX,CAAAA,kBAAkB,CAACO,KAAAA,CAAAA;AAC9C,IAAA,IAAI,CAACG,KAAO,EAAA;QACV,OAAO,KAAA;AACT;IAEA,OAAO,IAAA;AACT,CAAA;AAEA;;;IAIA,eAAeE,4BAA4B,EACzCC,EAAE,EACFZ,GAAG,EACHK,GAAG,EAKJ,EAAA;;AAEC,IAAA,MAAMJ,IAAOW,GAAAA,EAAAA,CAAGC,QAAQ,CAACC,GAAG,CAACT,GAAAA,CAAAA;;IAG7B,MAAMU,gBAAAA,GAAmBC,MAAOC,CAAAA,MAAM,CAAChB,IAAAA,CAAKiB,UAAU,CAAEC,CAAAA,MAAM,CAAC,CAACC,GAAKC,EAAAA,SAAAA,GAAAA;QACnE,IAAI;AAAC,YAAA;AAAK,SAAA,CAACC,QAAQ,CAACD,SAAUE,CAAAA,UAAU,CAAG,EAAA;YACzC,OAAOH,GAAAA;AACT;QAEA,IAAIV,wBAAAA,CAAac,iBAAiB,CAACH,SAAY,CAAA,EAAA;YAC7CD,GAAIK,CAAA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AA;AAC9D;AAEA;;;;;;;;;MAUA,MAAMsD,eAAkBC,GAAAA,2BAAAA,CAAsBrD,MAAQ,EAAA;QACpD,MAAMsD,sBAAAA,CAAAA,CAAuBC,CAAC,EAAEC,IAAI,EAAA;YAClC,OAAOA,IAAAA;AACT,SAAA;QACA,MAAMC,oBAAAA,CAAAA,CAAqBF,CAAC,EAAEC,IAAI,EAAA;;;;YAIhC,OAAOA,IAAAA;AACT;AACF,KAAA,CAAA;IAEA,KAAK,MAAMzD,SAASoD,QAAU,CAAA;QAC5B,MAAMO,YAAAA,GAAe,OAAOC,KAC1BP,GAAAA,eAAAA,CAAgBrD,MAAMD,GAAG,CAAA,CAAqB4D,YAAY,CAAC;AACzDE,gBAAAA,UAAAA,EAAYD,MAAMC,UAAU;AAC5BC,gBAAAA,MAAAA,EAAQF,MAAME;AAChB,aAAA,CAAA;QAEF,WAAW,MAAMrB,SAASX,iBAAkB,CAAA;AAAExB,YAAAA,EAAAA;AAAIZ,YAAAA,GAAAA;AAAKK,YAAAA,GAAAA,EAAKC,MAAMD;SAAQ,CAAA,CAAA;;;AAGxE,YAAA,MAAMgE,iBAAMzC,CAAAA,GAAG,CAACmB,KAAAA,EAAOkB,YAAc,EAAA;gBAAEK,WAAa,EAAA;AAAE,aAAA,CAAA;AACxD;AACF;AACF,CAAA;MAEaC,qBAAmC,GAAA;IAC9CC,IAAM,EAAA,4BAAA;IACN,MAAMC,EAAAA,CAAAA,CAAGzE,GAAG,EAAEY,EAAE,EAAA;AACd,QAAA,MAAM6C,UAAUzD,GAAKY,EAAAA,EAAAA,CAAAA;AACvB,KAAA;IACA,MAAM8D,IAAAA,CAAAA,GAAAA;AACJ,QAAA,MAAM,IAAIC,KAAM,CAAA,iBAAA,CAAA;AAClB;AACF;;;;;"}
1
+ {"version":3,"file":"5.0.0-discard-drafts.js","sources":["../../../src/migrations/database/5.0.0-discard-drafts.ts"],"sourcesContent":["/**\n * This migration is responsible for creating the draft counterpart for all the entries that were in a published state.\n *\n * In v4, entries could either be in a draft or published state, but not both at the same time.\n * In v5, we introduced the concept of document, and an entry can be in a draft or published state.\n *\n * This means the migration needs to create the draft counterpart if an entry was published.\n *\n * This migration performs the following steps:\n * 1. Creates draft entries for all published entries, without it's components, dynamic zones or relations.\n * 2. Using the document service, discard those same drafts to copy its relations.\n */\n\n/* eslint-disable no-continue */\nimport type { UID } from '@strapi/types';\nimport type { Database, Migration } from '@strapi/database';\nimport { contentTypes } from '@strapi/utils';\n\ntype DocumentVersion = { documentId: string; locale: string };\ntype Knex = Parameters<Migration['up']>[0];\n\n/**\n * Check if the model has draft and publish enabled.\n */\nconst hasDraftAndPublish = async (trx: Knex, meta: any) => {\n const hasTable = await trx.schema.hasTable(meta.tableName);\n\n if (!hasTable) {\n return false;\n }\n\n const uid = meta.uid as UID.ContentType;\n const model = strapi.getModel(uid);\n const hasDP = contentTypes.hasDraftAndPublish(model);\n if (!hasDP) {\n return false;\n }\n\n return true;\n};\n\n/**\n * Copy all the published entries to draft entries, without it's components, dynamic zones or relations.\n * This ensures all necessary draft's exist before copying it's relations.\n */\nasync function copyPublishedEntriesToDraft({\n db,\n trx,\n uid,\n}: {\n db: Database;\n trx: Knex;\n uid: string;\n}) {\n // Extract all scalar attributes to use in the insert query\n const meta = db.metadata.get(uid);\n\n // Get scalar attributes that will be copied over the new draft\n const scalarAttributes = Object.values(meta.attributes).reduce((acc, attribute: any) => {\n if (['id'].includes(attribute.columnName)) {\n return acc;\n }\n\n if (contentTypes.isScalarAttribute(attribute)) {\n acc.push(attribute.columnName);\n }\n\n return acc;\n }, [] as string[]);\n\n /**\n * Query to copy the published entries into draft entries.\n *\n * INSERT INTO tableName (columnName1, columnName2, columnName3, ...)\n * SELECT columnName1, columnName2, columnName3, ...\n * FROM tableName\n */\n await trx\n // INSERT INTO tableName (columnName1, columnName2, columnName3, ...)\n .into(\n trx.raw(`?? (${scalarAttributes.map(() => `??`).join(', ')})`, [\n meta.tableName,\n ...scalarAttributes,\n ])\n )\n .insert((subQb: typeof trx) => {\n // SELECT columnName1, columnName2, columnName3, ...\n subQb\n .select(\n ...scalarAttributes.map((att: string) => {\n // Override 'publishedAt' and 'updatedAt' attributes\n if (att === 'published_at') {\n return trx.raw('NULL as ??', 'published_at');\n }\n\n return att;\n })\n )\n .from(meta.tableName)\n // Only select entries that were published\n .whereNotNull('published_at');\n });\n}\n\n/**\n * Copy relations from published entries to draft entries using direct database queries.\n * This replaces the need to call discardDraft for each entry.\n */\nasync function copyRelationsToDrafts({ db, trx, uid }: { db: Database; trx: Knex; uid: string }) {\n const meta = db.metadata.get(uid);\n if (!meta) return;\n\n // Get all published entries for this content type\n const publishedEntries = (await trx(meta.tableName)\n .select(['id', 'documentId', 'locale'])\n .whereNotNull('published_at')) as Array<{ id: number; documentId: string; locale: string }>;\n\n // Get all draft entries for this content type\n const draftEntries = (await trx(meta.tableName)\n .select(['id', 'documentId', 'locale'])\n .whereNull('published_at')) as Array<{ id: number; documentId: string; locale: string }>;\n\n if (publishedEntries.length === 0 || draftEntries.length === 0) {\n return;\n }\n\n // Create mapping from documentId to draft entry ID (only for drafts created by migration)\n const draftByDocumentId = new Map();\n for (const draft of draftEntries) {\n if (draft.documentId) {\n draftByDocumentId.set(draft.documentId, draft);\n }\n }\n\n // Create mapping from published entry ID to draft entry ID\n const publishedToDraftMap = new Map();\n for (const published of publishedEntries) {\n const draft = draftByDocumentId.get(published.documentId);\n if (draft) {\n publishedToDraftMap.set(published.id, draft.id);\n }\n }\n\n if (publishedToDraftMap.size === 0) {\n return;\n }\n\n // Copy relations for this content type\n await copyRelationsForContentType({\n trx,\n uid,\n publishedToDraftMap,\n publishedEntries,\n });\n\n // Copy relations from other content types that target this content type\n await copyRelationsFromOtherContentTypes({\n trx,\n uid,\n publishedToDraftMap,\n publishedEntries,\n });\n}\n\n/**\n * Copy relations within the same content type (self-referential relations)\n */\nasync function copyRelationsForContentType({\n trx,\n uid,\n publishedToDraftMap,\n}: {\n trx: Knex;\n uid: string;\n publishedToDraftMap: Map<number, number>;\n publishedEntries: Array<{ id: number; documentId: string; locale: string }>;\n}) {\n const meta = strapi.db.metadata.get(uid);\n if (!meta) return;\n\n const publishedIds = Array.from(publishedToDraftMap.keys());\n\n for (const attribute of Object.values(meta.attributes) as any) {\n if (attribute.type !== 'relation' || attribute.target !== uid) {\n continue;\n }\n\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 // Get all relations where the source is a published entry\n const relations = await trx(joinTable.name).select('*').whereIn(sourceColumnName, publishedIds);\n\n if (relations.length === 0) {\n continue;\n }\n\n // Create new relations pointing to draft entries\n const newRelations = relations\n .map((relation) => {\n const newSourceId = publishedToDraftMap.get(relation[sourceColumnName]);\n const newTargetId = publishedToDraftMap.get(relation[targetColumnName]);\n\n if (!newSourceId || !newTargetId) {\n return null;\n }\n\n return {\n ...relation,\n [sourceColumnName]: newSourceId,\n [targetColumnName]: newTargetId,\n };\n })\n .filter(Boolean);\n\n if (newRelations.length > 0) {\n await trx.batchInsert(joinTable.name, newRelations, 1000);\n }\n }\n}\n\n/**\n * Copy relations from other content types that target this content type\n */\nasync function copyRelationsFromOtherContentTypes({\n trx,\n uid,\n publishedToDraftMap,\n}: {\n trx: Knex;\n uid: string;\n publishedToDraftMap: Map<number, number>;\n publishedEntries: Array<{ id: number; documentId: string; locale: string }>;\n}) {\n const targetIds = Array.from(publishedToDraftMap.keys());\n\n // Iterate through all content types and components to find relations targeting our content type\n const contentTypes = Object.values(strapi.contentTypes) as any[];\n const components = Object.values(strapi.components) as any[];\n\n for (const model of [...contentTypes, ...components]) {\n const dbModel = strapi.db.metadata.get(model.uid);\n if (!dbModel) continue;\n\n for (const attribute of Object.values(dbModel.attributes) as any) {\n if (attribute.type !== 'relation' || attribute.target !== uid) {\n continue;\n }\n\n const joinTable = attribute.joinTable;\n if (!joinTable) {\n continue;\n }\n\n const { name: targetColumnName } = joinTable.inverseJoinColumn;\n\n // Get all relations where the target is a published entry of our content type\n const relations = await trx(joinTable.name).select('*').whereIn(targetColumnName, targetIds);\n\n if (relations.length === 0) {\n continue;\n }\n\n // Create new relations pointing to draft entries\n const newRelations = relations\n .map((relation) => {\n const newTargetId = publishedToDraftMap.get(relation[targetColumnName]);\n\n if (!newTargetId) {\n return null;\n }\n\n return {\n ...relation,\n [targetColumnName]: newTargetId,\n };\n })\n .filter(Boolean);\n\n if (newRelations.length > 0) {\n await trx.batchInsert(joinTable.name, newRelations, 1000);\n }\n }\n }\n}\n\n/**\n * 2 pass migration to create the draft entries for all the published entries.\n * And then copy relations directly using database queries.\n */\nconst migrateUp = async (trx: Knex, db: Database) => {\n const dpModels = [];\n for (const meta of db.metadata.values()) {\n const hasDP = await hasDraftAndPublish(trx, meta);\n if (hasDP) {\n dpModels.push(meta);\n }\n }\n\n /**\n * Create plain draft entries for all the entries that were published.\n */\n for (const model of dpModels) {\n await copyPublishedEntriesToDraft({ db, trx, uid: model.uid });\n }\n\n /**\n * Copy relations from published entries to draft entries using direct database queries.\n * This is much more efficient than calling discardDraft for each entry.\n */\n for (const model of dpModels) {\n await copyRelationsToDrafts({ db, trx, uid: model.uid });\n }\n};\n\n/**\n * Load a batch of versions to discard.\n *\n * Versions with only a draft version will be ignored.\n * Only versions with a published version (which always have a draft version) will be discarded.\n */\nexport async function* getBatchToDiscard({\n db,\n trx,\n uid,\n defaultBatchSize = 1000,\n}: {\n db: Database;\n trx: Knex;\n uid: string;\n defaultBatchSize?: number;\n}) {\n const client = db.config.connection.client;\n const isSQLite =\n typeof client === 'string' && ['sqlite', 'sqlite3', 'better-sqlite3'].includes(client);\n\n // The SQLite documentation states that the maximum number of terms in a\n // compound SELECT statement is 500 by default.\n // See: https://www.sqlite.org/limits.html\n // To ensure a successful migration, we limit the batch size to 500 for SQLite.\n const batchSize = isSQLite ? Math.min(defaultBatchSize, 500) : defaultBatchSize;\n let offset = 0;\n let hasMore = true;\n\n while (hasMore) {\n // Look for the published entries to discard\n const batch: DocumentVersion[] = await db\n .queryBuilder(uid)\n .select(['id', 'documentId', 'locale'])\n .where({ publishedAt: { $ne: null } })\n .limit(batchSize)\n .offset(offset)\n .orderBy('id')\n .transacting(trx)\n .execute();\n\n if (batch.length < batchSize) {\n hasMore = false;\n }\n\n offset += batchSize;\n yield batch;\n }\n}\n\nexport const discardDocumentDrafts: Migration = {\n name: 'core::5.0.0-discard-drafts',\n async up(trx, db) {\n await migrateUp(trx, db);\n },\n async down() {\n throw new Error('not implemented');\n },\n};\n"],"names":["hasDraftAndPublish","trx","meta","hasTable","schema","tableName","uid","model","strapi","getModel","hasDP","contentTypes","copyPublishedEntriesToDraft","db","metadata","get","scalarAttributes","Object","values","attributes","reduce","acc","attribute","includes","columnName","isScalarAttribute","push","into","raw","map","join","insert","subQb","select","att","from","whereNotNull","copyRelationsToDrafts","publishedEntries","draftEntries","whereNull","length","draftByDocumentId","Map","draft","documentId","set","publishedToDraftMap","published","id","size","copyRelationsForContentType","copyRelationsFromOtherContentTypes","publishedIds","Array","keys","type","target","joinTable","name","sourceColumnName","joinColumn","targetColumnName","inverseJoinColumn","relations","whereIn","newRelations","relation","newSourceId","newTargetId","filter","Boolean","batchInsert","targetIds","components","dbModel","migrateUp","dpModels","getBatchToDiscard","defaultBatchSize","client","config","connection","isSQLite","batchSize","Math","min","offset","hasMore","batch","queryBuilder","where","publishedAt","$ne","limit","orderBy","transacting","execute","discardDocumentDrafts","up","down","Error"],"mappings":";;;;AAqBA;;IAGA,MAAMA,kBAAqB,GAAA,OAAOC,GAAWC,EAAAA,IAAAA,GAAAA;IAC3C,MAAMC,QAAAA,GAAW,MAAMF,GAAIG,CAAAA,MAAM,CAACD,QAAQ,CAACD,KAAKG,SAAS,CAAA;AAEzD,IAAA,IAAI,CAACF,QAAU,EAAA;QACb,OAAO,KAAA;AACT;IAEA,MAAMG,GAAAA,GAAMJ,KAAKI,GAAG;IACpB,MAAMC,KAAAA,GAAQC,MAA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