@strapi/core 0.0.0-experimental.da8483f09053a467ff0b2147c207ba0d24848901 → 0.0.0-experimental.da850ac6030a73229550aab5ce80ce47be683429

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 (259) 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/constants.d.ts +3 -0
  8. package/dist/constants.d.ts.map +1 -0
  9. package/dist/constants.js +6 -0
  10. package/dist/constants.js.map +1 -0
  11. package/dist/constants.mjs +4 -0
  12. package/dist/constants.mjs.map +1 -0
  13. package/dist/core-api/controller/index.d.ts.map +1 -1
  14. package/dist/core-api/controller/index.js +2 -1
  15. package/dist/core-api/controller/index.js.map +1 -1
  16. package/dist/core-api/controller/index.mjs +2 -1
  17. package/dist/core-api/controller/index.mjs.map +1 -1
  18. package/dist/core-api/controller/transform.d.ts +3 -2
  19. package/dist/core-api/controller/transform.d.ts.map +1 -1
  20. package/dist/core-api/controller/transform.js +13 -3
  21. package/dist/core-api/controller/transform.js.map +1 -1
  22. package/dist/core-api/controller/transform.mjs +13 -3
  23. package/dist/core-api/controller/transform.mjs.map +1 -1
  24. package/dist/core-api/routes/index.d.ts +4 -22
  25. package/dist/core-api/routes/index.d.ts.map +1 -1
  26. package/dist/core-api/routes/index.js +150 -8
  27. package/dist/core-api/routes/index.js.map +1 -1
  28. package/dist/core-api/routes/index.mjs +131 -8
  29. package/dist/core-api/routes/index.mjs.map +1 -1
  30. package/dist/core-api/routes/validation/attributes.d.ts +244 -0
  31. package/dist/core-api/routes/validation/attributes.d.ts.map +1 -0
  32. package/dist/core-api/routes/validation/attributes.js +560 -0
  33. package/dist/core-api/routes/validation/attributes.js.map +1 -0
  34. package/dist/core-api/routes/validation/attributes.mjs +521 -0
  35. package/dist/core-api/routes/validation/attributes.mjs.map +1 -0
  36. package/dist/core-api/routes/validation/common.d.ts +105 -0
  37. package/dist/core-api/routes/validation/common.d.ts.map +1 -0
  38. package/dist/core-api/routes/validation/common.js +116 -0
  39. package/dist/core-api/routes/validation/common.js.map +1 -0
  40. package/dist/core-api/routes/validation/common.mjs +95 -0
  41. package/dist/core-api/routes/validation/common.mjs.map +1 -0
  42. package/dist/core-api/routes/validation/component.d.ts +34 -0
  43. package/dist/core-api/routes/validation/component.d.ts.map +1 -0
  44. package/dist/core-api/routes/validation/component.js +45 -0
  45. package/dist/core-api/routes/validation/component.js.map +1 -0
  46. package/dist/core-api/routes/validation/component.mjs +43 -0
  47. package/dist/core-api/routes/validation/component.mjs.map +1 -0
  48. package/dist/core-api/routes/validation/constants.d.ts +8 -0
  49. package/dist/core-api/routes/validation/constants.d.ts.map +1 -0
  50. package/dist/core-api/routes/validation/constants.js +18 -0
  51. package/dist/core-api/routes/validation/constants.js.map +1 -0
  52. package/dist/core-api/routes/validation/constants.mjs +16 -0
  53. package/dist/core-api/routes/validation/constants.mjs.map +1 -0
  54. package/dist/core-api/routes/validation/content-type.d.ts +128 -0
  55. package/dist/core-api/routes/validation/content-type.d.ts.map +1 -0
  56. package/dist/core-api/routes/validation/content-type.js +201 -0
  57. package/dist/core-api/routes/validation/content-type.js.map +1 -0
  58. package/dist/core-api/routes/validation/content-type.mjs +180 -0
  59. package/dist/core-api/routes/validation/content-type.mjs.map +1 -0
  60. package/dist/core-api/routes/validation/index.d.ts +5 -0
  61. package/dist/core-api/routes/validation/index.d.ts.map +1 -0
  62. package/dist/core-api/routes/validation/mappers.d.ts +105 -0
  63. package/dist/core-api/routes/validation/mappers.d.ts.map +1 -0
  64. package/dist/core-api/routes/validation/mappers.js +273 -0
  65. package/dist/core-api/routes/validation/mappers.js.map +1 -0
  66. package/dist/core-api/routes/validation/mappers.mjs +249 -0
  67. package/dist/core-api/routes/validation/mappers.mjs.map +1 -0
  68. package/dist/core-api/routes/validation/utils.d.ts +47 -0
  69. package/dist/core-api/routes/validation/utils.d.ts.map +1 -0
  70. package/dist/core-api/routes/validation/utils.js +128 -0
  71. package/dist/core-api/routes/validation/utils.js.map +1 -0
  72. package/dist/core-api/routes/validation/utils.mjs +106 -0
  73. package/dist/core-api/routes/validation/utils.mjs.map +1 -0
  74. package/dist/domain/content-type/index.d.ts.map +1 -1
  75. package/dist/domain/content-type/index.js +17 -1
  76. package/dist/domain/content-type/index.js.map +1 -1
  77. package/dist/domain/content-type/index.mjs +17 -1
  78. package/dist/domain/content-type/index.mjs.map +1 -1
  79. package/dist/domain/module/index.d.ts.map +1 -1
  80. package/dist/domain/module/index.js +3 -0
  81. package/dist/domain/module/index.js.map +1 -1
  82. package/dist/domain/module/index.mjs +3 -0
  83. package/dist/domain/module/index.mjs.map +1 -1
  84. package/dist/ee/index.d.ts +6 -0
  85. package/dist/ee/index.d.ts.map +1 -1
  86. package/dist/ee/index.js +29 -3
  87. package/dist/ee/index.js.map +1 -1
  88. package/dist/ee/index.mjs +30 -4
  89. package/dist/ee/index.mjs.map +1 -1
  90. package/dist/ee/license.d.ts +3 -1
  91. package/dist/ee/license.d.ts.map +1 -1
  92. package/dist/ee/license.js +6 -1
  93. package/dist/ee/license.js.map +1 -1
  94. package/dist/ee/license.mjs +6 -2
  95. package/dist/ee/license.mjs.map +1 -1
  96. package/dist/factories.d.ts +3 -1
  97. package/dist/factories.d.ts.map +1 -1
  98. package/dist/factories.js +10 -2
  99. package/dist/factories.js.map +1 -1
  100. package/dist/factories.mjs +10 -3
  101. package/dist/factories.mjs.map +1 -1
  102. package/dist/index.js +1 -1
  103. package/dist/index.mjs +1 -1
  104. package/dist/loaders/plugins/index.js +1 -1
  105. package/dist/loaders/plugins/index.js.map +1 -1
  106. package/dist/loaders/plugins/index.mjs +1 -1
  107. package/dist/loaders/plugins/index.mjs.map +1 -1
  108. package/dist/middlewares/cors.d.ts +9 -1
  109. package/dist/middlewares/cors.d.ts.map +1 -1
  110. package/dist/middlewares/cors.js +39 -17
  111. package/dist/middlewares/cors.js.map +1 -1
  112. package/dist/middlewares/cors.mjs +39 -18
  113. package/dist/middlewares/cors.mjs.map +1 -1
  114. package/dist/middlewares/security.d.ts.map +1 -1
  115. package/dist/middlewares/security.js +2 -15
  116. package/dist/middlewares/security.js.map +1 -1
  117. package/dist/middlewares/security.mjs +2 -15
  118. package/dist/middlewares/security.mjs.map +1 -1
  119. package/dist/migrations/first-published-at.d.ts +4 -0
  120. package/dist/migrations/first-published-at.d.ts.map +1 -0
  121. package/dist/migrations/first-published-at.js +51 -0
  122. package/dist/migrations/first-published-at.js.map +1 -0
  123. package/dist/migrations/first-published-at.mjs +49 -0
  124. package/dist/migrations/first-published-at.mjs.map +1 -0
  125. package/dist/migrations/index.d.ts.map +1 -1
  126. package/dist/migrations/index.js +5 -0
  127. package/dist/migrations/index.js.map +1 -1
  128. package/dist/migrations/index.mjs +5 -0
  129. package/dist/migrations/index.mjs.map +1 -1
  130. package/dist/package.json.js +18 -13
  131. package/dist/package.json.js.map +1 -1
  132. package/dist/package.json.mjs +18 -13
  133. package/dist/package.json.mjs.map +1 -1
  134. package/dist/providers/index.d.ts.map +1 -1
  135. package/dist/providers/index.js +2 -0
  136. package/dist/providers/index.js.map +1 -1
  137. package/dist/providers/index.mjs +2 -0
  138. package/dist/providers/index.mjs.map +1 -1
  139. package/dist/providers/session-manager.d.ts +3 -0
  140. package/dist/providers/session-manager.d.ts.map +1 -0
  141. package/dist/providers/session-manager.js +23 -0
  142. package/dist/providers/session-manager.js.map +1 -0
  143. package/dist/providers/session-manager.mjs +21 -0
  144. package/dist/providers/session-manager.mjs.map +1 -0
  145. package/dist/services/content-api/index.d.ts +1 -1
  146. package/dist/services/content-api/index.d.ts.map +1 -1
  147. package/dist/services/content-api/index.js +1 -1
  148. package/dist/services/content-api/index.js.map +1 -1
  149. package/dist/services/content-api/index.mjs +2 -2
  150. package/dist/services/content-api/index.mjs.map +1 -1
  151. package/dist/services/content-source-maps.d.ts +13 -0
  152. package/dist/services/content-source-maps.d.ts.map +1 -0
  153. package/dist/services/content-source-maps.js +108 -0
  154. package/dist/services/content-source-maps.js.map +1 -0
  155. package/dist/services/content-source-maps.mjs +106 -0
  156. package/dist/services/content-source-maps.mjs.map +1 -0
  157. package/dist/services/core-store.d.ts +2 -2
  158. package/dist/services/core-store.d.ts.map +1 -1
  159. package/dist/services/core-store.js.map +1 -1
  160. package/dist/services/core-store.mjs.map +1 -1
  161. package/dist/services/document-service/components.d.ts +31 -1
  162. package/dist/services/document-service/components.d.ts.map +1 -1
  163. package/dist/services/document-service/components.js +109 -0
  164. package/dist/services/document-service/components.js.map +1 -1
  165. package/dist/services/document-service/components.mjs +107 -1
  166. package/dist/services/document-service/components.mjs.map +1 -1
  167. package/dist/services/document-service/entries.d.ts.map +1 -1
  168. package/dist/services/document-service/entries.js +42 -0
  169. package/dist/services/document-service/entries.js.map +1 -1
  170. package/dist/services/document-service/entries.mjs +43 -1
  171. package/dist/services/document-service/entries.mjs.map +1 -1
  172. package/dist/services/document-service/first-published-at.d.ts +7 -0
  173. package/dist/services/document-service/first-published-at.d.ts.map +1 -0
  174. package/dist/services/document-service/first-published-at.js +31 -0
  175. package/dist/services/document-service/first-published-at.js.map +1 -0
  176. package/dist/services/document-service/first-published-at.mjs +28 -0
  177. package/dist/services/document-service/first-published-at.mjs.map +1 -0
  178. package/dist/services/document-service/internationalization.d.ts +6 -1
  179. package/dist/services/document-service/internationalization.d.ts.map +1 -1
  180. package/dist/services/document-service/internationalization.js +32 -0
  181. package/dist/services/document-service/internationalization.js.map +1 -1
  182. package/dist/services/document-service/internationalization.mjs +32 -1
  183. package/dist/services/document-service/internationalization.mjs.map +1 -1
  184. package/dist/services/document-service/repository.d.ts.map +1 -1
  185. package/dist/services/document-service/repository.js +16 -8
  186. package/dist/services/document-service/repository.js.map +1 -1
  187. package/dist/services/document-service/repository.mjs +18 -10
  188. package/dist/services/document-service/repository.mjs.map +1 -1
  189. package/dist/services/document-service/utils/clean-component-join-table.d.ts +7 -0
  190. package/dist/services/document-service/utils/clean-component-join-table.d.ts.map +1 -0
  191. package/dist/services/document-service/utils/clean-component-join-table.js +145 -0
  192. package/dist/services/document-service/utils/clean-component-join-table.js.map +1 -0
  193. package/dist/services/document-service/utils/clean-component-join-table.mjs +143 -0
  194. package/dist/services/document-service/utils/clean-component-join-table.mjs.map +1 -0
  195. package/dist/services/document-service/utils/unidirectional-relations.d.ts +19 -2
  196. package/dist/services/document-service/utils/unidirectional-relations.d.ts.map +1 -1
  197. package/dist/services/document-service/utils/unidirectional-relations.js +21 -6
  198. package/dist/services/document-service/utils/unidirectional-relations.js.map +1 -1
  199. package/dist/services/document-service/utils/unidirectional-relations.mjs +21 -6
  200. package/dist/services/document-service/utils/unidirectional-relations.mjs.map +1 -1
  201. package/dist/services/entity-validator/index.d.ts.map +1 -1
  202. package/dist/services/entity-validator/index.js +9 -0
  203. package/dist/services/entity-validator/index.js.map +1 -1
  204. package/dist/services/entity-validator/index.mjs +9 -0
  205. package/dist/services/entity-validator/index.mjs.map +1 -1
  206. package/dist/services/entity-validator/validators.d.ts +1 -0
  207. package/dist/services/entity-validator/validators.d.ts.map +1 -1
  208. package/dist/services/entity-validator/validators.js +3 -0
  209. package/dist/services/entity-validator/validators.js.map +1 -1
  210. package/dist/services/entity-validator/validators.mjs +3 -0
  211. package/dist/services/entity-validator/validators.mjs.map +1 -1
  212. package/dist/services/metrics/index.d.ts +1 -1
  213. package/dist/services/metrics/index.d.ts.map +1 -1
  214. package/dist/services/metrics/index.js +11 -9
  215. package/dist/services/metrics/index.js.map +1 -1
  216. package/dist/services/metrics/index.mjs +11 -9
  217. package/dist/services/metrics/index.mjs.map +1 -1
  218. package/dist/services/metrics/middleware.d.ts +2 -1
  219. package/dist/services/metrics/middleware.d.ts.map +1 -1
  220. package/dist/services/metrics/middleware.js +2 -2
  221. package/dist/services/metrics/middleware.js.map +1 -1
  222. package/dist/services/metrics/middleware.mjs +2 -2
  223. package/dist/services/metrics/middleware.mjs.map +1 -1
  224. package/dist/services/metrics/sender.d.ts.map +1 -1
  225. package/dist/services/metrics/sender.js +4 -3
  226. package/dist/services/metrics/sender.js.map +1 -1
  227. package/dist/services/metrics/sender.mjs +4 -3
  228. package/dist/services/metrics/sender.mjs.map +1 -1
  229. package/dist/services/server/register-routes.js +22 -2
  230. package/dist/services/server/register-routes.js.map +1 -1
  231. package/dist/services/server/register-routes.mjs +22 -2
  232. package/dist/services/server/register-routes.mjs.map +1 -1
  233. package/dist/services/server/routing.d.ts +10 -0
  234. package/dist/services/server/routing.d.ts.map +1 -1
  235. package/dist/services/server/routing.js +7 -1
  236. package/dist/services/server/routing.js.map +1 -1
  237. package/dist/services/server/routing.mjs +7 -1
  238. package/dist/services/server/routing.mjs.map +1 -1
  239. package/dist/services/session-manager.d.ts +167 -0
  240. package/dist/services/session-manager.d.ts.map +1 -0
  241. package/dist/services/session-manager.js +529 -0
  242. package/dist/services/session-manager.js.map +1 -0
  243. package/dist/services/session-manager.mjs +526 -0
  244. package/dist/services/session-manager.mjs.map +1 -0
  245. package/dist/services/utils/conditional-fields.d.ts +3 -0
  246. package/dist/services/utils/conditional-fields.d.ts.map +1 -0
  247. package/dist/services/utils/conditional-fields.js +22 -0
  248. package/dist/services/utils/conditional-fields.js.map +1 -0
  249. package/dist/services/utils/conditional-fields.mjs +20 -0
  250. package/dist/services/utils/conditional-fields.mjs.map +1 -0
  251. package/dist/utils/fetch.d.ts +5 -1
  252. package/dist/utils/fetch.d.ts.map +1 -1
  253. package/dist/utils/fetch.js +8 -4
  254. package/dist/utils/fetch.js.map +1 -1
  255. package/dist/utils/fetch.mjs +8 -4
  256. package/dist/utils/fetch.mjs.map +1 -1
  257. package/dist/utils/transform-content-types-to-models.d.ts +197 -0
  258. package/dist/utils/transform-content-types-to-models.d.ts.map +1 -1
  259. package/package.json +18 -13
@@ -0,0 +1,143 @@
1
+ import { getParentSchemasForComponent, findComponentParent } from '../components.mjs';
2
+
3
+ /**
4
+ * Cleans ghost relations with publication state mismatches from a join table
5
+ * Uses schema-based draft/publish checking like prevention fix
6
+ */ const cleanComponentJoinTable = async (db, joinTableName, relation, sourceModel)=>{
7
+ try {
8
+ // Get the target model metadata
9
+ const targetModel = db.metadata.get(relation.target);
10
+ if (!targetModel) {
11
+ db.logger.debug(`Target model ${relation.target} not found, skipping ${joinTableName}`);
12
+ return 0;
13
+ }
14
+ // Check if source supports draft/publish, if it doesnt it should contain duplicate states
15
+ const sourceContentType = strapi.contentTypes[sourceModel.uid];
16
+ // It could be a model, which does not have the draftAndPublish option
17
+ const sourceSupportsDraftPublish = sourceContentType?.options?.draftAndPublish;
18
+ if (sourceContentType && !sourceSupportsDraftPublish) {
19
+ return 0;
20
+ }
21
+ // Check if target supports draft/publish using schema-based approach (like prevention fix)
22
+ const targetContentType = strapi.contentTypes[relation.target];
23
+ const targetSupportsDraftPublish = targetContentType?.options?.draftAndPublish || false;
24
+ if (!targetSupportsDraftPublish) {
25
+ return 0;
26
+ }
27
+ // Find entries with publication state mismatches
28
+ const ghostEntries = await findPublicationStateMismatches(db, joinTableName, relation, targetModel, sourceModel);
29
+ if (ghostEntries.length === 0) {
30
+ return 0;
31
+ }
32
+ // Remove ghost entries
33
+ await db.connection(joinTableName).whereIn('id', ghostEntries).del();
34
+ db.logger.debug(`Removed ${ghostEntries.length} ghost relations with publication state mismatches from ${joinTableName}`);
35
+ return ghostEntries.length;
36
+ } catch (error) {
37
+ const errorMessage = error instanceof Error ? error.message : String(error);
38
+ db.logger.error(`Failed to clean join table "${joinTableName}": ${errorMessage}`);
39
+ return 0;
40
+ }
41
+ };
42
+ const findContentTypeParentForComponentInstance = async (componentSchema, componentId)=>{
43
+ // Get the parent schemas that could contain this component
44
+ const parentSchemas = getParentSchemasForComponent(componentSchema);
45
+ if (parentSchemas.length === 0) {
46
+ // No potential parents
47
+ return null;
48
+ }
49
+ // Find the actual parent for THIS specific component instance
50
+ const parent = await findComponentParent(componentSchema, componentId, parentSchemas);
51
+ if (!parent) {
52
+ // No parent found for this component instance
53
+ return null;
54
+ }
55
+ if (strapi.components[parent.uid]) {
56
+ // If the parent is a component, we need to check its parents recursively
57
+ const parentComponentSchema = strapi.components[parent.uid];
58
+ return findContentTypeParentForComponentInstance(parentComponentSchema, parent.parentId);
59
+ }
60
+ if (strapi.contentTypes[parent.uid]) {
61
+ // Found a content type parent
62
+ return parent;
63
+ }
64
+ return null;
65
+ };
66
+ /**
67
+ * Finds join table entries with publication state mismatches
68
+ * Uses existing component parent detection from document service
69
+ */ const findPublicationStateMismatches = async (db, joinTableName, relation, targetModel, sourceModel)=>{
70
+ try {
71
+ // Get join column names using proper functions (addressing PR feedback)
72
+ const sourceColumn = relation.joinTable.joinColumn.name;
73
+ const targetColumn = relation.joinTable.inverseJoinColumn.name;
74
+ // Get all join entries with their target entities
75
+ const query = db.connection(joinTableName).select(`${joinTableName}.id as join_id`, `${joinTableName}.${sourceColumn} as source_id`, `${joinTableName}.${targetColumn} as target_id`, `${targetModel.tableName}.published_at as target_published_at`).leftJoin(targetModel.tableName, `${joinTableName}.${targetColumn}`, `${targetModel.tableName}.id`);
76
+ const joinEntries = await query;
77
+ // Group by source_id to find duplicates pointing to draft/published versions of same entity
78
+ const entriesBySource = {};
79
+ for (const entry of joinEntries){
80
+ const sourceId = entry.source_id;
81
+ if (!entriesBySource[sourceId]) {
82
+ entriesBySource[sourceId] = [];
83
+ }
84
+ entriesBySource[sourceId].push(entry);
85
+ }
86
+ const ghostEntries = [];
87
+ // Check if this is a join table (ends with _lnk)
88
+ const isRelationJoinTable = joinTableName.endsWith('_lnk');
89
+ const isComponentModel = !sourceModel.uid?.startsWith('api::') && !sourceModel.uid?.startsWith('plugin::') && sourceModel.uid?.includes('.');
90
+ // Check for draft/publish inconsistencies
91
+ for (const [sourceId, entries] of Object.entries(entriesBySource)){
92
+ // Skip entries with single relations
93
+ if (entries.length <= 1) {
94
+ continue;
95
+ }
96
+ // For component join tables, check if THIS specific component instance's parent supports D&P
97
+ if (isRelationJoinTable && isComponentModel) {
98
+ try {
99
+ const componentSchema = strapi.components[sourceModel.uid];
100
+ if (!componentSchema) {
101
+ continue;
102
+ }
103
+ const parent = await findContentTypeParentForComponentInstance(componentSchema, sourceId);
104
+ if (!parent) {
105
+ continue;
106
+ }
107
+ // Check if THIS component instance's parent supports draft/publish
108
+ const parentContentType = strapi.contentTypes[parent.uid];
109
+ if (!parentContentType?.options?.draftAndPublish) {
110
+ continue;
111
+ }
112
+ // If we reach here, this component instance's parent DOES support D&P
113
+ // Continue to process this component instance for ghost relations
114
+ } catch (error) {
115
+ continue;
116
+ }
117
+ }
118
+ // Find ghost relations (same logic as original but with improved parent checking)
119
+ for (const entry of entries){
120
+ if (entry.target_published_at === null) {
121
+ // This is a draft target - find its published version
122
+ const draftTarget = await db.connection(targetModel.tableName).select('document_id').where('id', entry.target_id).first();
123
+ if (draftTarget) {
124
+ const publishedVersion = await db.connection(targetModel.tableName).select('id', 'document_id').where('document_id', draftTarget.document_id).whereNotNull('published_at').first();
125
+ if (publishedVersion) {
126
+ // Check if we also have a relation to the published version
127
+ const publishedRelation = entries.find((e)=>e.target_id === publishedVersion.id);
128
+ if (publishedRelation) {
129
+ ghostEntries.push(publishedRelation.join_id);
130
+ }
131
+ }
132
+ }
133
+ }
134
+ }
135
+ }
136
+ return ghostEntries;
137
+ } catch (error) {
138
+ return [];
139
+ }
140
+ };
141
+
142
+ export { cleanComponentJoinTable };
143
+ //# sourceMappingURL=clean-component-join-table.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"clean-component-join-table.mjs","sources":["../../../../src/services/document-service/utils/clean-component-join-table.ts"],"sourcesContent":["import type { Database } from '@strapi/database';\nimport type { Schema } from '@strapi/types';\nimport { findComponentParent, getParentSchemasForComponent } from '../components';\n\n/**\n * Cleans ghost relations with publication state mismatches from a join table\n * Uses schema-based draft/publish checking like prevention fix\n */\nexport const cleanComponentJoinTable = async (\n db: Database,\n joinTableName: string,\n relation: any,\n sourceModel: any\n): Promise<number> => {\n try {\n // Get the target model metadata\n const targetModel = db.metadata.get(relation.target);\n if (!targetModel) {\n db.logger.debug(`Target model ${relation.target} not found, skipping ${joinTableName}`);\n return 0;\n }\n\n // Check if source supports draft/publish, if it doesnt it should contain duplicate states\n const sourceContentType = strapi.contentTypes[sourceModel.uid];\n // It could be a model, which does not have the draftAndPublish option\n const sourceSupportsDraftPublish = sourceContentType?.options?.draftAndPublish;\n\n if (sourceContentType && !sourceSupportsDraftPublish) {\n return 0;\n }\n\n // Check if target supports draft/publish using schema-based approach (like prevention fix)\n const targetContentType =\n strapi.contentTypes[relation.target as keyof typeof strapi.contentTypes];\n const targetSupportsDraftPublish = targetContentType?.options?.draftAndPublish || false;\n\n if (!targetSupportsDraftPublish) {\n return 0;\n }\n\n // Find entries with publication state mismatches\n const ghostEntries = await findPublicationStateMismatches(\n db,\n joinTableName,\n relation,\n targetModel,\n sourceModel\n );\n\n if (ghostEntries.length === 0) {\n return 0;\n }\n\n // Remove ghost entries\n await db.connection(joinTableName).whereIn('id', ghostEntries).del();\n db.logger.debug(\n `Removed ${ghostEntries.length} ghost relations with publication state mismatches from ${joinTableName}`\n );\n\n return ghostEntries.length;\n } catch (error) {\n const errorMessage = error instanceof Error ? error.message : String(error);\n db.logger.error(`Failed to clean join table \"${joinTableName}\": ${errorMessage}`);\n return 0;\n }\n};\n\nconst findContentTypeParentForComponentInstance = async (\n componentSchema: Schema.Component,\n componentId: number | string\n): Promise<{ uid: string; table: string; parentId: number | string } | null> => {\n // Get the parent schemas that could contain this component\n const parentSchemas = getParentSchemasForComponent(componentSchema);\n if (parentSchemas.length === 0) {\n // No potential parents\n return null;\n }\n\n // Find the actual parent for THIS specific component instance\n const parent = await findComponentParent(componentSchema, componentId, parentSchemas);\n if (!parent) {\n // No parent found for this component instance\n return null;\n }\n\n if (strapi.components[parent.uid as keyof typeof strapi.components]) {\n // If the parent is a component, we need to check its parents recursively\n const parentComponentSchema = strapi.components[parent.uid as keyof typeof strapi.components];\n return findContentTypeParentForComponentInstance(parentComponentSchema, parent.parentId);\n }\n\n if (strapi.contentTypes[parent.uid as keyof typeof strapi.contentTypes]) {\n // Found a content type parent\n return parent;\n }\n\n return null;\n};\n\n/**\n * Finds join table entries with publication state mismatches\n * Uses existing component parent detection from document service\n */\nconst findPublicationStateMismatches = async (\n db: Database,\n joinTableName: string,\n relation: any,\n targetModel: any,\n sourceModel: any\n): Promise<number[]> => {\n try {\n // Get join column names using proper functions (addressing PR feedback)\n const sourceColumn = relation.joinTable.joinColumn.name;\n const targetColumn = relation.joinTable.inverseJoinColumn.name;\n\n // Get all join entries with their target entities\n const query = db\n .connection(joinTableName)\n .select(\n `${joinTableName}.id as join_id`,\n `${joinTableName}.${sourceColumn} as source_id`,\n `${joinTableName}.${targetColumn} as target_id`,\n `${targetModel.tableName}.published_at as target_published_at`\n )\n .leftJoin(\n targetModel.tableName,\n `${joinTableName}.${targetColumn}`,\n `${targetModel.tableName}.id`\n );\n\n const joinEntries = await query;\n\n // Group by source_id to find duplicates pointing to draft/published versions of same entity\n const entriesBySource: { [key: string]: any[] } = {};\n for (const entry of joinEntries) {\n const sourceId = entry.source_id;\n if (!entriesBySource[sourceId]) {\n entriesBySource[sourceId] = [];\n }\n entriesBySource[sourceId].push(entry);\n }\n\n const ghostEntries: number[] = [];\n\n // Check if this is a join table (ends with _lnk)\n const isRelationJoinTable = joinTableName.endsWith('_lnk');\n const isComponentModel =\n !sourceModel.uid?.startsWith('api::') &&\n !sourceModel.uid?.startsWith('plugin::') &&\n sourceModel.uid?.includes('.');\n\n // Check for draft/publish inconsistencies\n for (const [sourceId, entries] of Object.entries(entriesBySource)) {\n // Skip entries with single relations\n if (entries.length <= 1) {\n // eslint-disable-next-line no-continue\n continue;\n }\n\n // For component join tables, check if THIS specific component instance's parent supports D&P\n if (isRelationJoinTable && isComponentModel) {\n try {\n const componentSchema = strapi.components[sourceModel.uid] as Schema.Component;\n if (!componentSchema) {\n // eslint-disable-next-line no-continue\n continue;\n }\n\n const parent = await findContentTypeParentForComponentInstance(componentSchema, sourceId);\n if (!parent) {\n continue;\n }\n\n // Check if THIS component instance's parent supports draft/publish\n const parentContentType =\n strapi.contentTypes[parent.uid as keyof typeof strapi.contentTypes];\n if (!parentContentType?.options?.draftAndPublish) {\n // This component instance's parent does NOT support D&P - skip cleanup\n // eslint-disable-next-line no-continue\n continue;\n }\n\n // If we reach here, this component instance's parent DOES support D&P\n // Continue to process this component instance for ghost relations\n } catch (error) {\n // Skip this component instance on error\n // eslint-disable-next-line no-continue\n continue;\n }\n }\n\n // Find ghost relations (same logic as original but with improved parent checking)\n for (const entry of entries) {\n if (entry.target_published_at === null) {\n // This is a draft target - find its published version\n const draftTarget = await db\n .connection(targetModel.tableName)\n .select('document_id')\n .where('id', entry.target_id)\n .first();\n\n if (draftTarget) {\n const publishedVersion = await db\n .connection(targetModel.tableName)\n .select('id', 'document_id')\n .where('document_id', draftTarget.document_id)\n .whereNotNull('published_at')\n .first();\n\n if (publishedVersion) {\n // Check if we also have a relation to the published version\n const publishedRelation = entries.find((e) => e.target_id === publishedVersion.id);\n if (publishedRelation) {\n ghostEntries.push(publishedRelation.join_id);\n }\n }\n }\n }\n }\n }\n\n return ghostEntries;\n } catch (error) {\n return [];\n }\n};\n"],"names":["cleanComponentJoinTable","db","joinTableName","relation","sourceModel","targetModel","metadata","get","target","logger","debug","sourceContentType","strapi","contentTypes","uid","sourceSupportsDraftPublish","options","draftAndPublish","targetContentType","targetSupportsDraftPublish","ghostEntries","findPublicationStateMismatches","length","connection","whereIn","del","error","errorMessage","Error","message","String","findContentTypeParentForComponentInstance","componentSchema","componentId","parentSchemas","getParentSchemasForComponent","parent","findComponentParent","components","parentComponentSchema","parentId","sourceColumn","joinTable","joinColumn","name","targetColumn","inverseJoinColumn","query","select","tableName","leftJoin","joinEntries","entriesBySource","entry","sourceId","source_id","push","isRelationJoinTable","endsWith","isComponentModel","startsWith","includes","entries","Object","parentContentType","target_published_at","draftTarget","where","target_id","first","publishedVersion","document_id","whereNotNull","publishedRelation","find","e","id","join_id"],"mappings":";;AAIA;;;AAGC,IACYA,MAAAA,uBAAAA,GAA0B,OACrCC,EAAAA,EACAC,eACAC,QACAC,EAAAA,WAAAA,GAAAA;IAEA,IAAI;;AAEF,QAAA,MAAMC,cAAcJ,EAAGK,CAAAA,QAAQ,CAACC,GAAG,CAACJ,SAASK,MAAM,CAAA;AACnD,QAAA,IAAI,CAACH,WAAa,EAAA;AAChBJ,YAAAA,EAAAA,CAAGQ,MAAM,CAACC,KAAK,CAAC,CAAC,aAAa,EAAEP,QAAAA,CAASK,MAAM,CAAC,qBAAqB,EAAEN,cAAc,CAAC,CAAA;YACtF,OAAO,CAAA;AACT;;AAGA,QAAA,MAAMS,oBAAoBC,MAAOC,CAAAA,YAAY,CAACT,WAAAA,CAAYU,GAAG,CAAC;;QAE9D,MAAMC,0BAAAA,GAA6BJ,mBAAmBK,OAASC,EAAAA,eAAAA;QAE/D,IAAIN,iBAAAA,IAAqB,CAACI,0BAA4B,EAAA;YACpD,OAAO,CAAA;AACT;;AAGA,QAAA,MAAMG,oBACJN,MAAOC,CAAAA,YAAY,CAACV,QAAAA,CAASK,MAAM,CAAqC;QAC1E,MAAMW,0BAAAA,GAA6BD,iBAAmBF,EAAAA,OAAAA,EAASC,eAAmB,IAAA,KAAA;AAElF,QAAA,IAAI,CAACE,0BAA4B,EAAA;YAC/B,OAAO,CAAA;AACT;;AAGA,QAAA,MAAMC,eAAe,MAAMC,8BAAAA,CACzBpB,EACAC,EAAAA,aAAAA,EACAC,UACAE,WACAD,EAAAA,WAAAA,CAAAA;QAGF,IAAIgB,YAAAA,CAAaE,MAAM,KAAK,CAAG,EAAA;YAC7B,OAAO,CAAA;AACT;;QAGA,MAAMrB,EAAAA,CAAGsB,UAAU,CAACrB,aAAAA,CAAAA,CAAesB,OAAO,CAAC,IAAA,EAAMJ,cAAcK,GAA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3BC,KAAK,EAAA;AAER,oBAAA,IAAIH,WAAa,EAAA;wBACf,MAAMI,gBAAAA,GAAmB,MAAMrE,EAC5BsB,CAAAA,UAAU,CAAClB,WAAY4C,CAAAA,SAAS,EAChCD,MAAM,CAAC,MAAM,aACbmB,CAAAA,CAAAA,KAAK,CAAC,aAAeD,EAAAA,WAAAA,CAAYK,WAAW,CAC5CC,CAAAA,YAAY,CAAC,cAAA,CAAA,CACbH,KAAK,EAAA;AAER,wBAAA,IAAIC,gBAAkB,EAAA;;4BAEpB,MAAMG,iBAAAA,GAAoBX,OAAQY,CAAAA,IAAI,CAAC,CAACC,IAAMA,CAAEP,CAAAA,SAAS,KAAKE,gBAAAA,CAAiBM,EAAE,CAAA;AACjF,4BAAA,IAAIH,iBAAmB,EAAA;gCACrBrD,YAAaoC,CAAAA,IAAI,CAACiB,iBAAAA,CAAkBI,OAAO,CAAA;AAC7C;AACF;AACF;AACF;AACF;AACF;QAEA,OAAOzD,YAAAA;AACT,KAAA,CAAE,OAAOM,KAAO,EAAA;AACd,QAAA,OAAO,EAAE;AACX;AACF,CAAA;;;;"}
@@ -1,4 +1,5 @@
1
- import type { UID } from '@strapi/types';
1
+ import type { UID, Schema } from '@strapi/types';
2
+ import type { JoinTable } from '@strapi/database';
2
3
  interface LoadContext {
3
4
  oldVersions: {
4
5
  id: string;
@@ -9,15 +10,30 @@ interface LoadContext {
9
10
  locale: string;
10
11
  }[];
11
12
  }
13
+ interface RelationUpdate {
14
+ joinTable: JoinTable;
15
+ relations: Record<string, any>[];
16
+ }
17
+ interface RelationFilterOptions {
18
+ /**
19
+ * Function to determine if a relation should be propagated to new document versions
20
+ * This replaces the hardcoded component-specific logic
21
+ */
22
+ shouldPropagateRelation?: (relation: Record<string, any>, model: Schema.Component | Schema.ContentType, trx: any) => Promise<boolean>;
23
+ }
12
24
  /**
13
25
  * Loads lingering relations that need to be updated when overriding a published or draft entry.
14
26
  * This is necessary because the relations are uni-directional and the target entry is not aware of the source entry.
15
27
  * This is not the case for bi-directional relations, where the target entry is also linked to the source entry.
16
28
  */
17
- declare const load: (uid: UID.ContentType, { oldVersions, newVersions }: LoadContext) => Promise<any>;
29
+ declare const load: (uid: UID.ContentType, { oldVersions, newVersions }: LoadContext, options?: RelationFilterOptions) => Promise<RelationUpdate[]>;
18
30
  /**
19
31
  * Updates uni directional relations to target the right entries when overriding published or draft entries.
20
32
  *
33
+ * This function:
34
+ * 1. Creates new relations pointing to the new entry versions
35
+ * 2. Precisely deletes only the old relations being replaced to prevent orphaned links
36
+ *
21
37
  * @param oldEntries The old entries that are being overridden
22
38
  * @param newEntries The new entries that are overriding the old ones
23
39
  * @param oldRelations The relations that were previously loaded with `load` @see load
@@ -33,4 +49,5 @@ declare const sync: (oldEntries: {
33
49
  relations: any[];
34
50
  }[]) => Promise<void>;
35
51
  export { load, sync };
52
+ export type { RelationFilterOptions };
36
53
  //# sourceMappingURL=unidirectional-relations.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"unidirectional-relations.d.ts","sourceRoot":"","sources":["../../../../src/services/document-service/utils/unidirectional-relations.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,GAAG,EAAU,MAAM,eAAe,CAAC;AAEjD,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"}
1
+ {"version":3,"file":"unidirectional-relations.d.ts","sourceRoot":"","sources":["../../../../src/services/document-service/utils/unidirectional-relations.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,GAAG,EAAE,MAAM,EAAE,MAAM,eAAe,CAAC;AAEjD,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,kBAAkB,CAAC;AAElD,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,UAAU,cAAc;IACtB,SAAS,EAAE,SAAS,CAAC;IACrB,SAAS,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,EAAE,CAAC;CAClC;AAED,UAAU,qBAAqB;IAC7B;;;OAGG;IACH,uBAAuB,CAAC,EAAE,CACxB,QAAQ,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,EAC7B,KAAK,EAAE,MAAM,CAAC,SAAS,GAAG,MAAM,CAAC,WAAW,EAC5C,GAAG,EAAE,GAAG,KACL,OAAO,CAAC,OAAO,CAAC,CAAC;CACvB;AAED;;;;GAIG;AACH,QAAA,MAAM,IAAI,QACH,IAAI,WAAW,gCACU,WAAW,YAChC,qBAAqB,KAC7B,QAAQ,cAAc,EAAE,CAyG1B,CAAC;AAEF;;;;;;;;;;GAUG;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;AACtB,YAAY,EAAE,qBAAqB,EAAE,CAAC"}
@@ -6,7 +6,7 @@ var fp = require('lodash/fp');
6
6
  * Loads lingering relations that need to be updated when overriding a published or draft entry.
7
7
  * This is necessary because the relations are uni-directional and the target entry is not aware of the source entry.
8
8
  * This is not the case for bi-directional relations, where the target entry is also linked to the source entry.
9
- */ const load = async (uid, { oldVersions, newVersions })=>{
9
+ */ const load = async (uid, { oldVersions, newVersions }, options = {})=>{
10
10
  const updates = [];
11
11
  // Iterate all components and content types to find relations that need to be updated
12
12
  await strapi.db.transaction(async ({ trx })=>{
@@ -53,17 +53,28 @@ var fp = require('lodash/fp');
53
53
  * create link to new draft version
54
54
  */ if (!model.options?.draftAndPublish) {
55
55
  const ids = newVersions.map((entry)=>entry.id);
56
+ // This is the step were we query the join table based on the id of the document
56
57
  const newVersionsRelations = await strapi.db.getConnection().select('*').from(joinTable.name).whereIn(targetColumnName, ids).transacting(trx);
57
- if (newVersionsRelations.length > 0) {
58
+ let versionRelations = newVersionsRelations;
59
+ if (options.shouldPropagateRelation) {
60
+ const relationsToPropagate = [];
61
+ for (const relation of newVersionsRelations){
62
+ if (await options.shouldPropagateRelation(relation, model, trx)) {
63
+ relationsToPropagate.push(relation);
64
+ }
65
+ }
66
+ versionRelations = relationsToPropagate;
67
+ }
68
+ if (versionRelations.length > 0) {
58
69
  // when publishing a draft that doesn't have a published version yet,
59
70
  // copy the links to the draft over to the published version
60
71
  // when discarding a published version, if no drafts exists
61
- const discardToAdd = newVersionsRelations.filter((relation)=>{
62
- const matchingOldVerion = oldVersionsRelations.find((oldRelation)=>{
72
+ const discardToAdd = versionRelations.filter((relation)=>{
73
+ const matchingOldVersion = oldVersionsRelations.find((oldRelation)=>{
63
74
  return oldRelation[sourceColumnName] === relation[sourceColumnName];
64
75
  });
65
- return !matchingOldVerion;
66
- }).map(fp.omit('id'));
76
+ return !matchingOldVersion;
77
+ }).map(fp.omit(strapi.db.metadata.identifiers.ID_COLUMN));
67
78
  updates.push({
68
79
  joinTable,
69
80
  relations: discardToAdd
@@ -78,6 +89,10 @@ var fp = require('lodash/fp');
78
89
  /**
79
90
  * Updates uni directional relations to target the right entries when overriding published or draft entries.
80
91
  *
92
+ * This function:
93
+ * 1. Creates new relations pointing to the new entry versions
94
+ * 2. Precisely deletes only the old relations being replaced to prevent orphaned links
95
+ *
81
96
  * @param oldEntries The old entries that are being overridden
82
97
  * @param newEntries The new entries that are overriding the old ones
83
98
  * @param oldRelations The relations that were previously loaded with `load` @see load
@@ -1 +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 type { 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":["load","uid","oldVersions","newVersions","updates","strapi","db","transaction","trx","contentTypes","Object","values","components","model","dbModel","metadata","get","attribute","attributes","type","target","inversedBy","mappedBy","joinTable","name","sourceColumnName","joinColumn","targetColumnName","inverseJoinColumn","ids","map","entry","id","oldVersionsRelations","getConnection","select","from","whereIn","transacting","length","push","relations","options","draftAndPublish","newVersionsRelations","discardToAdd","filter","relation","matchingOldVerion","find","oldRelation","omit","sync","oldEntries","newEntries","oldRelations","newEntryByLocale","keyBy","oldEntriesMap","reduce","acc","newEntry","locale","column","newRelations","newId","batchInsert"],"mappings":";;;;AAUA;;;;IAKA,MAAMA,OAAO,OAAOC,GAAAA,EAAsB,EAAEC,WAAW,EAAEC,WAAW,EAAe,GAAA;AACjF,IAAA,MAAMC,UAAU,EAAE;;IAGlB,MAAMC,MAAAA,CAAOC,EAAE,CAACC,WAAW,CAAC,OAAO,EAAEC,GAAG,EAAE,GAAA;AACxC,QAAA,MAAMC,YAAeC,GAAAA,MAAAA,CAAOC,MAAM,CAACN,OAAOI,YAAY,CAAA;AACtD,QAAA,MAAMG,UAAaF,GAAAA,MAAAA,CAAOC,MAAM,CAACN,OAAOO,UAAU,CAAA;AAElD,QAAA,KAAK,MAAMC,KAAS,IAAA;AAAIJ,YAA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CAAWM,MAAM,CACrC,CAACC,GAAK7B,EAAAA,KAAAA,GAAAA;AACJ,QAAA,MAAM8B,QAAWL,GAAAA,gBAAgB,CAACzB,KAAAA,CAAM+B,MAAM,CAAC;QAC/C,IAAI,CAACD,UAAU,OAAOD,GAAAA;AACtBA,QAAAA,GAAG,CAAC7B,KAAMC,CAAAA,EAAE,CAAC,GAAG6B,SAAS7B,EAAE;QAC3B,OAAO4B,GAAAA;AACT,KAAA,EACA,EAAC,CAAA;IAGH,MAAMvD,MAAAA,CAAOC,EAAE,CAACC,WAAW,CAAC,OAAO,EAAEC,GAAG,EAAE,GAAA;;AAExC,QAAA,KAAK,MAAM,EAAEe,SAAS,EAAEkB,SAAS,EAAE,IAAIc,YAAc,CAAA;;AAEnD,YAAA,MAAMQ,MAASxC,GAAAA,SAAAA,CAAUK,iBAAiB,CAACJ,IAAI;AAE/C,YAAA,MAAMwC,YAAevB,GAAAA,SAAAA,CAAUX,GAAG,CAAC,CAACiB,QAAAA,GAAAA;AAClC,gBAAA,MAAMkB,QAAQP,aAAa,CAACX,QAAQ,CAACgB,OAAO,CAAC;gBAC7C,OAAO;AAAE,oBAAA,GAAGhB,QAAQ;AAAE,oBAAA,CAACgB,SAASE;AAAM,iBAAA;AACxC,aAAA,CAAA;;AAGA,YAAA,MAAMzD,IAAI0D,WAAW,CAAC3C,SAAUC,CAAAA,IAAI,EAAEwC,YAAc,EAAA,IAAA,CAAA;AACtD;AACF,KAAA,CAAA;AACF;;;;;"}
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 type { UID, Schema } from '@strapi/types';\n\nimport type { JoinTable } from '@strapi/database';\n\ninterface LoadContext {\n oldVersions: { id: string; locale: string }[];\n newVersions: { id: string; locale: string }[];\n}\n\ninterface RelationUpdate {\n joinTable: JoinTable;\n relations: Record<string, any>[];\n}\n\ninterface RelationFilterOptions {\n /**\n * Function to determine if a relation should be propagated to new document versions\n * This replaces the hardcoded component-specific logic\n */\n shouldPropagateRelation?: (\n relation: Record<string, any>,\n model: Schema.Component | Schema.ContentType,\n trx: any\n ) => Promise<boolean>;\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 (\n uid: UID.ContentType,\n { oldVersions, newVersions }: LoadContext,\n options: RelationFilterOptions = {}\n): Promise<RelationUpdate[]> => {\n const updates: RelationUpdate[] = [];\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 if (!model.options?.draftAndPublish) {\n const ids = newVersions.map((entry) => entry.id);\n\n // This is the step were we query the join table based on the id of the document\n const newVersionsRelations = await strapi.db\n .getConnection()\n .select('*')\n .from(joinTable.name)\n .whereIn(targetColumnName, ids)\n .transacting(trx);\n\n let versionRelations = newVersionsRelations;\n if (options.shouldPropagateRelation) {\n const relationsToPropagate = [];\n for (const relation of newVersionsRelations) {\n if (await options.shouldPropagateRelation(relation, model, trx)) {\n relationsToPropagate.push(relation);\n }\n }\n versionRelations = relationsToPropagate;\n }\n\n if (versionRelations.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 = versionRelations\n .filter((relation) => {\n const matchingOldVersion = oldVersionsRelations.find((oldRelation) => {\n return oldRelation[sourceColumnName] === relation[sourceColumnName];\n });\n\n return !matchingOldVersion;\n })\n .map(omit(strapi.db.metadata.identifiers.ID_COLUMN));\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 * This function:\n * 1. Creates new relations pointing to the new entry versions\n * 2. Precisely deletes only the old relations being replaced to prevent orphaned links\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 };\nexport type { RelationFilterOptions };\n"],"names":["load","uid","oldVersions","newVersions","options","updates","strapi","db","transaction","trx","contentTypes","Object","values","components","model","dbModel","metadata","get","attribute","attributes","type","target","inversedBy","mappedBy","joinTable","name","sourceColumnName","joinColumn","targetColumnName","inverseJoinColumn","ids","map","entry","id","oldVersionsRelations","getConnection","select","from","whereIn","transacting","length","push","relations","draftAndPublish","newVersionsRelations","versionRelations","shouldPropagateRelation","relationsToPropagate","relation","discardToAdd","filter","matchingOldVersion","find","oldRelation","omit","identifiers","ID_COLUMN","sync","oldEntries","newEntries","oldRelations","newEntryByLocale","keyBy","oldEntriesMap","reduce","acc","newEntry","locale","column","newRelations","newId","batchInsert"],"mappings":";;;;AA6BA;;;;AAIC,IACKA,MAAAA,IAAAA,GAAO,OACXC,GAAAA,EACA,EAAEC,WAAW,EAAEC,WAAW,EAAe,EACzCC,OAAiC,GAAA,EAAE,GAAA;AAEnC,IAAA,MAAMC,UAA4B,EAAE;;IAGpC,MAAMC,MAAAA,CAAOC,EAAE,CAACC,WAAW,CAAC,OAAO,EAAEC,GAAG,EAAE,GAAA;AACxC,QAAA,MAAMC,YAAeC,GAAAA,MAAAA,CAAOC,MAAM,CAACN,OAAOI,YAAY,CAAA;AACtD,QAAA,MAAMG,UAAaF,GAAAA,MAAAA,CAAOC,MAAM,CAACN,OAAOO,UAAU,CAAA;AAElD,QAAA,KAAK,MAAMC,KAAS,IAAA;AAAIJ,YAAAA,GAAAA,YAAAA;AAAiBG,YAAAA,GAAAA;SAAW,CAAE;YACpD,MAAME,OAAAA,GAAUT,OAAOC,EAAE,CAACS,QAAQ,CAACC,GAAG,CAACH,KAAAA,CAAMb,GAAG,CAAA;AAEh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+4,7 @@ import { omit, keyBy } from 'lodash/fp';
4
4
  * Loads lingering relations that need to be updated when overriding a published or draft entry.
5
5
  * This is necessary because the relations are uni-directional and the target entry is not aware of the source entry.
6
6
  * This is not the case for bi-directional relations, where the target entry is also linked to the source entry.
7
- */ const load = async (uid, { oldVersions, newVersions })=>{
7
+ */ const load = async (uid, { oldVersions, newVersions }, options = {})=>{
8
8
  const updates = [];
9
9
  // Iterate all components and content types to find relations that need to be updated
10
10
  await strapi.db.transaction(async ({ trx })=>{
@@ -51,17 +51,28 @@ import { omit, keyBy } from 'lodash/fp';
51
51
  * create link to new draft version
52
52
  */ if (!model.options?.draftAndPublish) {
53
53
  const ids = newVersions.map((entry)=>entry.id);
54
+ // This is the step were we query the join table based on the id of the document
54
55
  const newVersionsRelations = await strapi.db.getConnection().select('*').from(joinTable.name).whereIn(targetColumnName, ids).transacting(trx);
55
- if (newVersionsRelations.length > 0) {
56
+ let versionRelations = newVersionsRelations;
57
+ if (options.shouldPropagateRelation) {
58
+ const relationsToPropagate = [];
59
+ for (const relation of newVersionsRelations){
60
+ if (await options.shouldPropagateRelation(relation, model, trx)) {
61
+ relationsToPropagate.push(relation);
62
+ }
63
+ }
64
+ versionRelations = relationsToPropagate;
65
+ }
66
+ if (versionRelations.length > 0) {
56
67
  // when publishing a draft that doesn't have a published version yet,
57
68
  // copy the links to the draft over to the published version
58
69
  // when discarding a published version, if no drafts exists
59
- const discardToAdd = newVersionsRelations.filter((relation)=>{
60
- const matchingOldVerion = oldVersionsRelations.find((oldRelation)=>{
70
+ const discardToAdd = versionRelations.filter((relation)=>{
71
+ const matchingOldVersion = oldVersionsRelations.find((oldRelation)=>{
61
72
  return oldRelation[sourceColumnName] === relation[sourceColumnName];
62
73
  });
63
- return !matchingOldVerion;
64
- }).map(omit('id'));
74
+ return !matchingOldVersion;
75
+ }).map(omit(strapi.db.metadata.identifiers.ID_COLUMN));
65
76
  updates.push({
66
77
  joinTable,
67
78
  relations: discardToAdd
@@ -76,6 +87,10 @@ import { omit, keyBy } from 'lodash/fp';
76
87
  /**
77
88
  * Updates uni directional relations to target the right entries when overriding published or draft entries.
78
89
  *
90
+ * This function:
91
+ * 1. Creates new relations pointing to the new entry versions
92
+ * 2. Precisely deletes only the old relations being replaced to prevent orphaned links
93
+ *
79
94
  * @param oldEntries The old entries that are being overridden
80
95
  * @param newEntries The new entries that are overriding the old ones
81
96
  * @param oldRelations The relations that were previously loaded with `load` @see load
@@ -1 +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 type { 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":["load","uid","oldVersions","newVersions","updates","strapi","db","transaction","trx","contentTypes","Object","values","components","model","dbModel","metadata","get","attribute","attributes","type","target","inversedBy","mappedBy","joinTable","name","sourceColumnName","joinColumn","targetColumnName","inverseJoinColumn","ids","map","entry","id","oldVersionsRelations","getConnection","select","from","whereIn","transacting","length","push","relations","options","draftAndPublish","newVersionsRelations","discardToAdd","filter","relation","matchingOldVerion","find","oldRelation","omit","sync","oldEntries","newEntries","oldRelations","newEntryByLocale","keyBy","oldEntriesMap","reduce","acc","newEntry","locale","column","newRelations","newId","batchInsert"],"mappings":";;AAUA;;;;IAKA,MAAMA,OAAO,OAAOC,GAAAA,EAAsB,EAAEC,WAAW,EAAEC,WAAW,EAAe,GAAA;AACjF,IAAA,MAAMC,UAAU,EAAE;;IAGlB,MAAMC,MAAAA,CAAOC,EAAE,CAACC,WAAW,CAAC,OAAO,EAAEC,GAAG,EAAE,GAAA;AACxC,QAAA,MAAMC,YAAeC,GAAAA,MAAAA,CAAOC,MAAM,CAACN,OAAOI,YAAY,CAAA;AACtD,QAAA,MAAMG,UAAaF,GAAAA,MAAAA,CAAOC,MAAM,CAACN,OAAOO,UAAU,CAAA;AAElD,QAAA,KAAK,MAAMC,KAAS,IAAA;AAAIJ,YAAAA,GAAAA,YAAAA;AAAiBG,YAAAA,GAAAA;SAAW,CAAE;YACpD,MAAME,OAAAA,GAAUT,OAAOC,EAAE,CAACS,QAAQ,CAACC,GAAG,CAACH,KAAAA,CAAMZ,GAAG,CAAA;AAEhD,YAAA,KAAK,MAAMgB,SAAaP,IAAAA,MAAAA,CAAOC,MAAM,CAACG,OAAAA,CAAQI,UAAU,CAA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,EAAE;QAC3B,OAAO4B,GAAAA;AACT,KAAA,EACA,EAAC,CAAA;IAGH,MAAMvD,MAAAA,CAAOC,EAAE,CAACC,WAAW,CAAC,OAAO,EAAEC,GAAG,EAAE,GAAA;;AAExC,QAAA,KAAK,MAAM,EAAEe,SAAS,EAAEkB,SAAS,EAAE,IAAIc,YAAc,CAAA;;AAEnD,YAAA,MAAMQ,MAASxC,GAAAA,SAAAA,CAAUK,iBAAiB,CAACJ,IAAI;AAE/C,YAAA,MAAMwC,YAAevB,GAAAA,SAAAA,CAAUX,GAAG,CAAC,CAACiB,QAAAA,GAAAA;AAClC,gBAAA,MAAMkB,QAAQP,aAAa,CAACX,QAAQ,CAACgB,OAAO,CAAC;gBAC7C,OAAO;AAAE,oBAAA,GAAGhB,QAAQ;AAAE,oBAAA,CAACgB,SAASE;AAAM,iBAAA;AACxC,aAAA,CAAA;;AAGA,YAAA,MAAMzD,IAAI0D,WAAW,CAAC3C,SAAUC,CAAAA,IAAI,EAAEwC,YAAc,EAAA,IAAA,CAAA;AACtD;AACF,KAAA,CAAA;AACF;;;;"}
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 type { UID, Schema } from '@strapi/types';\n\nimport type { JoinTable } from '@strapi/database';\n\ninterface LoadContext {\n oldVersions: { id: string; locale: string }[];\n newVersions: { id: string; locale: string }[];\n}\n\ninterface RelationUpdate {\n joinTable: JoinTable;\n relations: Record<string, any>[];\n}\n\ninterface RelationFilterOptions {\n /**\n * Function to determine if a relation should be propagated to new document versions\n * This replaces the hardcoded component-specific logic\n */\n shouldPropagateRelation?: (\n relation: Record<string, any>,\n model: Schema.Component | Schema.ContentType,\n trx: any\n ) => Promise<boolean>;\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 (\n uid: UID.ContentType,\n { oldVersions, newVersions }: LoadContext,\n options: RelationFilterOptions = {}\n): Promise<RelationUpdate[]> => {\n const updates: RelationUpdate[] = [];\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 if (!model.options?.draftAndPublish) {\n const ids = newVersions.map((entry) => entry.id);\n\n // This is the step were we query the join table based on the id of the document\n const newVersionsRelations = await strapi.db\n .getConnection()\n .select('*')\n .from(joinTable.name)\n .whereIn(targetColumnName, ids)\n .transacting(trx);\n\n let versionRelations = newVersionsRelations;\n if (options.shouldPropagateRelation) {\n const relationsToPropagate = [];\n for (const relation of newVersionsRelations) {\n if (await options.shouldPropagateRelation(relation, model, trx)) {\n relationsToPropagate.push(relation);\n }\n }\n versionRelations = relationsToPropagate;\n }\n\n if (versionRelations.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 = versionRelations\n .filter((relation) => {\n const matchingOldVersion = oldVersionsRelations.find((oldRelation) => {\n return oldRelation[sourceColumnName] === relation[sourceColumnName];\n });\n\n return !matchingOldVersion;\n })\n .map(omit(strapi.db.metadata.identifiers.ID_COLUMN));\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 * This function:\n * 1. Creates new relations pointing to the new entry versions\n * 2. Precisely deletes only the old relations being replaced to prevent orphaned links\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 };\nexport type { RelationFilterOptions };\n"],"names":["load","uid","oldVersions","newVersions","options","updates","strapi","db","transaction","trx","contentTypes","Object","values","components","model","dbModel","metadata","get","attribute","attributes","type","target","inversedBy","mappedBy","joinTable","name","sourceColumnName","joinColumn","targetColumnName","inverseJoinColumn","ids","map","entry","id","oldVersionsRelations","getConnection","select","from","whereIn","transacting","length","push","relations","draftAndPublish","newVersionsRelations","versionRelations","shouldPropagateRelation","relationsToPropagate","relation","discardToAdd","filter","matchingOldVersion","find","oldRelation","omit","identifiers","ID_COLUMN","sync","oldEntries","newEntries","oldRelations","newEntryByLocale","keyBy","oldEntriesMap","reduce","acc","newEntry","locale","column","newRelations","newId","batchInsert"],"mappings":";;AA6BA;;;;AAIC,IACKA,MAAAA,IAAAA,GAAO,OACXC,GAAAA,EACA,EAAEC,WAAW,EAAEC,WAAW,EAAe,EACzCC,OAAiC,GAAA,EAAE,GAAA;AAEnC,IAAA,MAAMC,UAA4B,EAAE;;IAGpC,MAAMC,MAAAA,CAAOC,EAAE,CAACC,WAAW,CAAC,OAAO,EAAEC,GAAG,EAAE,GAAA;AACxC,QAAA,MAAMC,YAAeC,GAAAA,MAAAA,CAAOC,MAAM,CAACN,OAAOI,YAAY,CAAA;AACtD,QAAA,MAAMG,UAAaF,GAAAA,MAAAA,CAAOC,MAAM,CAACN,OAAOO,UAAU,CAAA;AAElD,QAAA,KAAK,MAAMC,KAAS,IAAA;AAAIJ,YAAAA,GAAAA,YAAAA;AAAiBG,YAAAA,GAAAA;SAAW,CAAE;YACpD,MAAME,OAAAA,GAAUT,OAAOC,EAAE,CAACS,QAAQ,CAACC,GAAG,CAACH,KAAAA,CAAMb,GAAG,CAAA;AAEhD,YAAA,KAAK,MAAMiB,SAAaP,IAAAA,MAAAA,CAAOC,MAAM,CAACG,OAAAA,CAAQI,UAAU,CAAU,CAAA;AAChE;;AAEC,YACD,IACED,SAAAA,CAAUE,IAAI,KAAK,cACnBF,SAAUG,CAAAA,MAAM,KAAKpB,GAAAA,IACrBiB,SAAUI,CAAAA,UAAU,IACpBJ,SAAAA,CAAUK,QAAQ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AAe,EAAE,OAAO,CAAC,eAAe,CAAC,eAG9C,CAAC;AAEF,eAAe,eAAe,CAAC"}
@@ -2,6 +2,7 @@
2
2
 
3
3
  var _ = require('lodash');
4
4
  var fp = require('lodash/fp');
5
+ var jsonLogic = require('json-logic-js');
5
6
  var strapiUtils = require('@strapi/utils');
6
7
  var validators = require('./validators.js');
7
8
 
@@ -149,6 +150,13 @@ const createScalarAttributeValidator = (createOrUpdate)=>(metas, options)=>{
149
150
  };
150
151
  const createAttributeValidator = (createOrUpdate)=>(metas, options)=>{
151
152
  let validator = yup.mixed();
153
+ // If field is conditionally invisible, skip all validation for it
154
+ if (metas.attr.conditions?.visible) {
155
+ const isVisible = jsonLogic.apply(metas.attr.conditions.visible, metas.data);
156
+ if (!isVisible) {
157
+ return yup.mixed().notRequired(); // Completely skip validation
158
+ }
159
+ }
152
160
  if (isMediaAttribute(metas.attr)) {
153
161
  validator = yup.mixed();
154
162
  } else if (isScalarAttribute(metas.attr)) {
@@ -210,6 +218,7 @@ const createModelValidator = (createOrUpdate)=>({ componentContext, model, data,
210
218
  name: attributeName,
211
219
  value: fp.prop(attributeName, data)
212
220
  },
221
+ data,
213
222
  model,
214
223
  entity,
215
224
  componentContext