ParseRelation.js 4.3 KB

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