ParseRelation.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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. /**
  15. * Copyright (c) 2015-present, Parse, LLC.
  16. * All rights reserved.
  17. *
  18. * This source code is licensed under the BSD-style license found in the
  19. * LICENSE file in the root directory of this source tree. An additional grant
  20. * of patent rights can be found in the PATENTS file in the same directory.
  21. *
  22. * @flow
  23. */
  24. /**
  25. * Creates a new Relation for the given parent object and key. This
  26. * constructor should rarely be used directly, but rather created by
  27. * Parse.Object.relation.
  28. *
  29. * <p>
  30. * A class that is used to access all of the children of a many-to-many
  31. * relationship. Each instance of Parse.Relation is associated with a
  32. * particular parent object and key.
  33. * </p>
  34. *
  35. * @alias Parse.Relation
  36. */
  37. class ParseRelation {
  38. /*:: parent: ?ParseObject;*/
  39. /*:: key: ?string;*/
  40. /*:: targetClassName: ?string;*/
  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. this.parent = parent;
  51. this.key = key;
  52. this.targetClassName = null;
  53. }
  54. /*
  55. * Makes sure that this relation has the right parent and key.
  56. */
  57. _ensureParentAndKey(parent
  58. /*: ParseObject*/
  59. , key
  60. /*: string*/
  61. ) {
  62. this.key = this.key || key;
  63. if (this.key !== key) {
  64. throw new Error('Internal Error. Relation retrieved from two different keys.');
  65. }
  66. if (this.parent) {
  67. if (this.parent.className !== parent.className) {
  68. throw new Error('Internal Error. Relation retrieved from two different Objects.');
  69. }
  70. if (this.parent.id) {
  71. if (this.parent.id !== parent.id) {
  72. throw new Error('Internal Error. Relation retrieved from two different Objects.');
  73. }
  74. } else if (parent.id) {
  75. this.parent = parent;
  76. }
  77. } else {
  78. this.parent = parent;
  79. }
  80. }
  81. /**
  82. * Adds a Parse.Object or an array of Parse.Objects to the relation.
  83. *
  84. * @param {(Parse.Object|Array)} objects The item or items to add.
  85. * @returns {Parse.Object} The parent of the relation.
  86. */
  87. add(objects
  88. /*: ParseObject | Array<ParseObject | string>*/
  89. )
  90. /*: ParseObject*/
  91. {
  92. if (!Array.isArray(objects)) {
  93. objects = [objects];
  94. }
  95. const change = new _ParseOp.RelationOp(objects, []);
  96. const parent = this.parent;
  97. if (!parent) {
  98. throw new Error('Cannot add to a Relation without a parent');
  99. }
  100. if (objects.length === 0) {
  101. return parent;
  102. }
  103. parent.set(this.key, change);
  104. this.targetClassName = change._targetClassName;
  105. return parent;
  106. }
  107. /**
  108. * Removes a Parse.Object or an array of Parse.Objects from this relation.
  109. *
  110. * @param {(Parse.Object|Array)} objects The item or items to remove.
  111. */
  112. remove(objects
  113. /*: ParseObject | Array<ParseObject | string>*/
  114. ) {
  115. if (!Array.isArray(objects)) {
  116. objects = [objects];
  117. }
  118. const change = new _ParseOp.RelationOp([], objects);
  119. if (!this.parent) {
  120. throw new Error('Cannot remove from a Relation without a parent');
  121. }
  122. if (objects.length === 0) {
  123. return;
  124. }
  125. this.parent.set(this.key, change);
  126. this.targetClassName = change._targetClassName;
  127. }
  128. /**
  129. * Returns a JSON version of the object suitable for saving to disk.
  130. *
  131. * @returns {object} JSON representation of Relation
  132. */
  133. toJSON()
  134. /*: { __type: 'Relation', className: ?string }*/
  135. {
  136. return {
  137. __type: 'Relation',
  138. className: this.targetClassName
  139. };
  140. }
  141. /**
  142. * Returns a Parse.Query that is limited to objects in this
  143. * relation.
  144. *
  145. * @returns {Parse.Query} Relation Query
  146. */
  147. query()
  148. /*: ParseQuery*/
  149. {
  150. let query;
  151. const parent = this.parent;
  152. if (!parent) {
  153. throw new Error('Cannot construct a query for a Relation without a parent');
  154. }
  155. if (!this.targetClassName) {
  156. query = new _ParseQuery.default(parent.className);
  157. query._extraOptions.redirectClassNameForKey = this.key;
  158. } else {
  159. query = new _ParseQuery.default(this.targetClassName);
  160. }
  161. query._addCondition('$relatedTo', 'object', {
  162. __type: 'Pointer',
  163. className: parent.className,
  164. objectId: parent.id
  165. });
  166. query._addCondition('$relatedTo', 'key', this.key);
  167. return query;
  168. }
  169. }
  170. var _default = ParseRelation;
  171. exports.default = _default;