npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

node-sf-bulk2

v0.0.23

Published

Node.js implementation of Salesforce Bulk API 2.0

Downloads

6,099

Readme

Node-SF-BULK2

This is a node library to work with the Salesforce Bulk API 2.0.

You can use this library in combination with Salesforce CLI or any other library (like jsforce or @salesforce/sf-core) that handles authentication.

TypeScript compatible

The library is built using TypeScript 4.0 and provides typescript definitions to make it easier to use in Node.js projects using TypeScript

Installation

npm install node-sf-bulk2

Usage

See the API Documentation

Example usage

See examples folder for Typescript and JavaScript sample code on how to use this library

TypeScript example using jsforce for Bulk Query

The below code shows how to use the library to submit bulk query request

import jsforce from 'jsforce';
import { BulkAPI2 } from 'node-sf-bulk2';
import { BulkAPI2Connection, QueryInput } from 'node-sf-bulk2';

async function submitBulkQueryJob() {
    if (process.env.username && process.env.password) {
        const conn = new jsforce.Connection({});
        await conn.login(process.env.username, process.env.password);
        const bulkconnect: BulkAPI2Connection = {
            'accessToken': conn.accessToken,
            'apiVersion': '51.0',
            'instanceUrl': conn.instanceUrl
        };
        try {
            const bulkapi2 = new BulkAPI2(bulkconnect);
            const queryInput: QueryInput = {
                'query': 'Select Id from Account',
                'operation': 'query'
            };
            const response = await bulkapi2.submitBulkQueryJob(queryInput);
            return response;
        } catch (ex) {
            console.log(ex.response.data[0].errorCode);
            console.log(ex.response.data[0].message);
        }
    } else {
        throw 'set environment variable with your orgs username and password'
    }
}
// submit bulk query request
submitBulkQueryJob();

JavaScript example using jsforce for Bulk Query

The below code shows how to use the library to submit bulk query request using node.js (uses commonjs modules)

const jsforce = require('jsforce');
const sfbulk = require('node-sf-bulk2');

async function submitBulkQueryJob() {
    if (process.env.username && process.env.password) {
        const conn = new jsforce.Connection({});
        await conn.login(process.env.username, process.env.password);
        const bulkconnect = {
            'accessToken': conn.accessToken,
            'apiVersion': '51.0',
            'instanceUrl': conn.instanceUrl
        };
        try {
            const bulkapi2 = new sfbulk.BulkAPI2(bulkconnect);
            const queryInput = {
                'query': 'Select Id from Account',
                'operation': 'query'
            };
            const response = await bulkapi2.submitBulkQueryJob(queryInput);
            console.log(response);
        } catch (ex) {
            console.log(ex);
        }
    } else {
        throw 'set environment variable with your orgs username and password'
    }
}
// submit bulk query request
submitBulkQueryJob();

TypeScript example for uploading data from local CSV file

This assumes you have local file account.csv in project workspace

import jsforce from 'jsforce';
import { BulkAPI2 } from 'node-sf-bulk2';
import { BulkAPI2Connection, JobUploadRequest, JobUploadResponse, OPERATION, STATE } from 'node-sf-bulk2';
import * as fs from 'fs';
import { promisify } from "util";

class BulkInsert {
    async createDataUploadJob(bulkapi2: BulkAPI2): Promise<JobUploadResponse | undefined> {
        const jobRequest: JobUploadRequest = {
            'object': 'Account',
            'operation': OPERATION[0]
        };
        const response: JobUploadResponse = await bulkapi2.createDataUploadJob(jobRequest);
        return response;
    }
}
// anonymous function uploading data in CSV format to Salesforce using Bulk V2
 
(async () => {
    if (process.env.username && process.env.password) {
        // establish jsforce connection
        const conn = new jsforce.Connection({});
        await conn.login(process.env.username, process.env.password);
        // create a bulk connection object using jsforce connection
        const bulkconnect: BulkAPI2Connection = {
            'accessToken': conn.accessToken,
            'apiVersion': '51.0',
            'instanceUrl': conn.instanceUrl
        };
        try {
            // create a new BulkAPI2 class
            const bulkrequest = new BulkAPI2(bulkconnect);
            // create a bulk insert job
            const response: JobUploadResponse | undefined = await (new BulkInsert().createDataUploadJob(bulkrequest));
            if (response) {
                // read csv data from the local file system
                const data = await promisify(fs.readFile)(process.cwd() + "/account.csv", "UTF-8");
                const status: number = await bulkrequest.uploadJobData(response.contentUrl, data);
                if (status === 201) {
                    // close the job for processing
                    await bulkrequest.closeOrAbortJob(response.id, STATE[1]);
                }
            }
        } catch (ex) {
            console.log(ex.response.data[0].errorCode);
            console.log(ex.response.data[0].message);
        }
    } else {
        throw 'set environment variable with your orgs username and password'
    }
})();

JavaScript example for uploading data from local CSV file

This assumes you have local file account.csv in project workspace


const jsforce = require('jsforce');
const sfbulk = require('node-sf-bulk2');
const util = require('util');
const fs = require('fs');

(async () => {
    if (process.env.username && process.env.password) {
        const conn = new jsforce.Connection({});
        await conn.login(process.env.username, process.env.password);
        const bulkconnect = {
            'accessToken': conn.accessToken,
            'apiVersion': '51.0',
            'instanceUrl': conn.instanceUrl
        };
        try {
            // create a new BulkAPI2 class
            const bulkrequest = new sfbulk.BulkAPI2(bulkconnect);
            // create a bulk insert job
            const jobRequest = {
                'object': 'Account',
                'operation': 'insert'
            };
            const response = await bulkrequest.createDataUploadJob(jobRequest);
            if (response.id) {
                // read csv data from the local file system
                const data = await util.promisify(fs.readFile)(process.cwd() + "/account.csv", "UTF-8");
                const status = await bulkrequest.uploadJobData(response.contentUrl, data);
                if (status === 201) {
                    // close the job for processing
                    await bulkrequest.closeOrAbortJob(response.id, 'UploadComplete');
                }
            }
        } catch (ex) {
            console.log(ex);
        }
    } else {
        throw 'set environment variable with your orgs username and password'
    }
})();