ejholmes/restforce — pull request #96 by theSteveMitchell (2014-02-05 20:30:46+00:00)
Title: Add select action to retreive only select fields from an object

I hope this is something that others can find useful. I need to retrieve only a couple of fields from a sobject in my project. I want to do this as efficiently as possible since I'm building out a REST api that depends on this query.

I like the elegant syntax that Restforce.find() give me, because I don't want to write soql queries. Yet the problem with find is that it always retrieves the whole sobject. In my case this is a very large table and takes much longer to return. So query is the fastest method for me.

Here is my solution. use the REST-ful api endpoint like find does, but passing in field specifications. I created a new method called "select" rather than modifying "find" to avoid breaking changes.

Benchmarks:
[1] pry(main)> client = Restforce.new
=> #
[2] pry(main)> Benchmark.measure{ client.query("select My_Custom_field__c from my__c where Account__c = \'123456789012345678\'") }
=> 0.080000 0.010000 0.090000 ( 1.296294)

[1] pry(main)> client = Restforce.new
[4] pry(main)> Benchmark.measure{ client.find_fields('my__c', "123456789012345678-env", ["Id", "My_Custom_field__c"], 'Parent_Id__c')}
=> 0.050000 0.010000 0.060000 ( 0.398572)