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.
- package/LICENSE +21 -0
- package/README.md +589 -2
- package/bin/run.js +4 -0
- package/docs/artifact-format.md +527 -0
- package/docs/capabilities.md +1208 -0
- package/docs/git-workflow.md +160 -0
- package/docs/handover.md +10446 -0
- package/lib/api.d.ts +7 -0
- package/lib/api.js +7 -0
- package/lib/artifacts/code-fields.d.ts +49 -0
- package/lib/artifacts/code-fields.js +142 -0
- package/lib/artifacts/data-graph-document.d.ts +29 -0
- package/lib/artifacts/data-graph-document.js +52 -0
- package/lib/artifacts/data-space-field.d.ts +65 -0
- package/lib/artifacts/data-space-field.js +98 -0
- package/lib/artifacts/data-stream-document.d.ts +264 -0
- package/lib/artifacts/data-stream-document.js +680 -0
- package/lib/artifacts/data-transform-document.d.ts +152 -0
- package/lib/artifacts/data-transform-document.js +560 -0
- package/lib/artifacts/dependency-selection.d.ts +51 -0
- package/lib/artifacts/dependency-selection.js +154 -0
- package/lib/artifacts/dlo-document.d.ts +93 -0
- package/lib/artifacts/dlo-document.js +253 -0
- package/lib/artifacts/enum-case.d.ts +39 -0
- package/lib/artifacts/enum-case.js +67 -0
- package/lib/artifacts/git-deploy-selection.d.ts +68 -0
- package/lib/artifacts/git-deploy-selection.js +474 -0
- package/lib/artifacts/identity-resolution-document.d.ts +133 -0
- package/lib/artifacts/identity-resolution-document.js +625 -0
- package/lib/artifacts/mapping-document.d.ts +1 -0
- package/lib/artifacts/mapping-document.js +1 -0
- package/lib/artifacts/native-calculated-insight-document.d.ts +142 -0
- package/lib/artifacts/native-calculated-insight-document.js +321 -0
- package/lib/artifacts/native-data-graph-document.d.ts +124 -0
- package/lib/artifacts/native-data-graph-document.js +780 -0
- package/lib/artifacts/native-dmo-document.d.ts +151 -0
- package/lib/artifacts/native-dmo-document.js +313 -0
- package/lib/artifacts/native-mapping-document.d.ts +67 -0
- package/lib/artifacts/native-mapping-document.js +187 -0
- package/lib/artifacts/native-relationship-document.d.ts +76 -0
- package/lib/artifacts/native-relationship-document.js +232 -0
- package/lib/artifacts/path-selector.d.ts +55 -0
- package/lib/artifacts/path-selector.js +167 -0
- package/lib/artifacts/read-deploy-input.d.ts +239 -0
- package/lib/artifacts/read-deploy-input.js +1359 -0
- package/lib/artifacts/segment-document.d.ts +183 -0
- package/lib/artifacts/segment-document.js +570 -0
- package/lib/artifacts/verbatim-document.d.ts +203 -0
- package/lib/artifacts/verbatim-document.js +339 -0
- package/lib/artifacts/write-data-graph.d.ts +17 -0
- package/lib/artifacts/write-data-graph.js +162 -0
- package/lib/artifacts/write-data-stream.d.ts +11 -0
- package/lib/artifacts/write-data-stream.js +175 -0
- package/lib/artifacts/write-data-transform.d.ts +11 -0
- package/lib/artifacts/write-data-transform.js +180 -0
- package/lib/artifacts/write-dlo.d.ts +11 -0
- package/lib/artifacts/write-dlo.js +158 -0
- package/lib/artifacts/write-identity-resolution.d.ts +18 -0
- package/lib/artifacts/write-identity-resolution.js +165 -0
- package/lib/artifacts/write-mapping-definition.d.ts +17 -0
- package/lib/artifacts/write-mapping-definition.js +127 -0
- package/lib/artifacts/write-member-export.d.ts +88 -0
- package/lib/artifacts/write-member-export.js +199 -0
- package/lib/artifacts/write-native-calculated-insight.d.ts +21 -0
- package/lib/artifacts/write-native-calculated-insight.js +176 -0
- package/lib/artifacts/write-native-dmo.d.ts +11 -0
- package/lib/artifacts/write-native-dmo.js +158 -0
- package/lib/artifacts/write-native-mapping.d.ts +11 -0
- package/lib/artifacts/write-native-mapping.js +157 -0
- package/lib/artifacts/write-native-relationship.d.ts +11 -0
- package/lib/artifacts/write-native-relationship.js +157 -0
- package/lib/artifacts/write-segment.d.ts +26 -0
- package/lib/artifacts/write-segment.js +212 -0
- package/lib/commands/data360/delete.d.ts +280 -0
- package/lib/commands/data360/delete.js +2297 -0
- package/lib/commands/data360/deploy.d.ts +303 -0
- package/lib/commands/data360/deploy.js +2400 -0
- package/lib/commands/data360/publish.d.ts +93 -0
- package/lib/commands/data360/publish.js +292 -0
- package/lib/commands/data360/retrieve.d.ts +322 -0
- package/lib/commands/data360/retrieve.js +1963 -0
- package/lib/commands/data360/segment/members/export.d.ts +101 -0
- package/lib/commands/data360/segment/members/export.js +318 -0
- package/lib/errors/codes/calculated-insight.d.ts +2 -0
- package/lib/errors/codes/calculated-insight.js +25 -0
- package/lib/errors/codes/data-graph.d.ts +2 -0
- package/lib/errors/codes/data-graph.js +22 -0
- package/lib/errors/codes/data-stream.d.ts +2 -0
- package/lib/errors/codes/data-stream.js +70 -0
- package/lib/errors/codes/data-transform.d.ts +2 -0
- package/lib/errors/codes/data-transform.js +35 -0
- package/lib/errors/codes/dependency.d.ts +2 -0
- package/lib/errors/codes/dependency.js +11 -0
- package/lib/errors/codes/dlo.d.ts +2 -0
- package/lib/errors/codes/dlo.js +31 -0
- package/lib/errors/codes/dmo.d.ts +2 -0
- package/lib/errors/codes/dmo.js +28 -0
- package/lib/errors/codes/general.d.ts +2 -0
- package/lib/errors/codes/general.js +57 -0
- package/lib/errors/codes/identity-resolution.d.ts +2 -0
- package/lib/errors/codes/identity-resolution.js +24 -0
- package/lib/errors/codes/mapping.d.ts +2 -0
- package/lib/errors/codes/mapping.js +42 -0
- package/lib/errors/codes/relationship.d.ts +2 -0
- package/lib/errors/codes/relationship.js +21 -0
- package/lib/errors/codes/segment.d.ts +2 -0
- package/lib/errors/codes/segment.js +49 -0
- package/lib/errors/registry.d.ts +60 -0
- package/lib/errors/registry.js +123 -0
- package/lib/plugin.d.ts +5 -0
- package/lib/plugin.js +5 -0
- package/lib/services/acquire-retrieve-selection.d.ts +43 -0
- package/lib/services/acquire-retrieve-selection.js +145 -0
- package/lib/services/bind-identity-resolution-dependencies.d.ts +53 -0
- package/lib/services/bind-identity-resolution-dependencies.js +238 -0
- package/lib/services/bind-segment-dependencies.d.ts +49 -0
- package/lib/services/bind-segment-dependencies.js +115 -0
- package/lib/services/bounded-reads.d.ts +17 -0
- package/lib/services/bounded-reads.js +42 -0
- package/lib/services/calculated-insight-sql-checks.d.ts +33 -0
- package/lib/services/calculated-insight-sql-checks.js +193 -0
- package/lib/services/command-dlo.d.ts +48 -0
- package/lib/services/command-dlo.js +103 -0
- package/lib/services/command-native-data-graph.d.ts +54 -0
- package/lib/services/command-native-data-graph.js +485 -0
- package/lib/services/command-native-data-stream.d.ts +104 -0
- package/lib/services/command-native-data-stream.js +257 -0
- package/lib/services/command-native-dmo.d.ts +124 -0
- package/lib/services/command-native-dmo.js +338 -0
- package/lib/services/command-native-mapping.d.ts +65 -0
- package/lib/services/command-native-mapping.js +129 -0
- package/lib/services/command-native-relationship.d.ts +46 -0
- package/lib/services/command-native-relationship.js +103 -0
- package/lib/services/cross-dataspace-deploy.d.ts +111 -0
- package/lib/services/cross-dataspace-deploy.js +493 -0
- package/lib/services/cross-dataspace-rename.d.ts +74 -0
- package/lib/services/cross-dataspace-rename.js +1314 -0
- package/lib/services/cross-dataspace-sql.d.ts +109 -0
- package/lib/services/cross-dataspace-sql.js +1301 -0
- package/lib/services/cross-space-context.d.ts +23 -0
- package/lib/services/cross-space-context.js +36 -0
- package/lib/services/crud-plan.d.ts +137 -0
- package/lib/services/crud-plan.js +408 -0
- package/lib/services/data-space-check.d.ts +89 -0
- package/lib/services/data-space-check.js +162 -0
- package/lib/services/data-space-prefix.d.ts +77 -0
- package/lib/services/data-space-prefix.js +127 -0
- package/lib/services/data-transform-space.d.ts +31 -0
- package/lib/services/data-transform-space.js +79 -0
- package/lib/services/delete-calculated-insight.d.ts +44 -0
- package/lib/services/delete-calculated-insight.js +101 -0
- package/lib/services/delete-data-transform.d.ts +52 -0
- package/lib/services/delete-data-transform.js +222 -0
- package/lib/services/delete-dmo-field.d.ts +211 -0
- package/lib/services/delete-dmo-field.js +472 -0
- package/lib/services/delete-identity-resolution.d.ts +43 -0
- package/lib/services/delete-identity-resolution.js +295 -0
- package/lib/services/delete-segment.d.ts +42 -0
- package/lib/services/delete-segment.js +248 -0
- package/lib/services/deploy-result.d.ts +54 -0
- package/lib/services/deploy-result.js +60 -0
- package/lib/services/escaped-copy.d.ts +23 -0
- package/lib/services/escaped-copy.js +88 -0
- package/lib/services/execute-calculated-insight-deploy.d.ts +42 -0
- package/lib/services/execute-calculated-insight-deploy.js +96 -0
- package/lib/services/execute-data-transform-deploy.d.ts +56 -0
- package/lib/services/execute-data-transform-deploy.js +168 -0
- package/lib/services/execute-native-identity-resolution-deploy.d.ts +48 -0
- package/lib/services/execute-native-identity-resolution-deploy.js +360 -0
- package/lib/services/execute-segment-deploy.d.ts +45 -0
- package/lib/services/execute-segment-deploy.js +393 -0
- package/lib/services/local-file-removal.d.ts +36 -0
- package/lib/services/local-file-removal.js +103 -0
- package/lib/services/native-calculated-insight.d.ts +87 -0
- package/lib/services/native-calculated-insight.js +164 -0
- package/lib/services/native-crm-data-stream.d.ts +91 -0
- package/lib/services/native-crm-data-stream.js +272 -0
- package/lib/services/native-data-graph-context.d.ts +12 -0
- package/lib/services/native-data-graph-context.js +139 -0
- package/lib/services/native-data-graph-crd.d.ts +261 -0
- package/lib/services/native-data-graph-crd.js +866 -0
- package/lib/services/native-data-stream.d.ts +329 -0
- package/lib/services/native-data-stream.js +1830 -0
- package/lib/services/native-dlo.d.ts +163 -0
- package/lib/services/native-dlo.js +402 -0
- package/lib/services/native-dmo.d.ts +191 -0
- package/lib/services/native-dmo.js +614 -0
- package/lib/services/native-mapping.d.ts +255 -0
- package/lib/services/native-mapping.js +1226 -0
- package/lib/services/native-relationship.d.ts +132 -0
- package/lib/services/native-relationship.js +512 -0
- package/lib/services/operation-diagnostic.d.ts +83 -0
- package/lib/services/operation-diagnostic.js +272 -0
- package/lib/services/preflight-calculated-insight-deploy.d.ts +58 -0
- package/lib/services/preflight-calculated-insight-deploy.js +95 -0
- package/lib/services/preflight-data-transform-deploy.d.ts +33 -0
- package/lib/services/preflight-data-transform-deploy.js +104 -0
- package/lib/services/preflight-identity-resolution-deploy.d.ts +107 -0
- package/lib/services/preflight-identity-resolution-deploy.js +281 -0
- package/lib/services/preflight-segment-deploy.d.ts +31 -0
- package/lib/services/preflight-segment-deploy.js +109 -0
- package/lib/services/publish-calculated-insight.d.ts +40 -0
- package/lib/services/publish-calculated-insight.js +95 -0
- package/lib/services/publish-segment.d.ts +65 -0
- package/lib/services/publish-segment.js +194 -0
- package/lib/services/readback-pace.d.ts +16 -0
- package/lib/services/readback-pace.js +23 -0
- package/lib/services/retrieve-data-transform.d.ts +26 -0
- package/lib/services/retrieve-data-transform.js +65 -0
- package/lib/services/retrieve-identity-resolution.d.ts +40 -0
- package/lib/services/retrieve-identity-resolution.js +87 -0
- package/lib/services/retrieve-inventory.d.ts +32 -0
- package/lib/services/retrieve-inventory.js +151 -0
- package/lib/services/retrieve-segment.d.ts +120 -0
- package/lib/services/retrieve-segment.js +502 -0
- package/lib/services/segment-lookup.d.ts +46 -0
- package/lib/services/segment-lookup.js +108 -0
- package/lib/services/segment-members.d.ts +144 -0
- package/lib/services/segment-members.js +402 -0
- package/lib/services/select-native-retrieve.d.ts +60 -0
- package/lib/services/select-native-retrieve.js +226 -0
- package/lib/services/settle-calculated-insight.d.ts +22 -0
- package/lib/services/settle-calculated-insight.js +53 -0
- package/lib/services/sql-tokens.d.ts +58 -0
- package/lib/services/sql-tokens.js +147 -0
- package/lib/transport/api-version.d.ts +36 -0
- package/lib/transport/api-version.js +89 -0
- package/lib/transport/bound-ssot.d.ts +27 -0
- package/lib/transport/bound-ssot.js +116 -0
- package/lib/transport/calculated-insight-rest.d.ts +28 -0
- package/lib/transport/calculated-insight-rest.js +62 -0
- package/lib/transport/connect-client.d.ts +129 -0
- package/lib/transport/connect-client.js +446 -0
- package/lib/transport/connect-diagnostic.d.ts +89 -0
- package/lib/transport/connect-diagnostic.js +331 -0
- package/lib/transport/data-graph-rest.d.ts +27 -0
- package/lib/transport/data-graph-rest.js +76 -0
- package/lib/transport/data-stream-rest.d.ts +62 -0
- package/lib/transport/data-stream-rest.js +348 -0
- package/lib/transport/data-transform-rest.d.ts +25 -0
- package/lib/transport/data-transform-rest.js +100 -0
- package/lib/transport/dlo-native-rest.d.ts +22 -0
- package/lib/transport/dlo-native-rest.js +89 -0
- package/lib/transport/dlo-rest.d.ts +23 -0
- package/lib/transport/dlo-rest.js +135 -0
- package/lib/transport/dmo-native-rest.d.ts +18 -0
- package/lib/transport/dmo-native-rest.js +55 -0
- package/lib/transport/dmo-rest.d.ts +30 -0
- package/lib/transport/dmo-rest.js +163 -0
- package/lib/transport/mapping-rest.d.ts +63 -0
- package/lib/transport/mapping-rest.js +341 -0
- package/lib/transport/project-metadata.d.ts +36 -0
- package/lib/transport/project-metadata.js +118 -0
- package/lib/transport/relationship-rest.d.ts +43 -0
- package/lib/transport/relationship-rest.js +201 -0
- package/package.json +96 -4
|
@@ -0,0 +1,680 @@
|
|
|
1
|
+
import { explainInput, printableKey } from '../services/operation-diagnostic.js';
|
|
2
|
+
import { foldEnums, sameEnum } from './enum-case.js';
|
|
3
|
+
import { dataSpaceKey, isDataSpaceName, readDataSpaceList, refuseLegacyDataSpace, sortedDataSpaces, } from './data-space-field.js';
|
|
4
|
+
/** Payload-free refusal at the portable stream boundary. */
|
|
5
|
+
export class DataStreamError extends Error {
|
|
6
|
+
code;
|
|
7
|
+
constructor(code) {
|
|
8
|
+
super(`Data stream operation refused: ${code}.`);
|
|
9
|
+
this.code = code;
|
|
10
|
+
this.name = 'DataStreamError';
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
const name = /^[A-Za-z][A-Za-z0-9_]{0,199}$/u;
|
|
14
|
+
const dloName = /^[A-Za-z][A-Za-z0-9_]{0,199}__dll$/u;
|
|
15
|
+
/**
|
|
16
|
+
* Refuse a stream file, naming the key path that cannot be deployed.
|
|
17
|
+
* @param {string} code Refusal code.
|
|
18
|
+
* @param {string} keyPath Printable key path.
|
|
19
|
+
* @param {string} problem What is wrong with it.
|
|
20
|
+
* @returns {DataStreamError} Refusal with an explanation.
|
|
21
|
+
*/
|
|
22
|
+
function refuse(code, keyPath, problem) {
|
|
23
|
+
return explainInput(new DataStreamError(code), `The data stream file cannot be deployed: ${keyPath} ${problem}. Correct the file, or retrieve the stream again once the plugin supports it.`);
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Check one object shape without accepting runtime identifiers.
|
|
27
|
+
* @param {unknown} value Untrusted object.
|
|
28
|
+
* @param {string} keyPath Printable key path of the object (empty for the document).
|
|
29
|
+
* @param {string[]} required Required keys.
|
|
30
|
+
* @param {string[]} [optional] Optional keys.
|
|
31
|
+
* @param {boolean} [open] Whether keys outside the set are kept (retrieve mode).
|
|
32
|
+
* @returns {Record<string, unknown>} Checked object.
|
|
33
|
+
*/
|
|
34
|
+
function object(value, keyPath, required, optional = [], open = false) {
|
|
35
|
+
if (!value || typeof value !== 'object' || Array.isArray(value))
|
|
36
|
+
throw refuse('INVALID_STREAM_DOCUMENT', keyPath || 'the document', 'must be an object');
|
|
37
|
+
const result = value;
|
|
38
|
+
const prefix = keyPath ? `${keyPath}.` : '';
|
|
39
|
+
if (!open)
|
|
40
|
+
for (const key of Object.keys(result))
|
|
41
|
+
if (!required.includes(key) && !optional.includes(key))
|
|
42
|
+
throw refuse('INVALID_STREAM_DOCUMENT', `${prefix}${printableKey(key)}`, 'is not a key a data stream file can hold');
|
|
43
|
+
for (const key of required)
|
|
44
|
+
if (!Object.hasOwn(result, key))
|
|
45
|
+
throw refuse('INVALID_STREAM_DOCUMENT', `${prefix}${key}`, 'is missing');
|
|
46
|
+
return result;
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* Check text, preserving labels rather than rewriting them.
|
|
50
|
+
* @param {unknown} value Untrusted text.
|
|
51
|
+
* @param {string} keyPath Printable key path.
|
|
52
|
+
* @param {boolean} [bounded] Whether the 500-character limit applies (deploy mode).
|
|
53
|
+
* @returns {string} Checked text.
|
|
54
|
+
*/
|
|
55
|
+
function text(value, keyPath, bounded = true) {
|
|
56
|
+
if (typeof value !== 'string' ||
|
|
57
|
+
!value ||
|
|
58
|
+
value !== value.trim() ||
|
|
59
|
+
[...value].some((character) => character.codePointAt(0) < 32 || character.codePointAt(0) === 127))
|
|
60
|
+
throw refuse('INVALID_STREAM_DOCUMENT', keyPath, 'must be non-empty text without surrounding spaces or control characters');
|
|
61
|
+
if (bounded && value.length > 500)
|
|
62
|
+
throw refuse('INVALID_STREAM_DOCUMENT', keyPath, 'is longer than 500 characters');
|
|
63
|
+
return value;
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* Check a portable developer name.
|
|
67
|
+
* @param {unknown} value Untrusted name.
|
|
68
|
+
* @param {string} keyPath Printable key path.
|
|
69
|
+
* @param {boolean} [dlo] Whether a DLO suffix is required.
|
|
70
|
+
* @returns {string} Checked name.
|
|
71
|
+
*/
|
|
72
|
+
function identifier(value, keyPath, dlo = false) {
|
|
73
|
+
if (!(dlo ? dloName : name).test(text(value, keyPath)))
|
|
74
|
+
throw refuse('INVALID_STREAM_NAME', keyPath, dlo ? 'must be a developer name ending in __dll' : 'must be a developer name');
|
|
75
|
+
return value;
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* Freeze a detached JSON value recursively.
|
|
79
|
+
* @param {unknown} value Detached value.
|
|
80
|
+
* @returns {T} Frozen value.
|
|
81
|
+
*/
|
|
82
|
+
function freeze(value) {
|
|
83
|
+
if (value && typeof value === 'object') {
|
|
84
|
+
for (const child of Object.values(value))
|
|
85
|
+
freeze(child);
|
|
86
|
+
Object.freeze(value);
|
|
87
|
+
}
|
|
88
|
+
return value;
|
|
89
|
+
}
|
|
90
|
+
/**
|
|
91
|
+
* Validate authored intent separately from retained, read-only schema evidence. Deploy mode (the
|
|
92
|
+
* default) admits only what a deploy can send and names the first offending key path; retrieve
|
|
93
|
+
* mode lifts the 500-character text limit and the 40-character DLO label limit. Unknown keys
|
|
94
|
+
* inside `observed`, which deploy never sends, are kept in both modes.
|
|
95
|
+
* @param {unknown} value Untrusted portable document.
|
|
96
|
+
* @param {DocumentMode} [mode] `deploy` (strict) or `retrieve` (lenient).
|
|
97
|
+
* @returns {DataStreamDocument} Detached deeply frozen document.
|
|
98
|
+
*/
|
|
99
|
+
export function validateDataStreamDocument(value, mode = 'deploy') {
|
|
100
|
+
const lenient = mode === 'retrieve';
|
|
101
|
+
const bounded = !lenient;
|
|
102
|
+
const document = object(value, '', ['schemaVersion', 'kind', dataSpaceKey, 'identity', 'spec'], ['observed']);
|
|
103
|
+
const identity = object(document.identity, 'identity', ['apiName'], ['dataSpace']);
|
|
104
|
+
refuseLegacyDataSpace(identity, refuse);
|
|
105
|
+
identifier(identity.apiName, 'identity.apiName');
|
|
106
|
+
readDataSpaceList(document, refuse);
|
|
107
|
+
const spec = object(document.spec, 'spec', [
|
|
108
|
+
'connectorType',
|
|
109
|
+
'connectionName',
|
|
110
|
+
'event',
|
|
111
|
+
'datastreamType',
|
|
112
|
+
'refreshMode',
|
|
113
|
+
'label',
|
|
114
|
+
'newDlo',
|
|
115
|
+
'additiveFieldMappings',
|
|
116
|
+
], ['formulaMappings']);
|
|
117
|
+
identifier(spec.connectionName, 'spec.connectionName');
|
|
118
|
+
identifier(spec.event, 'spec.event');
|
|
119
|
+
text(spec.label, 'spec.label', bounded);
|
|
120
|
+
const dlo = object(spec.newDlo, 'spec.newDlo', ['name', 'label', 'primaryKey'], ['category', 'eventDateTimeFieldName']);
|
|
121
|
+
identifier(dlo.name, 'spec.newDlo.name', true);
|
|
122
|
+
if (text(dlo.label, 'spec.newDlo.label', bounded).length > 40 && !lenient)
|
|
123
|
+
throw refuse('INVALID_STREAM_LABEL', 'spec.newDlo.label', 'is longer than 40 characters');
|
|
124
|
+
const pk = object(dlo.primaryKey, 'spec.newDlo.primaryKey', ['name', 'label', 'dataType']);
|
|
125
|
+
identifier(pk.name, 'spec.newDlo.primaryKey.name');
|
|
126
|
+
text(pk.label, 'spec.newDlo.primaryKey.label', bounded);
|
|
127
|
+
// Category and field types are Salesforce's to accept or refuse; only their shape is checked.
|
|
128
|
+
if (dlo.category !== undefined)
|
|
129
|
+
text(dlo.category, 'spec.newDlo.category');
|
|
130
|
+
text(pk.dataType, 'spec.newDlo.primaryKey.dataType');
|
|
131
|
+
if (dlo.eventDateTimeFieldName !== undefined)
|
|
132
|
+
identifier(dlo.eventDateTimeFieldName, 'spec.newDlo.eventDateTimeFieldName');
|
|
133
|
+
// This file shape is the Ingestion API one (connection and event); a stream of any other
|
|
134
|
+
// connector uses the file shape with spec.connectorInfo. The constants ignore letter case like
|
|
135
|
+
// other stream enums and are sent as written. The refresh mode is Salesforce's to decide.
|
|
136
|
+
for (const [key, expected] of [
|
|
137
|
+
['connectorType', 'IngestApi'],
|
|
138
|
+
['datastreamType', 'INGEST_API'],
|
|
139
|
+
])
|
|
140
|
+
if (typeof spec[key] !== 'string' || !sameEnum(spec[key], expected))
|
|
141
|
+
throw refuse('UNSUPPORTED_STREAM_SPEC', `spec.${key}`, `must be ${expected} in this file shape (a stream of another connector keeps its connector in spec.connectorInfo)`);
|
|
142
|
+
text(spec.refreshMode, 'spec.refreshMode');
|
|
143
|
+
if (document.schemaVersion !== 1 ||
|
|
144
|
+
document.kind !== 'data-stream' ||
|
|
145
|
+
!Array.isArray(spec.additiveFieldMappings) ||
|
|
146
|
+
(spec.formulaMappings !== undefined && !Array.isArray(spec.formulaMappings)))
|
|
147
|
+
throw new DataStreamError('UNSUPPORTED_STREAM_SPEC');
|
|
148
|
+
const targets = new Set();
|
|
149
|
+
for (const [index, entry] of spec.additiveFieldMappings.entries()) {
|
|
150
|
+
const at = `spec.additiveFieldMappings[${index}]`;
|
|
151
|
+
const mapping = object(entry, at, [
|
|
152
|
+
'sourceFieldLabel',
|
|
153
|
+
'targetFieldName',
|
|
154
|
+
'targetFieldReturntype',
|
|
155
|
+
]);
|
|
156
|
+
// A label is free text (the read returns it in `formula`).
|
|
157
|
+
text(mapping.sourceFieldLabel, `${at}.sourceFieldLabel`);
|
|
158
|
+
const target = identifier(mapping.targetFieldName, `${at}.targetFieldName`);
|
|
159
|
+
// Field type, source and target are Salesforce's to accept or refuse; the file only needs
|
|
160
|
+
// one mapping per target, named without the DLO suffix.
|
|
161
|
+
text(mapping.targetFieldReturntype, `${at}.targetFieldReturntype`);
|
|
162
|
+
if (target.endsWith('__c') || targets.has(target.toLowerCase()))
|
|
163
|
+
throw new DataStreamError('INVALID_STREAM_MAPPING');
|
|
164
|
+
targets.add(target.toLowerCase());
|
|
165
|
+
}
|
|
166
|
+
for (const [index, entry] of (spec.formulaMappings ?? []).entries()) {
|
|
167
|
+
const at = `spec.formulaMappings[${index}]`;
|
|
168
|
+
const mapping = object(entry, at, ['targetFieldName', 'formula', 'targetFieldReturntype']);
|
|
169
|
+
const target = identifier(mapping.targetFieldName, `${at}.targetFieldName`);
|
|
170
|
+
text(mapping.formula, `${at}.formula`, false);
|
|
171
|
+
text(mapping.targetFieldReturntype, `${at}.targetFieldReturntype`);
|
|
172
|
+
if (targets.has(target.toLowerCase()))
|
|
173
|
+
throw new DataStreamError('INVALID_STREAM_MAPPING');
|
|
174
|
+
targets.add(target.toLowerCase());
|
|
175
|
+
}
|
|
176
|
+
// Deploy never sends `observed` (read-only schema evidence), so in both modes a key the plugin
|
|
177
|
+
// does not model inside it is kept, never refused.
|
|
178
|
+
if (document.observed !== undefined) {
|
|
179
|
+
const observed = object(document.observed, 'observed', ['mappings', 'sourceFields', 'dloFields', 'actualDloName'], [], true);
|
|
180
|
+
identifier(observed.actualDloName, 'observed.actualDloName', true);
|
|
181
|
+
for (const key of ['mappings', 'sourceFields', 'dloFields'])
|
|
182
|
+
if (!Array.isArray(observed[key]))
|
|
183
|
+
throw new DataStreamError('INVALID_STREAM_OBSERVED');
|
|
184
|
+
const observedTargets = new Set();
|
|
185
|
+
const sourceNames = new Set();
|
|
186
|
+
const fieldNames = new Set();
|
|
187
|
+
for (const [index, entry] of observed.mappings.entries()) {
|
|
188
|
+
const at = `observed.mappings[${index}]`;
|
|
189
|
+
const mapping = object(entry, at, ['targetFieldName'], ['sourceFieldName', 'formula'], true);
|
|
190
|
+
const target = identifier(mapping.targetFieldName, `${at}.targetFieldName`).toLowerCase();
|
|
191
|
+
if (observedTargets.has(target))
|
|
192
|
+
throw new DataStreamError('INVALID_STREAM_OBSERVED');
|
|
193
|
+
observedTargets.add(target);
|
|
194
|
+
if ((mapping.sourceFieldName === undefined) === (mapping.formula === undefined))
|
|
195
|
+
throw new DataStreamError('INVALID_STREAM_OBSERVED');
|
|
196
|
+
if (mapping.sourceFieldName !== undefined)
|
|
197
|
+
identifier(mapping.sourceFieldName, `${at}.sourceFieldName`);
|
|
198
|
+
if (mapping.formula !== undefined)
|
|
199
|
+
text(mapping.formula, `${at}.formula`, bounded);
|
|
200
|
+
}
|
|
201
|
+
for (const [index, entry] of observed.sourceFields.entries()) {
|
|
202
|
+
const at = `observed.sourceFields[${index}]`;
|
|
203
|
+
const field = object(entry, at, ['name', 'datatype'], [], true);
|
|
204
|
+
const source = identifier(field.name, `${at}.name`).toLowerCase();
|
|
205
|
+
if (sourceNames.has(source))
|
|
206
|
+
throw new DataStreamError('INVALID_STREAM_OBSERVED');
|
|
207
|
+
sourceNames.add(source);
|
|
208
|
+
text(field.datatype, `${at}.datatype`, bounded);
|
|
209
|
+
}
|
|
210
|
+
for (const [index, entry] of observed.dloFields.entries()) {
|
|
211
|
+
const at = `observed.dloFields[${index}]`;
|
|
212
|
+
const field = object(entry, at, ['name', 'label', 'dataType', 'isPrimaryKey'], [], true);
|
|
213
|
+
const fieldName = identifier(field.name, `${at}.name`).toLowerCase();
|
|
214
|
+
if (fieldNames.has(fieldName))
|
|
215
|
+
throw new DataStreamError('INVALID_STREAM_OBSERVED');
|
|
216
|
+
fieldNames.add(fieldName);
|
|
217
|
+
text(field.label, `${at}.label`, bounded);
|
|
218
|
+
text(field.dataType, `${at}.dataType`, bounded);
|
|
219
|
+
if (typeof field.isPrimaryKey !== 'boolean')
|
|
220
|
+
throw new DataStreamError('INVALID_STREAM_OBSERVED');
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
return freeze(structuredClone(value));
|
|
224
|
+
}
|
|
225
|
+
/**
|
|
226
|
+
* The refusal of a new stream whose file lists more than one data space: the create puts the
|
|
227
|
+
* landing DLO into one space, and the plugin never changes spaces through a stream file.
|
|
228
|
+
* @param {readonly string[]} spaces The file's c__dataSpace list.
|
|
229
|
+
* @returns {DataStreamError} STREAM_DATASPACE_MISMATCH with an explanation.
|
|
230
|
+
*/
|
|
231
|
+
export function createSpacesRefusal(spaces) {
|
|
232
|
+
return explainInput(new DataStreamError('STREAM_DATASPACE_MISMATCH'), `The stream does not exist yet, and its file lists the data spaces ${spaces.map((space) => printableKey(space)).join(', ')}. A new stream creates its landing DLO in one data space: list only the run's data space in ${dataSpaceKey}, deploy, then add the other spaces with the DLO file and retrieve the stream again.`);
|
|
233
|
+
}
|
|
234
|
+
/**
|
|
235
|
+
* Build the proven PK-only object-shaped POST; no observed fields are replayed. Salesforce creates
|
|
236
|
+
* every other DLO field from the event schema. Additive mappings are sent in the create's source-
|
|
237
|
+
* label shape (live API 68: 201, read back as plain source mappings). Formula mappings are sent
|
|
238
|
+
* with the event's typed source fields (the proven formula create); without formulas the source
|
|
239
|
+
* fields stay empty.
|
|
240
|
+
* @param {unknown} value Portable document.
|
|
241
|
+
* @param {Array<{name: string; dataType: string}>} [sourceFields] Event schema fields (name, type).
|
|
242
|
+
* @returns {object} Native create body, not dispatch authorization.
|
|
243
|
+
*/
|
|
244
|
+
export function buildDataStreamCreateBody(value, sourceFields = []) {
|
|
245
|
+
const document = validateDataStreamDocument(value);
|
|
246
|
+
// The landing DLO is created in one data space; the others are added through its DLO file.
|
|
247
|
+
if (document.c__dataSpace.length !== 1)
|
|
248
|
+
throw createSpacesRefusal(document.c__dataSpace);
|
|
249
|
+
const { spec } = document;
|
|
250
|
+
const formulas = spec.formulaMappings ?? [];
|
|
251
|
+
return freeze({
|
|
252
|
+
name: document.identity.apiName,
|
|
253
|
+
label: spec.label,
|
|
254
|
+
datastreamType: spec.datastreamType,
|
|
255
|
+
connectorInfo: {
|
|
256
|
+
connectorType: spec.connectorType,
|
|
257
|
+
connectorDetails: { name: spec.connectionName, events: [spec.event] },
|
|
258
|
+
},
|
|
259
|
+
dataLakeObjectInfo: {
|
|
260
|
+
name: spec.newDlo.name,
|
|
261
|
+
label: spec.newDlo.label,
|
|
262
|
+
...(spec.newDlo.category === undefined ? {} : { category: spec.newDlo.category }),
|
|
263
|
+
...(spec.newDlo.eventDateTimeFieldName === undefined
|
|
264
|
+
? {}
|
|
265
|
+
: { eventDateTimeFieldName: spec.newDlo.eventDateTimeFieldName }),
|
|
266
|
+
dataspaceInfo: [{ name: document.c__dataSpace[0] }],
|
|
267
|
+
dataLakeFieldInputRepresentations: [{ ...spec.newDlo.primaryKey, isPrimaryKey: true }],
|
|
268
|
+
},
|
|
269
|
+
mappings: [
|
|
270
|
+
...structuredClone(spec.additiveFieldMappings),
|
|
271
|
+
...formulas.map((mapping) => ({
|
|
272
|
+
targetFieldName: mapping.targetFieldName,
|
|
273
|
+
transformationFormula: mapping.formula,
|
|
274
|
+
targetFieldReturntype: mapping.targetFieldReturntype,
|
|
275
|
+
})),
|
|
276
|
+
],
|
|
277
|
+
sourceFields: formulas.length > 0
|
|
278
|
+
? sourceFields.map((field) => ({ name: field.name, dataType: field.dataType }))
|
|
279
|
+
: [],
|
|
280
|
+
refreshConfig: { refreshMode: spec.refreshMode },
|
|
281
|
+
});
|
|
282
|
+
}
|
|
283
|
+
/**
|
|
284
|
+
* Serialize explicit additive intent only; target binding and missing-pair selection belong to the service.
|
|
285
|
+
* @param {unknown} value Portable document containing only additions selected for dispatch.
|
|
286
|
+
* @returns {object} Proven source-label PATCH shape, not dispatch authorization.
|
|
287
|
+
*/
|
|
288
|
+
export function buildDataStreamPatchBody(value) {
|
|
289
|
+
const document = validateDataStreamDocument(value);
|
|
290
|
+
if (document.spec.additiveFieldMappings.length === 0)
|
|
291
|
+
throw new DataStreamError('EMPTY_STREAM_PATCH');
|
|
292
|
+
return freeze({
|
|
293
|
+
datastreamType: document.spec.datastreamType,
|
|
294
|
+
mappings: structuredClone(document.spec.additiveFieldMappings),
|
|
295
|
+
});
|
|
296
|
+
}
|
|
297
|
+
/**
|
|
298
|
+
* Convert a stream target selector to the independently read DLO field name.
|
|
299
|
+
* @param {unknown} value Unsuffixed mapping target.
|
|
300
|
+
* @returns {string} Persisted DLO field selector.
|
|
301
|
+
*/
|
|
302
|
+
export function dataStreamTargetFieldName(value) {
|
|
303
|
+
const target = identifier(value, 'targetFieldName');
|
|
304
|
+
if (target.endsWith('__c'))
|
|
305
|
+
throw new DataStreamError('INVALID_STREAM_MAPPING');
|
|
306
|
+
return `${target}__c`;
|
|
307
|
+
}
|
|
308
|
+
const ingestConnector = 'IngestApi';
|
|
309
|
+
const crmConnector = 'SalesforceDotCom';
|
|
310
|
+
// Keys of a stream GET that Salesforce owns: identifiers, run state and values it derives.
|
|
311
|
+
const ownedStreamKeys = ['recordId', 'status', 'lastRunStatus', 'isEnabled', 'dataSource'];
|
|
312
|
+
// Keys the GET returns as empty objects; kept when Salesforce fills them.
|
|
313
|
+
const emptyStreamKeys = ['advancedAttributes', 'capabilities'];
|
|
314
|
+
const ownedDloKeys = [
|
|
315
|
+
'id',
|
|
316
|
+
'status',
|
|
317
|
+
'capabilities',
|
|
318
|
+
'columnMappingProperties',
|
|
319
|
+
'dataLakeFieldInfoRepresentation',
|
|
320
|
+
];
|
|
321
|
+
const ownedRefreshKeys = [
|
|
322
|
+
'hasHeaders',
|
|
323
|
+
'shouldFetchImmediately',
|
|
324
|
+
'shouldTreatMissingFilesAsFailures',
|
|
325
|
+
];
|
|
326
|
+
/**
|
|
327
|
+
* Whether a value is a plain JSON object.
|
|
328
|
+
* @param {unknown} value Candidate.
|
|
329
|
+
* @returns {boolean} Object and not an array.
|
|
330
|
+
*/
|
|
331
|
+
function isBag(value) {
|
|
332
|
+
return Boolean(value) && typeof value === 'object' && !Array.isArray(value);
|
|
333
|
+
}
|
|
334
|
+
/**
|
|
335
|
+
* Whether a field, source field or mapping target is one Salesforce adds to every CRM stream.
|
|
336
|
+
* @param {unknown} fieldName Field name.
|
|
337
|
+
* @returns {boolean} A system field the create input must not carry.
|
|
338
|
+
*/
|
|
339
|
+
export function isCrmSystemField(fieldName) {
|
|
340
|
+
return (typeof fieldName === 'string' &&
|
|
341
|
+
(['DataSource', 'DataSourceObject', 'SfdcOrganizationId'].includes(fieldName) ||
|
|
342
|
+
fieldName.startsWith('KQ_') ||
|
|
343
|
+
fieldName.startsWith('cdp_sys_')));
|
|
344
|
+
}
|
|
345
|
+
/**
|
|
346
|
+
* Sort key of one entry: name, then target field, source field and formula.
|
|
347
|
+
* @param {unknown} value Array entry.
|
|
348
|
+
* @returns {string} Key.
|
|
349
|
+
*/
|
|
350
|
+
function key(value) {
|
|
351
|
+
const entry = isBag(value) ? value : {};
|
|
352
|
+
return [entry.name, entry.targetFieldName, entry.sourceFieldName, entry.formula]
|
|
353
|
+
.map((part) => (typeof part === 'string' ? part : ''))
|
|
354
|
+
.join('\u0000');
|
|
355
|
+
}
|
|
356
|
+
/**
|
|
357
|
+
* Order entries by name, then target field, source field and formula.
|
|
358
|
+
* @param {unknown[]} values Array entries.
|
|
359
|
+
* @returns {unknown[]} Sorted copy.
|
|
360
|
+
*/
|
|
361
|
+
function byName(values) {
|
|
362
|
+
// eslint-disable-next-line unicorn/no-array-sort -- Sort a copied array within the existing ES2022 library target.
|
|
363
|
+
return [...values].sort((left, right) => key(left) < key(right) ? -1 : key(left) > key(right) ? 1 : 0);
|
|
364
|
+
}
|
|
365
|
+
/**
|
|
366
|
+
* Whether a stream GET (or a stream spec) names a connector other than the Ingestion API, such as
|
|
367
|
+
* Salesforce CRM; those streams use the GET-shaped file.
|
|
368
|
+
* @param {unknown} value Stream GET or spec.
|
|
369
|
+
* @returns {boolean} A connector type other than IngestApi.
|
|
370
|
+
*/
|
|
371
|
+
export function isCrmDataStream(value) {
|
|
372
|
+
if (!isBag(value) || !isBag(value.connectorInfo))
|
|
373
|
+
return false;
|
|
374
|
+
const type = value.connectorInfo.connectorType;
|
|
375
|
+
return typeof type === 'string' && type !== '' && !sameEnum(type, ingestConnector);
|
|
376
|
+
}
|
|
377
|
+
/**
|
|
378
|
+
* Whether a stream document is a GET-shaped document (its spec names a connector other than the
|
|
379
|
+
* Ingestion API).
|
|
380
|
+
* @param {unknown} value Candidate document.
|
|
381
|
+
* @returns {boolean} CRM stream document.
|
|
382
|
+
*/
|
|
383
|
+
export function isCrmDataStreamDocument(value) {
|
|
384
|
+
return isBag(value) && isCrmDataStream(value.spec);
|
|
385
|
+
}
|
|
386
|
+
/**
|
|
387
|
+
* The spec of a CRM stream file: the GET without server-owned keys, system fields and system
|
|
388
|
+
* mappings, fields, source fields and mappings in a stable order. Formulas read as stored: the
|
|
389
|
+
* client asks Salesforce for plain text (plainTextHeaders).
|
|
390
|
+
* @param {unknown} value Stream GET response.
|
|
391
|
+
* @returns {Bag} Spec as stored in the file.
|
|
392
|
+
*/
|
|
393
|
+
export function crmDataStreamSpec(value) {
|
|
394
|
+
if (!isBag(value))
|
|
395
|
+
throw new DataStreamError('INVALID_STREAM_READ');
|
|
396
|
+
const spec = structuredClone(value);
|
|
397
|
+
for (const key of ownedStreamKeys)
|
|
398
|
+
delete spec[key];
|
|
399
|
+
for (const key of emptyStreamKeys)
|
|
400
|
+
if (isBag(spec[key]) && Object.keys(spec[key]).length === 0)
|
|
401
|
+
delete spec[key];
|
|
402
|
+
if (isBag(spec.connectorInfo) && isBag(spec.connectorInfo.connectorDetails))
|
|
403
|
+
delete spec.connectorInfo.connectorDetails.type;
|
|
404
|
+
if (isBag(spec.dataLakeObjectInfo)) {
|
|
405
|
+
const dlo = spec.dataLakeObjectInfo;
|
|
406
|
+
for (const key of ownedDloKeys)
|
|
407
|
+
delete dlo[key];
|
|
408
|
+
if (Array.isArray(dlo.fields))
|
|
409
|
+
dlo.fields = byName(dlo.fields.filter((field) => !isCrmSystemField(isBag(field) && field.name)));
|
|
410
|
+
}
|
|
411
|
+
// Salesforce CRM streams echo file-import refresh keys the create refuses (live, API 68); for
|
|
412
|
+
// file connectors such as S3 they are the user's settings and stay in the file.
|
|
413
|
+
if (isBag(spec.refreshConfig) &&
|
|
414
|
+
isBag(spec.connectorInfo) &&
|
|
415
|
+
sameEnum(spec.connectorInfo.connectorType, crmConnector))
|
|
416
|
+
for (const key of ownedRefreshKeys)
|
|
417
|
+
delete spec.refreshConfig[key];
|
|
418
|
+
if (Array.isArray(spec.sourceFields))
|
|
419
|
+
spec.sourceFields = byName(spec.sourceFields
|
|
420
|
+
.filter((field) => !isCrmSystemField(isBag(field) && field.name))
|
|
421
|
+
.map((field) => {
|
|
422
|
+
if (!isBag(field) || field.format === undefined)
|
|
423
|
+
return field;
|
|
424
|
+
// Salesforce derives the date format of a CRM source field; the create input has none.
|
|
425
|
+
const copy = { ...field };
|
|
426
|
+
delete copy.format;
|
|
427
|
+
return copy;
|
|
428
|
+
}));
|
|
429
|
+
if (Array.isArray(spec.mappings))
|
|
430
|
+
spec.mappings = byName(spec.mappings.filter((mapping) => !isCrmSystemField(isBag(mapping) && mapping.targetFieldName)));
|
|
431
|
+
return spec;
|
|
432
|
+
}
|
|
433
|
+
/**
|
|
434
|
+
* The data space names of a stream GET's `dataLakeObjectInfo.dataSpaceInfo`.
|
|
435
|
+
* @param {unknown} value The list.
|
|
436
|
+
* @returns {string[] | undefined} Names, or undefined when an entry names no usable data space.
|
|
437
|
+
*/
|
|
438
|
+
export function crmSpaceNames(value) {
|
|
439
|
+
if (!Array.isArray(value))
|
|
440
|
+
return undefined;
|
|
441
|
+
const names = value.map((entry) => (isBag(entry) ? entry.name : undefined));
|
|
442
|
+
return names.every((name) => isDataSpaceName(name)) ? names : undefined;
|
|
443
|
+
}
|
|
444
|
+
/**
|
|
445
|
+
* The CRM stream spec deploy sends: the file's spec with Salesforce's data space list filled from
|
|
446
|
+
* c__dataSpace when the file leaves it out (the validator has checked that a kept list agrees).
|
|
447
|
+
* @param {CrmDataStreamDocument} document Validated CRM stream file.
|
|
448
|
+
* @returns {Bag} Spec to send.
|
|
449
|
+
*/
|
|
450
|
+
export function crmDeploySpec(document) {
|
|
451
|
+
const spec = structuredClone(document.spec);
|
|
452
|
+
const dlo = spec.dataLakeObjectInfo;
|
|
453
|
+
if (dlo.dataSpaceInfo === undefined)
|
|
454
|
+
dlo.dataSpaceInfo = document.c__dataSpace.map((name) => ({ name }));
|
|
455
|
+
return spec;
|
|
456
|
+
}
|
|
457
|
+
/**
|
|
458
|
+
* Validate a CRM stream document. Only what names and places the file is checked; everything
|
|
459
|
+
* else is sent as written on deploy and Salesforce decides.
|
|
460
|
+
* @param {unknown} value Untrusted document.
|
|
461
|
+
* @param {DocumentMode} [mode] `deploy` bounds the label length; `retrieve` does not.
|
|
462
|
+
* @returns {CrmDataStreamDocument} Detached deeply frozen document.
|
|
463
|
+
*/
|
|
464
|
+
export function validateCrmDataStreamDocument(value, mode = 'deploy') {
|
|
465
|
+
const document = object(value, '', ['schemaVersion', 'kind', dataSpaceKey, 'identity', 'spec']);
|
|
466
|
+
const identity = object(document.identity, 'identity', ['apiName'], ['dataSpace']);
|
|
467
|
+
refuseLegacyDataSpace(identity, refuse);
|
|
468
|
+
identifier(identity.apiName, 'identity.apiName');
|
|
469
|
+
const spaces = readDataSpaceList(document, refuse);
|
|
470
|
+
if (document.schemaVersion !== 1 || document.kind !== 'data-stream')
|
|
471
|
+
throw new DataStreamError('UNSUPPORTED_STREAM_SPEC');
|
|
472
|
+
const spec = object(document.spec, 'spec', ['name', 'label', 'connectorInfo', 'dataLakeObjectInfo'], [], true);
|
|
473
|
+
if (spec.name !== identity.apiName)
|
|
474
|
+
throw refuse('INVALID_STREAM_DOCUMENT', 'spec.name', 'must equal identity.apiName');
|
|
475
|
+
text(spec.label, 'spec.label', mode === 'deploy');
|
|
476
|
+
const dlo = object(spec.dataLakeObjectInfo, 'spec.dataLakeObjectInfo', ['name'], [], true);
|
|
477
|
+
identifier(dlo.name, 'spec.dataLakeObjectInfo.name', true);
|
|
478
|
+
// Salesforce's own list of the DLO's data spaces, when the file keeps it, must name the same
|
|
479
|
+
// spaces as c__dataSpace; deploy fills it from c__dataSpace when the file leaves it out.
|
|
480
|
+
if (dlo.dataSpaceInfo !== undefined) {
|
|
481
|
+
const named = crmSpaceNames(dlo.dataSpaceInfo);
|
|
482
|
+
if (named?.length !== spaces.length ||
|
|
483
|
+
sortedDataSpaces(named).join('\u0000') !== sortedDataSpaces(spaces).join('\u0000'))
|
|
484
|
+
throw refuse('STREAM_DATASPACE_MISMATCH', 'spec.dataLakeObjectInfo.dataSpaceInfo', `must name the same data spaces as ${dataSpaceKey}`);
|
|
485
|
+
}
|
|
486
|
+
return freeze(structuredClone(value));
|
|
487
|
+
}
|
|
488
|
+
/**
|
|
489
|
+
* Validate either stream document: a CRM stream document, otherwise an Ingestion API one.
|
|
490
|
+
* @param {unknown} value Untrusted document.
|
|
491
|
+
* @param {DocumentMode} [mode] `deploy` (strict) or `retrieve` (lenient).
|
|
492
|
+
* @returns {DataStreamDocument | CrmDataStreamDocument} Detached deeply frozen document.
|
|
493
|
+
*/
|
|
494
|
+
export function validateAnyDataStreamDocument(value, mode = 'deploy') {
|
|
495
|
+
return isCrmDataStreamDocument(value)
|
|
496
|
+
? validateCrmDataStreamDocument(value, mode)
|
|
497
|
+
: validateDataStreamDocument(value, mode);
|
|
498
|
+
}
|
|
499
|
+
/**
|
|
500
|
+
* Rename one key of an object in place, when present.
|
|
501
|
+
* @param {Bag} bag Object.
|
|
502
|
+
* @param {string} from GET spelling.
|
|
503
|
+
* @param {string} to Input spelling.
|
|
504
|
+
*/
|
|
505
|
+
function rename(bag, from, to) {
|
|
506
|
+
if (!Object.hasOwn(bag, from))
|
|
507
|
+
return;
|
|
508
|
+
bag[to] = bag[from];
|
|
509
|
+
delete bag[from];
|
|
510
|
+
}
|
|
511
|
+
/**
|
|
512
|
+
* Translate a CRM stream spec into the create input: the GET spellings the input names
|
|
513
|
+
* differently are renamed, the data space keeps its name only, plain field mappings are left
|
|
514
|
+
* out (Salesforce maps source fields itself) and formula mappings carry the DLO field type.
|
|
515
|
+
* @param {Bag} spec CRM stream spec.
|
|
516
|
+
* @returns {Bag} Create (POST) body.
|
|
517
|
+
*/
|
|
518
|
+
export function crmDataStreamWriteBody(spec) {
|
|
519
|
+
const body = structuredClone(spec);
|
|
520
|
+
rename(body, 'dataStreamType', 'datastreamType');
|
|
521
|
+
rename(body, 'dataSource', 'datasource');
|
|
522
|
+
const types = new Map();
|
|
523
|
+
if (isBag(body.dataLakeObjectInfo)) {
|
|
524
|
+
const dlo = body.dataLakeObjectInfo;
|
|
525
|
+
if (Array.isArray(dlo.dataSpaceInfo))
|
|
526
|
+
dlo.dataSpaceInfo = dlo.dataSpaceInfo.map((space) => isBag(space) ? { name: space.name } : space);
|
|
527
|
+
rename(dlo, 'dataSpaceInfo', 'dataspaceInfo');
|
|
528
|
+
if (Array.isArray(dlo.fields))
|
|
529
|
+
for (const field of dlo.fields)
|
|
530
|
+
if (isBag(field))
|
|
531
|
+
types.set(String(field.name), field.dataType);
|
|
532
|
+
rename(dlo, 'fields', 'dataLakeFieldInputRepresentations');
|
|
533
|
+
}
|
|
534
|
+
if (Array.isArray(body.sourceFields))
|
|
535
|
+
body.sourceFields = body.sourceFields.map((field) => {
|
|
536
|
+
if (!isBag(field))
|
|
537
|
+
return field;
|
|
538
|
+
const copy = { ...field };
|
|
539
|
+
rename(copy, 'datatype', 'dataType');
|
|
540
|
+
return copy;
|
|
541
|
+
});
|
|
542
|
+
if (Array.isArray(body.mappings))
|
|
543
|
+
body.mappings = body.mappings
|
|
544
|
+
.filter((mapping) => isBag(mapping) && typeof mapping.formula === 'string')
|
|
545
|
+
.map((mapping) => {
|
|
546
|
+
const entry = { ...mapping, transformationFormula: mapping.formula };
|
|
547
|
+
delete entry.formula;
|
|
548
|
+
delete entry.sourceFieldName;
|
|
549
|
+
const type = types.get(String(entry.targetFieldName));
|
|
550
|
+
if (entry.targetFieldReturntype === undefined && type !== undefined)
|
|
551
|
+
entry.targetFieldReturntype = type;
|
|
552
|
+
return entry;
|
|
553
|
+
});
|
|
554
|
+
return body;
|
|
555
|
+
}
|
|
556
|
+
/**
|
|
557
|
+
* Stream keys whose values are enums Salesforce may store in its own letter case (a DLO
|
|
558
|
+
* category or field type, a refresh mode); a case-only difference is no change. Names, labels
|
|
559
|
+
* and formulas are compared exactly.
|
|
560
|
+
*/
|
|
561
|
+
export const streamEnumKeys = new Set([
|
|
562
|
+
'category',
|
|
563
|
+
'connectorType',
|
|
564
|
+
'dataType',
|
|
565
|
+
'datastreamType',
|
|
566
|
+
'datatype',
|
|
567
|
+
'frequencyType',
|
|
568
|
+
'refreshMode',
|
|
569
|
+
'targetFieldReturntype',
|
|
570
|
+
]);
|
|
571
|
+
/**
|
|
572
|
+
* A copy with object keys sorted and every array of named entries ordered by name.
|
|
573
|
+
* @param {unknown} value JSON value.
|
|
574
|
+
* @returns {unknown} Copy for comparison only.
|
|
575
|
+
*/
|
|
576
|
+
function comparable(value) {
|
|
577
|
+
if (Array.isArray(value)) {
|
|
578
|
+
const entries = value.map((entry) => comparable(entry));
|
|
579
|
+
return entries.every((entry) => isBag(entry) && typeof entry.name === 'string')
|
|
580
|
+
? byName(entries)
|
|
581
|
+
: entries;
|
|
582
|
+
}
|
|
583
|
+
if (!isBag(value))
|
|
584
|
+
return value;
|
|
585
|
+
// eslint-disable-next-line unicorn/no-array-sort -- Sort a fresh key array within the ES2022 library target.
|
|
586
|
+
const keys = Object.keys(value).sort();
|
|
587
|
+
return Object.fromEntries(keys.map((key) => [key, comparable(value[key])]));
|
|
588
|
+
}
|
|
589
|
+
/**
|
|
590
|
+
* Whether two JSON values are equal, ignoring key order and the order of named entries.
|
|
591
|
+
* @param {unknown} left First value.
|
|
592
|
+
* @param {unknown} right Second value.
|
|
593
|
+
* @returns {boolean} Equal.
|
|
594
|
+
*/
|
|
595
|
+
function sameValue(left, right) {
|
|
596
|
+
return (JSON.stringify(comparable(foldEnums(left, streamEnumKeys))) ===
|
|
597
|
+
JSON.stringify(comparable(foldEnums(right, streamEnumKeys))));
|
|
598
|
+
}
|
|
599
|
+
/**
|
|
600
|
+
* The PATCH that brings a CRM stream from its current spec to the file's spec: `datastreamType`
|
|
601
|
+
* plus each changed part only. Formula mappings are compared one by one (a removed formula is
|
|
602
|
+
* sent with an empty formula); refresh settings key by key; every other changed key is sent
|
|
603
|
+
* whole. The DLO fields of formula targets are left to the mappings. No parts means unchanged.
|
|
604
|
+
* Differences no write input can carry are returned as `unsupported`, never as parts: a plain
|
|
605
|
+
* source-to-target mapping added, removed or changed (`sourceFieldName` is no PATCH input). A
|
|
606
|
+
* changed formula return type is sent with the formula, and Salesforce decides.
|
|
607
|
+
* @param {Bag} wanted Spec of the file.
|
|
608
|
+
* @param {Bag} current Spec of the org's stream.
|
|
609
|
+
* @returns {{body: Bag; parts: string[]; unsupported: string[]}} PATCH body, the changed key
|
|
610
|
+
* paths, and the differing key paths that cannot be sent.
|
|
611
|
+
*/
|
|
612
|
+
export function crmDataStreamPatch(wanted, current) {
|
|
613
|
+
const want = crmDataStreamWriteBody(wanted);
|
|
614
|
+
const have = crmDataStreamWriteBody(current);
|
|
615
|
+
const body = { datastreamType: want.datastreamType };
|
|
616
|
+
const parts = [];
|
|
617
|
+
const formulas = (value) => new Map((Array.isArray(value.mappings) ? value.mappings : []).map((mapping) => [
|
|
618
|
+
String(mapping.targetFieldName),
|
|
619
|
+
mapping,
|
|
620
|
+
]));
|
|
621
|
+
const wantFormulas = formulas(want);
|
|
622
|
+
const haveFormulas = formulas(have);
|
|
623
|
+
const unsupported = [];
|
|
624
|
+
const plain = (value) => new Map((Array.isArray(value.mappings) ? value.mappings : [])
|
|
625
|
+
.filter((mapping) => isBag(mapping) && typeof mapping.formula !== 'string')
|
|
626
|
+
.map((mapping) => [String(mapping.targetFieldName), mapping]));
|
|
627
|
+
const wantPlain = plain(wanted);
|
|
628
|
+
const havePlain = plain(current);
|
|
629
|
+
// eslint-disable-next-line unicorn/no-array-sort -- Sort a fresh key array within the ES2022 library target.
|
|
630
|
+
for (const target of [...new Set([...wantPlain.keys(), ...havePlain.keys()])].sort())
|
|
631
|
+
if (!sameValue(wantPlain.get(target), havePlain.get(target)))
|
|
632
|
+
unsupported.push(`mappings[${target}]`);
|
|
633
|
+
const mappings = [];
|
|
634
|
+
for (const [target, mapping] of wantFormulas) {
|
|
635
|
+
const existing = haveFormulas.get(target);
|
|
636
|
+
if (existing?.transformationFormula !== mapping.transformationFormula ||
|
|
637
|
+
!sameEnum(existing?.targetFieldReturntype, mapping.targetFieldReturntype))
|
|
638
|
+
mappings.push(mapping);
|
|
639
|
+
}
|
|
640
|
+
for (const target of haveFormulas.keys())
|
|
641
|
+
if (!wantFormulas.has(target))
|
|
642
|
+
mappings.push({ targetFieldName: target, transformationFormula: '' });
|
|
643
|
+
if (mappings.length > 0) {
|
|
644
|
+
body.mappings = mappings;
|
|
645
|
+
parts.push('mappings');
|
|
646
|
+
}
|
|
647
|
+
const formulaTargets = new Set([...wantFormulas.keys(), ...haveFormulas.keys()]);
|
|
648
|
+
const withoutFormulaFields = (value) => isBag(value) && Array.isArray(value.dataLakeFieldInputRepresentations)
|
|
649
|
+
? {
|
|
650
|
+
...value,
|
|
651
|
+
dataLakeFieldInputRepresentations: value.dataLakeFieldInputRepresentations.filter((field) => !(isBag(field) && formulaTargets.has(String(field.name)))),
|
|
652
|
+
}
|
|
653
|
+
: value;
|
|
654
|
+
for (const key of new Set([...Object.keys(want), ...Object.keys(have)])) {
|
|
655
|
+
if (key === 'mappings' || key === 'name' || !Object.hasOwn(want, key))
|
|
656
|
+
continue;
|
|
657
|
+
if (key === 'refreshConfig' && isBag(want.refreshConfig)) {
|
|
658
|
+
const wantRefresh = want.refreshConfig;
|
|
659
|
+
const haveRefresh = isBag(have.refreshConfig) ? have.refreshConfig : {};
|
|
660
|
+
const refresh = {};
|
|
661
|
+
for (const setting of Object.keys(wantRefresh))
|
|
662
|
+
// Compared under its key, so an enum setting ignores letter case.
|
|
663
|
+
if (!sameValue({ [setting]: wantRefresh[setting] }, { [setting]: haveRefresh[setting] })) {
|
|
664
|
+
refresh[setting] = wantRefresh[setting];
|
|
665
|
+
parts.push(`refreshConfig.${setting}`);
|
|
666
|
+
}
|
|
667
|
+
if (Object.keys(refresh).length > 0)
|
|
668
|
+
body.refreshConfig = refresh;
|
|
669
|
+
continue;
|
|
670
|
+
}
|
|
671
|
+
const left = key === 'dataLakeObjectInfo' ? withoutFormulaFields(want[key]) : want[key];
|
|
672
|
+
const right = key === 'dataLakeObjectInfo' ? withoutFormulaFields(have[key]) : have[key];
|
|
673
|
+
if (sameValue(left, right))
|
|
674
|
+
continue;
|
|
675
|
+
parts.push(key);
|
|
676
|
+
if (key !== 'datastreamType')
|
|
677
|
+
body[key] = want[key];
|
|
678
|
+
}
|
|
679
|
+
return { body, parts, unsupported };
|
|
680
|
+
}
|