Source: record.js

  1. /**
  2. * @file Represents Salesforce record information
  3. * @author Shinichi Tomita <shinichi.tomita@gmail.com>
  4. */
  5. 'use strict';
  6. var _ = require('lodash/core');
  7. /**
  8. * A simple hash object including record field information
  9. *
  10. * @typedef {Object} Record
  11. */
  12. /**
  13. * Remote reference to record information
  14. *
  15. * @protected
  16. * @class
  17. * @constructor
  18. * @param {Connection} conn - Connection object
  19. * @param {String} type - SObject type
  20. * @param {String} id - Record ID
  21. */
  22. var RecordReference = module.exports = function(conn, type, id) {
  23. this._conn = conn;
  24. this.type = type;
  25. this.id = id;
  26. };
  27. /**
  28. * Retrieve record field information
  29. *
  30. * @param {Object} [options] - Options for rest api.
  31. * @param {Callback.<Record>} [callback] - Callback function
  32. * @returns {Promise.<Record>}
  33. */
  34. RecordReference.prototype.retrieve = function(options, callback) {
  35. if (typeof options === 'function') {
  36. callback = options;
  37. options = {};
  38. }
  39. return this._conn.retrieve(this.type, this.id, options, callback);
  40. };
  41. /**
  42. * Update record field information
  43. *
  44. * @param {Record} record - A Record which includes fields to update
  45. * @param {Object} [options] - Options for rest api.
  46. * @param {Callback.<RecordResult>} [callback] - Callback function
  47. * @returns {Promise.<RecordResult>}
  48. */
  49. RecordReference.prototype.update = function(record, options, callback) {
  50. if (typeof options === 'function') {
  51. callback = options;
  52. options = {};
  53. }
  54. record = _.clone(record);
  55. record.Id = this.id;
  56. return this._conn.update(this.type, record, options, callback);
  57. };
  58. /**
  59. * Synonym of Record#destroy()
  60. *
  61. * @method RecordReference#delete
  62. * @param {Object} [options] - Options for rest api.
  63. * @param {Callback.<RecordResult>} [callback] - Callback function
  64. * @returns {Promise.<RecordResult>}
  65. */
  66. RecordReference.prototype["delete"] =
  67. /**
  68. * Synonym of Record#destroy()
  69. *
  70. * @method RecordReference#del
  71. * @param {Callback.<RecordResult>} [callback] - Callback function
  72. * @returns {Promise.<RecordResult>}
  73. */
  74. RecordReference.prototype.del =
  75. /**
  76. * Delete record field
  77. *
  78. * @method RecordReference#destroy
  79. * @param {Object} [options] - Options for rest api.
  80. * @param {Callback.<RecordResult>} [callback] - Callback function
  81. * @returns {Promise.<RecordResult>}
  82. */
  83. RecordReference.prototype.destroy = function(options, callback) {
  84. if (typeof options === 'function') {
  85. callback = options;
  86. options = {};
  87. }
  88. return this._conn.destroy(this.type, this.id, options, callback);
  89. };
  90. /**
  91. * Get blob field as stream
  92. *
  93. * @param {String} fieldName - Blob field name
  94. * @returns {stream.Stream}
  95. */
  96. RecordReference.prototype.blob = function(fieldName) {
  97. var url = [ this._conn._baseUrl(), 'sobjects', this.type, this.id, fieldName ].join('/');
  98. return this._conn.request(url).stream();
  99. };