ParseRelation.js 4.0 KB

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