sf-plugin-data 0.0.0-stage → 0.2.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 (256) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +589 -2
  3. package/bin/run.js +4 -0
  4. package/docs/artifact-format.md +527 -0
  5. package/docs/capabilities.md +1208 -0
  6. package/docs/git-workflow.md +160 -0
  7. package/docs/handover.md +10446 -0
  8. package/lib/api.d.ts +7 -0
  9. package/lib/api.js +7 -0
  10. package/lib/artifacts/code-fields.d.ts +49 -0
  11. package/lib/artifacts/code-fields.js +142 -0
  12. package/lib/artifacts/data-graph-document.d.ts +29 -0
  13. package/lib/artifacts/data-graph-document.js +52 -0
  14. package/lib/artifacts/data-space-field.d.ts +65 -0
  15. package/lib/artifacts/data-space-field.js +98 -0
  16. package/lib/artifacts/data-stream-document.d.ts +264 -0
  17. package/lib/artifacts/data-stream-document.js +680 -0
  18. package/lib/artifacts/data-transform-document.d.ts +152 -0
  19. package/lib/artifacts/data-transform-document.js +560 -0
  20. package/lib/artifacts/dependency-selection.d.ts +51 -0
  21. package/lib/artifacts/dependency-selection.js +154 -0
  22. package/lib/artifacts/dlo-document.d.ts +93 -0
  23. package/lib/artifacts/dlo-document.js +253 -0
  24. package/lib/artifacts/enum-case.d.ts +39 -0
  25. package/lib/artifacts/enum-case.js +67 -0
  26. package/lib/artifacts/git-deploy-selection.d.ts +68 -0
  27. package/lib/artifacts/git-deploy-selection.js +474 -0
  28. package/lib/artifacts/identity-resolution-document.d.ts +133 -0
  29. package/lib/artifacts/identity-resolution-document.js +625 -0
  30. package/lib/artifacts/mapping-document.d.ts +1 -0
  31. package/lib/artifacts/mapping-document.js +1 -0
  32. package/lib/artifacts/native-calculated-insight-document.d.ts +142 -0
  33. package/lib/artifacts/native-calculated-insight-document.js +321 -0
  34. package/lib/artifacts/native-data-graph-document.d.ts +124 -0
  35. package/lib/artifacts/native-data-graph-document.js +780 -0
  36. package/lib/artifacts/native-dmo-document.d.ts +151 -0
  37. package/lib/artifacts/native-dmo-document.js +313 -0
  38. package/lib/artifacts/native-mapping-document.d.ts +67 -0
  39. package/lib/artifacts/native-mapping-document.js +187 -0
  40. package/lib/artifacts/native-relationship-document.d.ts +76 -0
  41. package/lib/artifacts/native-relationship-document.js +232 -0
  42. package/lib/artifacts/path-selector.d.ts +55 -0
  43. package/lib/artifacts/path-selector.js +167 -0
  44. package/lib/artifacts/read-deploy-input.d.ts +239 -0
  45. package/lib/artifacts/read-deploy-input.js +1359 -0
  46. package/lib/artifacts/segment-document.d.ts +183 -0
  47. package/lib/artifacts/segment-document.js +570 -0
  48. package/lib/artifacts/verbatim-document.d.ts +203 -0
  49. package/lib/artifacts/verbatim-document.js +339 -0
  50. package/lib/artifacts/write-data-graph.d.ts +17 -0
  51. package/lib/artifacts/write-data-graph.js +162 -0
  52. package/lib/artifacts/write-data-stream.d.ts +11 -0
  53. package/lib/artifacts/write-data-stream.js +175 -0
  54. package/lib/artifacts/write-data-transform.d.ts +11 -0
  55. package/lib/artifacts/write-data-transform.js +180 -0
  56. package/lib/artifacts/write-dlo.d.ts +11 -0
  57. package/lib/artifacts/write-dlo.js +158 -0
  58. package/lib/artifacts/write-identity-resolution.d.ts +18 -0
  59. package/lib/artifacts/write-identity-resolution.js +165 -0
  60. package/lib/artifacts/write-mapping-definition.d.ts +17 -0
  61. package/lib/artifacts/write-mapping-definition.js +127 -0
  62. package/lib/artifacts/write-member-export.d.ts +88 -0
  63. package/lib/artifacts/write-member-export.js +199 -0
  64. package/lib/artifacts/write-native-calculated-insight.d.ts +21 -0
  65. package/lib/artifacts/write-native-calculated-insight.js +176 -0
  66. package/lib/artifacts/write-native-dmo.d.ts +11 -0
  67. package/lib/artifacts/write-native-dmo.js +158 -0
  68. package/lib/artifacts/write-native-mapping.d.ts +11 -0
  69. package/lib/artifacts/write-native-mapping.js +157 -0
  70. package/lib/artifacts/write-native-relationship.d.ts +11 -0
  71. package/lib/artifacts/write-native-relationship.js +157 -0
  72. package/lib/artifacts/write-segment.d.ts +26 -0
  73. package/lib/artifacts/write-segment.js +212 -0
  74. package/lib/commands/data360/delete.d.ts +280 -0
  75. package/lib/commands/data360/delete.js +2297 -0
  76. package/lib/commands/data360/deploy.d.ts +303 -0
  77. package/lib/commands/data360/deploy.js +2400 -0
  78. package/lib/commands/data360/publish.d.ts +93 -0
  79. package/lib/commands/data360/publish.js +292 -0
  80. package/lib/commands/data360/retrieve.d.ts +322 -0
  81. package/lib/commands/data360/retrieve.js +1963 -0
  82. package/lib/commands/data360/segment/members/export.d.ts +101 -0
  83. package/lib/commands/data360/segment/members/export.js +318 -0
  84. package/lib/errors/codes/calculated-insight.d.ts +2 -0
  85. package/lib/errors/codes/calculated-insight.js +25 -0
  86. package/lib/errors/codes/data-graph.d.ts +2 -0
  87. package/lib/errors/codes/data-graph.js +22 -0
  88. package/lib/errors/codes/data-stream.d.ts +2 -0
  89. package/lib/errors/codes/data-stream.js +70 -0
  90. package/lib/errors/codes/data-transform.d.ts +2 -0
  91. package/lib/errors/codes/data-transform.js +35 -0
  92. package/lib/errors/codes/dependency.d.ts +2 -0
  93. package/lib/errors/codes/dependency.js +11 -0
  94. package/lib/errors/codes/dlo.d.ts +2 -0
  95. package/lib/errors/codes/dlo.js +31 -0
  96. package/lib/errors/codes/dmo.d.ts +2 -0
  97. package/lib/errors/codes/dmo.js +28 -0
  98. package/lib/errors/codes/general.d.ts +2 -0
  99. package/lib/errors/codes/general.js +57 -0
  100. package/lib/errors/codes/identity-resolution.d.ts +2 -0
  101. package/lib/errors/codes/identity-resolution.js +24 -0
  102. package/lib/errors/codes/mapping.d.ts +2 -0
  103. package/lib/errors/codes/mapping.js +42 -0
  104. package/lib/errors/codes/relationship.d.ts +2 -0
  105. package/lib/errors/codes/relationship.js +21 -0
  106. package/lib/errors/codes/segment.d.ts +2 -0
  107. package/lib/errors/codes/segment.js +49 -0
  108. package/lib/errors/registry.d.ts +60 -0
  109. package/lib/errors/registry.js +123 -0
  110. package/lib/plugin.d.ts +5 -0
  111. package/lib/plugin.js +5 -0
  112. package/lib/services/acquire-retrieve-selection.d.ts +43 -0
  113. package/lib/services/acquire-retrieve-selection.js +145 -0
  114. package/lib/services/bind-identity-resolution-dependencies.d.ts +53 -0
  115. package/lib/services/bind-identity-resolution-dependencies.js +238 -0
  116. package/lib/services/bind-segment-dependencies.d.ts +49 -0
  117. package/lib/services/bind-segment-dependencies.js +115 -0
  118. package/lib/services/bounded-reads.d.ts +17 -0
  119. package/lib/services/bounded-reads.js +42 -0
  120. package/lib/services/calculated-insight-sql-checks.d.ts +33 -0
  121. package/lib/services/calculated-insight-sql-checks.js +193 -0
  122. package/lib/services/command-dlo.d.ts +48 -0
  123. package/lib/services/command-dlo.js +103 -0
  124. package/lib/services/command-native-data-graph.d.ts +54 -0
  125. package/lib/services/command-native-data-graph.js +485 -0
  126. package/lib/services/command-native-data-stream.d.ts +104 -0
  127. package/lib/services/command-native-data-stream.js +257 -0
  128. package/lib/services/command-native-dmo.d.ts +124 -0
  129. package/lib/services/command-native-dmo.js +338 -0
  130. package/lib/services/command-native-mapping.d.ts +65 -0
  131. package/lib/services/command-native-mapping.js +129 -0
  132. package/lib/services/command-native-relationship.d.ts +46 -0
  133. package/lib/services/command-native-relationship.js +103 -0
  134. package/lib/services/cross-dataspace-deploy.d.ts +111 -0
  135. package/lib/services/cross-dataspace-deploy.js +493 -0
  136. package/lib/services/cross-dataspace-rename.d.ts +74 -0
  137. package/lib/services/cross-dataspace-rename.js +1314 -0
  138. package/lib/services/cross-dataspace-sql.d.ts +109 -0
  139. package/lib/services/cross-dataspace-sql.js +1301 -0
  140. package/lib/services/cross-space-context.d.ts +23 -0
  141. package/lib/services/cross-space-context.js +36 -0
  142. package/lib/services/crud-plan.d.ts +137 -0
  143. package/lib/services/crud-plan.js +408 -0
  144. package/lib/services/data-space-check.d.ts +89 -0
  145. package/lib/services/data-space-check.js +162 -0
  146. package/lib/services/data-space-prefix.d.ts +77 -0
  147. package/lib/services/data-space-prefix.js +127 -0
  148. package/lib/services/data-transform-space.d.ts +31 -0
  149. package/lib/services/data-transform-space.js +79 -0
  150. package/lib/services/delete-calculated-insight.d.ts +44 -0
  151. package/lib/services/delete-calculated-insight.js +101 -0
  152. package/lib/services/delete-data-transform.d.ts +52 -0
  153. package/lib/services/delete-data-transform.js +222 -0
  154. package/lib/services/delete-dmo-field.d.ts +211 -0
  155. package/lib/services/delete-dmo-field.js +472 -0
  156. package/lib/services/delete-identity-resolution.d.ts +43 -0
  157. package/lib/services/delete-identity-resolution.js +295 -0
  158. package/lib/services/delete-segment.d.ts +42 -0
  159. package/lib/services/delete-segment.js +248 -0
  160. package/lib/services/deploy-result.d.ts +54 -0
  161. package/lib/services/deploy-result.js +60 -0
  162. package/lib/services/escaped-copy.d.ts +23 -0
  163. package/lib/services/escaped-copy.js +88 -0
  164. package/lib/services/execute-calculated-insight-deploy.d.ts +42 -0
  165. package/lib/services/execute-calculated-insight-deploy.js +96 -0
  166. package/lib/services/execute-data-transform-deploy.d.ts +56 -0
  167. package/lib/services/execute-data-transform-deploy.js +168 -0
  168. package/lib/services/execute-native-identity-resolution-deploy.d.ts +48 -0
  169. package/lib/services/execute-native-identity-resolution-deploy.js +360 -0
  170. package/lib/services/execute-segment-deploy.d.ts +45 -0
  171. package/lib/services/execute-segment-deploy.js +393 -0
  172. package/lib/services/local-file-removal.d.ts +36 -0
  173. package/lib/services/local-file-removal.js +103 -0
  174. package/lib/services/native-calculated-insight.d.ts +87 -0
  175. package/lib/services/native-calculated-insight.js +164 -0
  176. package/lib/services/native-crm-data-stream.d.ts +91 -0
  177. package/lib/services/native-crm-data-stream.js +272 -0
  178. package/lib/services/native-data-graph-context.d.ts +12 -0
  179. package/lib/services/native-data-graph-context.js +139 -0
  180. package/lib/services/native-data-graph-crd.d.ts +261 -0
  181. package/lib/services/native-data-graph-crd.js +866 -0
  182. package/lib/services/native-data-stream.d.ts +329 -0
  183. package/lib/services/native-data-stream.js +1830 -0
  184. package/lib/services/native-dlo.d.ts +163 -0
  185. package/lib/services/native-dlo.js +402 -0
  186. package/lib/services/native-dmo.d.ts +191 -0
  187. package/lib/services/native-dmo.js +614 -0
  188. package/lib/services/native-mapping.d.ts +255 -0
  189. package/lib/services/native-mapping.js +1226 -0
  190. package/lib/services/native-relationship.d.ts +132 -0
  191. package/lib/services/native-relationship.js +512 -0
  192. package/lib/services/operation-diagnostic.d.ts +83 -0
  193. package/lib/services/operation-diagnostic.js +272 -0
  194. package/lib/services/preflight-calculated-insight-deploy.d.ts +58 -0
  195. package/lib/services/preflight-calculated-insight-deploy.js +95 -0
  196. package/lib/services/preflight-data-transform-deploy.d.ts +33 -0
  197. package/lib/services/preflight-data-transform-deploy.js +104 -0
  198. package/lib/services/preflight-identity-resolution-deploy.d.ts +107 -0
  199. package/lib/services/preflight-identity-resolution-deploy.js +281 -0
  200. package/lib/services/preflight-segment-deploy.d.ts +31 -0
  201. package/lib/services/preflight-segment-deploy.js +109 -0
  202. package/lib/services/publish-calculated-insight.d.ts +40 -0
  203. package/lib/services/publish-calculated-insight.js +95 -0
  204. package/lib/services/publish-segment.d.ts +65 -0
  205. package/lib/services/publish-segment.js +194 -0
  206. package/lib/services/readback-pace.d.ts +16 -0
  207. package/lib/services/readback-pace.js +23 -0
  208. package/lib/services/retrieve-data-transform.d.ts +26 -0
  209. package/lib/services/retrieve-data-transform.js +65 -0
  210. package/lib/services/retrieve-identity-resolution.d.ts +40 -0
  211. package/lib/services/retrieve-identity-resolution.js +87 -0
  212. package/lib/services/retrieve-inventory.d.ts +32 -0
  213. package/lib/services/retrieve-inventory.js +151 -0
  214. package/lib/services/retrieve-segment.d.ts +120 -0
  215. package/lib/services/retrieve-segment.js +502 -0
  216. package/lib/services/segment-lookup.d.ts +46 -0
  217. package/lib/services/segment-lookup.js +108 -0
  218. package/lib/services/segment-members.d.ts +144 -0
  219. package/lib/services/segment-members.js +402 -0
  220. package/lib/services/select-native-retrieve.d.ts +60 -0
  221. package/lib/services/select-native-retrieve.js +226 -0
  222. package/lib/services/settle-calculated-insight.d.ts +22 -0
  223. package/lib/services/settle-calculated-insight.js +53 -0
  224. package/lib/services/sql-tokens.d.ts +58 -0
  225. package/lib/services/sql-tokens.js +147 -0
  226. package/lib/transport/api-version.d.ts +36 -0
  227. package/lib/transport/api-version.js +89 -0
  228. package/lib/transport/bound-ssot.d.ts +27 -0
  229. package/lib/transport/bound-ssot.js +116 -0
  230. package/lib/transport/calculated-insight-rest.d.ts +28 -0
  231. package/lib/transport/calculated-insight-rest.js +62 -0
  232. package/lib/transport/connect-client.d.ts +129 -0
  233. package/lib/transport/connect-client.js +446 -0
  234. package/lib/transport/connect-diagnostic.d.ts +89 -0
  235. package/lib/transport/connect-diagnostic.js +331 -0
  236. package/lib/transport/data-graph-rest.d.ts +27 -0
  237. package/lib/transport/data-graph-rest.js +76 -0
  238. package/lib/transport/data-stream-rest.d.ts +62 -0
  239. package/lib/transport/data-stream-rest.js +348 -0
  240. package/lib/transport/data-transform-rest.d.ts +25 -0
  241. package/lib/transport/data-transform-rest.js +100 -0
  242. package/lib/transport/dlo-native-rest.d.ts +22 -0
  243. package/lib/transport/dlo-native-rest.js +89 -0
  244. package/lib/transport/dlo-rest.d.ts +23 -0
  245. package/lib/transport/dlo-rest.js +135 -0
  246. package/lib/transport/dmo-native-rest.d.ts +18 -0
  247. package/lib/transport/dmo-native-rest.js +55 -0
  248. package/lib/transport/dmo-rest.d.ts +30 -0
  249. package/lib/transport/dmo-rest.js +163 -0
  250. package/lib/transport/mapping-rest.d.ts +63 -0
  251. package/lib/transport/mapping-rest.js +341 -0
  252. package/lib/transport/project-metadata.d.ts +36 -0
  253. package/lib/transport/project-metadata.js +118 -0
  254. package/lib/transport/relationship-rest.d.ts +43 -0
  255. package/lib/transport/relationship-rest.js +201 -0
  256. package/package.json +96 -4
@@ -0,0 +1,164 @@
1
+ import { CalculatedInsightError, calculatedInsightName, observeNativeCalculatedInsight, } from '../artifacts/native-calculated-insight-document.js';
2
+ import { createCalculatedInsightClient } from '../transport/calculated-insight-rest.js';
3
+ import { ConnectClient, ConnectError } from '../transport/connect-client.js';
4
+ import { getConnectDiagnostic, retainConnectDiagnostic } from '../transport/connect-diagnostic.js';
5
+ import { explainInput, printableKey } from './operation-diagnostic.js';
6
+ /**
7
+ * Read context on one API 68 connection. `check` refuses a connection whose version or
8
+ * instance changed since, so every read and write of one item goes to the same org.
9
+ * @param {CalculatedInsightConnection} connection API 68 connection.
10
+ * @param {string} dataSpace Selected data space ('default').
11
+ * @returns {CalculatedInsightReadContext} Read context.
12
+ */
13
+ export function calculatedInsightReadContext(connection, dataSpace) {
14
+ const origin = connection.instanceUrl;
15
+ const check = () => {
16
+ if (connection.getApiVersion() !== '68.0' || connection.instanceUrl !== origin)
17
+ throw new CalculatedInsightError('INVALID_CI_SCOPE');
18
+ };
19
+ check();
20
+ const client = createCalculatedInsightClient(connection, '68.0', dataSpace);
21
+ return { dataSpace, check, get: async (apiName) => client.get(apiName) };
22
+ }
23
+ /**
24
+ * Refuse an insight whose GET names another name or data space than selected, naming both.
25
+ * @param {Record<string, unknown>} answered The GET.
26
+ * @param {string} apiName Selected name.
27
+ * @param {string} dataSpace The run's data space.
28
+ * @returns {CalculatedInsightError | undefined} CI_IDENTITY_CHANGED, or none when both match.
29
+ */
30
+ function identityChanged(answered, apiName, dataSpace) {
31
+ if (answered.apiName !== apiName)
32
+ return explainInput(new CalculatedInsightError('CI_IDENTITY_CHANGED'), `The read of ${apiName} answered another insight (${typeof answered.apiName === 'string' ? printableKey(answered.apiName) : 'no name'}).`);
33
+ if (answered.dataSpace !== dataSpace) {
34
+ const space = typeof answered.dataSpace === 'string' ? printableKey(answered.dataSpace) : undefined;
35
+ return explainInput(new CalculatedInsightError('CI_IDENTITY_CHANGED'), space === undefined
36
+ ? `${apiName} names no data space, not the run's data space ${printableKey(dataSpace)}.`
37
+ : `${apiName} is in the data space ${space}, not in the run's data space ${printableKey(dataSpace)}. Run the command with --dataspace ${space}.`);
38
+ }
39
+ return undefined;
40
+ }
41
+ /**
42
+ * Read one insight by name: HTTP 404 ITEM_NOT_FOUND or ENTITY_IS_DELETED is absent, any other failure throws
43
+ * (CI_RUNTIME_UNKNOWN with the Salesforce diagnostic). A present insight must answer with the
44
+ * selected name and data space (CI_IDENTITY_CHANGED otherwise): every write and the DELETE go
45
+ * by name, so they must address exactly the insight that was read.
46
+ * @param {Pick<CalculatedInsightReadContext, 'dataSpace' | 'get'>} context Read context.
47
+ * @param {string} apiName Exact name ending in __cio.
48
+ * @returns {Promise<CalculatedInsightAcquisition>} Absent, or the status and file form.
49
+ */
50
+ export async function acquireCalculatedInsight(context, apiName) {
51
+ const read = await readCalculatedInsight(context, apiName);
52
+ if (read.state === 'absent')
53
+ return read;
54
+ const { raw } = read;
55
+ // Name and data space first, from the GET itself: an insight of another name or data space is
56
+ // an item failure, never a projection (or a verbatim file stamped with the run's data space).
57
+ const answered = (raw && typeof raw === 'object' ? raw : {});
58
+ const changed = identityChanged(answered, apiName, context.dataSpace);
59
+ if (changed)
60
+ throw changed;
61
+ const document = observeNativeCalculatedInsight(raw);
62
+ const status = raw.calculatedInsightStatus;
63
+ return { state: 'present', status: typeof status === 'string' ? status : 'UNKNOWN', document };
64
+ }
65
+ /**
66
+ * The raw GET of one insight by name: HTTP 404 ITEM_NOT_FOUND or ENTITY_IS_DELETED is absent, any other failure
67
+ * throws CI_RUNTIME_UNKNOWN with the Salesforce diagnostic.
68
+ * @param {Pick<CalculatedInsightReadContext, 'get'>} context Read context.
69
+ * @param {string} apiName Exact name ending in __cio.
70
+ * @returns {Promise<{state: 'absent'} | {state: 'present'; raw: unknown}>} Absent, or the GET.
71
+ */
72
+ export async function readCalculatedInsight(context, apiName) {
73
+ if (!calculatedInsightName.test(apiName))
74
+ throw new CalculatedInsightError('INVALID_SELECTION');
75
+ try {
76
+ return { state: 'present', raw: await context.get(apiName) };
77
+ }
78
+ catch (error) {
79
+ // A deleting insight may answer 404 ENTITY_IS_DELETED instead (live API 68).
80
+ if (error instanceof ConnectError && error.statusCode === 404 && error.nativeCode !== undefined)
81
+ return { state: 'absent' };
82
+ throw retainConnectDiagnostic(new CalculatedInsightError('CI_RUNTIME_UNKNOWN'), getConnectDiagnostic(error));
83
+ }
84
+ }
85
+ /**
86
+ * Whether one insight exists, for a delete: like {@link acquireCalculatedInsight}, but only the
87
+ * name and data space of the GET are checked (CI_IDENTITY_CHANGED otherwise), so an insight the
88
+ * plugin cannot represent (no SQL, a Visual Builder draft) can still be deleted by name.
89
+ * @param {Pick<CalculatedInsightReadContext, 'dataSpace' | 'get'>} context Read context.
90
+ * @param {string} apiName Exact name ending in __cio.
91
+ * @returns {Promise<{state: 'absent'} | {state: 'present'; status: string}>} Absent, or its status.
92
+ */
93
+ export async function calculatedInsightPresence(context, apiName) {
94
+ const read = await readCalculatedInsight(context, apiName);
95
+ if (read.state === 'absent')
96
+ return read;
97
+ const raw = (read.raw && typeof read.raw === 'object' ? read.raw : {});
98
+ const changed = identityChanged(raw, apiName, context.dataSpace);
99
+ if (changed)
100
+ throw changed;
101
+ const status = raw.calculatedInsightStatus;
102
+ return { state: 'present', status: typeof status === 'string' ? status : 'UNKNOWN' };
103
+ }
104
+ /** What the API 68 GET returns as `expression` for an unpublished Visual Builder draft. */
105
+ export const visualDraftPlaceholder = 'SQL expression will be available when you publish the insight';
106
+ /**
107
+ * Read one calculated insight by name with the API 68 GET and project it to the file form.
108
+ * Read only; any status, schedule or SQL is projected, except a Visual Builder draft, whose
109
+ * GET carries a placeholder instead of SQL (`CI_VISUAL_DRAFT_WITHOUT_SQL`), and an insight
110
+ * without SQL (`INVALID_CI_DOCUMENT`). The retrieve command writes those two as returned
111
+ * (verbatim, with the code as reason), and deploy refuses such a file.
112
+ * @param {Pick<CalculatedInsightClient, 'get'>} client API 68 default-space client.
113
+ * @param {string} apiName Exact name ending in __cio.
114
+ * @param {string} dataSpace Selected data space.
115
+ * @returns {Promise<NativeCalculatedInsight>} Document with the SQL as plain text.
116
+ */
117
+ export async function retrieveNativeCalculatedInsight(client, apiName, dataSpace) {
118
+ const acquired = await acquireCalculatedInsight({ dataSpace, get: async (name) => client.get(name) }, apiName);
119
+ if (acquired.state === 'absent')
120
+ throw new CalculatedInsightError('CI_NOT_FOUND');
121
+ // A Visual Builder draft reads this sentence instead of SQL until it is published; the
122
+ // retrieve command writes it verbatim with this code as the reason.
123
+ if (acquired.document.expression.trim() === visualDraftPlaceholder)
124
+ throw new CalculatedInsightError('CI_VISUAL_DRAFT_WITHOUT_SQL');
125
+ return acquired.document;
126
+ }
127
+ /**
128
+ * Every calculated insight the API 68 list returns in the data space, following its offset
129
+ * paging until `total` or an empty page. A malformed page stops the listing; an entry without a
130
+ * usable API name is skipped with a warning and a repeated name is listed once. A page that adds
131
+ * no new name ends the listing (a server ignoring the offset).
132
+ * @param {Pick<Connection, 'instanceUrl' | 'request'>} connection Authenticated connection.
133
+ * @param {string} dataSpace Selected data space.
134
+ * @param {string[]} [warnings] Receives one warning per skipped entry.
135
+ * @returns {Promise<string[]>} Sorted API names.
136
+ */
137
+ export async function listCalculatedInsightNames(connection, dataSpace, warnings) {
138
+ const client = new ConnectClient(connection, '68.0');
139
+ const names = new Set();
140
+ let offset = 0;
141
+ for (;;) {
142
+ const response = await client.get(`/services/data/v68.0/ssot/calculated-insights?dataspace=${encodeURIComponent(dataSpace)}&batchSize=25&offset=${offset}`);
143
+ const collection = response?.collection;
144
+ if (!collection ||
145
+ typeof collection !== 'object' ||
146
+ !Array.isArray(collection.items) ||
147
+ typeof collection.total !== 'number' ||
148
+ !Number.isSafeInteger(collection.total))
149
+ throw new CalculatedInsightError('CI_ACQUISITION_FAILED');
150
+ const before = names.size;
151
+ for (const [index, item] of collection.items.entries()) {
152
+ const apiName = item?.apiName;
153
+ if (typeof apiName !== 'string' || !calculatedInsightName.test(apiName)) {
154
+ warnings?.push(`calculated-insight inventory: skipped ${typeof apiName === 'string' ? `"${apiName}"` : `the entry at position ${offset + index}`} because it has no usable API name.`);
155
+ continue;
156
+ }
157
+ names.add(apiName);
158
+ }
159
+ offset += collection.items.length;
160
+ if (collection.items.length === 0 || offset >= collection.total || names.size === before)
161
+ // eslint-disable-next-line unicorn/no-array-sort -- Sorting a new array.
162
+ return [...names].sort();
163
+ }
164
+ }
@@ -0,0 +1,91 @@
1
+ import type { CrmDataStreamDocument } from '../artifacts/data-stream-document.js';
2
+ import type { DocumentMode } from '../artifacts/verbatim-document.js';
3
+ import type { DataStreamClient } from '../transport/data-stream-rest.js';
4
+ import type { DataStreamDocument } from '../artifacts/data-stream-document.js';
5
+ import type { DataStreamReadClients, DataStreamReadMode } from './native-data-stream.js';
6
+ /** Native stream access a CRM stream deploy needs. */
7
+ export type CrmDataStreamClients = {
8
+ streams: Pick<DataStreamClient, 'get' | 'create' | 'patch' | 'relabel'>;
9
+ /** DLO reader: the stream's data spaces come from its DLO when the stream GET names none. */
10
+ dlo?: DataStreamReadClients['dlo'];
11
+ };
12
+ export type CrmDataStreamDeployOptions = {
13
+ allowProcessing: boolean;
14
+ /** The run's data space (default when absent): the file's c__dataSpace must list it. */
15
+ dataSpace?: string;
16
+ verifyInput: () => Promise<void>;
17
+ now?: () => number;
18
+ wait?: (milliseconds: number) => Promise<unknown>;
19
+ };
20
+ /**
21
+ * Project a CRM stream GET into its stream file: c__dataSpace lists the data spaces of its DLO
22
+ * (see streamSpaces), and the spec keeps Salesforce's own list of them.
23
+ * @param {unknown} read Stream GET response.
24
+ * @param {string} apiName Requested stream name.
25
+ * @param {DocumentMode} mode Validation mode.
26
+ * @param {readonly string[]} spaces The data spaces of the stream's DLO.
27
+ * @returns {CrmDataStreamDocument} Stream document.
28
+ */
29
+ export declare function projectCrmDataStream(read: unknown, apiName: string, mode: DocumentMode, spaces: readonly string[]): CrmDataStreamDocument;
30
+ /**
31
+ * Retrieve one stream as its file: a CRM stream (retrieve mode) as its GET without server-owned
32
+ * keys, every other stream through the Ingestion API projection.
33
+ * @param {string} apiName Exact stream name.
34
+ * @param {DataStreamReadClients} clients Native readers.
35
+ * @param {string} dataSpace The run's data space: the stream's DLO must belong to it.
36
+ * @param {boolean} postWrite Whether this read belongs to create/update settlement.
37
+ * @param {DataStreamReadMode} mode `retrieve` or `deploy`.
38
+ * @returns {Promise<DataStreamDocument | CrmDataStreamDocument>} Stream document.
39
+ */
40
+ export declare function retrieveDataStreamDocument(apiName: string, clients: DataStreamReadClients, dataSpace?: string, postWrite?: boolean, mode?: DataStreamReadMode): Promise<DataStreamDocument | CrmDataStreamDocument>;
41
+ /**
42
+ * Read-only comparison of a CRM stream file with the org.
43
+ * @param {unknown} value CRM stream document.
44
+ * @param {CrmDataStreamClients} clients Native reader.
45
+ * @param {string} [dataSpace] The run's data space.
46
+ * @returns {Promise<object>} Preview item.
47
+ */
48
+ export declare function preflightCrmDataStream(value: unknown, clients: CrmDataStreamClients, dataSpace?: string): Promise<{
49
+ diagnostics: never[];
50
+ changes?: string[] | undefined;
51
+ family: "data-stream";
52
+ outcome: "create" | "update" | "unchanged";
53
+ target: string;
54
+ absenceProof: string;
55
+ requiredEffects: string[];
56
+ }>;
57
+ /**
58
+ * Create or update a CRM stream from its file and read it back until it is ACTIVE. The file is
59
+ * translated into the create input or a PATCH of the changed parts and sent; Salesforce decides,
60
+ * and its answer is reported. Create and update need --allow-processing.
61
+ * @param {unknown} value CRM stream document.
62
+ * @param {CrmDataStreamClients} clients Native clients.
63
+ * @param {CrmDataStreamDeployOptions} options Processing acknowledgement, input guard, read bounds.
64
+ * @returns {Promise<object>} Result item.
65
+ */
66
+ export declare function executeCrmDataStream(value: unknown, clients: CrmDataStreamClients, options: CrmDataStreamDeployOptions): Promise<{
67
+ family: "data-stream";
68
+ outcome: "blocked" | "failed";
69
+ mutation: "unknown" | "not-attempted" | "rejected";
70
+ diagnostics: {
71
+ code: string;
72
+ message: string;
73
+ }[];
74
+ } | {
75
+ family: "data-stream";
76
+ outcome: "unchanged";
77
+ mutation: "not-attempted";
78
+ targetApiName: string;
79
+ targetDloName: string;
80
+ diagnostics: never[];
81
+ } | {
82
+ family: "data-stream";
83
+ outcome: "updated" | "created";
84
+ mutation: "confirmed-created" | "confirmed-updated";
85
+ targetApiName: string;
86
+ targetDloName: string;
87
+ diagnostics: {
88
+ code: string;
89
+ message: string;
90
+ }[];
91
+ }>;
@@ -0,0 +1,272 @@
1
+ import { setTimeout as wait } from 'node:timers/promises';
2
+ import { refuseEscapedCopy } from './escaped-copy.js';
3
+ import { DataStreamError, createSpacesRefusal, crmDataStreamPatch, crmDataStreamSpec, crmDeploySpec, isCrmDataStream, validateCrmDataStreamDocument, } from '../artifacts/data-stream-document.js';
4
+ import { dataSpaceKey, sortedDataSpaces } from '../artifacts/data-space-field.js';
5
+ import { WriteLedger, rejectedNextAction } from './deploy-result.js';
6
+ import { explainInput, operationDiagnostic } from './operation-diagnostic.js';
7
+ import { sameEnum } from '../artifacts/enum-case.js';
8
+ import { checkRetrievedStreamScope, isStreamNotFound, projectNativeDataStream, requireRunSpace, retainStreamFailure, sameSpaces, spacesDiffer, streamSpaces, } from './native-data-stream.js';
9
+ /**
10
+ * Project a CRM stream GET into its stream file: c__dataSpace lists the data spaces of its DLO
11
+ * (see streamSpaces), and the spec keeps Salesforce's own list of them.
12
+ * @param {unknown} read Stream GET response.
13
+ * @param {string} apiName Requested stream name.
14
+ * @param {DocumentMode} mode Validation mode.
15
+ * @param {readonly string[]} spaces The data spaces of the stream's DLO.
16
+ * @returns {CrmDataStreamDocument} Stream document.
17
+ */
18
+ export function projectCrmDataStream(read, apiName, mode, spaces) {
19
+ return validateCrmDataStreamDocument({
20
+ schemaVersion: 1,
21
+ kind: 'data-stream',
22
+ [dataSpaceKey]: sortedDataSpaces(spaces),
23
+ identity: { apiName },
24
+ spec: crmDataStreamSpec(read),
25
+ }, mode);
26
+ }
27
+ /**
28
+ * Retrieve one stream as its file: a CRM stream (retrieve mode) as its GET without server-owned
29
+ * keys, every other stream through the Ingestion API projection.
30
+ * @param {string} apiName Exact stream name.
31
+ * @param {DataStreamReadClients} clients Native readers.
32
+ * @param {string} dataSpace The run's data space: the stream's DLO must belong to it.
33
+ * @param {boolean} postWrite Whether this read belongs to create/update settlement.
34
+ * @param {DataStreamReadMode} mode `retrieve` or `deploy`.
35
+ * @returns {Promise<DataStreamDocument | CrmDataStreamDocument>} Stream document.
36
+ */
37
+ export async function retrieveDataStreamDocument(apiName, clients, dataSpace = 'default', postWrite = false, mode = 'deploy') {
38
+ if (!/^[A-Za-z][A-Za-z0-9_]{0,199}$/u.test(apiName))
39
+ throw new DataStreamError('INVALID_STREAM_BINDING');
40
+ const read = await clients.streams.get(apiName);
41
+ if (mode === 'retrieve' && isCrmDataStream(read)) {
42
+ checkRetrievedStreamScope(read, apiName, dataSpace);
43
+ const spaces = await streamSpaces(read, apiName, dataSpace, clients);
44
+ return projectCrmDataStream(read, apiName, mode, spaces);
45
+ }
46
+ return projectNativeDataStream(read, apiName, clients, dataSpace, postWrite, mode);
47
+ }
48
+ /**
49
+ * The spec of an org stream for comparison: a GET that names no data space gets the spaces read
50
+ * from its DLO, so a file that keeps Salesforce's list is not taken as a change of it.
51
+ * @param {Json} current Stream GET.
52
+ * @param {readonly string[]} spaces The DLO's data spaces.
53
+ * @returns {Json} Spec.
54
+ */
55
+ function orgSpec(current, spaces) {
56
+ const spec = crmDataStreamSpec(current);
57
+ const dlo = spec.dataLakeObjectInfo;
58
+ if (dlo && typeof dlo === 'object' && dlo.dataSpaceInfo === undefined)
59
+ dlo.dataSpaceInfo = spaces.map((name) => ({ name }));
60
+ return spec;
61
+ }
62
+ /**
63
+ * Read the stream of the file's name; an HTTP 404 means absent.
64
+ * @param {string} apiName Stream name.
65
+ * @param {unknown} connectorType Connector type of the file.
66
+ * @param {CrmDataStreamClients} clients Native reader.
67
+ * @returns {Promise<Json | undefined>} Stream GET, or undefined when absent.
68
+ */
69
+ async function readCrmStream(apiName, connectorType, clients) {
70
+ let read;
71
+ try {
72
+ read = await clients.streams.get(apiName);
73
+ }
74
+ catch (error) {
75
+ if (isStreamNotFound(error))
76
+ return undefined;
77
+ throw error;
78
+ }
79
+ if (!read || typeof read !== 'object' || Array.isArray(read))
80
+ throw new DataStreamError('INVALID_STREAM_READ');
81
+ const stream = read;
82
+ // A stream of that name of another connector is another object; never overwrite it.
83
+ if (stream.name !== apiName ||
84
+ !isCrmDataStream(stream) ||
85
+ !sameEnum(stream.connectorInfo.connectorType, connectorType))
86
+ throw new DataStreamError('STREAM_TARGET_MISMATCH');
87
+ return stream;
88
+ }
89
+ /**
90
+ * The connector type a GET-shaped stream file names.
91
+ * @param {CrmDataStreamDocument} document Validated stream file.
92
+ * @returns {unknown} `spec.connectorInfo.connectorType`.
93
+ */
94
+ function connectorTypeOf(document) {
95
+ return document.spec.connectorInfo.connectorType;
96
+ }
97
+ /**
98
+ * Compare the file with the org: absent creates, equal is unchanged, otherwise a PATCH of the
99
+ * changed parts. The file's c__dataSpace must name the run's data space; a new stream must list
100
+ * it alone, an existing one must list exactly the data spaces of its DLO (check only).
101
+ * @param {CrmDataStreamDocument} document Validated CRM stream file.
102
+ * @param {CrmDataStreamClients} clients Native reader.
103
+ * @param {string} dataSpace The run's data space.
104
+ * @returns {Promise<CrmPlan>} Plan.
105
+ */
106
+ async function planCrmDataStream(document, clients, dataSpace) {
107
+ requireRunSpace(document, dataSpace);
108
+ const current = await readCrmStream(document.identity.apiName, connectorTypeOf(document), clients);
109
+ if (!current) {
110
+ if (document.c__dataSpace.length !== 1)
111
+ throw createSpacesRefusal(document.c__dataSpace);
112
+ return { operation: 'create' };
113
+ }
114
+ const org = await streamSpaces(current, document.identity.apiName, dataSpace, clients);
115
+ if (!sameSpaces(document.c__dataSpace, org))
116
+ throw spacesDiffer(document.identity.apiName, document.c__dataSpace, org);
117
+ refuseEscapedCopy(crmDeploySpec(document), orgSpec(current, org), (code) => new DataStreamError(code));
118
+ const { body, parts, unsupported } = crmDataStreamPatch(crmDeploySpec(document), orgSpec(current, org));
119
+ if (unsupported.length > 0)
120
+ throw explainInput(new DataStreamError('UNSUPPORTED_STREAM_SETTINGS_CHANGE'), `The file differs from the stream in ${unsupported.join(', ')}, which no known stream update can change. Use the values from a freshly retrieved file, or change them in Salesforce.`);
121
+ return { operation: parts.length > 0 ? 'update' : 'noop', current, body, parts };
122
+ }
123
+ /**
124
+ * Read-only comparison of a CRM stream file with the org.
125
+ * @param {unknown} value CRM stream document.
126
+ * @param {CrmDataStreamClients} clients Native reader.
127
+ * @param {string} [dataSpace] The run's data space.
128
+ * @returns {Promise<object>} Preview item.
129
+ */
130
+ export async function preflightCrmDataStream(value, clients, dataSpace = 'default') {
131
+ const document = validateCrmDataStreamDocument(value);
132
+ const plan = await planCrmDataStream(document, clients, dataSpace);
133
+ return {
134
+ family: 'data-stream',
135
+ outcome: plan.operation === 'noop' ? 'unchanged' : plan.operation,
136
+ target: plan.operation === 'create' ? 'absent' : 'existing',
137
+ absenceProof: plan.operation === 'create' ? 'stream-404' : 'none',
138
+ requiredEffects: plan.operation === 'noop' ? [] : ['processing'],
139
+ ...(plan.operation === 'update' ? { changes: plan.parts } : {}),
140
+ diagnostics: [],
141
+ };
142
+ }
143
+ /**
144
+ * The parts Salesforce did not apply, as one notice. A label still differing gets its own
145
+ * sentence (Salesforce answers a label PATCH with 200 and keeps the old label).
146
+ * @param {string[]} parts Key paths that still differ after the write settled.
147
+ * @returns {Array<{code: string; message: string}>} Notices.
148
+ */
149
+ function notApplied(parts) {
150
+ if (parts.length === 0)
151
+ return [];
152
+ const others = parts.filter((part) => part !== 'label');
153
+ const sentences = [
154
+ ...(parts.includes('label')
155
+ ? ['Salesforce accepted the label change but kept the old label; change it in Salesforce.']
156
+ : []),
157
+ ...(others.length > 0
158
+ ? [
159
+ `Salesforce accepted the write but the stream still differs from the file in ${others.join(', ')}.`,
160
+ ]
161
+ : []),
162
+ ];
163
+ return [
164
+ {
165
+ code: 'STREAM_CHANGE_NOT_APPLIED',
166
+ message: `${sentences.join(' ')} Retrieve the stream to see what it holds.`,
167
+ },
168
+ ];
169
+ }
170
+ /**
171
+ * Create or update a CRM stream from its file and read it back until it is ACTIVE. The file is
172
+ * translated into the create input or a PATCH of the changed parts and sent; Salesforce decides,
173
+ * and its answer is reported. Create and update need --allow-processing.
174
+ * @param {unknown} value CRM stream document.
175
+ * @param {CrmDataStreamClients} clients Native clients.
176
+ * @param {CrmDataStreamDeployOptions} options Processing acknowledgement, input guard, read bounds.
177
+ * @returns {Promise<object>} Result item.
178
+ */
179
+ export async function executeCrmDataStream(value, clients, options) {
180
+ const ledger = new WriteLedger();
181
+ try {
182
+ const document = validateCrmDataStreamDocument(value);
183
+ const apiName = document.identity.apiName;
184
+ const targetDloName = document.spec.dataLakeObjectInfo.name;
185
+ await options.verifyInput();
186
+ const plan = await planCrmDataStream(document, clients, options.dataSpace ?? 'default');
187
+ if (plan.operation === 'noop')
188
+ return {
189
+ family: 'data-stream',
190
+ outcome: 'unchanged',
191
+ mutation: 'not-attempted',
192
+ targetApiName: apiName,
193
+ targetDloName,
194
+ diagnostics: [],
195
+ };
196
+ if (options.allowProcessing !== true)
197
+ throw new DataStreamError('PROCESSING_ACKNOWLEDGEMENT_REQUIRED');
198
+ await options.verifyInput();
199
+ if (plan.operation === 'create')
200
+ await ledger.run(async () => clients.streams.create(document));
201
+ else {
202
+ // Salesforce ignores a label in a stream PATCH; the Metadata API update of the stream
203
+ // definition changes it (live, API 68). It is sent first, while the stream is ACTIVE.
204
+ const relabel = plan.parts.includes('label') ? clients.streams.relabel : undefined;
205
+ // Only the update is a write: a failed read before it changed nothing.
206
+ if (relabel)
207
+ await relabel(apiName, String(document.spec.label), async (write) => ledger.run(write));
208
+ const parts = relabel ? plan.parts.filter((part) => part !== 'label') : plan.parts;
209
+ const body = { ...plan.body };
210
+ if (relabel)
211
+ delete body.label;
212
+ if (parts.length > 0)
213
+ await ledger.run(async () => clients.streams.patch(String(plan.current.recordId), body));
214
+ }
215
+ const now = options.now ?? Date.now;
216
+ const pause = options.wait ?? wait;
217
+ const deadline = now() + 15 * 60 * 1000;
218
+ let remaining;
219
+ let settledReads = 0;
220
+ for (let attempt = 0; attempt < 20 && now() < deadline; attempt++) {
221
+ const current = await readCrmStream(apiName, connectorTypeOf(document), clients);
222
+ if (current && !['ACTIVE', 'PROCESSING'].includes(current.status))
223
+ throw new DataStreamError(plan.operation === 'create'
224
+ ? 'STREAM_CREATE_SETTLEMENT_UNCONFIRMED'
225
+ : 'STREAM_UPDATE_SETTLEMENT_UNCONFIRMED');
226
+ if (current?.status === 'ACTIVE') {
227
+ const left = crmDataStreamPatch(crmDeploySpec(document), orgSpec(current, document.c__dataSpace));
228
+ // Plain mappings are never sent: Salesforce maps source fields itself on create.
229
+ remaining = [...left.parts, ...left.unsupported];
230
+ settledReads++;
231
+ // Done when the stream holds the file; a part Salesforce ignores stays after 3 reads.
232
+ if (remaining.length === 0 || settledReads >= 3)
233
+ break;
234
+ }
235
+ if (attempt < 19 && now() < deadline)
236
+ await pause(Math.min(5000, deadline - now()));
237
+ }
238
+ if (remaining === undefined)
239
+ throw new DataStreamError(plan.operation === 'create'
240
+ ? 'STREAM_CREATE_SETTLEMENT_UNCONFIRMED'
241
+ : 'STREAM_UPDATE_SETTLEMENT_UNCONFIRMED');
242
+ await options.verifyInput();
243
+ return {
244
+ family: 'data-stream',
245
+ outcome: plan.operation === 'create' ? 'created' : 'updated',
246
+ mutation: plan.operation === 'create'
247
+ ? 'confirmed-created'
248
+ : 'confirmed-updated',
249
+ targetApiName: apiName,
250
+ targetDloName,
251
+ diagnostics: notApplied(remaining),
252
+ };
253
+ }
254
+ catch (error) {
255
+ const { outcome, mutation } = ledger.failure(error);
256
+ return retainStreamFailure({
257
+ family: 'data-stream',
258
+ outcome,
259
+ mutation,
260
+ diagnostics: [
261
+ {
262
+ code: error instanceof DataStreamError ? error.code : 'DATA_STREAM_REQUEST_UNCONFIRMED',
263
+ message: operationDiagnostic(error, mutation === 'rejected'
264
+ ? rejectedNextAction
265
+ : mutation === 'unknown'
266
+ ? 'Write outcome is unknown; inspect target before another attempt. No automatic retry.'
267
+ : 'No write attempted; correct the reported check before another attempt.'),
268
+ },
269
+ ],
270
+ }, error);
271
+ }
272
+ }
@@ -0,0 +1,12 @@
1
+ import type { Connection } from '@salesforce/core';
2
+ import type { NativeGraphContext } from './native-data-graph-crd.js';
3
+ /**
4
+ * Bind concrete read-only native68 routes to an independently verified authenticated org.
5
+ * Raw wire status/body is retained privately because core otherwise discards Graph error envelopes.
6
+ * No caller-supplied URL, SOQL, org identity, auth refresh or pagination replay is accepted.
7
+ * @param {Connection} connection Existing API68 core connection.
8
+ * @param {string} dataSpace The run's data space: the metadata, inventory, root DMO and DataSpace
9
+ * reads name it (graph item reads are by name, unique in the org).
10
+ * @returns {Promise<NativeGraphContext>} Scope-checked concrete readers, never write authority.
11
+ */
12
+ export declare function nativeDataGraphReadContext(connection: Connection, dataSpace: string): Promise<NativeGraphContext>;