From 3dec6f206e027f2331cc1ffb9037ac3b50996a3f Mon Sep 17 00:00:00 2001 From: Shinichi Tomita Date: Fri, 31 Jan 2014 02:55:01 +0900 Subject: [PATCH] change README and add notice of rename --- README.md | 1355 +---------------------------------------------------- 1 file changed, 1 insertion(+), 1354 deletions(-) diff --git a/README.md b/README.md index c835d24..3221e96 100644 --- a/README.md +++ b/README.md @@ -1,1356 +1,3 @@ # node-salesforce -Salesforce API Connection Library for Node.js Applications - -[![Build Status](https://secure.travis-ci.org/stomita/node-salesforce.png?branch=travis-ci)](http://travis-ci.org/stomita/node-salesforce) - -## Abstract - -Node-salesforce, which is designed to be a wrapper of Salesforce REST API in Node.js, enables Salesforce application development in event-driven style. -It capsulates the access to REST API end point in asynchronous JavaScript function call. -You can use both OAuth2 authorization scheme and SOAP API login for API authentication. - - -## Install - -If you are using node-salesforce as an API library in your Node.js project : - -
-  $ npm install node-salesforce
-
- -If you want to utilize node-salesforce REPL (interactive API console) in tty: - -
-  $ npm install node-salesforce -g
-
- -If you want to get the latest from GitHub : - -
-  $ git clone git://github.com/stomita/node-salesforce.git 
-  $ cd node-salesforce
-  $ npm link
-
- - -## API Usage - -### Connection - -#### Username and Password Login - -When you have Salesforce username and password (and maybe security token if required), you can use `Connection#login(username, password)` to establish connection to Salesforce. - -By default, it uses SOAP login API (so no OAuth2 client information is required). - -```javascript -var sf = require('node-salesforce'); -var conn = new sf.Connection({ - // you can change loginUrl to connect to sandbox or prerelease env. - // loginUrl : 'https://test.salesforce.com' -}); -conn.login(username, password, function(err, userInfo) { - if (err) { return console.error(err); } - // Now you can get the access token and instance URL information. - // Save them to establish connection next time. - console.log(conn.accessToken); - console.log(conn.instanceUrl); - // logged in user property - console.log("User ID: " + userInfo.id); - console.log("Org ID: " + userInfo.organizationId); - // ... -}); -``` - -#### Username and Password Login (OAuth2 Resource Owner Password Credential) - -When OAuth2 client information is given, `Connection#login(username, password)` uses OAuth2 Resource Owner Password Credential flow to login to Salesforce. - -```javascript -var sf = require('node-salesforce'); -var conn = new sf.Connection({ - oauth2 : { - // you can change loginUrl to connect to sandbox or prerelease env. - // loginUrl : 'https://test.salesforce.com', - clientId : '', - clientSecret : '', - redirectUri : '' - } -}); -conn.login(username, password, function(err, userInfo) { - if (err) { return console.error(err); } - // Now you can get the access token and instance URL information. - // Save them to establish connection next time. - console.log(conn.accessToken); - console.log(conn.instanceUrl); - // logged in user property - console.log("User ID: " + userInfo.id); - console.log("Org ID: " + userInfo.organizationId); - // ... -}); -``` - -#### Session ID - -If Salesforce session ID and its server URL information is passed from Salesforce (from 'Custom Link' or something), you can pass it to constructor. - - -```javascript -var sf = require('node-salesforce'); -var conn = new sf.Connection({ - serverUrl : '', - sessionId : '' -}); -``` - -#### Access Token - -After the login API call or OAuth2 authorization, you can get Salesforce access token and its instance URL. Next time you can use them to establish connection. - -```javascript -var sf = require('node-salesforce'); -var conn = new sf.Connection({ - instanceUrl : '', - accessToken : '' -}); -``` - -#### Access Token with Refresh Token - -If refresh token is given in constructor, the connection will automatically refresh access token when it has expired - -NOTE: Refresh token is only available for OAuth2 authorization code flow. - -```javascript -var sf = require('node-salesforce'); -var conn = new sf.Connection({ - oauth2 : { - clientId : '', - clientSecret : '', - redirectUri : '' - }, - instanceUrl : '', - accessToken : '', - refreshToken : '' -}); -conn.on("refresh", function(accessToken, res) { - // Refresh event will be fired when renewed access token - // to store it in your storage for next request -}); -``` - - -#### Logout - -`Connection#logout()` to logout from server and invalidate current session. Currently this method only works for SOAP API session. - -```javascript -var sf = require('node-salesforce'); -var conn = new sf.Connection({ - sessionId : '', - serverUrl : '' -}); -conn.logout(function(err) { - if (err) { return console.error(err); } - // now the session has been expired. -}); -``` - -### OAuth2 Authorization - -(Following examples are assuming running on express.js framework.) - -#### Authorization Request - -First, you should redirect user to Salesforce page to get authorized. You can get Salesforce authorization page URL by `OAuth2#getAuthorizationUrl(options)`. - -```javascript -var sf = require('node-salesforce'); -// -// OAuth2 client information can be shared with multiple connections. -// -var oauth2 = new sf.OAuth2({ - // you can change loginUrl to connect to sandbox or prerelease env. - // loginUrl : 'https://test.salesforce.com', - clientId : '', - clientSecret : '', - redirectUri : '' -}); -// -// Get authz url and redirect to it. -// -app.get('/oauth2/auth', function(req, res) { - res.redirect(oauth2.getAuthorizationUrl({ scope : 'api id web' })); -}); -``` - - -#### Access Token Request - -After the acceptance of authorization request, your app is callbacked from Salesforce with authorization code in URL parameter. Pass the code to `Connection#authorize(code)` and get access token. - -```javascript -// -// Pass received authz code and get access token -// -app.get('/oauth2/callback', function(req, res) { - var conn = new sf.Connection({ oauth2 : oauth2 }); - var code = req.param('code'); - conn.authorize(code, function(err, userInfo) { - if (err) { return console.error(err); } - // Now you can get the access token, refresh token, and instance URL information. - // Save them to establish connection next time. - console.log(conn.accessToken); - console.log(conn.refreshToken); - console.log(conn.instanceUrl); - console.log("User ID: " + userInfo.id); - console.log("Org ID: " + userInfo.organizationId); - // ... - }); -}); -``` - - -### Query - -#### Using SOQL - -By using `Connection#query(soql)`, you can achieve very basic SOQL query to fetch Salesforce records. - -```javascript -var records = []; -conn.query("SELECT Id, Name FROM Account", function(err, result) { - if (err) { return console.error(err); } - console.log("total : " + result.totalSize); - console.log("fetched : " + result.records.length); -}); -``` - -##### Callback Style - -There are two ways to retrieve the result records. - -As we have seen above, our package provides widely-used callback style API call for query execution. It returns one API call result in its callback. - -```javascript -var records = []; -conn.query("SELECT Id, Name FROM Account", function(err, result) { - if (err) { return console.error(err); } - console.log("total : " + result.totalSize); - console.log("fetched : " + result.records.length); - console.log("done ? : " + result.done); - if (!result.done) { - // you can use the locator to fetch next records set. - // Connection#queryMore() - console.log("next records URL : " + result.nextRecordsUrl); - } -}); -``` - -##### Event-Driven Style - -When a query is executed, it emits "record" event for each fetched record. By listening the event you can collect fetched records. - -If you want to fetch records exceeding the limit number of returning records per one query, you can use `autoFetch` option in `Query#execute(options)` (or its synonym `Query#exec(options)`, `Query#run(options)`) method. It is recommended to use `maxFetch` option also, if you have no idea how large the query result will become. - -When query is completed, `end` event will be fired. The `error` event occurs something wrong when doing query. - -```javascript -var records = []; -conn.query("SELECT Id, Name FROM Account") - .on("record", function(record) { - records.push(record); - }) - .on("end", function(query) { - console.log("total in database : " + query.totalSize); - console.log("total fetched : " + query.totalFetched); - }) - .on("error", function(err) { - console.error(err); - }) - .run({ autoFetch : true, maxFetch : 4000 }); // synonym of Query#execute(); -``` - -#### Using Query Method-Chain - -##### Basic Method Chaining - -By using `SObject#find(conditions, fields)`, you can do query in JSON-based condition expression (like MongoDB). By chaining other query construction methods, you can create a query programatically. - -```javascript -// -// Following query is equivalent to this SOQL -// -// "SELECT Id, Name, CreatedDate FROM Contact -// WHERE LastName LIKE 'A%' AND CreatedDate >= YESTERDAY AND Account.Name = 'Sony, Inc.' -// ORDER BY CreatedDate DESC, Name ASC -// LIMIT 5 OFFSET 10" -// -conn.sobject("Contact") - .find( - // conditions in JSON object - { LastName : { $like : 'A%' }, - CreatedDate: { $gte : sf.Date.YESTERDAY }, - 'Account.Name' : 'Sony, Inc.' }, - // fields in JSON object - { Id: 1, - Name: 1, - CreatedDate: 1 } - ) - .sort({ CreatedDate: -1, Name : 1 }) - .limit(5) - .skip(10) - .execute(function(err, records) { - if (err) { return console.error(err); } - console.log("fetched : " + records.length); - }); -``` - -Another representation of the query above. - -```javascript -conn.sobject("Contact") - .find({ - LastName : { $like : 'A%' }, - CreatedDate: { $gte : sf.Date.YESTERDAY }, - 'Account.Name' : 'Sony, Inc.' - }, - 'Id, Name, CreatedDate' // fields can be string of comma-separated field names - // or array of field names (e.g. [ 'Id', 'Name', 'CreatedDate' ]) - ) - .sort('-CreatedDate Name') // if "-" is prefixed to field name, considered as descending. - .limit(5) - .skip(10) - .execute(function(err, records) { - for (var i=0; i login("username@example.org", "mypassword123"); -{ id: '005xxxxxxxxxxxxxxx', - organizationId: '00Dyyyyyyyyyyyyyyy' } -> sobject('Account').find({}, "Id, Name").sort({ CreatedDate: 1}).limit(5); -[ { attributes: - { type: 'Account', - url: '/services/data/v28.0/sobjects/Account/001i0000009PyDrAAK' }, - Id: '001i0000009PyDrAAK', - Name: 'GenePoint' }, - { attributes: - { type: 'Account', - url: '/services/data/v28.0/sobjects/Account/001i0000009PyDsAAK' }, - Id: '001i0000009PyDsAAK', - Name: 'United Oil & Gas, UK' }, - { attributes: - { type: 'Account', - url: '/services/data/v28.0/sobjects/Account/001i0000009PyDtAAK' }, - Id: '001i0000009PyDtAAK', - Name: 'United Oil & Gas, Singapore' }, - { attributes: - { type: 'Account', - url: '/services/data/v28.0/sobjects/Account/001i0000009PyDuAAK' }, - Id: '001i0000009PyDuAAK', - Name: 'Edge Communications' }, - { attributes: - { type: 'Account', - url: '/services/data/v28.0/sobjects/Account/001i0000009PyDvAAK' }, - Id: '001i0000009PyDvAAK', - Name: 'Burlington Textiles Corp of America' } ] -> _[0].Name -'GenePoint' -> -``` - - -## Change History - -v0.8.0 (Jan 22, 2014): - -* Support Chatter API. - -* Support Metadata API. - - -v0.7.2 (Jan 16, 2014): - -* Removed unneeded files in npm-published package. - - -v0.7.1 (Dec 19, 2013): - -* Support SObject get updated/deleted. - - -v0.7.0 (Dec 11, 2013): - -* Support Analytics API and Tooling API. - -* Add Connection#queryAll to include deleted/archived records in query. - -* Add Connection#recent to fetch recently viewed record information. - -* Add RecordReference#blob(fieldName) to access blob in a record field. - -* Fix installation issue in Windows environment. - - -v0.6.4 (Dec 5, 2013): - -* Add Topic#unsubscribe for unsubscribing from a topic in Streaming API. - -v0.6.3 (Oct 31, 2013): - -* Fix issue in building query using $exists operator in SObject#find() - -v0.6.2 (Oct 15, 2013): - -* Change default Salesforce API ver. to 29.0 (Winter '14) - -* Fix issue in Connection#queryMore - -* Add identity URL information in the callback response of Connection#login/authorize. - - -v0.6.0 (Aug 23, 2013): - -* Change default Salesforce API ver. to 28.0 (Summer '13) - -* Add REPL interface for interactive API inspection. - -* Return Promises/A+ interface object for all async call. The interface is also added to Query / Batch. - -* Accept "*" in fields argument in `SObject#find()` to select all fields defined in SObject. - -* Add `Connection#describe$()`, `Connection#describeGlobal$()`, and `SObject#describe$()`, as caching versions of correspondings. - -* Changed `SObject#find(conditions, fields)` behavior in fields argument omission. - -* Add `SObject#select()` and `Query#where()` methods to construct a query in SQL-like verb. - -* Add `Query#update()` and `Query#destroy()` to apply bulk operation for queried records. - -* Add child relationship query support in `Query#include()` - -* Add Apex REST support. - -* Move streaming API methods from connection object to separated object. - -v0.5.1 (Jan 11, 2013): - -* Move Query#stream() method to RecordStream#stream() to support stream serialization even in filtered stream. - -v0.5.0 (Jan 11, 2013): - -* Support Bulk API for insert/update/upsert/delete/hardDelete operation (except for 'query'). - -* Refine Query#pipe to pipe to other output record stream (like bulk upload batch). - -* Add Query#stream() method to convert record stream to general node.js readable stream (generates CSV data). - - -v0.4.0 (Nov 05, 2012): - -* Support JSON-style query object to query records other than SOQL, inspired by MongoDB query interface. - -* Change default API version to 26.0 (Winter '13). - -* Return logged-in user info in the callback response of Connection#login() and Connection#authorize(). - -* Add Connection#logout() method to terminate session explicitly (Note: only useful for SOAP API login session). - - -v0.3.4 (Oct 19, 2012): - -* Fix issue to refresh access token multiple time in concurrent requests. - -* Change to use "Bearer", not "OAuth" in HTTP Authorization header to attach access token. - -* Separate oauth2 configuration into different hash object in connection constructor option - (old style is still supported for backward compatiblity). - - -v0.3.2 (Oct 18, 2012): - -* Fix error handling in access token refresh flow. - - -v0.3.1 (Jun 26, 2012): - -* Add support of Node.js 0.8.x. - - -v0.3.0 (May 10, 2012): - -* Support Salesforce Streaming API. - +Node-Salesforce is moved and renamed to JSforce (https://github.com/jsforce/jsforce). \ No newline at end of file