ParseRelation.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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. * @alias Parse.Relation
  35. */
  36. class ParseRelation {
  37. /*:: parent: ?ParseObject;*/
  38. /*:: key: ?string;*/
  39. /*:: targetClassName: ?string;*/
  40. /**
  41. * @param {Parse.Object} parent The parent of this relation.
  42. * @param {String} key The key for this relation on the parent.
  43. */
  44. constructor(parent
  45. /*: ?ParseObject*/
  46. , key
  47. /*: ?string*/
  48. ) {
  49. this.parent = parent;
  50. this.key = key;
  51. this.targetClassName = null;
  52. }
  53. /*
  54. * Makes sure that this relation has the right parent and key.
  55. */
  56. _ensureParentAndKey(parent
  57. /*: ParseObject*/
  58. , key
  59. /*: string*/
  60. ) {
  61. this.key = this.key || key;
  62. if (this.key !== key) {
  63. throw new Error('Internal Error. Relation retrieved from two different keys.');
  64. }
  65. if (this.parent) {
  66. if (this.parent.className !== parent.className) {
  67. throw new Error('Internal Error. Relation retrieved from two different Objects.');
  68. }
  69. if (this.parent.id) {
  70. if (this.parent.id !== parent.id) {
  71. throw new Error('Internal Error. Relation retrieved from two different Objects.');
  72. }
  73. } else if (parent.id) {
  74. this.parent = parent;
  75. }
  76. } else {
  77. this.parent = parent;
  78. }
  79. }
  80. /**
  81. * Adds a Parse.Object or an array of Parse.Objects to the relation.
  82. * @param {} objects The item or items to add.
  83. */
  84. add(objects
  85. /*: ParseObject | Array<ParseObject | string>*/
  86. )
  87. /*: ParseObject*/
  88. {
  89. if (!Array.isArray(objects)) {
  90. objects = [objects];
  91. }
  92. const change = new _ParseOp.RelationOp(objects, []);
  93. const parent = this.parent;
  94. if (!parent) {
  95. throw new Error('Cannot add to a Relation without a parent');
  96. }
  97. parent.set(this.key, change);
  98. this.targetClassName = change._targetClassName;
  99. return parent;
  100. }
  101. /**
  102. * Removes a Parse.Object or an array of Parse.Objects from this relation.
  103. * @param {} objects The item or items to remove.
  104. */
  105. remove(objects
  106. /*: ParseObject | Array<ParseObject | string>*/
  107. ) {
  108. if (!Array.isArray(objects)) {
  109. objects = [objects];
  110. }
  111. const change = new _ParseOp.RelationOp([], objects);
  112. if (!this.parent) {
  113. throw new Error('Cannot remove from a Relation without a parent');
  114. }
  115. this.parent.set(this.key, change);
  116. this.targetClassName = change._targetClassName;
  117. }
  118. /**
  119. * Returns a JSON version of the object suitable for saving to disk.
  120. * @return {Object}
  121. */
  122. toJSON()
  123. /*: { __type: 'Relation', className: ?string }*/
  124. {
  125. return {
  126. __type: 'Relation',
  127. className: this.targetClassName
  128. };
  129. }
  130. /**
  131. * Returns a Parse.Query that is limited to objects in this
  132. * relation.
  133. * @return {Parse.Query}
  134. */
  135. query()
  136. /*: ParseQuery*/
  137. {
  138. let query;
  139. const parent = this.parent;
  140. if (!parent) {
  141. throw new Error('Cannot construct a query for a Relation without a parent');
  142. }
  143. if (!this.targetClassName) {
  144. query = new _ParseQuery.default(parent.className);
  145. query._extraOptions.redirectClassNameForKey = this.key;
  146. } else {
  147. query = new _ParseQuery.default(this.targetClassName);
  148. }
  149. query._addCondition('$relatedTo', 'object', {
  150. __type: 'Pointer',
  151. className: parent.className,
  152. objectId: parent.id
  153. });
  154. query._addCondition('$relatedTo', 'key', this.key);
  155. return query;
  156. }
  157. }
  158. var _default = ParseRelation;
  159. exports.default = _default;