ParseRelation.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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 _defineProperty2 = _interopRequireDefault(require("@babel/runtime-corejs3/helpers/defineProperty"));
  10. var _CoreManager = _interopRequireDefault(require("./CoreManager"));
  11. /**
  12. * Creates a new Relation for the given parent object and key. This
  13. * constructor should rarely be used directly, but rather created by
  14. * Parse.Object.relation.
  15. *
  16. * <p>
  17. * A class that is used to access all of the children of a many-to-many
  18. * relationship. Each instance of Parse.Relation is associated with a
  19. * particular parent object and key.
  20. * </p>
  21. *
  22. * @alias Parse.Relation
  23. */
  24. class ParseRelation {
  25. /**
  26. * @param {Parse.Object} parent The parent of this relation.
  27. * @param {string} key The key for this relation on the parent.
  28. */
  29. constructor(parent, key) {
  30. (0, _defineProperty2.default)(this, "parent", void 0);
  31. (0, _defineProperty2.default)(this, "key", void 0);
  32. (0, _defineProperty2.default)(this, "targetClassName", void 0);
  33. this.parent = parent;
  34. this.key = key;
  35. this.targetClassName = null;
  36. }
  37. /*
  38. * Makes sure that this relation has the right parent and key.
  39. */
  40. _ensureParentAndKey(parent, key) {
  41. this.key = this.key || key;
  42. if (this.key !== key) {
  43. throw new Error('Internal Error. Relation retrieved from two different keys.');
  44. }
  45. if (this.parent) {
  46. if (this.parent.className !== parent.className) {
  47. throw new Error('Internal Error. Relation retrieved from two different Objects.');
  48. }
  49. if (this.parent.id) {
  50. if (this.parent.id !== parent.id) {
  51. throw new Error('Internal Error. Relation retrieved from two different Objects.');
  52. }
  53. } else if (parent.id) {
  54. this.parent = parent;
  55. }
  56. } else {
  57. this.parent = parent;
  58. }
  59. }
  60. /**
  61. * Adds a Parse.Object or an array of Parse.Objects to the relation.
  62. *
  63. * @param {(Parse.Object|Array)} objects The item or items to add.
  64. * @returns {Parse.Object} The parent of the relation.
  65. */
  66. add(objects) {
  67. if (!(0, _isArray.default)(objects)) {
  68. objects = [objects];
  69. }
  70. const {
  71. RelationOp
  72. } = _CoreManager.default.getParseOp();
  73. const change = new RelationOp(objects, []);
  74. const parent = this.parent;
  75. if (!parent) {
  76. throw new Error('Cannot add to a Relation without a parent');
  77. }
  78. if (objects.length === 0) {
  79. return parent;
  80. }
  81. parent.set(this.key, change);
  82. this.targetClassName = change._targetClassName;
  83. return parent;
  84. }
  85. /**
  86. * Removes a Parse.Object or an array of Parse.Objects from this relation.
  87. *
  88. * @param {(Parse.Object|Array)} objects The item or items to remove.
  89. */
  90. remove(objects) {
  91. if (!(0, _isArray.default)(objects)) {
  92. objects = [objects];
  93. }
  94. const {
  95. RelationOp
  96. } = _CoreManager.default.getParseOp();
  97. const change = new 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() {
  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() {
  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. const ParseQuery = _CoreManager.default.getParseQuery();
  131. if (!this.targetClassName) {
  132. query = new ParseQuery(parent.className);
  133. query._extraOptions.redirectClassNameForKey = this.key;
  134. } else {
  135. query = new ParseQuery(this.targetClassName);
  136. }
  137. query._addCondition('$relatedTo', 'object', {
  138. __type: 'Pointer',
  139. className: parent.className,
  140. objectId: parent.id
  141. });
  142. query._addCondition('$relatedTo', 'key', this.key);
  143. return query;
  144. }
  145. }
  146. var _default = exports.default = ParseRelation;