generated from salesforcecli/lerna-template
-
Notifications
You must be signed in to change notification settings - Fork 12
/
resume.ts
92 lines (83 loc) · 3.66 KB
/
resume.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
/*
* Copyright (c) 2020, salesforce.com, inc.
* All rights reserved.
* Licensed under the BSD 3-Clause license.
* For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause
*/
import { Messages } from '@salesforce/core';
import { QueryJobV2 } from '@jsforce/jsforce-node/lib/api/bulk2.js';
import type { Record as jsforceRecord } from '@jsforce/jsforce-node';
import {
Flags,
loglevel,
optionalOrgFlagWithDeprecations,
orgApiVersionFlagWithDeprecations,
SfCommand,
} from '@salesforce/sf-plugins-core';
import type { ResumeOptions } from '../../../types.js';
import { resultFormatFlag } from '../../../flags.js';
import { displayResults, transformBulkResults } from '../../../queryUtils.js';
import { BulkQueryRequestCache } from '../../../bulkDataRequestCache.js';
Messages.importMessagesDirectoryFromMetaUrl(import.meta.url);
const reportMessages = Messages.loadMessages('@salesforce/plugin-data', 'bulk.report');
// needed by the flags loaded from the other command
const queryMessages = Messages.loadMessages('@salesforce/plugin-data', 'bulk.resume.command');
export class BulkQueryReport extends SfCommand<unknown> {
public static readonly summary = reportMessages.getMessage('summary');
public static readonly description = reportMessages.getMessage('description');
public static readonly examples = reportMessages.getMessages('examples');
public static readonly aliases = ['force:data:soql:bulk:report'];
public static readonly deprecateAliases = true;
public static readonly flags = {
'target-org': { ...optionalOrgFlagWithDeprecations, summary: queryMessages.getMessage('flags.targetOrg.summary') },
'api-version': orgApiVersionFlagWithDeprecations,
loglevel,
'result-format': resultFormatFlag,
'bulk-query-id': Flags.salesforceId({
length: 18,
char: 'i',
startsWith: '750',
summary: reportMessages.getMessage('flags.bulkQueryId.summary'),
aliases: ['bulkqueryid'],
exactlyOne: ['bulk-query-id', 'use-most-recent'],
deprecateAliases: true,
}),
'use-most-recent': Flags.boolean({
summary: reportMessages.getMessage('flags.useMostRecent.summary'),
exactlyOne: ['bulk-query-id', 'use-most-recent'],
}),
};
public async run(): Promise<unknown> {
const [{ flags }, cache] = await Promise.all([this.parse(BulkQueryReport), BulkQueryRequestCache.create()]);
const resumeOptions = await cache.resolveResumeOptionsFromCache(
flags['bulk-query-id'],
flags['use-most-recent'],
flags['target-org'],
flags['api-version']
);
const job = new QueryJobV2(resumeOptions.options.connection, {
id: resumeOptions.jobInfo.id,
pollingOptions: getNonZeroTimeoutPollingOptions(resumeOptions.options.pollingOptions),
});
await job.poll();
const results = await job.result();
const queryResult = transformBulkResults((await results.toArray()) as jsforceRecord[], resumeOptions.options.query);
if (!this.jsonEnabled()) {
displayResults({ ...queryResult }, flags['result-format']);
}
if (queryResult.result.done) {
await BulkQueryRequestCache.unset(resumeOptions.jobInfo.id);
}
return queryResult.result;
}
}
/**
* polling options are retrieved from the cache.
* If the data:query used `--async` or `--wait` 0, we'd be passing that to the jsforce poll method,
* which means it would never check the actual result, and always throw a timeout error */
const getNonZeroTimeoutPollingOptions = (
pollingOptions: ResumeOptions['options']['pollingOptions']
): ResumeOptions['options']['pollingOptions'] => ({
...pollingOptions,
pollTimeout: Math.max(pollingOptions.pollTimeout, 1000),
});