ParseRelation.js 5.8 KB

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