ParseRelation.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = void 0;
  6. var _ParseOp = require("./ParseOp");
  7. var _ParseObject = _interopRequireDefault(require("./ParseObject"));
  8. var _ParseQuery = _interopRequireDefault(require("./ParseQuery"));
  9. function _interopRequireDefault(obj) {
  10. return obj && obj.__esModule ? obj : {
  11. default: obj
  12. };
  13. }
  14. function _defineProperty(obj, key, value) {
  15. if (key in obj) {
  16. Object.defineProperty(obj, key, {
  17. value: value,
  18. enumerable: true,
  19. configurable: true,
  20. writable: true
  21. });
  22. } else {
  23. obj[key] = value;
  24. }
  25. return obj;
  26. }
  27. /**
  28. * Creates a new Relation for the given parent object and key. This
  29. * constructor should rarely be used directly, but rather created by
  30. * Parse.Object.relation.
  31. *
  32. * <p>
  33. * A class that is used to access all of the children of a many-to-many
  34. * relationship. Each instance of Parse.Relation is associated with a
  35. * particular parent object and key.
  36. * </p>
  37. *
  38. * @alias Parse.Relation
  39. */
  40. class ParseRelation {
  41. /**
  42. * @param {Parse.Object} parent The parent of this relation.
  43. * @param {string} key The key for this relation on the parent.
  44. */
  45. constructor(parent
  46. /*: ?ParseObject*/
  47. , key
  48. /*: ?string*/
  49. ) {
  50. _defineProperty(this, "parent", void 0);
  51. _defineProperty(this, "key", void 0);
  52. _defineProperty(this, "targetClassName", void 0);
  53. this.parent = parent;
  54. this.key = key;
  55. this.targetClassName = null;
  56. }
  57. /*
  58. * Makes sure that this relation has the right parent and key.
  59. */
  60. _ensureParentAndKey(parent
  61. /*: ParseObject*/
  62. , key
  63. /*: string*/
  64. ) {
  65. this.key = this.key || key;
  66. if (this.key !== key) {
  67. throw new Error('Internal Error. Relation retrieved from two different keys.');
  68. }
  69. if (this.parent) {
  70. if (this.parent.className !== parent.className) {
  71. throw new Error('Internal Error. Relation retrieved from two different Objects.');
  72. }
  73. if (this.parent.id) {
  74. if (this.parent.id !== parent.id) {
  75. throw new Error('Internal Error. Relation retrieved from two different Objects.');
  76. }
  77. } else if (parent.id) {
  78. this.parent = parent;
  79. }
  80. } else {
  81. this.parent = parent;
  82. }
  83. }
  84. /**
  85. * Adds a Parse.Object or an array of Parse.Objects to the relation.
  86. *
  87. * @param {(Parse.Object|Array)} objects The item or items to add.
  88. * @returns {Parse.Object} The parent of the relation.
  89. */
  90. add(objects
  91. /*: ParseObject | Array<ParseObject | string>*/
  92. )
  93. /*: ParseObject*/
  94. {
  95. if (!Array.isArray(objects)) {
  96. objects = [objects];
  97. }
  98. const change = new _ParseOp.RelationOp(objects, []);
  99. const {
  100. parent
  101. } = this;
  102. if (!parent) {
  103. throw new Error('Cannot add to a Relation without a parent');
  104. }
  105. if (objects.length === 0) {
  106. return parent;
  107. }
  108. parent.set(this.key, change);
  109. this.targetClassName = change._targetClassName;
  110. return parent;
  111. }
  112. /**
  113. * Removes a Parse.Object or an array of Parse.Objects from this relation.
  114. *
  115. * @param {(Parse.Object|Array)} objects The item or items to remove.
  116. */
  117. remove(objects
  118. /*: ParseObject | Array<ParseObject | string>*/
  119. ) {
  120. if (!Array.isArray(objects)) {
  121. objects = [objects];
  122. }
  123. const change = new _ParseOp.RelationOp([], objects);
  124. if (!this.parent) {
  125. throw new Error('Cannot remove from a Relation without a parent');
  126. }
  127. if (objects.length === 0) {
  128. return;
  129. }
  130. this.parent.set(this.key, change);
  131. this.targetClassName = change._targetClassName;
  132. }
  133. /**
  134. * Returns a JSON version of the object suitable for saving to disk.
  135. *
  136. * @returns {object} JSON representation of Relation
  137. */
  138. toJSON()
  139. /*: { __type: 'Relation', className: ?string }*/
  140. {
  141. return {
  142. __type: 'Relation',
  143. className: this.targetClassName
  144. };
  145. }
  146. /**
  147. * Returns a Parse.Query that is limited to objects in this
  148. * relation.
  149. *
  150. * @returns {Parse.Query} Relation Query
  151. */
  152. query()
  153. /*: ParseQuery*/
  154. {
  155. let query;
  156. const {
  157. parent
  158. } = this;
  159. if (!parent) {
  160. throw new Error('Cannot construct a query for a Relation without a parent');
  161. }
  162. if (!this.targetClassName) {
  163. query = new _ParseQuery.default(parent.className);
  164. query._extraOptions.redirectClassNameForKey = this.key;
  165. } else {
  166. query = new _ParseQuery.default(this.targetClassName);
  167. }
  168. query._addCondition('$relatedTo', 'object', {
  169. __type: 'Pointer',
  170. className: parent.className,
  171. objectId: parent.id
  172. });
  173. query._addCondition('$relatedTo', 'key', this.key);
  174. return query;
  175. }
  176. }
  177. var _default = ParseRelation;
  178. exports.default = _default;