generated from salesforcecli/lerna-template
-
Notifications
You must be signed in to change notification settings - Fork 14
/
upsert.ts
91 lines (81 loc) · 2.95 KB
/
upsert.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
/*
* 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 fs from 'node:fs';
import { Messages } from '@salesforce/core';
import { Flags, SfCommand, Ux } from '@salesforce/sf-plugins-core';
import { orgFlags } from '../../../../flags.js';
import { Batcher, BatcherReturnType } from '../../../../batcher.js';
import { validateSobjectType } from '../../../../bulkUtils.js';
Messages.importMessagesDirectoryFromMetaUrl(import.meta.url);
const messages = Messages.loadMessages('@salesforce/plugin-data', 'bulk.upsert');
export default class Upsert extends SfCommand<BatcherReturnType> {
public static readonly summary = messages.getMessage('summary');
public static readonly description = messages.getMessage('description');
public static readonly examples = messages.getMessages('examples');
public static readonly flags = {
...orgFlags,
'external-id': Flags.string({
char: 'i',
summary: messages.getMessage('flags.external-id.summary'),
required: true,
aliases: ['externalid'],
deprecateAliases: true,
}),
file: Flags.file({
exists: true,
char: 'f',
summary: messages.getMessage('flags.file.summary'),
required: true,
aliases: ['csvfile'],
deprecateAliases: true,
}),
sobject: Flags.string({
char: 's',
summary: messages.getMessage('flags.sobject.summary'),
required: true,
aliases: ['sobjecttype'],
deprecateAliases: true,
}),
wait: Flags.duration({
char: 'w',
unit: 'minutes',
summary: messages.getMessage('flags.wait.summary'),
min: 0,
defaultValue: 0,
}),
serial: Flags.boolean({
char: 'r',
summary: messages.getMessage('flags.serial.summary'),
default: false,
}),
};
public async run(): Promise<BatcherReturnType> {
const { flags } = await this.parse(Upsert);
const conn = flags['target-org'].getConnection(flags['api-version']);
this.spinner.start('Bulk Upsert');
const sobject = await validateSobjectType(flags.sobject, conn);
const batcher = new Batcher(conn, new Ux({ jsonEnabled: this.jsonEnabled() }));
const csvStream = fs.createReadStream(flags.file, { encoding: 'utf-8' });
const job = conn.bulk.createJob(sobject, 'upsert', {
extIdField: flags['external-id'],
concurrencyMode: flags.serial ? 'Serial' : 'Parallel',
});
// eslint-disable-next-line @typescript-eslint/no-misused-promises,no-async-promise-executor
return new Promise(async (resolve, reject) => {
job.on('error', (err): void => {
reject(err);
});
try {
resolve(await batcher.createAndExecuteBatches(job, csvStream, sobject, flags.wait?.minutes));
this.spinner.stop();
} catch (e) {
this.spinner.stop('error');
reject(e);
}
});
}
}