sf-plugin-data 0.1.0 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -21
- package/README.md +618 -240
- package/docs/artifact-format.md +528 -633
- package/docs/capabilities.md +1160 -168
- package/docs/git-workflow.md +135 -12
- package/docs/handover.md +7826 -12
- package/lib/api.d.ts +13 -0
- package/lib/api.js +10 -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 +222 -6
- package/lib/artifacts/read-deploy-input.js +1213 -53
- 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 +2347 -0
- package/lib/commands/data360/deploy.d.ts +235 -16
- package/lib/commands/data360/deploy.js +2339 -102
- package/lib/commands/data360/list/dataspace.d.ts +53 -0
- package/lib/commands/data360/list/dataspace.js +109 -0
- package/lib/commands/data360/publish.d.ts +93 -0
- package/lib/commands/data360/publish.js +298 -0
- package/lib/commands/data360/retrieve.d.ts +283 -14
- package/lib/commands/data360/retrieve.js +1945 -95
- package/lib/commands/data360/segment/members/export.d.ts +101 -0
- package/lib/commands/data360/segment/members/export.js +320 -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 +58 -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/exit-code.d.ts +10 -0
- package/lib/exit-code.js +24 -0
- package/lib/plugin.d.ts +1 -0
- package/lib/plugin.js +3 -1
- 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 +38 -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 +92 -0
- package/lib/services/data-space-check.js +169 -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/list-data-spaces.d.ts +17 -0
- package/lib/services/list-data-spaces.js +24 -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/run-retrieve.d.ts +24 -0
- package/lib/services/run-retrieve.js +51 -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 +94 -32
- package/lib/transport/connect-client.js +318 -178
- 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 +29 -6
- package/lib/artifacts/dmo-document.d.ts +0 -181
- package/lib/artifacts/dmo-document.js +0 -848
- package/lib/artifacts/dmo-update-delta.d.ts +0 -28
- package/lib/artifacts/dmo-update-delta.js +0 -86
- package/lib/artifacts/write-other-dmo.d.ts +0 -17
- package/lib/artifacts/write-other-dmo.js +0 -140
- package/lib/services/execute-dmo-create.d.ts +0 -24
- package/lib/services/execute-dmo-create.js +0 -118
- package/lib/services/execute-dmo-deploy.d.ts +0 -33
- package/lib/services/execute-dmo-deploy.js +0 -197
- package/lib/services/preflight-dmo-deploy.d.ts +0 -69
- package/lib/services/preflight-dmo-deploy.js +0 -161
- package/lib/services/retrieve-other-dmo.d.ts +0 -46
- package/lib/services/retrieve-other-dmo.js +0 -140
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
import type { NativeRelationshipDocument, ObservedRelationship } from '../artifacts/native-relationship-document.js';
|
|
2
|
+
import type { RelationshipClient } from '../transport/relationship-rest.js';
|
|
3
|
+
import type { DocumentMode, VerbatimDocument } from '../artifacts/verbatim-document.js';
|
|
4
|
+
/**
|
|
5
|
+
* Read every relationship the DMO is source or target of, paging by offset. `nextPageUrl` is not
|
|
6
|
+
* followed: Salesforce repeats the current offset in it. The collected count must equal the
|
|
7
|
+
* reported totalSize and names must be unique, otherwise the inventory is refused as incomplete.
|
|
8
|
+
* @param {string} dmoName Exact DMO name.
|
|
9
|
+
* @param {Pick<RelationshipClient, 'list'>} client Relationship reader.
|
|
10
|
+
* @param {DocumentMode} [mode] `retrieve` takes entries as listed; only text names count towards
|
|
11
|
+
* the uniqueness check.
|
|
12
|
+
* @returns {Promise<ObservedRelationship[]>} Complete, verified inventory (empty for 204).
|
|
13
|
+
*/
|
|
14
|
+
export declare function listNativeRelationships(dmoName: string, client: Pick<RelationshipClient, 'list'>, mode?: DocumentMode): Promise<ObservedRelationship[]>;
|
|
15
|
+
/**
|
|
16
|
+
* Retrieve every relationship whose source object is the DMO, as portable documents.
|
|
17
|
+
* @param {string} dmoName Exact source DMO.
|
|
18
|
+
* @param {Pick<RelationshipClient, 'list'>} client Relationship reader.
|
|
19
|
+
* @param {string} requestScope The run's data space.
|
|
20
|
+
* A relationship the document cannot represent is returned as a verbatim document of its raw list
|
|
21
|
+
* entry; each saved document carries its raw entry for the same fallback at write time.
|
|
22
|
+
* @param {DocumentMode} [mode] `retrieve` checks only the entries it keeps and keeps their values
|
|
23
|
+
* as listed.
|
|
24
|
+
* @returns {Promise<Array<NativeRelationshipDocument | VerbatimDocument>>} Documents ordered by name.
|
|
25
|
+
*/
|
|
26
|
+
export declare function retrieveNativeRelationships(dmoName: string, client: Pick<RelationshipClient, 'list' | 'readDefinitions'>, requestScope: string, mode?: DocumentMode): Promise<Array<NativeRelationshipDocument | VerbatimDocument>>;
|
|
27
|
+
/**
|
|
28
|
+
* The relationships whose source object is the DMO, as {@link retrieveNativeRelationships}
|
|
29
|
+
* returns them but without their labels (no Metadata API read), so the labels of many DMOs can
|
|
30
|
+
* be read together.
|
|
31
|
+
* @param {string} dmoName Exact source DMO.
|
|
32
|
+
* @param {Pick<RelationshipClient, 'list'>} client Relationship reader.
|
|
33
|
+
* @param {string} requestScope The run's data space.
|
|
34
|
+
* @param {DocumentMode} [mode] `retrieve` keeps values as listed.
|
|
35
|
+
* @returns {Promise<Array<NativeRelationshipDocument | VerbatimDocument>>} Documents ordered by name.
|
|
36
|
+
*/
|
|
37
|
+
export declare function retrieveUnlabelledRelationships(dmoName: string, client: Pick<RelationshipClient, 'list'>, requestScope: string, mode?: DocumentMode): Promise<Array<NativeRelationshipDocument | VerbatimDocument>>;
|
|
38
|
+
/**
|
|
39
|
+
* The names whose label a retrieve reads: those of the documents it can save (a verbatim file
|
|
40
|
+
* keeps the list entry as read and takes no label).
|
|
41
|
+
* @param {Array<NativeRelationshipDocument | VerbatimDocument>} documents Unlabelled documents.
|
|
42
|
+
* @returns {string[]} Relationship names.
|
|
43
|
+
*/
|
|
44
|
+
export declare function relationshipLabelNames(documents: Array<NativeRelationshipDocument | VerbatimDocument>): string[];
|
|
45
|
+
/**
|
|
46
|
+
* Put each read label into its saved document; the raw list entry each carries is kept.
|
|
47
|
+
* @param {Array<NativeRelationshipDocument | VerbatimDocument>} documents Unlabelled documents.
|
|
48
|
+
* @param {Map<string, string>} labels Label by relationship name.
|
|
49
|
+
* @returns {Array<NativeRelationshipDocument | VerbatimDocument>} Documents in the same order.
|
|
50
|
+
*/
|
|
51
|
+
export declare function labelRelationships(documents: Array<NativeRelationshipDocument | VerbatimDocument>, labels: Map<string, string>): Array<NativeRelationshipDocument | VerbatimDocument>;
|
|
52
|
+
/**
|
|
53
|
+
* The labels (Metadata API masterLabel) of relationships, read in batches over the bound
|
|
54
|
+
* Connection. Without a Metadata API reader, or for a name without a record, there is none.
|
|
55
|
+
* @param {string[]} names Relationship names.
|
|
56
|
+
* @param {Pick<RelationshipClient, 'readDefinitions'>} client Relationship reader.
|
|
57
|
+
* @returns {Promise<Map<string, string>>} Label by name.
|
|
58
|
+
*/
|
|
59
|
+
export declare function readLabels(names: string[], client: Pick<RelationshipClient, 'readDefinitions'>): Promise<Map<string, string>>;
|
|
60
|
+
export type NativeRelationshipDeployPlan = {
|
|
61
|
+
operation: 'create' | 'update' | 'noop';
|
|
62
|
+
desired: NativeRelationshipDocument;
|
|
63
|
+
/** Name of the org relationship the file means, when present. */
|
|
64
|
+
existing?: string;
|
|
65
|
+
/** Update only: the file keys whose values differ from the org. */
|
|
66
|
+
changes?: string[];
|
|
67
|
+
/** Update only: the whole Metadata API record to send. */
|
|
68
|
+
definition?: Record<string, unknown>;
|
|
69
|
+
/** Update only: the Metadata API record as the org held it before the update. */
|
|
70
|
+
current?: Record<string, unknown>;
|
|
71
|
+
};
|
|
72
|
+
/**
|
|
73
|
+
* Plan a relationship deployment read-only. The org relationship is the one named like the file
|
|
74
|
+
* (`identity.apiName`) in the source DMO's list, otherwise the one carrying the file's binding
|
|
75
|
+
* (Salesforce generates the name on create). Absent is a create; present with every value of the
|
|
76
|
+
* file (the label only when the file states one) is a no-op; otherwise an update of the whole
|
|
77
|
+
* Metadata API record with the file's values. Salesforce decides which changes it accepts.
|
|
78
|
+
* @param {unknown} value Portable relationship.
|
|
79
|
+
* @param {Pick<RelationshipClient, 'list' | 'readDefinitions'>} client Relationship reader.
|
|
80
|
+
* @param {string} requestScope The run's data space.
|
|
81
|
+
* @returns {Promise<NativeRelationshipDeployPlan>} Create, update or no-op.
|
|
82
|
+
*/
|
|
83
|
+
export declare function preflightNativeRelationship(value: unknown, client: Pick<RelationshipClient, 'list' | 'readDefinitions'>, requestScope: string): Promise<NativeRelationshipDeployPlan>;
|
|
84
|
+
/**
|
|
85
|
+
* Deploy one relationship: no-op when the org holds the file; an update of the whole Metadata API
|
|
86
|
+
* record when it differs; otherwise one POST, a readback of the source DMO's inventory until the
|
|
87
|
+
* binding is ACTIVE with the requested cardinality and owner, and an update of the label when the
|
|
88
|
+
* file states another one than Salesforce generated.
|
|
89
|
+
* @param {unknown} value Portable relationship.
|
|
90
|
+
* @param {RelationshipClient} client Relationship transport.
|
|
91
|
+
* @param {string} requestScope The run's data space.
|
|
92
|
+
* @returns {Promise<{operation: 'create' | 'update' | 'noop'; apiName: string}>} Confirmed outcome
|
|
93
|
+
* and org name.
|
|
94
|
+
*/
|
|
95
|
+
export declare function deployNativeRelationship(value: unknown, client: Pick<RelationshipClient, 'list' | 'create' | 'readDefinitions' | 'update'>, requestScope: string): Promise<{
|
|
96
|
+
operation: 'create' | 'update' | 'noop';
|
|
97
|
+
apiName: string;
|
|
98
|
+
}>;
|
|
99
|
+
/**
|
|
100
|
+
* Read-only: find a relationship by name among the relationships of its source DMO, ignoring
|
|
101
|
+
* letter case. No match is absence, as is the source DMO itself not existing (HTTP404
|
|
102
|
+
* ITEM_NOT_FOUND on its relationship list); two matches are refused.
|
|
103
|
+
* @param {string} sourceDmo Exact source DMO named in the selector.
|
|
104
|
+
* @param {string} name Relationship name named in the selector.
|
|
105
|
+
* @param {Pick<RelationshipClient, 'list'>} client Relationship reader.
|
|
106
|
+
* @param {string} requestScope The run's data space.
|
|
107
|
+
* @returns {Promise<NativeRelationshipDocument | undefined>} The relationship as a document.
|
|
108
|
+
*/
|
|
109
|
+
export declare function findNativeRelationshipByName(sourceDmo: string, name: string, client: Pick<RelationshipClient, 'list'>, requestScope: string): Promise<NativeRelationshipDocument | undefined>;
|
|
110
|
+
/**
|
|
111
|
+
* Qualify a relationship deletion read-only: the name must carry the document's binding.
|
|
112
|
+
* @param {string} apiName Exact relationship name.
|
|
113
|
+
* @param {unknown} value Local relationship file naming the source DMO.
|
|
114
|
+
* @param {Pick<RelationshipClient, 'list'>} client Relationship reader.
|
|
115
|
+
* @param {string} requestScope The run's data space.
|
|
116
|
+
* @returns {Promise<{present: boolean; document: NativeRelationshipDocument}>} Presence.
|
|
117
|
+
*/
|
|
118
|
+
export declare function preflightDeleteNativeRelationship(apiName: string, value: unknown, client: Pick<RelationshipClient, 'list'>, requestScope: string): Promise<{
|
|
119
|
+
present: boolean;
|
|
120
|
+
document: NativeRelationshipDocument;
|
|
121
|
+
}>;
|
|
122
|
+
/**
|
|
123
|
+
* Delete one relationship by name with a single DELETE, then confirm its absence by listing.
|
|
124
|
+
* @param {string} apiName Exact relationship name.
|
|
125
|
+
* @param {unknown} value Local relationship file naming the source DMO.
|
|
126
|
+
* @param {Pick<RelationshipClient, 'list' | 'remove'>} client Relationship transport.
|
|
127
|
+
* @param {string} requestScope The run's data space.
|
|
128
|
+
* @returns {Promise<{outcome: 'deleted' | 'unchanged'}>} Confirmed absence.
|
|
129
|
+
*/
|
|
130
|
+
export declare function deleteNativeRelationship(apiName: string, value: unknown, client: Pick<RelationshipClient, 'list' | 'remove'>, requestScope: string): Promise<{
|
|
131
|
+
outcome: 'deleted' | 'unchanged';
|
|
132
|
+
}>;
|
|
@@ -0,0 +1,512 @@
|
|
|
1
|
+
import { isDeepStrictEqual } from 'node:util';
|
|
2
|
+
import { refuseEscapedCopy } from './escaped-copy.js';
|
|
3
|
+
import { sameEnum } from '../artifacts/enum-case.js';
|
|
4
|
+
import { paceReadback, readbackPace } from './readback-pace.js';
|
|
5
|
+
import { explainSpaceMismatch, explanationOf, replaceExplanation } from './operation-diagnostic.js';
|
|
6
|
+
import { getConnectDiagnostic } from '../transport/connect-diagnostic.js';
|
|
7
|
+
import { NativeRelationshipError, nativeRelationshipName, normalizeNativeRelationship, observeNativeRelationship, relationshipDmoName, relationshipSpec, sameRelationshipBinding, validateNativeRelationshipDocument, } from '../artifacts/native-relationship-document.js';
|
|
8
|
+
import { isUnexpectedUpdateRefusal } from '../transport/relationship-rest.js';
|
|
9
|
+
import { carriedNativeResponse, carryNativeResponse, createVerbatimDocument, isVerbatimDocument, verbatimReason, } from '../artifacts/verbatim-document.js';
|
|
10
|
+
/** Upper bound of pages read for one DMO; far above any observed inventory. */
|
|
11
|
+
const maximumPages = 100;
|
|
12
|
+
/**
|
|
13
|
+
* Read every relationship the DMO is source or target of, paging by offset. `nextPageUrl` is not
|
|
14
|
+
* followed: Salesforce repeats the current offset in it. The collected count must equal the
|
|
15
|
+
* reported totalSize and names must be unique, otherwise the inventory is refused as incomplete.
|
|
16
|
+
* @param {string} dmoName Exact DMO name.
|
|
17
|
+
* @param {Pick<RelationshipClient, 'list'>} client Relationship reader.
|
|
18
|
+
* @param {DocumentMode} [mode] `retrieve` takes entries as listed; only text names count towards
|
|
19
|
+
* the uniqueness check.
|
|
20
|
+
* @returns {Promise<ObservedRelationship[]>} Complete, verified inventory (empty for 204).
|
|
21
|
+
*/
|
|
22
|
+
export async function listNativeRelationships(dmoName, client, mode = 'deploy') {
|
|
23
|
+
if (!relationshipDmoName.test(dmoName))
|
|
24
|
+
throw new NativeRelationshipError('INVALID_RELATIONSHIP_BINDING');
|
|
25
|
+
const collected = [];
|
|
26
|
+
let total;
|
|
27
|
+
for (let page = 0; page < maximumPages; page++) {
|
|
28
|
+
const response = await client.list(dmoName, collected.length);
|
|
29
|
+
if (response === undefined) {
|
|
30
|
+
// 204: the DMO has no relationship at all.
|
|
31
|
+
if (page === 0)
|
|
32
|
+
return [];
|
|
33
|
+
break;
|
|
34
|
+
}
|
|
35
|
+
if (!response || typeof response !== 'object' || Array.isArray(response))
|
|
36
|
+
throw new NativeRelationshipError('INVALID_RELATIONSHIP_RESPONSE');
|
|
37
|
+
const { relationships, totalSize } = response;
|
|
38
|
+
if (!Array.isArray(relationships) ||
|
|
39
|
+
typeof totalSize !== 'number' ||
|
|
40
|
+
!Number.isSafeInteger(totalSize) ||
|
|
41
|
+
totalSize < 0)
|
|
42
|
+
throw new NativeRelationshipError('INVALID_RELATIONSHIP_RESPONSE');
|
|
43
|
+
if (total !== undefined && total !== totalSize)
|
|
44
|
+
throw new NativeRelationshipError('RELATIONSHIP_INVENTORY_INCOMPLETE');
|
|
45
|
+
total = totalSize;
|
|
46
|
+
// Each observation keeps its raw list entry, for a verbatim file when it cannot be saved.
|
|
47
|
+
collected.push(...relationships.map((entry) => carryNativeResponse(observeNativeRelationship(entry, mode), entry)));
|
|
48
|
+
if (relationships.length === 0 || collected.length >= total)
|
|
49
|
+
break;
|
|
50
|
+
}
|
|
51
|
+
const named = collected.filter((entry) => typeof entry.name === 'string');
|
|
52
|
+
const names = new Set(named.map((entry) => entry.name));
|
|
53
|
+
if (collected.length !== total || names.size !== named.length)
|
|
54
|
+
throw new NativeRelationshipError('RELATIONSHIP_INVENTORY_INCOMPLETE');
|
|
55
|
+
return collected;
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* Retrieve every relationship whose source object is the DMO, as portable documents.
|
|
59
|
+
* @param {string} dmoName Exact source DMO.
|
|
60
|
+
* @param {Pick<RelationshipClient, 'list'>} client Relationship reader.
|
|
61
|
+
* @param {string} requestScope The run's data space.
|
|
62
|
+
* A relationship the document cannot represent is returned as a verbatim document of its raw list
|
|
63
|
+
* entry; each saved document carries its raw entry for the same fallback at write time.
|
|
64
|
+
* @param {DocumentMode} [mode] `retrieve` checks only the entries it keeps and keeps their values
|
|
65
|
+
* as listed.
|
|
66
|
+
* @returns {Promise<Array<NativeRelationshipDocument | VerbatimDocument>>} Documents ordered by name.
|
|
67
|
+
*/
|
|
68
|
+
export async function retrieveNativeRelationships(dmoName, client, requestScope, mode = 'deploy') {
|
|
69
|
+
const documents = await retrieveUnlabelledRelationships(dmoName, client, requestScope, mode);
|
|
70
|
+
return labelRelationships(documents, await readLabels(relationshipLabelNames(documents), client));
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* The relationships whose source object is the DMO, as {@link retrieveNativeRelationships}
|
|
74
|
+
* returns them but without their labels (no Metadata API read), so the labels of many DMOs can
|
|
75
|
+
* be read together.
|
|
76
|
+
* @param {string} dmoName Exact source DMO.
|
|
77
|
+
* @param {Pick<RelationshipClient, 'list'>} client Relationship reader.
|
|
78
|
+
* @param {string} requestScope The run's data space.
|
|
79
|
+
* @param {DocumentMode} [mode] `retrieve` keeps values as listed.
|
|
80
|
+
* @returns {Promise<Array<NativeRelationshipDocument | VerbatimDocument>>} Documents ordered by name.
|
|
81
|
+
*/
|
|
82
|
+
export async function retrieveUnlabelledRelationships(dmoName, client, requestScope, mode = 'deploy') {
|
|
83
|
+
const inventory = await listNativeRelationships(dmoName, client, mode);
|
|
84
|
+
const kept = inventory.filter((entry) => entry.sourceObjectName === dmoName);
|
|
85
|
+
const documents = kept.map((entry) => {
|
|
86
|
+
const native = carriedNativeResponse(entry);
|
|
87
|
+
try {
|
|
88
|
+
return carryNativeResponse(normalizeNativeRelationship(entryToWire(entry), requestScope, mode), native?.native);
|
|
89
|
+
}
|
|
90
|
+
catch (error) {
|
|
91
|
+
const reason = verbatimReason(error);
|
|
92
|
+
const verbatim = reason === undefined || native === undefined
|
|
93
|
+
? undefined
|
|
94
|
+
: createVerbatimDocument('relationship', { dataSpace: requestScope, apiName: entry.name }, reason, native.native);
|
|
95
|
+
if (verbatim === undefined)
|
|
96
|
+
throw error;
|
|
97
|
+
return verbatim;
|
|
98
|
+
}
|
|
99
|
+
});
|
|
100
|
+
// eslint-disable-next-line unicorn/no-array-sort -- Sort a fresh mapped array within the ES2022 library target.
|
|
101
|
+
return documents.sort((a, b) => a.identity.apiName.localeCompare(b.identity.apiName));
|
|
102
|
+
}
|
|
103
|
+
/**
|
|
104
|
+
* The names whose label a retrieve reads: those of the documents it can save (a verbatim file
|
|
105
|
+
* keeps the list entry as read and takes no label).
|
|
106
|
+
* @param {Array<NativeRelationshipDocument | VerbatimDocument>} documents Unlabelled documents.
|
|
107
|
+
* @returns {string[]} Relationship names.
|
|
108
|
+
*/
|
|
109
|
+
export function relationshipLabelNames(documents) {
|
|
110
|
+
return documents
|
|
111
|
+
.filter((document) => !isVerbatimDocument(document))
|
|
112
|
+
.map((document) => document.identity.apiName);
|
|
113
|
+
}
|
|
114
|
+
/**
|
|
115
|
+
* Put each read label into its saved document; the raw list entry each carries is kept.
|
|
116
|
+
* @param {Array<NativeRelationshipDocument | VerbatimDocument>} documents Unlabelled documents.
|
|
117
|
+
* @param {Map<string, string>} labels Label by relationship name.
|
|
118
|
+
* @returns {Array<NativeRelationshipDocument | VerbatimDocument>} Documents in the same order.
|
|
119
|
+
*/
|
|
120
|
+
export function labelRelationships(documents, labels) {
|
|
121
|
+
return documents.map((document) => {
|
|
122
|
+
if (isVerbatimDocument(document))
|
|
123
|
+
return document;
|
|
124
|
+
const label = labels.get(document.identity.apiName);
|
|
125
|
+
if (label === undefined)
|
|
126
|
+
return document;
|
|
127
|
+
return carryNativeResponse({ ...document, spec: { ...document.spec, label } }, carriedNativeResponse(document)?.native);
|
|
128
|
+
});
|
|
129
|
+
}
|
|
130
|
+
/**
|
|
131
|
+
* The labels (Metadata API masterLabel) of relationships, read in batches over the bound
|
|
132
|
+
* Connection. Without a Metadata API reader, or for a name without a record, there is none.
|
|
133
|
+
* @param {string[]} names Relationship names.
|
|
134
|
+
* @param {Pick<RelationshipClient, 'readDefinitions'>} client Relationship reader.
|
|
135
|
+
* @returns {Promise<Map<string, string>>} Label by name.
|
|
136
|
+
*/
|
|
137
|
+
export async function readLabels(names, client) {
|
|
138
|
+
const labels = new Map();
|
|
139
|
+
if (names.length === 0 || !client.readDefinitions)
|
|
140
|
+
return labels;
|
|
141
|
+
for (const definition of await client.readDefinitions(names))
|
|
142
|
+
if (typeof definition.masterLabel === 'string' && typeof definition.fullName === 'string')
|
|
143
|
+
labels.set(definition.fullName, definition.masterLabel);
|
|
144
|
+
return labels;
|
|
145
|
+
}
|
|
146
|
+
/**
|
|
147
|
+
* Rebuild the native entry shape from an observation, for the shared normaliser.
|
|
148
|
+
* @param {ObservedRelationship} entry Observation.
|
|
149
|
+
* @returns {object} Native-shaped entry.
|
|
150
|
+
*/
|
|
151
|
+
function entryToWire(entry) {
|
|
152
|
+
return {
|
|
153
|
+
name: entry.name,
|
|
154
|
+
status: entry.status,
|
|
155
|
+
cardinality: entry.cardinality,
|
|
156
|
+
owner: entry.relationshipOwner,
|
|
157
|
+
sourceObject: { name: entry.sourceObjectName },
|
|
158
|
+
sourceField: { name: entry.sourceFieldName },
|
|
159
|
+
targetObject: { name: entry.targetObjectName },
|
|
160
|
+
targetField: { name: entry.targetFieldName },
|
|
161
|
+
};
|
|
162
|
+
}
|
|
163
|
+
/**
|
|
164
|
+
* Admit a portable relationship for the request scope.
|
|
165
|
+
* @param {unknown} value Portable input.
|
|
166
|
+
* @param {string} requestScope The run's data space.
|
|
167
|
+
* @param {DocumentMode} [mode] `retrieve` for a delete: values the plugin does not model are kept.
|
|
168
|
+
* @returns {NativeRelationshipDocument} Checked document.
|
|
169
|
+
*/
|
|
170
|
+
function admit(value, requestScope, mode = 'deploy') {
|
|
171
|
+
const document = validateNativeRelationshipDocument(value, mode);
|
|
172
|
+
if (document.c__dataSpace !== requestScope)
|
|
173
|
+
throw explainSpaceMismatch(new NativeRelationshipError('UNSUPPORTED_RELATIONSHIP_DATASPACE'), document.identity.apiName, document.c__dataSpace, requestScope, mode === 'deploy');
|
|
174
|
+
return document;
|
|
175
|
+
}
|
|
176
|
+
/**
|
|
177
|
+
* Find the relationship carrying the document's binding in the source DMO's inventory.
|
|
178
|
+
* @param {NativeRelationshipDocument} desired Portable document.
|
|
179
|
+
* @param {ObservedRelationship[]} inventory Source DMO inventory.
|
|
180
|
+
* @returns {ObservedRelationship | undefined} Unique match or confirmed absence.
|
|
181
|
+
*/
|
|
182
|
+
function findBinding(desired, inventory) {
|
|
183
|
+
const matches = inventory.filter((entry) => sameRelationshipBinding(entry, desired.spec));
|
|
184
|
+
if (matches.length > 1)
|
|
185
|
+
throw new NativeRelationshipError('AMBIGUOUS_RELATIONSHIP_TARGET');
|
|
186
|
+
return matches[0];
|
|
187
|
+
}
|
|
188
|
+
/**
|
|
189
|
+
* Whether a Metadata API value is an explicit nil (`xsi:nil`), which a write must not send back.
|
|
190
|
+
* @param {unknown} value Record value.
|
|
191
|
+
* @returns {boolean} Explicit nil.
|
|
192
|
+
*/
|
|
193
|
+
function metadataNil(value) {
|
|
194
|
+
if (!value || typeof value !== 'object' || Array.isArray(value))
|
|
195
|
+
return false;
|
|
196
|
+
const attributes = value.$;
|
|
197
|
+
return (!!attributes &&
|
|
198
|
+
typeof attributes === 'object' &&
|
|
199
|
+
attributes['xsi:nil'] === 'true');
|
|
200
|
+
}
|
|
201
|
+
/**
|
|
202
|
+
* Read the Metadata API record of one relationship.
|
|
203
|
+
* @param {string} name Relationship name.
|
|
204
|
+
* @param {Pick<RelationshipClient, 'readDefinitions'>} client Relationship reader.
|
|
205
|
+
* @returns {Promise<Record<string, unknown>>} The record.
|
|
206
|
+
*/
|
|
207
|
+
async function readDefinition(name, client) {
|
|
208
|
+
if (!client.readDefinitions)
|
|
209
|
+
throw new NativeRelationshipError('RELATIONSHIP_REQUEST_UNCONFIRMED');
|
|
210
|
+
const definitions = await client.readDefinitions([name]);
|
|
211
|
+
const found = definitions.find((entry) => entry.fullName === name);
|
|
212
|
+
if (!found) {
|
|
213
|
+
const error = new NativeRelationshipError('RELATIONSHIP_REQUEST_UNCONFIRMED');
|
|
214
|
+
replaceExplanation(error, `The relationship ${name} is listed for its DMO, but the Metadata API returns no FieldSrcTrgtRelationship record of that name. Nothing was changed.`);
|
|
215
|
+
throw error;
|
|
216
|
+
}
|
|
217
|
+
return found;
|
|
218
|
+
}
|
|
219
|
+
/**
|
|
220
|
+
* The record to send: the org's record with the file's values, without explicit nil values.
|
|
221
|
+
* @param {Record<string, unknown>} current Org record.
|
|
222
|
+
* @param {NativeRelationshipDocument['spec']} spec File values.
|
|
223
|
+
* @returns {Record<string, unknown>} Whole record for `updateMetadata`.
|
|
224
|
+
*/
|
|
225
|
+
function updateDefinition(current, spec) {
|
|
226
|
+
const kept = Object.fromEntries(Object.entries(current).filter(([, value]) => !metadataNil(value)));
|
|
227
|
+
return {
|
|
228
|
+
...kept,
|
|
229
|
+
...(spec.label === undefined ? {} : { masterLabel: spec.label }),
|
|
230
|
+
owner: spec.relationshipOwner,
|
|
231
|
+
relationshipCardinality: spec.cardinality,
|
|
232
|
+
sourceFieldName: `${spec.sourceObjectName}.${spec.sourceFieldName}`,
|
|
233
|
+
targetFieldName: `${spec.targetObjectName}.${spec.targetFieldName}`,
|
|
234
|
+
};
|
|
235
|
+
}
|
|
236
|
+
/**
|
|
237
|
+
* The spec keys of the file whose values the listed relationship does not hold (label aside).
|
|
238
|
+
* @param {ObservedRelationship} found Listed relationship.
|
|
239
|
+
* @param {NativeRelationshipDocument['spec']} spec File values.
|
|
240
|
+
* @returns {string[]} Differing keys.
|
|
241
|
+
*/
|
|
242
|
+
function listedChanges(found, spec) {
|
|
243
|
+
const keys = [
|
|
244
|
+
'sourceObjectName',
|
|
245
|
+
'sourceFieldName',
|
|
246
|
+
'targetObjectName',
|
|
247
|
+
'targetFieldName',
|
|
248
|
+
'cardinality',
|
|
249
|
+
'relationshipOwner',
|
|
250
|
+
];
|
|
251
|
+
// Cardinality and owner are enum values; a case-only difference is no change.
|
|
252
|
+
return keys.filter((key) => key === 'cardinality' || key === 'relationshipOwner'
|
|
253
|
+
? !sameEnum(found[key], spec[key])
|
|
254
|
+
: found[key] !== spec[key]);
|
|
255
|
+
}
|
|
256
|
+
/**
|
|
257
|
+
* Plan a relationship deployment read-only. The org relationship is the one named like the file
|
|
258
|
+
* (`identity.apiName`) in the source DMO's list, otherwise the one carrying the file's binding
|
|
259
|
+
* (Salesforce generates the name on create). Absent is a create; present with every value of the
|
|
260
|
+
* file (the label only when the file states one) is a no-op; otherwise an update of the whole
|
|
261
|
+
* Metadata API record with the file's values. Salesforce decides which changes it accepts.
|
|
262
|
+
* @param {unknown} value Portable relationship.
|
|
263
|
+
* @param {Pick<RelationshipClient, 'list' | 'readDefinitions'>} client Relationship reader.
|
|
264
|
+
* @param {string} requestScope The run's data space.
|
|
265
|
+
* @returns {Promise<NativeRelationshipDeployPlan>} Create, update or no-op.
|
|
266
|
+
*/
|
|
267
|
+
export async function preflightNativeRelationship(value, client, requestScope) {
|
|
268
|
+
const desired = admit(value, requestScope);
|
|
269
|
+
const inventory = await listNativeRelationships(desired.spec.sourceObjectName, client);
|
|
270
|
+
const found = inventory.find((entry) => entry.name === desired.identity.apiName) ??
|
|
271
|
+
findBinding(desired, inventory);
|
|
272
|
+
if (!found)
|
|
273
|
+
return { operation: 'create', desired };
|
|
274
|
+
refuseEscapedCopy(desired.spec, found, (code) => new NativeRelationshipError(code));
|
|
275
|
+
const changes = listedChanges(found, desired.spec);
|
|
276
|
+
if (changes.length === 0 && desired.spec.label === undefined)
|
|
277
|
+
return { operation: 'noop', desired, existing: found.name };
|
|
278
|
+
const current = await readDefinition(found.name, client);
|
|
279
|
+
refuseEscapedCopy({ label: desired.spec.label }, { label: current.masterLabel }, (code) => new NativeRelationshipError(code));
|
|
280
|
+
if (desired.spec.label !== undefined && current.masterLabel !== desired.spec.label)
|
|
281
|
+
changes.push('label');
|
|
282
|
+
if (changes.length === 0)
|
|
283
|
+
return { operation: 'noop', desired, existing: found.name };
|
|
284
|
+
return {
|
|
285
|
+
operation: 'update',
|
|
286
|
+
desired,
|
|
287
|
+
existing: found.name,
|
|
288
|
+
changes,
|
|
289
|
+
definition: updateDefinition(current, desired.spec),
|
|
290
|
+
current,
|
|
291
|
+
};
|
|
292
|
+
}
|
|
293
|
+
/**
|
|
294
|
+
* Whether the relationship's Metadata API label is the file's (always true without one).
|
|
295
|
+
* @param {string} name Relationship name.
|
|
296
|
+
* @param {NativeRelationshipDocument} desired File.
|
|
297
|
+
* @param {Pick<RelationshipClient, 'readDefinitions'>} client Relationship reader.
|
|
298
|
+
* @returns {Promise<boolean>} Label confirmed.
|
|
299
|
+
*/
|
|
300
|
+
async function holdsLabel(name, desired, client) {
|
|
301
|
+
if (desired.spec.label === undefined)
|
|
302
|
+
return true;
|
|
303
|
+
const current = await readDefinition(name, client);
|
|
304
|
+
return current.masterLabel === desired.spec.label;
|
|
305
|
+
}
|
|
306
|
+
/**
|
|
307
|
+
* Send one update. When Salesforce answers its generic "unexpected error", re-read the record:
|
|
308
|
+
* if the org still holds it as before, send the same update once more (a valid change has
|
|
309
|
+
* succeeded on the second try, live API 68); never a third time, and never for another error.
|
|
310
|
+
* @param {string} name Relationship name.
|
|
311
|
+
* @param {Record<string, unknown>} definition Whole Metadata API record.
|
|
312
|
+
* @param {Record<string, unknown>} before The record as the org held it before the update.
|
|
313
|
+
* @param {Pick<RelationshipClient, 'readDefinitions' | 'update'>} client Transport.
|
|
314
|
+
*/
|
|
315
|
+
async function sendUpdate(name, definition, before, client) {
|
|
316
|
+
if (!client.update)
|
|
317
|
+
throw new NativeRelationshipError('RELATIONSHIP_REQUEST_UNCONFIRMED');
|
|
318
|
+
try {
|
|
319
|
+
await client.update(definition);
|
|
320
|
+
}
|
|
321
|
+
catch (error) {
|
|
322
|
+
if (!isUnexpectedUpdateRefusal(error))
|
|
323
|
+
throw error;
|
|
324
|
+
if (!isDeepStrictEqual(await readDefinition(name, client), before)) {
|
|
325
|
+
const changed = new NativeRelationshipError('RELATIONSHIP_PERSISTENCE_UNCONFIRMED');
|
|
326
|
+
replaceExplanation(changed, `Salesforce answered the update of the relationship ${name} with an unexpected error, but the relationship changed. Retrieve it before another attempt.`);
|
|
327
|
+
throw changed;
|
|
328
|
+
}
|
|
329
|
+
await client.update(definition);
|
|
330
|
+
}
|
|
331
|
+
}
|
|
332
|
+
/**
|
|
333
|
+
* Send one update (see sendUpdate) and read the relationship back by name until it holds the file.
|
|
334
|
+
* @param {string} name Relationship name.
|
|
335
|
+
* @param {Record<string, unknown>} definition Whole Metadata API record.
|
|
336
|
+
* @param {Record<string, unknown>} before The record as the org held it before the update.
|
|
337
|
+
* @param {NativeRelationshipDocument} desired File.
|
|
338
|
+
* @param {Pick<RelationshipClient, 'list' | 'readDefinitions' | 'update'>} client Transport.
|
|
339
|
+
*/
|
|
340
|
+
async function updateAndConfirm(name, definition, before, desired, client) {
|
|
341
|
+
await sendUpdate(name, definition, before, client);
|
|
342
|
+
for (let attempt = 0; attempt < readbackPace.attempts; attempt++) {
|
|
343
|
+
await paceReadback(attempt);
|
|
344
|
+
const inventory = await listNativeRelationships(desired.spec.sourceObjectName, client);
|
|
345
|
+
const found = inventory.find((entry) => entry.name === name);
|
|
346
|
+
if (found !== undefined &&
|
|
347
|
+
listedChanges(found, desired.spec).length === 0 &&
|
|
348
|
+
(await holdsLabel(name, desired, client)))
|
|
349
|
+
return;
|
|
350
|
+
}
|
|
351
|
+
const error = new NativeRelationshipError('RELATIONSHIP_PERSISTENCE_UNCONFIRMED');
|
|
352
|
+
replaceExplanation(error, `Salesforce accepted the update of the relationship ${name}, but it did not read back with the file's values in time. Retrieve it before another attempt.`);
|
|
353
|
+
throw error;
|
|
354
|
+
}
|
|
355
|
+
/**
|
|
356
|
+
* Deploy one relationship: no-op when the org holds the file; an update of the whole Metadata API
|
|
357
|
+
* record when it differs; otherwise one POST, a readback of the source DMO's inventory until the
|
|
358
|
+
* binding is ACTIVE with the requested cardinality and owner, and an update of the label when the
|
|
359
|
+
* file states another one than Salesforce generated.
|
|
360
|
+
* @param {unknown} value Portable relationship.
|
|
361
|
+
* @param {RelationshipClient} client Relationship transport.
|
|
362
|
+
* @param {string} requestScope The run's data space.
|
|
363
|
+
* @returns {Promise<{operation: 'create' | 'update' | 'noop'; apiName: string}>} Confirmed outcome
|
|
364
|
+
* and org name.
|
|
365
|
+
*/
|
|
366
|
+
export async function deployNativeRelationship(value, client, requestScope) {
|
|
367
|
+
const plan = await preflightNativeRelationship(value, client, requestScope);
|
|
368
|
+
if (plan.operation === 'noop')
|
|
369
|
+
return { operation: 'noop', apiName: plan.existing };
|
|
370
|
+
if (plan.operation === 'update') {
|
|
371
|
+
await updateAndConfirm(plan.existing, plan.definition, plan.current, plan.desired, client);
|
|
372
|
+
return { operation: 'update', apiName: plan.existing };
|
|
373
|
+
}
|
|
374
|
+
await client.create(relationshipSpec(plan.desired.spec));
|
|
375
|
+
for (let attempt = 0; attempt < readbackPace.attempts; attempt++) {
|
|
376
|
+
await paceReadback(attempt);
|
|
377
|
+
const found = findBinding(plan.desired, await listNativeRelationships(plan.desired.spec.sourceObjectName, client));
|
|
378
|
+
// Only CREATING may still turn ACTIVE; any other status is terminal.
|
|
379
|
+
if (found && found.status !== 'ACTIVE' && found.status !== 'CREATING') {
|
|
380
|
+
const error = new NativeRelationshipError('RELATIONSHIP_PERSISTENCE_UNCONFIRMED');
|
|
381
|
+
replaceExplanation(error, `The relationship ${found.name} was created but reads back with status ${found.status}, not ACTIVE. Inspect it before another attempt.`);
|
|
382
|
+
throw error;
|
|
383
|
+
}
|
|
384
|
+
if (found?.status === 'ACTIVE' &&
|
|
385
|
+
sameEnum(found.cardinality, plan.desired.spec.cardinality) &&
|
|
386
|
+
sameEnum(found.relationshipOwner, plan.desired.spec.relationshipOwner)) {
|
|
387
|
+
if (plan.desired.spec.label !== undefined)
|
|
388
|
+
await labelCreated(found.name, plan.desired, client);
|
|
389
|
+
return { operation: 'create', apiName: found.name };
|
|
390
|
+
}
|
|
391
|
+
}
|
|
392
|
+
throw new NativeRelationshipError('RELATIONSHIP_PERSISTENCE_UNCONFIRMED');
|
|
393
|
+
}
|
|
394
|
+
/**
|
|
395
|
+
* Set the file's label on a relationship just created: the create body has no label, and
|
|
396
|
+
* Salesforce names the label like the relationship.
|
|
397
|
+
* @param {string} name Created relationship.
|
|
398
|
+
* @param {NativeRelationshipDocument} desired File with a label.
|
|
399
|
+
* @param {Pick<RelationshipClient, 'list' | 'readDefinitions' | 'update'>} client Transport.
|
|
400
|
+
*/
|
|
401
|
+
async function labelCreated(name, desired, client) {
|
|
402
|
+
try {
|
|
403
|
+
const current = await readDefinition(name, client);
|
|
404
|
+
if (current.masterLabel === desired.spec.label)
|
|
405
|
+
return;
|
|
406
|
+
await updateAndConfirm(name, updateDefinition(current, desired.spec), current, desired, client);
|
|
407
|
+
}
|
|
408
|
+
catch (error) {
|
|
409
|
+
if (error instanceof NativeRelationshipError)
|
|
410
|
+
replaceExplanation(error, `The relationship ${name} was created, but its label was not set: ${explanationOf(error) ?? error.message}`);
|
|
411
|
+
throw error;
|
|
412
|
+
}
|
|
413
|
+
}
|
|
414
|
+
/**
|
|
415
|
+
* Read-only: find a relationship by name among the relationships of its source DMO, ignoring
|
|
416
|
+
* letter case. No match is absence, as is the source DMO itself not existing (HTTP404
|
|
417
|
+
* ITEM_NOT_FOUND on its relationship list); two matches are refused.
|
|
418
|
+
* @param {string} sourceDmo Exact source DMO named in the selector.
|
|
419
|
+
* @param {string} name Relationship name named in the selector.
|
|
420
|
+
* @param {Pick<RelationshipClient, 'list'>} client Relationship reader.
|
|
421
|
+
* @param {string} requestScope The run's data space.
|
|
422
|
+
* @returns {Promise<NativeRelationshipDocument | undefined>} The relationship as a document.
|
|
423
|
+
*/
|
|
424
|
+
export async function findNativeRelationshipByName(sourceDmo, name, client, requestScope) {
|
|
425
|
+
if (!nativeRelationshipName.test(name))
|
|
426
|
+
throw new NativeRelationshipError('INVALID_RELATIONSHIP_NAME');
|
|
427
|
+
let inventory;
|
|
428
|
+
try {
|
|
429
|
+
// Delete reads the inventory as retrieve does: unmodelled values never block it.
|
|
430
|
+
inventory = await listNativeRelationships(sourceDmo, client, 'retrieve');
|
|
431
|
+
}
|
|
432
|
+
catch (error) {
|
|
433
|
+
// A relationship cannot exist without its source DMO: only the list's exact HTTP404
|
|
434
|
+
// ITEM_NOT_FOUND for that DMO is absence; any other error still blocks.
|
|
435
|
+
const errors = getConnectDiagnostic(error)?.errors;
|
|
436
|
+
if (error instanceof NativeRelationshipError &&
|
|
437
|
+
error.code === 'RELATIONSHIP_HTTP_ERROR' &&
|
|
438
|
+
error.status === 404 &&
|
|
439
|
+
errors?.length === 1 &&
|
|
440
|
+
errors[0].code === 'ITEM_NOT_FOUND')
|
|
441
|
+
return undefined;
|
|
442
|
+
throw error;
|
|
443
|
+
}
|
|
444
|
+
const matches = inventory.filter((entry) => entry.name.toLowerCase() === name.toLowerCase());
|
|
445
|
+
if (matches.length > 1) {
|
|
446
|
+
const error = new NativeRelationshipError('AMBIGUOUS_RELATIONSHIP_TARGET');
|
|
447
|
+
replaceExplanation(error, `${sourceDmo} has ${matches.length} relationships named ${name} when letter case is ignored, so the name does not resolve to one relationship. Nothing was deleted.`);
|
|
448
|
+
throw error;
|
|
449
|
+
}
|
|
450
|
+
const [found] = matches;
|
|
451
|
+
if (found === undefined)
|
|
452
|
+
return undefined;
|
|
453
|
+
if (found.sourceObjectName !== sourceDmo) {
|
|
454
|
+
const error = new NativeRelationshipError('RELATIONSHIP_TARGET_BINDING_MISMATCH');
|
|
455
|
+
replaceExplanation(error, `The relationship ${found.name} has the source DMO ${found.sourceObjectName}, not ${sourceDmo}. Nothing was deleted; name its source DMO in the selector (relationship:${found.sourceObjectName}:${found.name}).`);
|
|
456
|
+
throw error;
|
|
457
|
+
}
|
|
458
|
+
return normalizeNativeRelationship(entryToWire(found), requestScope, 'retrieve');
|
|
459
|
+
}
|
|
460
|
+
/**
|
|
461
|
+
* Qualify a relationship deletion read-only: the name must carry the document's binding.
|
|
462
|
+
* @param {string} apiName Exact relationship name.
|
|
463
|
+
* @param {unknown} value Local relationship file naming the source DMO.
|
|
464
|
+
* @param {Pick<RelationshipClient, 'list'>} client Relationship reader.
|
|
465
|
+
* @param {string} requestScope The run's data space.
|
|
466
|
+
* @returns {Promise<{present: boolean; document: NativeRelationshipDocument}>} Presence.
|
|
467
|
+
*/
|
|
468
|
+
export async function preflightDeleteNativeRelationship(apiName, value, client, requestScope) {
|
|
469
|
+
if (!nativeRelationshipName.test(apiName))
|
|
470
|
+
throw new NativeRelationshipError('INVALID_RELATIONSHIP_NAME');
|
|
471
|
+
// A delete reads the file and the inventory as retrieve writes them: values the plugin does
|
|
472
|
+
// not model never block it; the name, data space and binding still must agree.
|
|
473
|
+
const document = admit(value, requestScope, 'retrieve');
|
|
474
|
+
if (document.identity.apiName !== apiName)
|
|
475
|
+
throw new NativeRelationshipError('RELATIONSHIP_FILE_IDENTITY_MISMATCH');
|
|
476
|
+
const inventory = await listNativeRelationships(document.spec.sourceObjectName, client, 'retrieve');
|
|
477
|
+
const found = inventory.find((entry) => entry.name === apiName);
|
|
478
|
+
if (!found) {
|
|
479
|
+
// The binding may exist under another generated name, e.g. created by a later deploy.
|
|
480
|
+
const other = findBinding(document, inventory);
|
|
481
|
+
if (other) {
|
|
482
|
+
const error = new NativeRelationshipError('RELATIONSHIP_NAME_MISMATCH');
|
|
483
|
+
replaceExplanation(error, `No relationship is named ${apiName}, but the org relationship ${other.name} carries the file's binding. It was not deleted. Retrieve it (sf data360 retrieve -m relationship:${document.spec.sourceObjectName}) and delete it by that name (sf data360 delete -m relationship:${document.spec.sourceObjectName}:${other.name}).`);
|
|
484
|
+
throw error;
|
|
485
|
+
}
|
|
486
|
+
return { present: false, document };
|
|
487
|
+
}
|
|
488
|
+
if (!sameRelationshipBinding(found, document.spec))
|
|
489
|
+
throw new NativeRelationshipError('RELATIONSHIP_TARGET_BINDING_MISMATCH');
|
|
490
|
+
return { present: true, document };
|
|
491
|
+
}
|
|
492
|
+
/**
|
|
493
|
+
* Delete one relationship by name with a single DELETE, then confirm its absence by listing.
|
|
494
|
+
* @param {string} apiName Exact relationship name.
|
|
495
|
+
* @param {unknown} value Local relationship file naming the source DMO.
|
|
496
|
+
* @param {Pick<RelationshipClient, 'list' | 'remove'>} client Relationship transport.
|
|
497
|
+
* @param {string} requestScope The run's data space.
|
|
498
|
+
* @returns {Promise<{outcome: 'deleted' | 'unchanged'}>} Confirmed absence.
|
|
499
|
+
*/
|
|
500
|
+
export async function deleteNativeRelationship(apiName, value, client, requestScope) {
|
|
501
|
+
const plan = await preflightDeleteNativeRelationship(apiName, value, client, requestScope);
|
|
502
|
+
if (!plan.present)
|
|
503
|
+
return { outcome: 'unchanged' };
|
|
504
|
+
await client.remove(apiName);
|
|
505
|
+
for (let attempt = 0; attempt < readbackPace.attempts; attempt++) {
|
|
506
|
+
await paceReadback(attempt);
|
|
507
|
+
const inventory = await listNativeRelationships(plan.document.spec.sourceObjectName, client, 'retrieve');
|
|
508
|
+
if (!inventory.some((entry) => entry.name === apiName))
|
|
509
|
+
return { outcome: 'deleted' };
|
|
510
|
+
}
|
|
511
|
+
throw new NativeRelationshipError('RELATIONSHIP_DELETION_UNCONFIRMED');
|
|
512
|
+
}
|