sf-plugin-data 0.1.0 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (277) hide show
  1. package/LICENSE +21 -21
  2. package/README.md +618 -240
  3. package/docs/artifact-format.md +528 -633
  4. package/docs/capabilities.md +1160 -168
  5. package/docs/git-workflow.md +135 -12
  6. package/docs/handover.md +7826 -12
  7. package/lib/api.d.ts +13 -0
  8. package/lib/api.js +10 -0
  9. package/lib/artifacts/code-fields.d.ts +49 -0
  10. package/lib/artifacts/code-fields.js +142 -0
  11. package/lib/artifacts/data-graph-document.d.ts +29 -0
  12. package/lib/artifacts/data-graph-document.js +52 -0
  13. package/lib/artifacts/data-space-field.d.ts +65 -0
  14. package/lib/artifacts/data-space-field.js +98 -0
  15. package/lib/artifacts/data-stream-document.d.ts +264 -0
  16. package/lib/artifacts/data-stream-document.js +680 -0
  17. package/lib/artifacts/data-transform-document.d.ts +152 -0
  18. package/lib/artifacts/data-transform-document.js +560 -0
  19. package/lib/artifacts/dependency-selection.d.ts +51 -0
  20. package/lib/artifacts/dependency-selection.js +154 -0
  21. package/lib/artifacts/dlo-document.d.ts +93 -0
  22. package/lib/artifacts/dlo-document.js +253 -0
  23. package/lib/artifacts/enum-case.d.ts +39 -0
  24. package/lib/artifacts/enum-case.js +67 -0
  25. package/lib/artifacts/git-deploy-selection.d.ts +68 -0
  26. package/lib/artifacts/git-deploy-selection.js +474 -0
  27. package/lib/artifacts/identity-resolution-document.d.ts +133 -0
  28. package/lib/artifacts/identity-resolution-document.js +625 -0
  29. package/lib/artifacts/mapping-document.d.ts +1 -0
  30. package/lib/artifacts/mapping-document.js +1 -0
  31. package/lib/artifacts/native-calculated-insight-document.d.ts +142 -0
  32. package/lib/artifacts/native-calculated-insight-document.js +321 -0
  33. package/lib/artifacts/native-data-graph-document.d.ts +124 -0
  34. package/lib/artifacts/native-data-graph-document.js +780 -0
  35. package/lib/artifacts/native-dmo-document.d.ts +151 -0
  36. package/lib/artifacts/native-dmo-document.js +313 -0
  37. package/lib/artifacts/native-mapping-document.d.ts +67 -0
  38. package/lib/artifacts/native-mapping-document.js +187 -0
  39. package/lib/artifacts/native-relationship-document.d.ts +76 -0
  40. package/lib/artifacts/native-relationship-document.js +232 -0
  41. package/lib/artifacts/path-selector.d.ts +55 -0
  42. package/lib/artifacts/path-selector.js +167 -0
  43. package/lib/artifacts/read-deploy-input.d.ts +222 -6
  44. package/lib/artifacts/read-deploy-input.js +1213 -53
  45. package/lib/artifacts/segment-document.d.ts +183 -0
  46. package/lib/artifacts/segment-document.js +570 -0
  47. package/lib/artifacts/verbatim-document.d.ts +203 -0
  48. package/lib/artifacts/verbatim-document.js +339 -0
  49. package/lib/artifacts/write-data-graph.d.ts +17 -0
  50. package/lib/artifacts/write-data-graph.js +162 -0
  51. package/lib/artifacts/write-data-stream.d.ts +11 -0
  52. package/lib/artifacts/write-data-stream.js +175 -0
  53. package/lib/artifacts/write-data-transform.d.ts +11 -0
  54. package/lib/artifacts/write-data-transform.js +180 -0
  55. package/lib/artifacts/write-dlo.d.ts +11 -0
  56. package/lib/artifacts/write-dlo.js +158 -0
  57. package/lib/artifacts/write-identity-resolution.d.ts +18 -0
  58. package/lib/artifacts/write-identity-resolution.js +165 -0
  59. package/lib/artifacts/write-mapping-definition.d.ts +17 -0
  60. package/lib/artifacts/write-mapping-definition.js +127 -0
  61. package/lib/artifacts/write-member-export.d.ts +88 -0
  62. package/lib/artifacts/write-member-export.js +199 -0
  63. package/lib/artifacts/write-native-calculated-insight.d.ts +21 -0
  64. package/lib/artifacts/write-native-calculated-insight.js +176 -0
  65. package/lib/artifacts/write-native-dmo.d.ts +11 -0
  66. package/lib/artifacts/write-native-dmo.js +158 -0
  67. package/lib/artifacts/write-native-mapping.d.ts +11 -0
  68. package/lib/artifacts/write-native-mapping.js +157 -0
  69. package/lib/artifacts/write-native-relationship.d.ts +11 -0
  70. package/lib/artifacts/write-native-relationship.js +157 -0
  71. package/lib/artifacts/write-segment.d.ts +26 -0
  72. package/lib/artifacts/write-segment.js +212 -0
  73. package/lib/commands/data360/delete.d.ts +280 -0
  74. package/lib/commands/data360/delete.js +2347 -0
  75. package/lib/commands/data360/deploy.d.ts +235 -16
  76. package/lib/commands/data360/deploy.js +2339 -102
  77. package/lib/commands/data360/list/dataspace.d.ts +53 -0
  78. package/lib/commands/data360/list/dataspace.js +109 -0
  79. package/lib/commands/data360/publish.d.ts +93 -0
  80. package/lib/commands/data360/publish.js +298 -0
  81. package/lib/commands/data360/retrieve.d.ts +283 -14
  82. package/lib/commands/data360/retrieve.js +1945 -95
  83. package/lib/commands/data360/segment/members/export.d.ts +101 -0
  84. package/lib/commands/data360/segment/members/export.js +320 -0
  85. package/lib/errors/codes/calculated-insight.d.ts +2 -0
  86. package/lib/errors/codes/calculated-insight.js +25 -0
  87. package/lib/errors/codes/data-graph.d.ts +2 -0
  88. package/lib/errors/codes/data-graph.js +22 -0
  89. package/lib/errors/codes/data-stream.d.ts +2 -0
  90. package/lib/errors/codes/data-stream.js +70 -0
  91. package/lib/errors/codes/data-transform.d.ts +2 -0
  92. package/lib/errors/codes/data-transform.js +35 -0
  93. package/lib/errors/codes/dependency.d.ts +2 -0
  94. package/lib/errors/codes/dependency.js +11 -0
  95. package/lib/errors/codes/dlo.d.ts +2 -0
  96. package/lib/errors/codes/dlo.js +31 -0
  97. package/lib/errors/codes/dmo.d.ts +2 -0
  98. package/lib/errors/codes/dmo.js +28 -0
  99. package/lib/errors/codes/general.d.ts +2 -0
  100. package/lib/errors/codes/general.js +58 -0
  101. package/lib/errors/codes/identity-resolution.d.ts +2 -0
  102. package/lib/errors/codes/identity-resolution.js +24 -0
  103. package/lib/errors/codes/mapping.d.ts +2 -0
  104. package/lib/errors/codes/mapping.js +42 -0
  105. package/lib/errors/codes/relationship.d.ts +2 -0
  106. package/lib/errors/codes/relationship.js +21 -0
  107. package/lib/errors/codes/segment.d.ts +2 -0
  108. package/lib/errors/codes/segment.js +49 -0
  109. package/lib/errors/registry.d.ts +60 -0
  110. package/lib/errors/registry.js +123 -0
  111. package/lib/exit-code.d.ts +10 -0
  112. package/lib/exit-code.js +24 -0
  113. package/lib/plugin.d.ts +1 -0
  114. package/lib/plugin.js +3 -1
  115. package/lib/services/acquire-retrieve-selection.d.ts +43 -0
  116. package/lib/services/acquire-retrieve-selection.js +145 -0
  117. package/lib/services/bind-identity-resolution-dependencies.d.ts +53 -0
  118. package/lib/services/bind-identity-resolution-dependencies.js +238 -0
  119. package/lib/services/bind-segment-dependencies.d.ts +49 -0
  120. package/lib/services/bind-segment-dependencies.js +115 -0
  121. package/lib/services/bounded-reads.d.ts +17 -0
  122. package/lib/services/bounded-reads.js +42 -0
  123. package/lib/services/calculated-insight-sql-checks.d.ts +33 -0
  124. package/lib/services/calculated-insight-sql-checks.js +193 -0
  125. package/lib/services/command-dlo.d.ts +48 -0
  126. package/lib/services/command-dlo.js +103 -0
  127. package/lib/services/command-native-data-graph.d.ts +54 -0
  128. package/lib/services/command-native-data-graph.js +485 -0
  129. package/lib/services/command-native-data-stream.d.ts +104 -0
  130. package/lib/services/command-native-data-stream.js +257 -0
  131. package/lib/services/command-native-dmo.d.ts +124 -0
  132. package/lib/services/command-native-dmo.js +338 -0
  133. package/lib/services/command-native-mapping.d.ts +65 -0
  134. package/lib/services/command-native-mapping.js +129 -0
  135. package/lib/services/command-native-relationship.d.ts +46 -0
  136. package/lib/services/command-native-relationship.js +103 -0
  137. package/lib/services/cross-dataspace-deploy.d.ts +111 -0
  138. package/lib/services/cross-dataspace-deploy.js +493 -0
  139. package/lib/services/cross-dataspace-rename.d.ts +74 -0
  140. package/lib/services/cross-dataspace-rename.js +1314 -0
  141. package/lib/services/cross-dataspace-sql.d.ts +109 -0
  142. package/lib/services/cross-dataspace-sql.js +1301 -0
  143. package/lib/services/cross-space-context.d.ts +23 -0
  144. package/lib/services/cross-space-context.js +38 -0
  145. package/lib/services/crud-plan.d.ts +137 -0
  146. package/lib/services/crud-plan.js +408 -0
  147. package/lib/services/data-space-check.d.ts +92 -0
  148. package/lib/services/data-space-check.js +169 -0
  149. package/lib/services/data-space-prefix.d.ts +77 -0
  150. package/lib/services/data-space-prefix.js +127 -0
  151. package/lib/services/data-transform-space.d.ts +31 -0
  152. package/lib/services/data-transform-space.js +79 -0
  153. package/lib/services/delete-calculated-insight.d.ts +44 -0
  154. package/lib/services/delete-calculated-insight.js +101 -0
  155. package/lib/services/delete-data-transform.d.ts +52 -0
  156. package/lib/services/delete-data-transform.js +222 -0
  157. package/lib/services/delete-dmo-field.d.ts +211 -0
  158. package/lib/services/delete-dmo-field.js +472 -0
  159. package/lib/services/delete-identity-resolution.d.ts +43 -0
  160. package/lib/services/delete-identity-resolution.js +295 -0
  161. package/lib/services/delete-segment.d.ts +42 -0
  162. package/lib/services/delete-segment.js +248 -0
  163. package/lib/services/deploy-result.d.ts +54 -0
  164. package/lib/services/deploy-result.js +60 -0
  165. package/lib/services/escaped-copy.d.ts +23 -0
  166. package/lib/services/escaped-copy.js +88 -0
  167. package/lib/services/execute-calculated-insight-deploy.d.ts +42 -0
  168. package/lib/services/execute-calculated-insight-deploy.js +96 -0
  169. package/lib/services/execute-data-transform-deploy.d.ts +56 -0
  170. package/lib/services/execute-data-transform-deploy.js +168 -0
  171. package/lib/services/execute-native-identity-resolution-deploy.d.ts +48 -0
  172. package/lib/services/execute-native-identity-resolution-deploy.js +360 -0
  173. package/lib/services/execute-segment-deploy.d.ts +45 -0
  174. package/lib/services/execute-segment-deploy.js +393 -0
  175. package/lib/services/list-data-spaces.d.ts +17 -0
  176. package/lib/services/list-data-spaces.js +24 -0
  177. package/lib/services/local-file-removal.d.ts +36 -0
  178. package/lib/services/local-file-removal.js +103 -0
  179. package/lib/services/native-calculated-insight.d.ts +87 -0
  180. package/lib/services/native-calculated-insight.js +164 -0
  181. package/lib/services/native-crm-data-stream.d.ts +91 -0
  182. package/lib/services/native-crm-data-stream.js +272 -0
  183. package/lib/services/native-data-graph-context.d.ts +12 -0
  184. package/lib/services/native-data-graph-context.js +139 -0
  185. package/lib/services/native-data-graph-crd.d.ts +261 -0
  186. package/lib/services/native-data-graph-crd.js +866 -0
  187. package/lib/services/native-data-stream.d.ts +329 -0
  188. package/lib/services/native-data-stream.js +1830 -0
  189. package/lib/services/native-dlo.d.ts +163 -0
  190. package/lib/services/native-dlo.js +402 -0
  191. package/lib/services/native-dmo.d.ts +191 -0
  192. package/lib/services/native-dmo.js +614 -0
  193. package/lib/services/native-mapping.d.ts +255 -0
  194. package/lib/services/native-mapping.js +1226 -0
  195. package/lib/services/native-relationship.d.ts +132 -0
  196. package/lib/services/native-relationship.js +512 -0
  197. package/lib/services/operation-diagnostic.d.ts +83 -0
  198. package/lib/services/operation-diagnostic.js +272 -0
  199. package/lib/services/preflight-calculated-insight-deploy.d.ts +58 -0
  200. package/lib/services/preflight-calculated-insight-deploy.js +95 -0
  201. package/lib/services/preflight-data-transform-deploy.d.ts +33 -0
  202. package/lib/services/preflight-data-transform-deploy.js +104 -0
  203. package/lib/services/preflight-identity-resolution-deploy.d.ts +107 -0
  204. package/lib/services/preflight-identity-resolution-deploy.js +281 -0
  205. package/lib/services/preflight-segment-deploy.d.ts +31 -0
  206. package/lib/services/preflight-segment-deploy.js +109 -0
  207. package/lib/services/publish-calculated-insight.d.ts +40 -0
  208. package/lib/services/publish-calculated-insight.js +95 -0
  209. package/lib/services/publish-segment.d.ts +65 -0
  210. package/lib/services/publish-segment.js +194 -0
  211. package/lib/services/readback-pace.d.ts +16 -0
  212. package/lib/services/readback-pace.js +23 -0
  213. package/lib/services/retrieve-data-transform.d.ts +26 -0
  214. package/lib/services/retrieve-data-transform.js +65 -0
  215. package/lib/services/retrieve-identity-resolution.d.ts +40 -0
  216. package/lib/services/retrieve-identity-resolution.js +87 -0
  217. package/lib/services/retrieve-inventory.d.ts +32 -0
  218. package/lib/services/retrieve-inventory.js +151 -0
  219. package/lib/services/retrieve-segment.d.ts +120 -0
  220. package/lib/services/retrieve-segment.js +502 -0
  221. package/lib/services/run-retrieve.d.ts +24 -0
  222. package/lib/services/run-retrieve.js +51 -0
  223. package/lib/services/segment-lookup.d.ts +46 -0
  224. package/lib/services/segment-lookup.js +108 -0
  225. package/lib/services/segment-members.d.ts +144 -0
  226. package/lib/services/segment-members.js +402 -0
  227. package/lib/services/select-native-retrieve.d.ts +60 -0
  228. package/lib/services/select-native-retrieve.js +226 -0
  229. package/lib/services/settle-calculated-insight.d.ts +22 -0
  230. package/lib/services/settle-calculated-insight.js +53 -0
  231. package/lib/services/sql-tokens.d.ts +58 -0
  232. package/lib/services/sql-tokens.js +147 -0
  233. package/lib/transport/api-version.d.ts +36 -0
  234. package/lib/transport/api-version.js +89 -0
  235. package/lib/transport/bound-ssot.d.ts +27 -0
  236. package/lib/transport/bound-ssot.js +116 -0
  237. package/lib/transport/calculated-insight-rest.d.ts +28 -0
  238. package/lib/transport/calculated-insight-rest.js +62 -0
  239. package/lib/transport/connect-client.d.ts +94 -32
  240. package/lib/transport/connect-client.js +318 -178
  241. package/lib/transport/connect-diagnostic.d.ts +89 -0
  242. package/lib/transport/connect-diagnostic.js +331 -0
  243. package/lib/transport/data-graph-rest.d.ts +27 -0
  244. package/lib/transport/data-graph-rest.js +76 -0
  245. package/lib/transport/data-stream-rest.d.ts +62 -0
  246. package/lib/transport/data-stream-rest.js +348 -0
  247. package/lib/transport/data-transform-rest.d.ts +25 -0
  248. package/lib/transport/data-transform-rest.js +100 -0
  249. package/lib/transport/dlo-native-rest.d.ts +22 -0
  250. package/lib/transport/dlo-native-rest.js +89 -0
  251. package/lib/transport/dlo-rest.d.ts +23 -0
  252. package/lib/transport/dlo-rest.js +135 -0
  253. package/lib/transport/dmo-native-rest.d.ts +18 -0
  254. package/lib/transport/dmo-native-rest.js +55 -0
  255. package/lib/transport/dmo-rest.d.ts +30 -0
  256. package/lib/transport/dmo-rest.js +163 -0
  257. package/lib/transport/mapping-rest.d.ts +63 -0
  258. package/lib/transport/mapping-rest.js +341 -0
  259. package/lib/transport/project-metadata.d.ts +36 -0
  260. package/lib/transport/project-metadata.js +118 -0
  261. package/lib/transport/relationship-rest.d.ts +43 -0
  262. package/lib/transport/relationship-rest.js +201 -0
  263. package/package.json +29 -6
  264. package/lib/artifacts/dmo-document.d.ts +0 -181
  265. package/lib/artifacts/dmo-document.js +0 -848
  266. package/lib/artifacts/dmo-update-delta.d.ts +0 -28
  267. package/lib/artifacts/dmo-update-delta.js +0 -86
  268. package/lib/artifacts/write-other-dmo.d.ts +0 -17
  269. package/lib/artifacts/write-other-dmo.js +0 -140
  270. package/lib/services/execute-dmo-create.d.ts +0 -24
  271. package/lib/services/execute-dmo-create.js +0 -118
  272. package/lib/services/execute-dmo-deploy.d.ts +0 -33
  273. package/lib/services/execute-dmo-deploy.js +0 -197
  274. package/lib/services/preflight-dmo-deploy.d.ts +0 -69
  275. package/lib/services/preflight-dmo-deploy.js +0 -161
  276. package/lib/services/retrieve-other-dmo.d.ts +0 -46
  277. package/lib/services/retrieve-other-dmo.js +0 -140
@@ -0,0 +1,493 @@
1
+ import { readFile } from 'node:fs/promises';
2
+ import path from 'node:path';
3
+ import { translatedDeployInput } from '../artifacts/read-deploy-input.js';
4
+ import { leanNativeDmoDocument, validateNativeDmoDocument, } from '../artifacts/native-dmo-document.js';
5
+ import { serializeSegmentFiles } from '../artifacts/segment-document.js';
6
+ import { codeFilePath } from '../artifacts/code-fields.js';
7
+ import { calculatedInsightReference, serializeNativeCalculatedInsight, } from '../artifacts/native-calculated-insight-document.js';
8
+ import { writeNativeCalculatedInsightDocument } from '../artifacts/write-native-calculated-insight.js';
9
+ import { validateNativeDataGraphDocument } from '../artifacts/native-data-graph-document.js';
10
+ import { serializeIdentityResolutionDocument, validateIdentityResolutionDocument, } from '../artifacts/identity-resolution-document.js';
11
+ import { serializeDataTransformDocument, validateDataTransformDocument, } from '../artifacts/data-transform-document.js';
12
+ import { writeDataGraphDocument } from '../artifacts/write-data-graph.js';
13
+ import { writeIdentityResolutionDocument } from '../artifacts/write-identity-resolution.js';
14
+ import { writeDataTransformDocument } from '../artifacts/write-data-transform.js';
15
+ import { dataTransformArtifactPath } from './retrieve-data-transform.js';
16
+ import { writeNativeDmoDocument } from '../artifacts/write-native-dmo.js';
17
+ import { writeNativeMappingDocument } from '../artifacts/write-native-mapping.js';
18
+ import { writeNativeRelationshipDocument } from '../artifacts/write-native-relationship.js';
19
+ import { writeSegmentDocument } from '../artifacts/write-segment.js';
20
+ import { customDmosOf, pairedByRulesets, translateCalculatedInsight, translateDocument, } from './cross-dataspace-rename.js';
21
+ import { explainInput } from './operation-diagnostic.js';
22
+ /** Payload-free refusal of a cross-data-space deploy. */
23
+ export class CrossSpaceError extends Error {
24
+ code;
25
+ constructor(code) {
26
+ super(`Cross-data-space deploy refused: ${code}.`);
27
+ this.code = code;
28
+ this.name = 'CrossSpaceError';
29
+ }
30
+ }
31
+ /**
32
+ * The data spaces a file names: a list for a DLO or data stream, one name otherwise (a
33
+ * calculated insight in its identity).
34
+ * @param {Record<string, unknown>} document Admitted file.
35
+ * @returns {string[]} Data space names.
36
+ */
37
+ export function fileDataSpaces(document) {
38
+ const named = document.c__dataSpace;
39
+ if (Array.isArray(named))
40
+ return named;
41
+ if (typeof named === 'string')
42
+ return [named];
43
+ if (document.kind === 'calculated-insight') {
44
+ const space = document.identity?.dataSpace;
45
+ return typeof space === 'string' ? [space] : [];
46
+ }
47
+ return [];
48
+ }
49
+ /**
50
+ * The data space a file comes from when it is not the run's: the one it names, or for a DLO or
51
+ * stream whose list lacks the run's data space, its first listed space.
52
+ * @param {Record<string, unknown>} document Admitted file.
53
+ * @param {string} run The run's data space.
54
+ * @returns {string | undefined} Source data space, or undefined for a file of the run's space.
55
+ */
56
+ export function crossSpaceSource(document, run) {
57
+ const spaces = fileDataSpaces(document);
58
+ if (spaces.length === 0 || spaces.includes(run))
59
+ return undefined;
60
+ return spaces[0];
61
+ }
62
+ /**
63
+ * The family name deploy reports for a file.
64
+ * @param {Record<string, unknown>} document Admitted file.
65
+ * @returns {string} Family.
66
+ */
67
+ function familyOf(document) {
68
+ return document.kind === 'native-data-graph' ? 'data-graph' : String(document.kind);
69
+ }
70
+ /**
71
+ * The name of the item a file describes: `definition.name` for a data graph or transform,
72
+ * `identity.apiName` otherwise.
73
+ * @param {Record<string, unknown>} document Admitted file.
74
+ * @returns {string} Name.
75
+ */
76
+ function documentName(document) {
77
+ return document.kind === 'native-data-graph' || document.kind === 'data-transform'
78
+ ? String(document.definition.name)
79
+ : document.identity.apiName;
80
+ }
81
+ /**
82
+ * Every string of a value, for the scan of names before translation.
83
+ * @param {unknown} value Value.
84
+ * @param {string[]} into Receives the strings.
85
+ * @returns {string[]} The strings.
86
+ */
87
+ function stringsOf(value, into = []) {
88
+ if (typeof value === 'string')
89
+ into.push(value);
90
+ else if (Array.isArray(value))
91
+ for (const entry of value)
92
+ stringsOf(entry, into);
93
+ else if (value && typeof value === 'object')
94
+ for (const entry of Object.values(value))
95
+ stringsOf(entry, into);
96
+ return into;
97
+ }
98
+ /**
99
+ * Whether a selected file of another data space names an identity resolution output (by the
100
+ * rule's spelling, or an output of the oldest default ruleset without suffix such as
101
+ * `UnifiedIndividual__dlm`): only then does the deploy read the ruleset list, once.
102
+ * @param {readonly DeployInput[]} inputs The admitted selection.
103
+ * @param {string} run The run's data space.
104
+ * @param {readonly OrgDataSpace[]} spaces The org's data spaces.
105
+ * @returns {boolean} Whether the ruleset list is needed.
106
+ */
107
+ export function crossSpaceNeedsRulesets(inputs, run, spaces) {
108
+ return inputs.some((input) => {
109
+ const document = input.document;
110
+ const from = crossSpaceSource(document, run);
111
+ if (from === undefined)
112
+ return false;
113
+ const context = { source: from, target: run, spaces };
114
+ // A SQL text (an insight's dependencies, a segment model) is read name by name.
115
+ return stringsOf(document).some((value) => (value.match(/[A-Za-z0-9_]+__dlm\b/gu) ?? []).some((name) => pairedByRulesets(context, name)));
116
+ });
117
+ }
118
+ const rulesetsRead = 'CROSS_DATASPACE_RULESETS_READ_FAILED';
119
+ /**
120
+ * The refusal when the ruleset list cannot be read.
121
+ * @returns {CrossSpaceError} CROSS_DATASPACE_RULESETS_READ_FAILED with an explanation.
122
+ */
123
+ function rulesetsReadFailed() {
124
+ return explainInput(new CrossSpaceError(rulesetsRead), 'A file names an identity resolution output, and the identity resolution ruleset list that places it could not be read. Nothing was sent; rerun the deploy.');
125
+ }
126
+ /**
127
+ * A nonempty text value, or undefined.
128
+ * @param {unknown} value Candidate.
129
+ * @returns {string | undefined} The text.
130
+ */
131
+ function text(value) {
132
+ return typeof value === 'string' && value ? value : undefined;
133
+ }
134
+ /** The route of the identity resolution ruleset list (every data space at once, API 68). */
135
+ const rulesetsRoute = '/services/data/v68.0/ssot/identity-resolutions';
136
+ /**
137
+ * Read the org's identity resolution rulesets with one `GET /ssot/identity-resolutions`: each
138
+ * one's name, data space, type and the unified and link output of each reconciliation entity.
139
+ * The list answers every data space at once (live API 68). An entry without a name, data space or
140
+ * type is left out; a malformed answer is CROSS_DATASPACE_RULESETS_READ_FAILED.
141
+ * @param {DataSpaceSender} open Opens the command's shared bound sender.
142
+ * @returns {Promise<RulesetEntry[]>} Rulesets.
143
+ */
144
+ export async function readRulesetEntries(open) {
145
+ const sender = await open();
146
+ let response;
147
+ try {
148
+ response = await sender.request('GET', rulesetsRoute);
149
+ }
150
+ catch (error) {
151
+ if (error.code === 'AUTHENTICATION_FAILED')
152
+ throw error;
153
+ throw rulesetsReadFailed();
154
+ }
155
+ const entries = response?.identityResolutions;
156
+ if (!Array.isArray(entries))
157
+ throw rulesetsReadFailed();
158
+ const rulesets = [];
159
+ for (const entry of entries) {
160
+ const apiName = text(entry?.objectApiName);
161
+ const dataSpace = text(entry?.dataSpaceName);
162
+ const configurationType = text(entry?.configurationType);
163
+ if (!apiName || !dataSpace || !configurationType)
164
+ continue;
165
+ const rules = Array.isArray(entry?.reconciliationRules)
166
+ ? entry.reconciliationRules
167
+ : [];
168
+ rulesets.push({
169
+ apiName,
170
+ dataSpace,
171
+ configurationType,
172
+ outputs: rules.flatMap((rule) => {
173
+ const entity = text(rule?.entityName);
174
+ return entity
175
+ ? [{ entity, unified: text(rule?.unifiedDmoName), link: text(rule?.linkDmoName) }]
176
+ : [];
177
+ }),
178
+ });
179
+ }
180
+ return rulesets;
181
+ }
182
+ /**
183
+ * Translate every selected file of another data space into the run's data space, in memory. The
184
+ * files on disk never change; each translated input still checks the file's own bytes.
185
+ * @param {readonly DeployInput[]} inputs The admitted selection.
186
+ * @param {string} run The run's data space.
187
+ * @param {readonly OrgDataSpace[]} spaces The org's data spaces with their prefixes.
188
+ * @param {readonly RulesetEntry[]} [rulesets] The ruleset list, read when a file names an
189
+ * identity resolution output (crossSpaceNeedsRulesets).
190
+ * @returns {CrossSpacePlan} The translated selection and its report.
191
+ */
192
+ export function planCrossSpaceInputs(inputs, run, spaces, rulesets) {
193
+ const documents = inputs.map((input) => input.document);
194
+ // Custom DMO evidence per source data space: only DMO files of that data space count.
195
+ const customDmos = new Map();
196
+ const customDmosFrom = (source) => {
197
+ let found = customDmos.get(source);
198
+ if (!found) {
199
+ found = customDmosOf(documents, source);
200
+ customDmos.set(source, found);
201
+ }
202
+ return found;
203
+ };
204
+ // Rulesets this deploy translates too: their outputs keep the rule names (the ID is kept).
205
+ const selectedRulesets = new Set(documents
206
+ .filter((document) => document.kind === 'identity-resolution' && crossSpaceSource(document, run) !== undefined)
207
+ .map((document) => document.identity.apiName));
208
+ const files = [];
209
+ const unsupported = [];
210
+ const translated = inputs.map((input, index) => {
211
+ const document = documents[index];
212
+ const from = crossSpaceSource(document, run);
213
+ if (from === undefined)
214
+ return input;
215
+ const family = familyOf(document);
216
+ const context = {
217
+ source: from,
218
+ target: run,
219
+ spaces,
220
+ customDmos: customDmosFrom(from),
221
+ selectedRulesets,
222
+ ...(rulesets ? { rulesets } : {}),
223
+ };
224
+ if (document.kind === 'calculated-insight') {
225
+ // An insight is translated from its JSON with the SQL joined, then planned again from it.
226
+ const insight = translateCalculatedInsight(input.native, context);
227
+ files.push({
228
+ path: input.path,
229
+ family,
230
+ from,
231
+ rewrites: insight.rewrites,
232
+ unresolved: insight.unresolved,
233
+ });
234
+ return translatedDeployInput(input, calculatedInsightReference(insight.document), insight.document);
235
+ }
236
+ const result = translateDocument(document, context);
237
+ if ('unsupported' in result) {
238
+ unsupported.push({ path: input.path, family, from, reason: result.unsupported });
239
+ return input;
240
+ }
241
+ files.push({
242
+ path: input.path,
243
+ family,
244
+ from,
245
+ rewrites: result.rewrites,
246
+ unresolved: result.unresolved,
247
+ });
248
+ return translatedDeployInput(input, result.document);
249
+ });
250
+ return { inputs: translated, files, unsupported };
251
+ }
252
+ /**
253
+ * The refusal of a plan that holds an unsupported file or a name it cannot place, or undefined.
254
+ * @param {CrossSpacePlan} plan Translated selection.
255
+ * @param {string} run The run's data space.
256
+ * @param {(file: string) => string} shown How a file path is shown.
257
+ * @returns {CrossSpaceError | undefined} Refusal with its explanation.
258
+ */
259
+ export function crossSpaceRefusal(plan, run, shown) {
260
+ if (plan.unsupported.length > 0)
261
+ return explainInput(new CrossSpaceError('CROSS_DATASPACE_UNSUPPORTED'), `--allow-cross-dataspace cannot deploy these files into ${run} yet: ${plan.unsupported
262
+ .map((file) => `${shown(file.path)} (${file.reason})`)
263
+ .join('; ')}. Nothing was sent. Deploy them with --dataspace ${plan.unsupported[0].from}, or leave them out of this deploy.`);
264
+ const unresolved = plan.files.flatMap((file) => file.unresolved.map((entry) => `${shown(file.path)} ${entry.at} ${entry.name}: ${entry.reason}`));
265
+ if (unresolved.length > 0)
266
+ return explainInput(new CrossSpaceError('CROSS_DATASPACE_UNRESOLVED'), `These names cannot be placed in ${run} exactly, so nothing was sent: ${unresolved.join('; ')}.`);
267
+ return duplicateTargets(plan, run, shown);
268
+ }
269
+ /**
270
+ * The keys under which an input reaches an org item: family and API name, and for a mapping its
271
+ * source DLO and target DMO, for a relationship both its sides (their org names differ from the
272
+ * file's).
273
+ * @param {DeployInput} input Input, translated or not.
274
+ * @returns {string[]} Lowercase target keys.
275
+ */
276
+ function targetKeys(input) {
277
+ const document = input.document;
278
+ const keys = [`${familyOf(document)}:${documentName(document)}`];
279
+ const spec = document.spec;
280
+ if (document.kind === 'mapping')
281
+ keys.push(`mapping-binding:${spec.sourceEntityDeveloperName}:${spec.targetEntityDeveloperName}`);
282
+ if (document.kind === 'relationship')
283
+ keys.push(`relationship-binding:${spec.sourceObjectName}.${spec.sourceFieldName}:${spec.targetObjectName}.${spec.targetFieldName}`);
284
+ return keys.map((key) => key.toLowerCase());
285
+ }
286
+ /**
287
+ * Refuse two inputs that reach the same org item once translated (a copy file selected beside
288
+ * its original), before any write: both would deploy over each other.
289
+ * @param {CrossSpacePlan} plan Translated selection.
290
+ * @param {string} run The run's data space.
291
+ * @param {(file: string) => string} shown How a file path is shown.
292
+ * @returns {CrossSpaceError | undefined} DUPLICATE_IDENTITY with both files named.
293
+ */
294
+ function duplicateTargets(plan, run, shown) {
295
+ const seen = new Map();
296
+ for (const input of plan.inputs)
297
+ for (const key of targetKeys(input)) {
298
+ const prior = seen.get(key);
299
+ if (prior !== undefined && prior !== input.path)
300
+ return explainInput(new CrossSpaceError('DUPLICATE_IDENTITY'), `${shown(prior)} and ${shown(input.path)} describe the same ${familyOf(input.document)} once translated into ${run}, so nothing was sent. Select only one of them, for example leave out the copy file of an earlier --allow-cross-dataspace deploy, then rerun.`);
301
+ seen.set(key, input.path);
302
+ }
303
+ return undefined;
304
+ }
305
+ /**
306
+ * The project root of an admitted file (`<root>/data360/<family folder>/<file>`).
307
+ * @param {string} file Absolute file path.
308
+ * @returns {string} Project root.
309
+ */
310
+ function projectRootOf(file) {
311
+ return path.dirname(path.dirname(path.dirname(file)));
312
+ }
313
+ /**
314
+ * The JSON file and its SQL files, as one copy.
315
+ * @param {string} jsonPath JSON file path.
316
+ * @param {{json: string; files: Array<{extension: string; name?: string; text: string}>}} serialized Serialized files.
317
+ * @param {string} serialized.json JSON text.
318
+ * @param {Array<{extension: string; name?: string; text: string}>} serialized.files Code files.
319
+ * @returns {PlannedCopy} Paths and bytes.
320
+ */
321
+ function withCode(jsonPath, serialized) {
322
+ return [
323
+ { path: jsonPath, bytes: Buffer.from(serialized.json) },
324
+ ...serialized.files.map((file) => ({
325
+ path: codeFilePath(jsonPath, file),
326
+ bytes: Buffer.from(file.text),
327
+ })),
328
+ ];
329
+ }
330
+ /**
331
+ * The copy files of a translated DMO, segment (with its SQL files), calculated insight (JSON and
332
+ * SQL), data graph, ruleset or transform, as retrieve would write them.
333
+ * @param {DeployInput} input Translated input.
334
+ * @returns {PlannedCopy | undefined} Paths and bytes; undefined for another family.
335
+ */
336
+ function plannedCopy(input) {
337
+ const document = input.document;
338
+ const root = projectRootOf(input.path);
339
+ const apiName = documentName(document);
340
+ if (document.kind === 'calculated-insight')
341
+ return withCode(path.join(root, 'data360', 'calculatedInsights', `${apiName}.calculatedInsight-meta.json`), serializeNativeCalculatedInsight(input.native));
342
+ if (document.kind === 'native-data-graph')
343
+ return [
344
+ {
345
+ path: path.join(root, 'data360', 'dataGraphs', `${apiName}.native-dataGraph.json`),
346
+ bytes: Buffer.from(`${JSON.stringify(validateNativeDataGraphDocument(document, 'retrieve'), undefined, 2)}\n`),
347
+ },
348
+ ];
349
+ if (document.kind === 'identity-resolution')
350
+ return [
351
+ {
352
+ path: path.join(root, 'data360', 'identityResolution', `${apiName}.identityResolution.json`),
353
+ bytes: Buffer.from(serializeIdentityResolutionDocument(validateIdentityResolutionDocument(document, 'retrieve'), 'retrieve')),
354
+ },
355
+ ];
356
+ if (document.kind === 'data-transform')
357
+ return [
358
+ {
359
+ path: path.join(root, dataTransformArtifactPath(apiName)),
360
+ bytes: Buffer.from(serializeDataTransformDocument(validateDataTransformDocument(document, 'retrieve'), 'retrieve')),
361
+ },
362
+ ];
363
+ if (document.kind === 'dmo')
364
+ return [
365
+ {
366
+ path: path.join(root, 'data360', 'dmo', `${apiName}.dmo-meta.json`),
367
+ bytes: Buffer.from(`${JSON.stringify(leanNativeDmoDocument(validateNativeDmoDocument(document, 'retrieve')), undefined, 2)}\n`),
368
+ },
369
+ ];
370
+ if (document.kind === 'segment')
371
+ return withCode(path.join(root, 'data360', 'segments', `${apiName}.segment.json`), serializeSegmentFiles(document, 'retrieve'));
372
+ return undefined;
373
+ }
374
+ /**
375
+ * Check, before any write, that the copy file of a translated item named before the write (DMO,
376
+ * segment, data graph, ruleset, transform) can be written: a
377
+ * new path, or an existing file with exactly the same content (reused). Mapping and
378
+ * relationship copies are named by Salesforce, so they are checked when they are written.
379
+ * @param {readonly DeployInput[]} inputs Translated inputs.
380
+ * @param {(file: string) => string} shown How a file path is shown.
381
+ * @returns {Promise<string[]>} Copy paths planned (new or reused).
382
+ */
383
+ export async function precheckCopyFiles(inputs, shown) {
384
+ const planned = [];
385
+ for (const input of inputs)
386
+ for (const copy of plannedCopy(input) ?? []) {
387
+ let existing;
388
+ try {
389
+ existing = await readFile(copy.path);
390
+ }
391
+ catch (error) {
392
+ if (error.code !== 'ENOENT')
393
+ throw error;
394
+ }
395
+ if (existing !== undefined &&
396
+ (path.resolve(copy.path) === path.resolve(input.path) || !existing.equals(copy.bytes)))
397
+ throw explainInput(new CrossSpaceError('CROSS_DATASPACE_COPY_CONFLICT'), `The translated copy of ${shown(input.path)} would be written to ${shown(copy.path)}, but that file already exists with other content. Existing files are never changed, so nothing was sent. Move or remove that file, then rerun.`);
398
+ planned.push(copy.path);
399
+ }
400
+ return planned;
401
+ }
402
+ /**
403
+ * The key of one mapping pair: its source and target field.
404
+ * @param {{sourceFieldDeveloperName: string, targetFieldDeveloperName: string}} pair Pair.
405
+ * @param {string} pair.sourceFieldDeveloperName DLO field.
406
+ * @param {string} pair.targetFieldDeveloperName DMO field.
407
+ * @returns {string} Pair key.
408
+ */
409
+ function pairKey(pair) {
410
+ return `${pair.sourceFieldDeveloperName}:${pair.targetFieldDeveloperName}`;
411
+ }
412
+ /**
413
+ * The copy file of a deployed mapping as a retrieve of the target writes it: the mapping as read
414
+ * back (its name and the pair selectors Salesforce holds), with the deployed pairs only (no pair
415
+ * Salesforce generated) and the label the file set, unless that equals the name.
416
+ * @param {NativeMappingDocument} deployed Translated mapping that was deployed.
417
+ * @param {NativeMappingDocument} org The mapping as the deploy read it back.
418
+ * @returns {NativeMappingDocument} Copy document.
419
+ */
420
+ function mappingCopy(deployed, org) {
421
+ const authored = new Set(deployed.spec.fieldMappings.map((pair) => pairKey(pair)));
422
+ const label = deployed.spec.label;
423
+ return {
424
+ ...org,
425
+ spec: {
426
+ ...org.spec,
427
+ fieldMappings: org.spec.fieldMappings.filter((pair) => authored.has(pairKey(pair))),
428
+ ...(label === undefined || label === org.identity.apiName ? {} : { label }),
429
+ },
430
+ };
431
+ }
432
+ /**
433
+ * Write the translated copy of one deployed item, only to a new path; an existing file with the
434
+ * same content is reused, one with other content is left alone and reported. A DLO gets no copy
435
+ * (only its data space list changed in the org).
436
+ * @param {DeployInput} input Translated input.
437
+ * @param {string} [orgName] The name Salesforce returned, for a relationship.
438
+ * @param {NativeMappingDocument} [orgMapping] For a mapping, the document the deploy read back.
439
+ * @returns {Promise<CopyOutcome | undefined>} Outcome; undefined for a DLO.
440
+ */
441
+ export async function writeCopyFile(input, orgName, orgMapping) {
442
+ const document = input.document;
443
+ const root = projectRootOf(input.path);
444
+ if (document.kind === 'dlo')
445
+ return undefined;
446
+ const named = document.kind === 'mapping'
447
+ ? orgMapping === undefined
448
+ ? undefined
449
+ : mappingCopy(document, orgMapping)
450
+ : document.kind === 'relationship'
451
+ ? orgName === undefined
452
+ ? undefined
453
+ : { ...document, identity: { ...document.identity, apiName: orgName } }
454
+ : document;
455
+ if (!named)
456
+ return {
457
+ source: input.path,
458
+ outcome: 'not-written',
459
+ reason: 'Salesforce returned no name for it',
460
+ };
461
+ try {
462
+ const receipt = document.kind === 'calculated-insight'
463
+ ? await writeNativeCalculatedInsightDocument(input.native, root, false)
464
+ : document.kind === 'dmo'
465
+ ? await writeNativeDmoDocument(named, root, false)
466
+ : document.kind === 'mapping'
467
+ ? await writeNativeMappingDocument(named, root, false)
468
+ : document.kind === 'relationship'
469
+ ? await writeNativeRelationshipDocument(named, root, false)
470
+ : document.kind === 'native-data-graph'
471
+ ? await writeDataGraphDocument(named, root, false)
472
+ : document.kind === 'identity-resolution'
473
+ ? await writeIdentityResolutionDocument(named, root, false)
474
+ : document.kind === 'data-transform'
475
+ ? await writeDataTransformDocument(named, root, false)
476
+ : await writeSegmentDocument(named, root, false);
477
+ return {
478
+ source: input.path,
479
+ path: receipt.path,
480
+ outcome: receipt.outcome === 'unchanged' ? 'unchanged' : 'written',
481
+ };
482
+ }
483
+ catch (error) {
484
+ const code = error.code;
485
+ return {
486
+ source: input.path,
487
+ outcome: 'not-written',
488
+ reason: code === 'OVERWRITE_REQUIRED'
489
+ ? 'a file of that name already exists with other content; existing files are never changed'
490
+ : `the file could not be written (${typeof code === 'string' ? code : 'OUTPUT_FAILED'})`,
491
+ };
492
+ }
493
+ }
@@ -0,0 +1,74 @@
1
+ import type { SqlNamer } from './cross-dataspace-sql.js';
2
+ import type { CrossSpaceContext, CrossSpaceRule } from 'salesforce-data360-rules';
3
+ export type { CrossSpaceContext, CrossSpaceRule, DmoKind, NotPlaced, Placed, RulesetEntry, } from 'salesforce-data360-rules';
4
+ export { pairedByRulesets, placeDmo, translateDmoName, translateFieldName, translateRulesetName, translateSegmentName, translateUniqueName, } from 'salesforce-data360-rules';
5
+ /** One value the translation changes: where in the file, from what, to what, by which rule. */
6
+ export type CrossSpaceRewrite = {
7
+ at: string;
8
+ from: string;
9
+ to: string;
10
+ rule: CrossSpaceRule;
11
+ };
12
+ /** A name the translation cannot place exactly; the file is refused before any write. */
13
+ export type CrossSpaceUnresolved = {
14
+ at: string;
15
+ name: string;
16
+ reason: string;
17
+ };
18
+ /** The file translated into the run's data space, with every change and every unplaced name. */
19
+ export type CrossSpaceTranslation = {
20
+ document: Record<string, unknown>;
21
+ rewrites: CrossSpaceRewrite[];
22
+ unresolved: CrossSpaceUnresolved[];
23
+ };
24
+ /** A file of a kind the translation does not cover yet. */
25
+ export type CrossSpaceUnsupported = {
26
+ unsupported: string;
27
+ };
28
+ /**
29
+ * Why a file of this kind cannot be translated yet, or undefined when it can.
30
+ * @param {Record<string, unknown>} document Admitted file.
31
+ * @returns {string | undefined} Reason.
32
+ */
33
+ export declare function crossSpaceUnsupported(document: Record<string, unknown>): string | undefined;
34
+ /**
35
+ * The DMOs of a non-default data space that a selected DMO file of that same data space shows to
36
+ * be custom: a field with creationType Custom and none with Standard (as retrieve writes them). A
37
+ * DMO file of another data space is no evidence.
38
+ * @param {ReadonlyArray<Record<string, unknown>>} documents Admitted files.
39
+ * @param {string} source The data space of the files being translated.
40
+ * @returns {Set<string>} DMO names.
41
+ */
42
+ export declare function customDmosOf(documents: ReadonlyArray<Record<string, unknown>>, source: string): Set<string>;
43
+ /**
44
+ * The rule engine as the SQL rewrite uses it. A `__dlm` table is placed by the DMO rules (and the
45
+ * ruleset list), a `__cio` table by the prefix rule of insights, and a `__dll` table keeps its
46
+ * name. A column of a table the rules do not know is placed only when it never changes in this
47
+ * direction. A name still holding `ssot__` towards a non-default data space, or another data
48
+ * space's prefix, is left over.
49
+ * @param {CrossSpaceContext} context Translation context.
50
+ * @returns {SqlNamer} The namer.
51
+ */
52
+ export declare function sqlNamer(context: CrossSpaceContext): SqlNamer;
53
+ /**
54
+ * Translate one calculated insight (its JSON with the SQL joined) into the target data space: its
55
+ * name and a source insight by the prefix rule, its data space, and every table and column of its
56
+ * SQL (classic and DC_SQL alike). Salesforce accepts an insight left in the source spelling and
57
+ * then fails it a few minutes after activation without a message (live API 68), so every name-like
58
+ * value the translation does not account for is a name it cannot place.
59
+ * @param {Record<string, unknown>} native Insight document (not changed).
60
+ * @param {CrossSpaceContext} context From which data space to which one.
61
+ * @returns {CrossSpaceTranslation} The translated insight.
62
+ */
63
+ export declare function translateCalculatedInsight(native: Record<string, unknown>, context: CrossSpaceContext): CrossSpaceTranslation;
64
+ /**
65
+ * Translate one admitted file of the source data space into the target data space, in memory.
66
+ * DMO: its name and field names. Mapping: its target DMO and target fields (the source DLO side
67
+ * and the mapping's own name stay; the mapping is found by its binding). Relationship: both
68
+ * sides. DLO: the target data space is added to its list. UI segment: its name, DMO and criteria
69
+ * names; Waterfall: its name, DMO and child segments.
70
+ * @param {Record<string, unknown>} document Admitted file (not changed).
71
+ * @param {CrossSpaceContext} context From which data space to which one.
72
+ * @returns {CrossSpaceTranslation | CrossSpaceUnsupported} The translated copy, or why not.
73
+ */
74
+ export declare function translateDocument(document: Record<string, unknown>, context: CrossSpaceContext): CrossSpaceTranslation | CrossSpaceUnsupported;