/// /// /// /** * @file Manages Salesforce Bulk API related operations * @author Shinichi Tomita */ import { EventEmitter } from 'events'; import { Duplex, Readable, Writable } from 'stream'; import Connection from '../connection'; import { Serializable, Parsable } from '../record-stream'; import { Logger } from '../util/logger'; import { HttpMethods, Record, Schema } from '../types'; export type BulkOperation = 'insert' | 'update' | 'upsert' | 'delete' | 'hardDelete' | 'query' | 'queryAll'; export type BulkOptions = { extIdField?: string; concurrencyMode?: 'Serial' | 'Parallel'; assignmentRuleId?: string; }; export type JobState = 'Open' | 'Closed' | 'Aborted' | 'Failed' | 'Unknown' | 'NotProcessed'; export type JobInfo = { id: string; object: string; operation: BulkOperation; state: JobState; createdById: string; createdDate: string; systemModstamp: string; concurrencyMode: string; contentType: string; numberBatchesQueued: string; numberBatchesInProgress: string; numberBatchesCompleted: string; numberBatchesFailed: string; numberBatchesTotal: string; numberRecordsProcessed: string; numberRetries: string; apiVersion: string; numberRecordsFailed: string; totalProcessingTime: string; apiActiveProcessingTime: string; apexProcessingTime: string; }; export type BatchState = 'Queued' | 'InProgress' | 'Completed' | 'Failed' | 'NotProcessed'; export type BatchInfo = { id: string; jobId: string; state: BatchState; stateMessage: string; numberRecordsProcessed: string; numberRecordsFailed: string; totalProcessingTime: string; }; export type BulkQueryBatchResult = Array<{ id: string; batchId: string; jobId: string; }>; export type BulkIngestBatchResult = Array<{ id: string | null; success: boolean; created: boolean; errors: string[]; }>; export type BatchResult = Opr extends 'query' | 'queryAll' ? BulkQueryBatchResult : BulkIngestBatchResult; export type BulkRequest = { method: HttpMethods; path?: string; body?: string; headers?: { [name: string]: string; }; responseType?: string; }; /** * Class for Bulk API Job */ export declare class Job extends EventEmitter { type: string | null; operation: Opr | null; options: BulkOptions; id: string | null; state: JobState; _bulk: Bulk; _batches: { [id: string]: Batch; }; _jobInfo: Promise | undefined; _error: Error | undefined; /** * */ constructor(bulk: Bulk, type: string | null, operation: Opr | null, options: BulkOptions | null, jobId?: string); /** * Return latest jobInfo from cache */ info(): Promise; /** * Open new job and get jobinfo */ open(): Promise; /** * Create a new batch instance in the job */ createBatch(): Batch; /** * Get a batch instance specified by given batch ID */ batch(batchId: string): Batch; /** * Check the latest job status from server */ check(): Promise; /** * Wait till the job is assigned to server */ ready(): Promise; /** * List all registered batch info in job */ list(): Promise; /** * Close opened job */ close(): Promise; /** * Set the status to abort */ abort(): Promise; /** * @private */ _changeState(state: JobState): Promise; } /** * Batch (extends Writable) */ export declare class Batch extends Writable { job: Job; id: string | undefined; _bulk: Bulk; _uploadStream: Serializable; _downloadStream: Parsable; _dataStream: Duplex; _result: Promise> | undefined; _error: Error | undefined; /** * */ constructor(job: Job, id?: string); /** * Connect batch API and create stream instance of request/response * * @private */ _createRequestStream(): Duplex; /** * Implementation of Writable */ _write(record_: Record, enc: BufferEncoding, cb: () => void): void; /** * Returns duplex stream which accepts CSV data input and batch result output */ stream(): Duplex; /** * Execute batch operation */ execute(input?: string | Record[] | Readable): this; run: (input?: string | Record[] | Readable) => this; exec: (input?: string | Record[] | Readable) => this; /** * Promise/A+ interface * Delegate to promise, return promise instance for batch result */ then(onResolved: (res: BatchResult) => void, onReject: (err: any) => void): Promise; /** * Check the latest batch status in server */ check(): Promise; /** * Polling the batch result and retrieve */ poll(interval: number, timeout: number): void; /** * Retrieve batch result */ retrieve(): Promise; /** * Fetch query batch result as a record stream * * @param {String} resultId - Result id * @returns {RecordStream} - Record stream, convertible to CSV data stream */ result(resultId: string): Parsable; } /** * Class for Bulk API * * @class */ export declare class Bulk { private readonly _conn; readonly _logger: Logger; /** * Polling interval in milliseconds * * Default: 1000 (1 second) */ pollInterval: number; /** * Polling timeout in milliseconds * * Default: 30000 (30 seconds) */ pollTimeout: number; /** * */ constructor(conn: Connection); /** * */ _request(request_: BulkRequest): import("../types").StreamPromise; /** * Create and start bulkload job and batch * * This method will return a Batch instance (writable stream) * which you can write records into as a CSV string. * * Batch also implements the a promise interface so you can `await` this method to get the job results. * * @example * // Insert an array of records and get the job results * * const res = await connection.bulk.load('Account', 'insert', accounts) * * @example * // Insert records from a csv file using the returned batch stream * * const csvFile = fs.createReadStream('accounts.csv') * * const batch = conn.bulk.load('Account', 'insert') * * // The `response` event is emitted when the job results are retrieved * batch.on('response', res => { * console.log(res) * }) * csvFile.pipe(batch.stream()) * * */ load(type: string, operation: Opr, input?: Record[] | Readable | string): Batch; load(type: string, operation: Opr, optionsOrInput?: BulkOptions | Record[] | Readable | string, input?: Record[] | Readable | string): Batch; /** * Execute bulk query and get record stream */ query(soql: string): Promise>; /** * Create a new job instance */ createJob(type: string, operation: Opr, options?: BulkOptions): Job; /** * Get a job instance specified by given job ID * * @param {String} jobId - Job ID * @returns {Bulk~Job} */ job(jobId: string): Job; } export default Bulk;