Source: api/tooling.js

  1. /**
  2. * @file Manages Tooling APIs
  3. * @author Shinichi Tomita <shinichi.tomita@gmail.com>
  4. */
  5. 'use strict';
  6. var jsforce = require('../core'),
  7. _ = require('lodash/core'),
  8. Cache = require('../cache');
  9. /**
  10. * API class for Tooling API call
  11. *
  12. * @class
  13. * @param {Connection} conn - Connection
  14. */
  15. var Tooling = function(conn) {
  16. this._conn = conn;
  17. this._logger = conn._logger;
  18. var delegates = [
  19. "query",
  20. "queryMore",
  21. "create",
  22. "insert",
  23. "retrieve",
  24. "update",
  25. "upsert",
  26. "del",
  27. "delete",
  28. "destroy",
  29. "describe",
  30. "describeGlobal",
  31. "sobject"
  32. ];
  33. delegates.forEach(function(method) {
  34. this[method] = conn.constructor.prototype[method];
  35. }, this);
  36. this.cache = new Cache();
  37. var cacheOptions = {
  38. key: function(type) { return type ? "describe." + type : "describe"; }
  39. };
  40. this.describe$ = this.cache.makeCacheable(this.describe, this, cacheOptions);
  41. this.describe = this.cache.makeResponseCacheable(this.describe, this, cacheOptions);
  42. this.describeSObject$ = this.describe$;
  43. this.describeSObject = this.describe;
  44. cacheOptions = { key: 'describeGlobal' };
  45. this.describeGlobal$ = this.cache.makeCacheable(this.describeGlobal, this, cacheOptions);
  46. this.describeGlobal = this.cache.makeResponseCacheable(this.describeGlobal, this, cacheOptions);
  47. this.initialize();
  48. };
  49. /**
  50. * Initialize tooling API
  51. * @protected
  52. */
  53. Tooling.prototype.initialize = function() {
  54. this.sobjects = {};
  55. this.cache.clear();
  56. this.cache.get('describeGlobal').on('value', _.bind(function(res) {
  57. if (res.result) {
  58. var types = _.map(res.result.sobjects, function(so) { return so.name; });
  59. types.forEach(this.sobject, this);
  60. }
  61. }, this));
  62. };
  63. /**
  64. * @private
  65. */
  66. Tooling.prototype._baseUrl = function() {
  67. return this._conn._baseUrl() + "/tooling";
  68. };
  69. /**
  70. * @private
  71. */
  72. Tooling.prototype.request = function() {
  73. return this._conn.request.apply(this._conn, arguments);
  74. };
  75. /**
  76. * Execute query by using SOQL
  77. *
  78. * @param {String} soql - SOQL string
  79. * @param {Callback.<QueryResult>} [callback] - Callback function
  80. * @returns {Query.<QueryResult>}
  81. */
  82. /**
  83. * Query next record set by using query locator
  84. *
  85. * @method Tooling#query
  86. * @param {String} locator - Next record set locator
  87. * @param {Callback.<QueryResult>} [callback] - Callback function
  88. * @returns {Query.<QueryResult>}
  89. */
  90. /**
  91. * Retrieve specified records
  92. *
  93. * @method Tooling#queryMore
  94. * @param {String} type - SObject Type
  95. * @param {String|Array.<String>} ids - A record ID or array of record IDs
  96. * @param {Callback.<Record|Array.<Record>>} [callback] - Callback function
  97. * @returns {Promise.<Record|Array.<Record>>}
  98. */
  99. /**
  100. * Synonym of Tooling#create()
  101. *
  102. * @method Tooling#insert
  103. * @param {String} type - SObject Type
  104. * @param {Object|Array.<Object>} records - A record or array of records to create
  105. * @param {Callback.<RecordResult|Array.<RecordResult>>} [callback] - Callback function
  106. * @returns {Promise.<RecordResult|Array.<RecordResult>>}
  107. */
  108. /**
  109. * Create records
  110. *
  111. * @method Tooling#create
  112. * @param {String} type - SObject Type
  113. * @param {Record|Array.<Record>} records - A record or array of records to create
  114. * @param {Callback.<RecordResult|Array.<RecordResult>>} [callback] - Callback function
  115. * @returns {Promise.<RecordResult|Array.<RecordResult>>}
  116. */
  117. /**
  118. * Update records
  119. *
  120. * @method Tooling#update
  121. * @param {String} type - SObject Type
  122. * @param {Record|Array.<Record>} records - A record or array of records to update
  123. * @param {Callback.<RecordResult|Array.<RecordResult>>} [callback] - Callback function
  124. * @returns {Promise.<RecordResult|Array.<RecordResult>>}
  125. */
  126. /**
  127. * Upsert records
  128. *
  129. * @method Tooling#upsert
  130. * @param {String} type - SObject Type
  131. * @param {Record|Array.<Record>} records - Record or array of records to upsert
  132. * @param {String} extIdField - External ID field name
  133. * @param {Callback.<RecordResult|Array.<RecordResult>>} [callback] - Callback
  134. * @returns {Promise.<RecordResult|Array.<RecordResult>>}
  135. */
  136. /**
  137. * Synonym of Tooling#destroy()
  138. *
  139. * @method Tooling#delete
  140. * @param {String} type - SObject Type
  141. * @param {String|Array.<String>} ids - A ID or array of IDs to delete
  142. * @param {Callback.<RecordResult|Array.<RecordResult>>} [callback] - Callback
  143. * @returns {Promise.<RecordResult|Array.<RecordResult>>}
  144. */
  145. /**
  146. * Synonym of Tooling#destroy()
  147. *
  148. * @method Tooling#del
  149. * @param {String} type - SObject Type
  150. * @param {String|Array.<String>} ids - A ID or array of IDs to delete
  151. * @param {Callback.<RecordResult|Array.<RecordResult>>} [callback] - Callback
  152. * @returns {Promise.<RecordResult|Array.<RecordResult>>}
  153. */
  154. /**
  155. * Delete records
  156. *
  157. * @method Tooling#destroy
  158. * @param {String} type - SObject Type
  159. * @param {String|Array.<String>} ids - A ID or array of IDs to delete
  160. * @param {Callback.<RecordResult|Array.<RecordResult>>} [callback] - Callback
  161. * @returns {Promise.<RecordResult|Array.<RecordResult>>}
  162. */
  163. /**
  164. * Synonym of Tooling#describe()
  165. *
  166. * @method Tooling#describeSObject
  167. * @param {String} type - SObject Type
  168. * @param {Callback.<DescribeSObjectResult>} [callback] - Callback function
  169. * @returns {Promise.<DescribeSObjectResult>}
  170. */
  171. /**
  172. * Describe SObject metadata
  173. *
  174. * @method Tooling#describe
  175. * @param {String} type - SObject Type
  176. * @param {Callback.<DescribeSObjectResult>} [callback] - Callback function
  177. * @returns {Promise.<DescribeSObjectResult>}
  178. */
  179. /**
  180. * Describe global SObjects
  181. *
  182. * @method Tooling#describeGlobal
  183. * @param {Callback.<DescribeGlobalResult>} [callback] - Callback function
  184. * @returns {Promise.<DescribeGlobalResult>}
  185. */
  186. /**
  187. * Get SObject instance
  188. *
  189. * @method Tooling#sobject
  190. * @param {String} type - SObject Type
  191. * @returns {SObject}
  192. */
  193. /**
  194. * @typedef {Object} Tooling~ExecuteAnonymousResult
  195. * @prop {Boolean} compiled - Flag if the query is compiled successfully
  196. * @prop {String} compileProblem - Error reason in compilation
  197. * @prop {Boolean} success - Flag if the code is executed successfully
  198. * @prop {Number} line - Line number for the error
  199. * @prop {Number} column - Column number for the error
  200. * @prop {String} exceptionMessage - Exception message
  201. * @prop {String} exceptionStackTrace - Exception stack trace
  202. */
  203. /**
  204. * Executes Apex code anonymously
  205. *
  206. * @param {String} body - Anonymous Apex code
  207. * @param {Callback.<Tooling~ExecuteAnonymousResult>} [callback] - Callback function
  208. * @returns {Promise.<Tooling~ExecuteAnonymousResult>}
  209. */
  210. Tooling.prototype.executeAnonymous = function(body, callback) {
  211. var url = this._baseUrl() + "/executeAnonymous?anonymousBody=" + encodeURIComponent(body);
  212. return this.request(url).thenCall(callback);
  213. };
  214. /**
  215. * Executes Apex tests asynchronously
  216. *
  217. * @param {Array.<String>} classids - Comma separated list of class IDs
  218. * @param {Callback.<Tooling~ExecuteAnonymousResult>} [callback] - Callback function
  219. * @returns {Promise.<Tooling~ExecuteAnonymousResult>}
  220. */
  221. Tooling.prototype.runTestsAsynchronous = function(classids, callback) {
  222. var url = this._baseUrl() + "/runTestsAsynchronous/?classids=" + classids.join(',');
  223. return this.request(url).thenCall(callback);
  224. };
  225. /**
  226. * @typedef {Object} Tooling~CompletionsResult
  227. * @prop {Object} publicDeclarations
  228. */
  229. /**
  230. * Retrieves available code completions of the referenced type
  231. *
  232. * @param {String} [type] - completion type (default 'apex')
  233. * @param {Callback.<Tooling~CompletionsResult>} [callback] - Callback function
  234. * @returns {Promise.<Tooling~CompletionsResult>}
  235. */
  236. Tooling.prototype.completions = function(type, callback) {
  237. if (!_.isString(type)) {
  238. callback = type;
  239. type = 'apex';
  240. }
  241. var url = this._baseUrl() + "/completions?type=" + encodeURIComponent(type);
  242. return this.request(url).thenCall(callback);
  243. };
  244. /*--------------------------------------------*/
  245. /*
  246. * Register hook in connection instantiation for dynamically adding this API module features
  247. */
  248. jsforce.on('connection:new', function(conn) {
  249. conn.tooling = new Tooling(conn);
  250. });
  251. module.exports = Tooling;