ParseQuery.js 90 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404
  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 _regenerator = _interopRequireDefault(require("@babel/runtime-corejs3/regenerator"));
  9. var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime-corejs3/helpers/slicedToArray"));
  10. var _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime-corejs3/helpers/toConsumableArray"));
  11. var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime-corejs3/helpers/asyncToGenerator"));
  12. var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime-corejs3/helpers/classCallCheck"));
  13. var _createClass2 = _interopRequireDefault(require("@babel/runtime-corejs3/helpers/createClass"));
  14. var _defineProperty2 = _interopRequireDefault(require("@babel/runtime-corejs3/helpers/defineProperty"));
  15. var _typeof2 = _interopRequireDefault(require("@babel/runtime-corejs3/helpers/typeof"));
  16. var _forEach = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/instance/for-each"));
  17. var _indexOf = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/instance/index-of"));
  18. var _keys = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/object/keys"));
  19. var _slice = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/instance/slice"));
  20. var _map2 = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/instance/map"));
  21. var _filter2 = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/instance/filter"));
  22. var _keys2 = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/instance/keys"));
  23. var _concat = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/instance/concat"));
  24. var _includes = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/instance/includes"));
  25. var _sort = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/instance/sort"));
  26. var _splice = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/instance/splice"));
  27. var _promise = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/promise"));
  28. var _find = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/instance/find"));
  29. var _isArray = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/array/is-array"));
  30. var _entries = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/object/entries"));
  31. var _CoreManager = _interopRequireDefault(require("./CoreManager"));
  32. var _encode = _interopRequireDefault(require("./encode"));
  33. var _promiseUtils = require("./promiseUtils");
  34. var _ParseError = _interopRequireDefault(require("./ParseError"));
  35. var _ParseGeoPoint = _interopRequireDefault(require("./ParseGeoPoint"));
  36. var _ParseObject = _interopRequireDefault(require("./ParseObject"));
  37. var _OfflineQuery = _interopRequireDefault(require("./OfflineQuery"));
  38. var _LocalDatastoreUtils = require("./LocalDatastoreUtils");
  39. /*
  40. * @flow
  41. */
  42. /*:: import type LiveQuerySubscription from './LiveQuerySubscription';*/
  43. /*:: import type { RequestOptions, FullOptions } from './RESTController';*/
  44. /*:: type BatchOptions = FullOptions & { batchSize?: number };*/
  45. /*:: export type WhereClause = {
  46. [attr: string]: mixed,
  47. };*/
  48. /*:: export type QueryJSON = {
  49. where: WhereClause,
  50. watch?: string,
  51. include?: string,
  52. excludeKeys?: string,
  53. keys?: string,
  54. limit?: number,
  55. skip?: number,
  56. order?: string,
  57. className?: string,
  58. count?: number,
  59. hint?: mixed,
  60. explain?: boolean,
  61. readPreference?: string,
  62. includeReadPreference?: string,
  63. subqueryReadPreference?: string,
  64. };*/
  65. /**
  66. * Converts a string into a regex that matches it.
  67. * Surrounding with \Q .. \E does this, we just need to escape any \E's in
  68. * the text separately.
  69. *
  70. * @param s
  71. * @private
  72. * @returns {string}
  73. */
  74. function quote(s /*: string*/) /*: string*/{
  75. return '\\Q' + s.replace('\\E', '\\E\\\\E\\Q') + '\\E';
  76. }
  77. /**
  78. * Extracts the class name from queries. If not all queries have the same
  79. * class name an error will be thrown.
  80. *
  81. * @param queries
  82. * @private
  83. * @returns {string}
  84. */
  85. function _getClassNameFromQueries(queries /*: Array<ParseQuery>*/) /*: ?string*/{
  86. var className = null;
  87. (0, _forEach.default)(queries).call(queries, function (q) {
  88. if (!className) {
  89. className = q.className;
  90. }
  91. if (className !== q.className) {
  92. throw new Error('All queries must be for the same class.');
  93. }
  94. });
  95. return className;
  96. }
  97. /*
  98. * Handles pre-populating the result data of a query with select fields,
  99. * making sure that the data object contains keys for all objects that have
  100. * been requested with a select, so that our cached state updates correctly.
  101. */
  102. function handleSelectResult(data /*: any*/, select /*: Array<string>*/) {
  103. var serverDataMask = {};
  104. (0, _forEach.default)(select).call(select, function (field) {
  105. var hasSubObjectSelect = (0, _indexOf.default)(field).call(field, '.') !== -1;
  106. if (!hasSubObjectSelect && !data.hasOwnProperty(field)) {
  107. // this field was selected, but is missing from the retrieved data
  108. data[field] = undefined;
  109. } else if (hasSubObjectSelect) {
  110. // this field references a sub-object,
  111. // so we need to walk down the path components
  112. var pathComponents = field.split('.');
  113. var _obj = data;
  114. var serverMask = serverDataMask;
  115. (0, _forEach.default)(pathComponents).call(pathComponents, function (component, index, arr) {
  116. // add keys if the expected data is missing
  117. if (_obj && !_obj.hasOwnProperty(component)) {
  118. _obj[component] = undefined;
  119. }
  120. if (_obj && (0, _typeof2.default)(_obj) === 'object') {
  121. _obj = _obj[component];
  122. }
  123. //add this path component to the server mask so we can fill it in later if needed
  124. if (index < arr.length - 1) {
  125. if (!serverMask[component]) {
  126. serverMask[component] = {};
  127. }
  128. serverMask = serverMask[component];
  129. }
  130. });
  131. }
  132. });
  133. if ((0, _keys.default)(serverDataMask).length > 0) {
  134. // When selecting from sub-objects, we don't want to blow away the missing
  135. // information that we may have retrieved before. We've already added any
  136. // missing selected keys to sub-objects, but we still need to add in the
  137. // data for any previously retrieved sub-objects that were not selected.
  138. var serverData = _CoreManager.default.getObjectStateController().getServerData({
  139. id: data.objectId,
  140. className: data.className
  141. });
  142. copyMissingDataWithMask(serverData, data, serverDataMask, false);
  143. }
  144. }
  145. function copyMissingDataWithMask(src, dest, mask, copyThisLevel) {
  146. //copy missing elements at this level
  147. if (copyThisLevel) {
  148. for (var _key in src) {
  149. if (src.hasOwnProperty(_key) && !dest.hasOwnProperty(_key)) {
  150. dest[_key] = src[_key];
  151. }
  152. }
  153. }
  154. for (var _key2 in mask) {
  155. if (dest[_key2] !== undefined && dest[_key2] !== null && src !== undefined && src !== null) {
  156. //traverse into objects as needed
  157. copyMissingDataWithMask(src[_key2], dest[_key2], mask[_key2], true);
  158. }
  159. }
  160. }
  161. function handleOfflineSort(a, b, sorts) {
  162. var order = sorts[0];
  163. var operator = (0, _slice.default)(order).call(order, 0, 1);
  164. var isDescending = operator === '-';
  165. if (isDescending) {
  166. order = order.substring(1);
  167. }
  168. if (order === '_created_at') {
  169. order = 'createdAt';
  170. }
  171. if (order === '_updated_at') {
  172. order = 'updatedAt';
  173. }
  174. if (!/^[A-Za-z][0-9A-Za-z_]*$/.test(order) || order === 'password') {
  175. throw new _ParseError.default(_ParseError.default.INVALID_KEY_NAME, "Invalid Key: ".concat(order));
  176. }
  177. var field1 = a.get(order);
  178. var field2 = b.get(order);
  179. if (field1 < field2) {
  180. return isDescending ? 1 : -1;
  181. }
  182. if (field1 > field2) {
  183. return isDescending ? -1 : 1;
  184. }
  185. if (sorts.length > 1) {
  186. var remainingSorts = (0, _slice.default)(sorts).call(sorts, 1);
  187. return handleOfflineSort(a, b, remainingSorts);
  188. }
  189. return 0;
  190. }
  191. /**
  192. * Creates a new parse Parse.Query for the given Parse.Object subclass.
  193. *
  194. * <p>Parse.Query defines a query that is used to fetch Parse.Objects. The
  195. * most common use case is finding all objects that match a query through the
  196. * <code>find</code> method. for example, this sample code fetches all objects
  197. * of class <code>myclass</code>. it calls a different function depending on
  198. * whether the fetch succeeded or not.
  199. *
  200. * <pre>
  201. * var query = new Parse.Query(myclass);
  202. * query.find().then((results) => {
  203. * // results is an array of parse.object.
  204. * }).catch((error) => {
  205. * // error is an instance of parse.error.
  206. * });</pre></p>
  207. *
  208. * <p>a Parse.Query can also be used to retrieve a single object whose id is
  209. * known, through the get method. for example, this sample code fetches an
  210. * object of class <code>myclass</code> and id <code>myid</code>. it calls a
  211. * different function depending on whether the fetch succeeded or not.
  212. *
  213. * <pre>
  214. * var query = new Parse.Query(myclass);
  215. * query.get(myid).then((object) => {
  216. * // object is an instance of parse.object.
  217. * }).catch((error) => {
  218. * // error is an instance of parse.error.
  219. * });</pre></p>
  220. *
  221. * <p>a Parse.Query can also be used to count the number of objects that match
  222. * the query without retrieving all of those objects. for example, this
  223. * sample code counts the number of objects of the class <code>myclass</code>
  224. * <pre>
  225. * var query = new Parse.Query(myclass);
  226. * query.count().then((number) => {
  227. * // there are number instances of myclass.
  228. * }).catch((error) => {
  229. * // error is an instance of Parse.Error.
  230. * });</pre></p>
  231. *
  232. * @alias Parse.Query
  233. */
  234. var ParseQuery = /*#__PURE__*/function () {
  235. /**
  236. * @param {(string | Parse.Object)} objectClass An instance of a subclass of Parse.Object, or a Parse className string.
  237. */
  238. function ParseQuery(objectClass /*: string | ParseObject*/) {
  239. (0, _classCallCheck2.default)(this, ParseQuery);
  240. /**
  241. * @property {string} className
  242. */
  243. (0, _defineProperty2.default)(this, "className", void 0);
  244. (0, _defineProperty2.default)(this, "_where", void 0);
  245. (0, _defineProperty2.default)(this, "_watch", void 0);
  246. (0, _defineProperty2.default)(this, "_include", void 0);
  247. (0, _defineProperty2.default)(this, "_exclude", void 0);
  248. (0, _defineProperty2.default)(this, "_select", void 0);
  249. (0, _defineProperty2.default)(this, "_limit", void 0);
  250. (0, _defineProperty2.default)(this, "_skip", void 0);
  251. (0, _defineProperty2.default)(this, "_count", void 0);
  252. (0, _defineProperty2.default)(this, "_order", void 0);
  253. (0, _defineProperty2.default)(this, "_readPreference", void 0);
  254. (0, _defineProperty2.default)(this, "_includeReadPreference", void 0);
  255. (0, _defineProperty2.default)(this, "_subqueryReadPreference", void 0);
  256. (0, _defineProperty2.default)(this, "_queriesLocalDatastore", void 0);
  257. (0, _defineProperty2.default)(this, "_localDatastorePinName", void 0);
  258. (0, _defineProperty2.default)(this, "_extraOptions", void 0);
  259. (0, _defineProperty2.default)(this, "_hint", void 0);
  260. (0, _defineProperty2.default)(this, "_explain", void 0);
  261. (0, _defineProperty2.default)(this, "_xhrRequest", void 0);
  262. if (typeof objectClass === 'string') {
  263. if (objectClass === 'User' && _CoreManager.default.get('PERFORM_USER_REWRITE')) {
  264. this.className = '_User';
  265. } else {
  266. this.className = objectClass;
  267. }
  268. } else if (objectClass instanceof _ParseObject.default) {
  269. this.className = objectClass.className;
  270. } else if (typeof objectClass === 'function') {
  271. if (typeof objectClass.className === 'string') {
  272. this.className = objectClass.className;
  273. } else {
  274. var _obj2 = new objectClass();
  275. this.className = _obj2.className;
  276. }
  277. } else {
  278. throw new TypeError('A ParseQuery must be constructed with a ParseObject or class name.');
  279. }
  280. this._where = {};
  281. this._watch = [];
  282. this._include = [];
  283. this._exclude = [];
  284. this._count = false;
  285. this._limit = -1; // negative limit is not sent in the server request
  286. this._skip = 0;
  287. this._readPreference = null;
  288. this._includeReadPreference = null;
  289. this._subqueryReadPreference = null;
  290. this._queriesLocalDatastore = false;
  291. this._localDatastorePinName = null;
  292. this._extraOptions = {};
  293. this._xhrRequest = {
  294. task: null,
  295. onchange: function () {}
  296. };
  297. }
  298. /**
  299. * Adds constraint that at least one of the passed in queries matches.
  300. *
  301. * @param {Array} queries
  302. * @returns {Parse.Query} Returns the query, so you can chain this call.
  303. */
  304. (0, _createClass2.default)(ParseQuery, [{
  305. key: "_orQuery",
  306. value: function (queries /*: Array<ParseQuery>*/) /*: ParseQuery*/{
  307. var queryJSON = (0, _map2.default)(queries).call(queries, function (q) {
  308. return q.toJSON().where;
  309. });
  310. this._where.$or = queryJSON;
  311. return this;
  312. }
  313. /**
  314. * Adds constraint that all of the passed in queries match.
  315. *
  316. * @param {Array} queries
  317. * @returns {Parse.Query} Returns the query, so you can chain this call.
  318. */
  319. }, {
  320. key: "_andQuery",
  321. value: function (queries /*: Array<ParseQuery>*/) /*: ParseQuery*/{
  322. var queryJSON = (0, _map2.default)(queries).call(queries, function (q) {
  323. return q.toJSON().where;
  324. });
  325. this._where.$and = queryJSON;
  326. return this;
  327. }
  328. /**
  329. * Adds constraint that none of the passed in queries match.
  330. *
  331. * @param {Array} queries
  332. * @returns {Parse.Query} Returns the query, so you can chain this call.
  333. */
  334. }, {
  335. key: "_norQuery",
  336. value: function (queries /*: Array<ParseQuery>*/) /*: ParseQuery*/{
  337. var queryJSON = (0, _map2.default)(queries).call(queries, function (q) {
  338. return q.toJSON().where;
  339. });
  340. this._where.$nor = queryJSON;
  341. return this;
  342. }
  343. /**
  344. * Helper for condition queries
  345. *
  346. * @param key
  347. * @param condition
  348. * @param value
  349. * @returns {Parse.Query}
  350. */
  351. }, {
  352. key: "_addCondition",
  353. value: function (key /*: string*/, condition /*: string*/, value /*: mixed*/) /*: ParseQuery*/{
  354. if (!this._where[key] || typeof this._where[key] === 'string') {
  355. this._where[key] = {};
  356. }
  357. this._where[key][condition] = (0, _encode.default)(value, false, true);
  358. return this;
  359. }
  360. /**
  361. * Converts string for regular expression at the beginning
  362. *
  363. * @param string
  364. * @returns {string}
  365. */
  366. }, {
  367. key: "_regexStartWith",
  368. value: function (string /*: string*/) /*: string*/{
  369. return '^' + quote(string);
  370. }
  371. }, {
  372. key: "_handleOfflineQuery",
  373. value: function () {
  374. var _handleOfflineQuery2 = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee(params /*: any*/) {
  375. var _context,
  376. _this2 = this;
  377. var localDatastore, objects, results, keys, alwaysSelectedKeys, sorts, count, limit;
  378. return _regenerator.default.wrap(function (_context3) {
  379. while (1) switch (_context3.prev = _context3.next) {
  380. case 0:
  381. _OfflineQuery.default.validateQuery(this);
  382. localDatastore = _CoreManager.default.getLocalDatastore();
  383. _context3.next = 4;
  384. return localDatastore._serializeObjectsFromPinName(this._localDatastorePinName);
  385. case 4:
  386. objects = _context3.sent;
  387. results = (0, _filter2.default)(_context = (0, _map2.default)(objects).call(objects, function (json, index, arr) {
  388. var object = _ParseObject.default.fromJSON(json, false);
  389. if (json._localId && !json.objectId) {
  390. object._localId = json._localId;
  391. }
  392. if (!_OfflineQuery.default.matchesQuery(_this2.className, object, arr, _this2)) {
  393. return null;
  394. }
  395. return object;
  396. })).call(_context, function (object) {
  397. return object !== null;
  398. });
  399. if ((0, _keys2.default)(params)) {
  400. keys = (0, _keys2.default)(params).split(',');
  401. alwaysSelectedKeys = ['className', 'objectId', 'createdAt', 'updatedAt', 'ACL'];
  402. keys = (0, _concat.default)(keys).call(keys, alwaysSelectedKeys);
  403. results = (0, _map2.default)(results).call(results, function (object) {
  404. var _context2;
  405. var json = object._toFullJSON();
  406. (0, _forEach.default)(_context2 = (0, _keys.default)(json)).call(_context2, function (key) {
  407. if (!(0, _includes.default)(keys).call(keys, key)) {
  408. delete json[key];
  409. }
  410. });
  411. return _ParseObject.default.fromJSON(json, false);
  412. });
  413. }
  414. if (params.order) {
  415. sorts = params.order.split(',');
  416. (0, _sort.default)(results).call(results, function (a, b) {
  417. return handleOfflineSort(a, b, sorts);
  418. });
  419. }
  420. // count total before applying limit/skip
  421. if (params.count) {
  422. count = results.length; // total count from response
  423. }
  424. if (params.skip) {
  425. if (params.skip >= results.length) {
  426. results = [];
  427. } else {
  428. results = (0, _splice.default)(results).call(results, params.skip, results.length);
  429. }
  430. }
  431. limit = results.length;
  432. if (params.limit !== 0 && params.limit < results.length) {
  433. limit = params.limit;
  434. }
  435. results = (0, _splice.default)(results).call(results, 0, limit);
  436. if (!(typeof count === 'number')) {
  437. _context3.next = 15;
  438. break;
  439. }
  440. return _context3.abrupt("return", {
  441. results: results,
  442. count: count
  443. });
  444. case 15:
  445. return _context3.abrupt("return", results);
  446. case 16:
  447. case "end":
  448. return _context3.stop();
  449. }
  450. }, _callee, this);
  451. }));
  452. function _handleOfflineQuery() {
  453. return _handleOfflineQuery2.apply(this, arguments);
  454. }
  455. return _handleOfflineQuery;
  456. }()
  457. /**
  458. * Returns a JSON representation of this query.
  459. *
  460. * @returns {object} The JSON representation of the query.
  461. */
  462. }, {
  463. key: "toJSON",
  464. value: function () /*: QueryJSON*/{
  465. var params /*: QueryJSON*/ = {
  466. where: this._where
  467. };
  468. if (this._watch.length) {
  469. params.watch = this._watch.join(',');
  470. }
  471. if (this._include.length) {
  472. params.include = this._include.join(',');
  473. }
  474. if (this._exclude.length) {
  475. params.excludeKeys = this._exclude.join(',');
  476. }
  477. if (this._select) {
  478. params.keys = this._select.join(',');
  479. }
  480. if (this._count) {
  481. params.count = 1;
  482. }
  483. if (this._limit >= 0) {
  484. params.limit = this._limit;
  485. }
  486. if (this._skip > 0) {
  487. params.skip = this._skip;
  488. }
  489. if (this._order) {
  490. params.order = this._order.join(',');
  491. }
  492. if (this._readPreference) {
  493. params.readPreference = this._readPreference;
  494. }
  495. if (this._includeReadPreference) {
  496. params.includeReadPreference = this._includeReadPreference;
  497. }
  498. if (this._subqueryReadPreference) {
  499. params.subqueryReadPreference = this._subqueryReadPreference;
  500. }
  501. if (this._hint) {
  502. params.hint = this._hint;
  503. }
  504. if (this._explain) {
  505. params.explain = true;
  506. }
  507. for (var _key3 in this._extraOptions) {
  508. params[_key3] = this._extraOptions[_key3];
  509. }
  510. return params;
  511. }
  512. /**
  513. * Return a query with conditions from json, can be useful to send query from server side to client
  514. * Not static, all query conditions was set before calling this method will be deleted.
  515. * For example on the server side we have
  516. * var query = new Parse.Query("className");
  517. * query.equalTo(key: value);
  518. * query.limit(100);
  519. * ... (others queries)
  520. * Create JSON representation of Query Object
  521. * var jsonFromServer = query.fromJSON();
  522. *
  523. * On client side getting query:
  524. * var query = new Parse.Query("className");
  525. * query.fromJSON(jsonFromServer);
  526. *
  527. * and continue to query...
  528. * query.skip(100).find().then(...);
  529. *
  530. * @param {QueryJSON} json from Parse.Query.toJSON() method
  531. * @returns {Parse.Query} Returns the query, so you can chain this call.
  532. */
  533. }, {
  534. key: "withJSON",
  535. value: function (json /*: QueryJSON*/) /*: ParseQuery*/{
  536. if (json.where) {
  537. this._where = json.where;
  538. }
  539. if (json.watch) {
  540. this._watch = json.watch.split(',');
  541. }
  542. if (json.include) {
  543. this._include = json.include.split(',');
  544. }
  545. if ((0, _keys2.default)(json)) {
  546. this._select = (0, _keys2.default)(json).split(',');
  547. }
  548. if (json.excludeKeys) {
  549. this._exclude = json.excludeKeys.split(',');
  550. }
  551. if (json.count) {
  552. this._count = json.count === 1;
  553. }
  554. if (json.limit) {
  555. this._limit = json.limit;
  556. }
  557. if (json.skip) {
  558. this._skip = json.skip;
  559. }
  560. if (json.order) {
  561. this._order = json.order.split(',');
  562. }
  563. if (json.readPreference) {
  564. this._readPreference = json.readPreference;
  565. }
  566. if (json.includeReadPreference) {
  567. this._includeReadPreference = json.includeReadPreference;
  568. }
  569. if (json.subqueryReadPreference) {
  570. this._subqueryReadPreference = json.subqueryReadPreference;
  571. }
  572. if (json.hint) {
  573. this._hint = json.hint;
  574. }
  575. if (json.explain) {
  576. this._explain = !!json.explain;
  577. }
  578. for (var _key4 in json) {
  579. if (json.hasOwnProperty(_key4)) {
  580. var _context4;
  581. if ((0, _indexOf.default)(_context4 = ['where', 'include', 'keys', 'count', 'limit', 'skip', 'order', 'readPreference', 'includeReadPreference', 'subqueryReadPreference', 'hint', 'explain']).call(_context4, _key4) === -1) {
  582. this._extraOptions[_key4] = json[_key4];
  583. }
  584. }
  585. }
  586. return this;
  587. }
  588. /**
  589. * Static method to restore Parse.Query by json representation
  590. * Internally calling Parse.Query.withJSON
  591. *
  592. * @param {string} className
  593. * @param {QueryJSON} json from Parse.Query.toJSON() method
  594. * @returns {Parse.Query} new created query
  595. */
  596. }, {
  597. key: "get",
  598. value:
  599. /**
  600. * Constructs a Parse.Object whose id is already known by fetching data from
  601. * the server. Unlike the <code>first</code> method, it never returns undefined.
  602. *
  603. * @param {string} objectId The id of the object to be fetched.
  604. * @param {object} options
  605. * Valid options are:<ul>
  606. * <li>useMasterKey: In Cloud Code and Node only, causes the Master Key to
  607. * be used for this request.
  608. * <li>sessionToken: A valid session token, used for making a request on
  609. * behalf of a specific user.
  610. * <li>context: A dictionary that is accessible in Cloud Code `beforeFind` trigger.
  611. * <li>json: Return raw json without converting to Parse.Object
  612. * </ul>
  613. * @returns {Promise} A promise that is resolved with the result when
  614. * the query completes.
  615. */
  616. function (objectId /*: string*/, options /*:: ?: FullOptions*/) /*: Promise<ParseObject>*/{
  617. this.equalTo('objectId', objectId);
  618. var firstOptions = {};
  619. if (options && options.hasOwnProperty('useMasterKey')) {
  620. firstOptions.useMasterKey = options.useMasterKey;
  621. }
  622. if (options && options.hasOwnProperty('sessionToken')) {
  623. firstOptions.sessionToken = options.sessionToken;
  624. }
  625. if (options && options.hasOwnProperty('context') && (0, _typeof2.default)(options.context) === 'object') {
  626. firstOptions.context = options.context;
  627. }
  628. if (options && options.hasOwnProperty('json')) {
  629. firstOptions.json = options.json;
  630. }
  631. return this.first(firstOptions).then(function (response) {
  632. if (response) {
  633. return response;
  634. }
  635. var errorObject = new _ParseError.default(_ParseError.default.OBJECT_NOT_FOUND, 'Object not found.');
  636. return _promise.default.reject(errorObject);
  637. });
  638. }
  639. /**
  640. * Retrieves a list of ParseObjects that satisfy this query.
  641. *
  642. * @param {object} options Valid options
  643. * are:<ul>
  644. * <li>useMasterKey: In Cloud Code and Node only, causes the Master Key to
  645. * be used for this request.
  646. * <li>sessionToken: A valid session token, used for making a request on
  647. * behalf of a specific user.
  648. * <li>context: A dictionary that is accessible in Cloud Code `beforeFind` trigger.
  649. * <li>json: Return raw json without converting to Parse.Object
  650. * </ul>
  651. * @returns {Promise} A promise that is resolved with the results when
  652. * the query completes.
  653. */
  654. }, {
  655. key: "find",
  656. value: function (options /*:: ?: FullOptions*/) /*: Promise<Array<ParseObject>>*/{
  657. var _this3 = this;
  658. options = options || {};
  659. var findOptions = {};
  660. if (options.hasOwnProperty('useMasterKey')) {
  661. findOptions.useMasterKey = options.useMasterKey;
  662. }
  663. if (options.hasOwnProperty('sessionToken')) {
  664. findOptions.sessionToken = options.sessionToken;
  665. }
  666. if (options.hasOwnProperty('context') && (0, _typeof2.default)(options.context) === 'object') {
  667. findOptions.context = options.context;
  668. }
  669. this._setRequestTask(findOptions);
  670. var controller = _CoreManager.default.getQueryController();
  671. var select = this._select;
  672. if (this._queriesLocalDatastore) {
  673. return this._handleOfflineQuery(this.toJSON());
  674. }
  675. return (0, _find.default)(controller).call(controller, this.className, this.toJSON(), findOptions).then(function (response) {
  676. var _context5;
  677. // Return generic object when explain is used
  678. if (_this3._explain) {
  679. return response.results;
  680. }
  681. var results = (0, _map2.default)(_context5 = response.results).call(_context5, function (data) {
  682. // In cases of relations, the server may send back a className
  683. // on the top level of the payload
  684. var override = response.className || _this3.className;
  685. if (!data.className) {
  686. data.className = override;
  687. }
  688. // Make sure the data object contains keys for all objects that
  689. // have been requested with a select, so that our cached state
  690. // updates correctly.
  691. if (select) {
  692. handleSelectResult(data, select);
  693. }
  694. if (options.json) {
  695. return data;
  696. } else {
  697. return _ParseObject.default.fromJSON(data, !select);
  698. }
  699. });
  700. var count = response.count;
  701. if (typeof count === 'number') {
  702. return {
  703. results: results,
  704. count: count
  705. };
  706. } else {
  707. return results;
  708. }
  709. });
  710. }
  711. /**
  712. * Retrieves a complete list of ParseObjects that satisfy this query.
  713. * Using `eachBatch` under the hood to fetch all the valid objects.
  714. *
  715. * @param {object} options Valid options are:<ul>
  716. * <li>batchSize: How many objects to yield in each batch (default: 100)
  717. * <li>useMasterKey: In Cloud Code and Node only, causes the Master Key to
  718. * be used for this request.
  719. * <li>sessionToken: A valid session token, used for making a request on
  720. * behalf of a specific user.
  721. * </ul>
  722. * @returns {Promise} A promise that is resolved with the results when
  723. * the query completes.
  724. */
  725. }, {
  726. key: "findAll",
  727. value: function () {
  728. var _findAll = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee2(options /*:: ?: BatchOptions*/) {
  729. var result;
  730. return _regenerator.default.wrap(function (_context7) {
  731. while (1) switch (_context7.prev = _context7.next) {
  732. case 0:
  733. result /*: ParseObject[]*/ = [];
  734. _context7.next = 3;
  735. return this.eachBatch(function (objects /*: ParseObject[]*/) {
  736. var _context6;
  737. result = (0, _concat.default)(_context6 = []).call(_context6, (0, _toConsumableArray2.default)(result), (0, _toConsumableArray2.default)(objects));
  738. }, options);
  739. case 3:
  740. return _context7.abrupt("return", result);
  741. case 4:
  742. case "end":
  743. return _context7.stop();
  744. }
  745. }, _callee2, this);
  746. }));
  747. function findAll() {
  748. return _findAll.apply(this, arguments);
  749. }
  750. return findAll;
  751. }()
  752. /**
  753. * Counts the number of objects that match this query.
  754. *
  755. * @param {object} options
  756. * Valid options are:<ul>
  757. * <li>useMasterKey: In Cloud Code and Node only, causes the Master Key to
  758. * be used for this request.
  759. * <li>sessionToken: A valid session token, used for making a request on
  760. * behalf of a specific user.
  761. * </ul>
  762. * @returns {Promise} A promise that is resolved with the count when
  763. * the query completes.
  764. */
  765. }, {
  766. key: "count",
  767. value: function (options /*:: ?: FullOptions*/) /*: Promise<number>*/{
  768. options = options || {};
  769. var findOptions = {};
  770. if (options.hasOwnProperty('useMasterKey')) {
  771. findOptions.useMasterKey = options.useMasterKey;
  772. }
  773. if (options.hasOwnProperty('sessionToken')) {
  774. findOptions.sessionToken = options.sessionToken;
  775. }
  776. this._setRequestTask(findOptions);
  777. var controller = _CoreManager.default.getQueryController();
  778. var params = this.toJSON();
  779. params.limit = 0;
  780. params.count = 1;
  781. return (0, _find.default)(controller).call(controller, this.className, params, findOptions).then(function (result) {
  782. return result.count;
  783. });
  784. }
  785. /**
  786. * Executes a distinct query and returns unique values
  787. *
  788. * @param {string} key A field to find distinct values
  789. * @param {object} options
  790. * Valid options are:<ul>
  791. * <li>sessionToken: A valid session token, used for making a request on
  792. * behalf of a specific user.
  793. * </ul>
  794. * @returns {Promise} A promise that is resolved with the query completes.
  795. */
  796. }, {
  797. key: "distinct",
  798. value: function (key /*: string*/, options /*:: ?: FullOptions*/) /*: Promise<Array<mixed>>*/{
  799. options = options || {};
  800. var distinctOptions = {};
  801. distinctOptions.useMasterKey = true;
  802. if (options.hasOwnProperty('sessionToken')) {
  803. distinctOptions.sessionToken = options.sessionToken;
  804. }
  805. this._setRequestTask(distinctOptions);
  806. var controller = _CoreManager.default.getQueryController();
  807. var params = {
  808. distinct: key,
  809. where: this._where,
  810. hint: this._hint
  811. };
  812. return controller.aggregate(this.className, params, distinctOptions).then(function (results) {
  813. return results.results;
  814. });
  815. }
  816. /**
  817. * Executes an aggregate query and returns aggregate results
  818. *
  819. * @param {(Array|object)} pipeline Array or Object of stages to process query
  820. * @param {object} options Valid options are:<ul>
  821. * <li>sessionToken: A valid session token, used for making a request on
  822. * behalf of a specific user.
  823. * </ul>
  824. * @returns {Promise} A promise that is resolved with the query completes.
  825. */
  826. }, {
  827. key: "aggregate",
  828. value: function (pipeline /*: mixed*/, options /*:: ?: FullOptions*/) /*: Promise<Array<mixed>>*/{
  829. options = options || {};
  830. var aggregateOptions = {};
  831. aggregateOptions.useMasterKey = true;
  832. if (options.hasOwnProperty('sessionToken')) {
  833. aggregateOptions.sessionToken = options.sessionToken;
  834. }
  835. this._setRequestTask(aggregateOptions);
  836. var controller = _CoreManager.default.getQueryController();
  837. if (!(0, _isArray.default)(pipeline) && (0, _typeof2.default)(pipeline) !== 'object') {
  838. throw new Error('Invalid pipeline must be Array or Object');
  839. }
  840. if ((0, _keys.default)(this._where || {}).length) {
  841. if (!(0, _isArray.default)(pipeline)) {
  842. pipeline = [pipeline];
  843. }
  844. pipeline.unshift({
  845. $match: this._where
  846. });
  847. }
  848. var params = {
  849. pipeline: pipeline,
  850. hint: this._hint,
  851. explain: this._explain,
  852. readPreference: this._readPreference
  853. };
  854. return controller.aggregate(this.className, params, aggregateOptions).then(function (results) {
  855. return results.results;
  856. });
  857. }
  858. /**
  859. * Retrieves at most one Parse.Object that satisfies this query.
  860. *
  861. * Returns the object if there is one, otherwise undefined.
  862. *
  863. * @param {object} options Valid options are:<ul>
  864. * <li>useMasterKey: In Cloud Code and Node only, causes the Master Key to
  865. * be used for this request.
  866. * <li>sessionToken: A valid session token, used for making a request on
  867. * behalf of a specific user.
  868. * <li>context: A dictionary that is accessible in Cloud Code `beforeFind` trigger.
  869. * <li>json: Return raw json without converting to Parse.Object
  870. * </ul>
  871. * @returns {Promise} A promise that is resolved with the object when
  872. * the query completes.
  873. */
  874. }, {
  875. key: "first",
  876. value: function (options /*:: ?: FullOptions*/) /*: Promise<ParseObject | void>*/{
  877. var _this4 = this;
  878. options = options || {};
  879. var findOptions = {};
  880. if (options.hasOwnProperty('useMasterKey')) {
  881. findOptions.useMasterKey = options.useMasterKey;
  882. }
  883. if (options.hasOwnProperty('sessionToken')) {
  884. findOptions.sessionToken = options.sessionToken;
  885. }
  886. if (options.hasOwnProperty('context') && (0, _typeof2.default)(options.context) === 'object') {
  887. findOptions.context = options.context;
  888. }
  889. this._setRequestTask(findOptions);
  890. var controller = _CoreManager.default.getQueryController();
  891. var params = this.toJSON();
  892. params.limit = 1;
  893. var select = this._select;
  894. if (this._queriesLocalDatastore) {
  895. return this._handleOfflineQuery(params).then(function (objects) {
  896. if (!objects[0]) {
  897. return undefined;
  898. }
  899. return objects[0];
  900. });
  901. }
  902. return (0, _find.default)(controller).call(controller, this.className, params, findOptions).then(function (response) {
  903. var objects = response.results;
  904. if (!objects[0]) {
  905. return undefined;
  906. }
  907. if (!objects[0].className) {
  908. objects[0].className = _this4.className;
  909. }
  910. // Make sure the data object contains keys for all objects that
  911. // have been requested with a select, so that our cached state
  912. // updates correctly.
  913. if (select) {
  914. handleSelectResult(objects[0], select);
  915. }
  916. if (options.json) {
  917. return objects[0];
  918. } else {
  919. return _ParseObject.default.fromJSON(objects[0], !select);
  920. }
  921. });
  922. }
  923. /**
  924. * Iterates over objects matching a query, calling a callback for each batch.
  925. * If the callback returns a promise, the iteration will not continue until
  926. * that promise has been fulfilled. If the callback returns a rejected
  927. * promise, then iteration will stop with that error. The items are processed
  928. * in an unspecified order. The query may not have any sort order, and may
  929. * not use limit or skip.
  930. *
  931. * @param {Function} callback Callback that will be called with each result
  932. * of the query.
  933. * @param {object} options Valid options are:<ul>
  934. * <li>batchSize: How many objects to yield in each batch (default: 100)
  935. * <li>useMasterKey: In Cloud Code and Node only, causes the Master Key to
  936. * be used for this request.
  937. * <li>sessionToken: A valid session token, used for making a request on
  938. * behalf of a specific user.
  939. * <li>context: A dictionary that is accessible in Cloud Code `beforeFind` trigger.
  940. * </ul>
  941. * @returns {Promise} A promise that will be fulfilled once the
  942. * iteration has completed.
  943. */
  944. }, {
  945. key: "eachBatch",
  946. value: function (callback /*: (objs: Array<ParseObject>) => Promise<*>*/, options /*:: ?: BatchOptions*/) /*: Promise<void>*/{
  947. var _context8;
  948. options = options || {};
  949. if (this._order || this._skip || this._limit >= 0) {
  950. return _promise.default.reject('Cannot iterate on a query with sort, skip, or limit.');
  951. }
  952. var query = new ParseQuery(this.className);
  953. query._limit = options.batchSize || 100;
  954. query._include = (0, _map2.default)(_context8 = this._include).call(_context8, function (i) {
  955. return i;
  956. });
  957. if (this._select) {
  958. var _context9;
  959. query._select = (0, _map2.default)(_context9 = this._select).call(_context9, function (s) {
  960. return s;
  961. });
  962. }
  963. query._hint = this._hint;
  964. query._where = {};
  965. for (var _attr in this._where) {
  966. var val = this._where[_attr];
  967. if ((0, _isArray.default)(val)) {
  968. query._where[_attr] = (0, _map2.default)(val).call(val, function (v) {
  969. return v;
  970. });
  971. } else if (val && (0, _typeof2.default)(val) === 'object') {
  972. var conditionMap = {};
  973. query._where[_attr] = conditionMap;
  974. for (var cond in val) {
  975. conditionMap[cond] = val[cond];
  976. }
  977. } else {
  978. query._where[_attr] = val;
  979. }
  980. }
  981. query.ascending('objectId');
  982. var findOptions = {};
  983. if (options.hasOwnProperty('useMasterKey')) {
  984. findOptions.useMasterKey = options.useMasterKey;
  985. }
  986. if (options.hasOwnProperty('sessionToken')) {
  987. findOptions.sessionToken = options.sessionToken;
  988. }
  989. if (options.hasOwnProperty('context') && (0, _typeof2.default)(options.context) === 'object') {
  990. findOptions.context = options.context;
  991. }
  992. if (options.hasOwnProperty('json')) {
  993. findOptions.json = options.json;
  994. }
  995. var finished = false;
  996. var previousResults = [];
  997. return (0, _promiseUtils.continueWhile)(function () {
  998. return !finished;
  999. }, /*#__PURE__*/(0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee3() {
  1000. var _yield$Promise$all, _yield$Promise$all2, results;
  1001. return _regenerator.default.wrap(function (_context10) {
  1002. while (1) switch (_context10.prev = _context10.next) {
  1003. case 0:
  1004. _context10.next = 2;
  1005. return _promise.default.all([(0, _find.default)(query).call(query, findOptions), _promise.default.resolve(previousResults.length > 0 && callback(previousResults))]);
  1006. case 2:
  1007. _yield$Promise$all = _context10.sent;
  1008. _yield$Promise$all2 = (0, _slicedToArray2.default)(_yield$Promise$all, 1);
  1009. results = _yield$Promise$all2[0];
  1010. if (!(results.length >= query._limit)) {
  1011. _context10.next = 10;
  1012. break;
  1013. }
  1014. query.greaterThan('objectId', results[results.length - 1].id);
  1015. previousResults = results;
  1016. _context10.next = 17;
  1017. break;
  1018. case 10:
  1019. if (!(results.length > 0)) {
  1020. _context10.next = 16;
  1021. break;
  1022. }
  1023. _context10.next = 13;
  1024. return _promise.default.resolve(callback(results));
  1025. case 13:
  1026. finished = true;
  1027. _context10.next = 17;
  1028. break;
  1029. case 16:
  1030. finished = true;
  1031. case 17:
  1032. case "end":
  1033. return _context10.stop();
  1034. }
  1035. }, _callee3);
  1036. })));
  1037. }
  1038. /**
  1039. * Iterates over each result of a query, calling a callback for each one. If
  1040. * the callback returns a promise, the iteration will not continue until
  1041. * that promise has been fulfilled. If the callback returns a rejected
  1042. * promise, then iteration will stop with that error. The items are
  1043. * processed in an unspecified order. The query may not have any sort order,
  1044. * and may not use limit or skip.
  1045. *
  1046. * @param {Function} callback Callback that will be called with each result
  1047. * of the query.
  1048. * @param {object} options Valid options are:<ul>
  1049. * <li>useMasterKey: In Cloud Code and Node only, causes the Master Key to
  1050. * be used for this request.
  1051. * <li>sessionToken: A valid session token, used for making a request on
  1052. * behalf of a specific user.
  1053. * <li>json: Return raw json without converting to Parse.Object
  1054. * </ul>
  1055. * @returns {Promise} A promise that will be fulfilled once the
  1056. * iteration has completed.
  1057. */
  1058. }, {
  1059. key: "each",
  1060. value: function (callback /*: (obj: ParseObject) => any*/, options /*:: ?: BatchOptions*/) /*: Promise<void>*/{
  1061. return this.eachBatch(function (results) {
  1062. var callbacksDone = _promise.default.resolve();
  1063. (0, _forEach.default)(results).call(results, function (result) {
  1064. callbacksDone = callbacksDone.then(function () {
  1065. return callback(result);
  1066. });
  1067. });
  1068. return callbacksDone;
  1069. }, options);
  1070. }
  1071. /**
  1072. * Adds a hint to force index selection. (https://docs.mongodb.com/manual/reference/operator/meta/hint/)
  1073. *
  1074. * @param {(string|object)} value String or Object of index that should be used when executing query
  1075. * @returns {Parse.Query} Returns the query, so you can chain this call.
  1076. */
  1077. }, {
  1078. key: "hint",
  1079. value: function (value /*: mixed*/) /*: ParseQuery*/{
  1080. if (typeof value === 'undefined') {
  1081. delete this._hint;
  1082. }
  1083. this._hint = value;
  1084. return this;
  1085. }
  1086. /**
  1087. * Investigates the query execution plan. Useful for optimizing queries. (https://docs.mongodb.com/manual/reference/operator/meta/explain/)
  1088. *
  1089. * @param {boolean} explain Used to toggle the information on the query plan.
  1090. * @returns {Parse.Query} Returns the query, so you can chain this call.
  1091. */
  1092. }, {
  1093. key: "explain",
  1094. value: function () /*: ParseQuery*/{
  1095. var _explain /*: boolean*/ = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : true;
  1096. if (typeof _explain !== 'boolean') {
  1097. throw new Error('You can only set explain to a boolean value');
  1098. }
  1099. this._explain = _explain;
  1100. return this;
  1101. }
  1102. /**
  1103. * Iterates over each result of a query, calling a callback for each one. If
  1104. * the callback returns a promise, the iteration will not continue until
  1105. * that promise has been fulfilled. If the callback returns a rejected
  1106. * promise, then iteration will stop with that error. The items are
  1107. * processed in an unspecified order. The query may not have any sort order,
  1108. * and may not use limit or skip.
  1109. *
  1110. * @param {Function} callback Callback <ul>
  1111. * <li>currentObject: The current Parse.Object being processed in the array.</li>
  1112. * <li>index: The index of the current Parse.Object being processed in the array.</li>
  1113. * <li>query: The query map was called upon.</li>
  1114. * </ul>
  1115. * @param {object} options Valid options are:<ul>
  1116. * <li>useMasterKey: In Cloud Code and Node only, causes the Master Key to
  1117. * be used for this request.
  1118. * <li>sessionToken: A valid session token, used for making a request on
  1119. * behalf of a specific user.
  1120. * </ul>
  1121. * @returns {Promise} A promise that will be fulfilled once the
  1122. * iteration has completed.
  1123. */
  1124. }, {
  1125. key: "map",
  1126. value: function () {
  1127. var _map = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee4(callback /*: (currentObject: ParseObject, index: number, query: ParseQuery) => any*/, options /*:: ?: BatchOptions*/) {
  1128. var _this5 = this;
  1129. var array, index;
  1130. return _regenerator.default.wrap(function (_context11) {
  1131. while (1) switch (_context11.prev = _context11.next) {
  1132. case 0:
  1133. array = [];
  1134. index = 0;
  1135. _context11.next = 4;
  1136. return this.each(function (object) {
  1137. return _promise.default.resolve(callback(object, index, _this5)).then(function (result) {
  1138. array.push(result);
  1139. index += 1;
  1140. });
  1141. }, options);
  1142. case 4:
  1143. return _context11.abrupt("return", array);
  1144. case 5:
  1145. case "end":
  1146. return _context11.stop();
  1147. }
  1148. }, _callee4, this);
  1149. }));
  1150. function map() {
  1151. return _map.apply(this, arguments);
  1152. }
  1153. return map;
  1154. }()
  1155. /**
  1156. * Iterates over each result of a query, calling a callback for each one. If
  1157. * the callback returns a promise, the iteration will not continue until
  1158. * that promise has been fulfilled. If the callback returns a rejected
  1159. * promise, then iteration will stop with that error. The items are
  1160. * processed in an unspecified order. The query may not have any sort order,
  1161. * and may not use limit or skip.
  1162. *
  1163. * @param {Function} callback Callback <ul>
  1164. * <li>accumulator: The accumulator accumulates the callback's return values. It is the accumulated value previously returned in the last invocation of the callback.</li>
  1165. * <li>currentObject: The current Parse.Object being processed in the array.</li>
  1166. * <li>index: The index of the current Parse.Object being processed in the array.</li>
  1167. * </ul>
  1168. * @param {*} initialValue A value to use as the first argument to the first call of the callback. If no initialValue is supplied, the first object in the query will be used and skipped.
  1169. * @param {object} options Valid options are:<ul>
  1170. * <li>useMasterKey: In Cloud Code and Node only, causes the Master Key to
  1171. * be used for this request.
  1172. * <li>sessionToken: A valid session token, used for making a request on
  1173. * behalf of a specific user.
  1174. * </ul>
  1175. * @returns {Promise} A promise that will be fulfilled once the
  1176. * iteration has completed.
  1177. */
  1178. }, {
  1179. key: "reduce",
  1180. value: function () {
  1181. var _reduce = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee5(callback /*: (accumulator: any, currentObject: ParseObject, index: number) => any*/, initialValue /*: any*/, options /*:: ?: BatchOptions*/) {
  1182. var accumulator, index;
  1183. return _regenerator.default.wrap(function (_context12) {
  1184. while (1) switch (_context12.prev = _context12.next) {
  1185. case 0:
  1186. accumulator = initialValue;
  1187. index = 0;
  1188. _context12.next = 4;
  1189. return this.each(function (object) {
  1190. // If no initial value was given, we take the first object from the query
  1191. // as the initial value and don't call the callback with it.
  1192. if (index === 0 && initialValue === undefined) {
  1193. accumulator = object;
  1194. index += 1;
  1195. return;
  1196. }
  1197. return _promise.default.resolve(callback(accumulator, object, index)).then(function (result) {
  1198. accumulator = result;
  1199. index += 1;
  1200. });
  1201. }, options);
  1202. case 4:
  1203. if (!(index === 0 && initialValue === undefined)) {
  1204. _context12.next = 6;
  1205. break;
  1206. }
  1207. throw new TypeError('Reducing empty query result set with no initial value');
  1208. case 6:
  1209. return _context12.abrupt("return", accumulator);
  1210. case 7:
  1211. case "end":
  1212. return _context12.stop();
  1213. }
  1214. }, _callee5, this);
  1215. }));
  1216. function reduce() {
  1217. return _reduce.apply(this, arguments);
  1218. }
  1219. return reduce;
  1220. }()
  1221. /**
  1222. * Iterates over each result of a query, calling a callback for each one. If
  1223. * the callback returns a promise, the iteration will not continue until
  1224. * that promise has been fulfilled. If the callback returns a rejected
  1225. * promise, then iteration will stop with that error. The items are
  1226. * processed in an unspecified order. The query may not have any sort order,
  1227. * and may not use limit or skip.
  1228. *
  1229. * @param {Function} callback Callback <ul>
  1230. * <li>currentObject: The current Parse.Object being processed in the array.</li>
  1231. * <li>index: The index of the current Parse.Object being processed in the array.</li>
  1232. * <li>query: The query filter was called upon.</li>
  1233. * </ul>
  1234. * @param {object} options Valid options are:<ul>
  1235. * <li>useMasterKey: In Cloud Code and Node only, causes the Master Key to
  1236. * be used for this request.
  1237. * <li>sessionToken: A valid session token, used for making a request on
  1238. * behalf of a specific user.
  1239. * </ul>
  1240. * @returns {Promise} A promise that will be fulfilled once the
  1241. * iteration has completed.
  1242. */
  1243. }, {
  1244. key: "filter",
  1245. value: function () {
  1246. var _filter = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee6(callback /*: (currentObject: ParseObject, index: number, query: ParseQuery) => boolean*/, options /*:: ?: BatchOptions*/) {
  1247. var _this6 = this;
  1248. var array, index;
  1249. return _regenerator.default.wrap(function (_context13) {
  1250. while (1) switch (_context13.prev = _context13.next) {
  1251. case 0:
  1252. array = [];
  1253. index = 0;
  1254. _context13.next = 4;
  1255. return this.each(function (object) {
  1256. return _promise.default.resolve(callback(object, index, _this6)).then(function (flag) {
  1257. if (flag) {
  1258. array.push(object);
  1259. }
  1260. index += 1;
  1261. });
  1262. }, options);
  1263. case 4:
  1264. return _context13.abrupt("return", array);
  1265. case 5:
  1266. case "end":
  1267. return _context13.stop();
  1268. }
  1269. }, _callee6, this);
  1270. }));
  1271. function filter() {
  1272. return _filter.apply(this, arguments);
  1273. }
  1274. return filter;
  1275. }()
  1276. /* Query Conditions */
  1277. /**
  1278. * Adds a constraint to the query that requires a particular key's value to
  1279. * be equal to the provided value.
  1280. *
  1281. * @param {string} key The key to check.
  1282. * @param value The value that the Parse.Object must contain.
  1283. * @returns {Parse.Query} Returns the query, so you can chain this call.
  1284. */
  1285. }, {
  1286. key: "equalTo",
  1287. value: function (key /*: string | { [key: string]: any }*/, value /*: ?mixed*/) /*: ParseQuery*/{
  1288. var _this7 = this;
  1289. if (key && (0, _typeof2.default)(key) === 'object') {
  1290. var _context14;
  1291. (0, _forEach.default)(_context14 = (0, _entries.default)(key)).call(_context14, function (_ref2) {
  1292. var _ref3 = (0, _slicedToArray2.default)(_ref2, 2),
  1293. k = _ref3[0],
  1294. val = _ref3[1];
  1295. return _this7.equalTo(k, val);
  1296. });
  1297. return this;
  1298. }
  1299. if (typeof value === 'undefined') {
  1300. return this.doesNotExist(key);
  1301. }
  1302. this._where[key] = (0, _encode.default)(value, false, true);
  1303. return this;
  1304. }
  1305. /**
  1306. * Adds a constraint to the query that requires a particular key's value to
  1307. * be not equal to the provided value.
  1308. *
  1309. * @param {string} key The key to check.
  1310. * @param value The value that must not be equalled.
  1311. * @returns {Parse.Query} Returns the query, so you can chain this call.
  1312. */
  1313. }, {
  1314. key: "notEqualTo",
  1315. value: function (key /*: string | { [key: string]: any }*/, value /*: ?mixed*/) /*: ParseQuery*/{
  1316. var _this8 = this;
  1317. if (key && (0, _typeof2.default)(key) === 'object') {
  1318. var _context15;
  1319. (0, _forEach.default)(_context15 = (0, _entries.default)(key)).call(_context15, function (_ref4) {
  1320. var _ref5 = (0, _slicedToArray2.default)(_ref4, 2),
  1321. k = _ref5[0],
  1322. val = _ref5[1];
  1323. return _this8.notEqualTo(k, val);
  1324. });
  1325. return this;
  1326. }
  1327. return this._addCondition(key, '$ne', value);
  1328. }
  1329. /**
  1330. * Adds a constraint to the query that requires a particular key's value to
  1331. * be less than the provided value.
  1332. *
  1333. * @param {string} key The key to check.
  1334. * @param value The value that provides an upper bound.
  1335. * @returns {Parse.Query} Returns the query, so you can chain this call.
  1336. */
  1337. }, {
  1338. key: "lessThan",
  1339. value: function (key /*: string*/, value /*: mixed*/) /*: ParseQuery*/{
  1340. return this._addCondition(key, '$lt', value);
  1341. }
  1342. /**
  1343. * Adds a constraint to the query that requires a particular key's value to
  1344. * be greater than the provided value.
  1345. *
  1346. * @param {string} key The key to check.
  1347. * @param value The value that provides an lower bound.
  1348. * @returns {Parse.Query} Returns the query, so you can chain this call.
  1349. */
  1350. }, {
  1351. key: "greaterThan",
  1352. value: function (key /*: string*/, value /*: mixed*/) /*: ParseQuery*/{
  1353. return this._addCondition(key, '$gt', value);
  1354. }
  1355. /**
  1356. * Adds a constraint to the query that requires a particular key's value to
  1357. * be less than or equal to the provided value.
  1358. *
  1359. * @param {string} key The key to check.
  1360. * @param value The value that provides an upper bound.
  1361. * @returns {Parse.Query} Returns the query, so you can chain this call.
  1362. */
  1363. }, {
  1364. key: "lessThanOrEqualTo",
  1365. value: function (key /*: string*/, value /*: mixed*/) /*: ParseQuery*/{
  1366. return this._addCondition(key, '$lte', value);
  1367. }
  1368. /**
  1369. * Adds a constraint to the query that requires a particular key's value to
  1370. * be greater than or equal to the provided value.
  1371. *
  1372. * @param {string} key The key to check.
  1373. * @param {*} value The value that provides an lower bound.
  1374. * @returns {Parse.Query} Returns the query, so you can chain this call.
  1375. */
  1376. }, {
  1377. key: "greaterThanOrEqualTo",
  1378. value: function (key /*: string*/, value /*: mixed*/) /*: ParseQuery*/{
  1379. return this._addCondition(key, '$gte', value);
  1380. }
  1381. /**
  1382. * Adds a constraint to the query that requires a particular key's value to
  1383. * be contained in the provided list of values.
  1384. *
  1385. * @param {string} key The key to check.
  1386. * @param {Array<*>} value The values that will match.
  1387. * @returns {Parse.Query} Returns the query, so you can chain this call.
  1388. */
  1389. }, {
  1390. key: "containedIn",
  1391. value: function (key /*: string*/, value /*: Array<mixed>*/) /*: ParseQuery*/{
  1392. return this._addCondition(key, '$in', value);
  1393. }
  1394. /**
  1395. * Adds a constraint to the query that requires a particular key's value to
  1396. * not be contained in the provided list of values.
  1397. *
  1398. * @param {string} key The key to check.
  1399. * @param {Array<*>} value The values that will not match.
  1400. * @returns {Parse.Query} Returns the query, so you can chain this call.
  1401. */
  1402. }, {
  1403. key: "notContainedIn",
  1404. value: function (key /*: string*/, value /*: Array<mixed>*/) /*: ParseQuery*/{
  1405. return this._addCondition(key, '$nin', value);
  1406. }
  1407. /**
  1408. * Adds a constraint to the query that requires a particular key's value to
  1409. * be contained by the provided list of values. Get objects where all array elements match.
  1410. *
  1411. * @param {string} key The key to check.
  1412. * @param {Array} values The values that will match.
  1413. * @returns {Parse.Query} Returns the query, so you can chain this call.
  1414. */
  1415. }, {
  1416. key: "containedBy",
  1417. value: function (key /*: string*/, values /*: Array<mixed>*/) /*: ParseQuery*/{
  1418. return this._addCondition(key, '$containedBy', values);
  1419. }
  1420. /**
  1421. * Adds a constraint to the query that requires a particular key's value to
  1422. * contain each one of the provided list of values.
  1423. *
  1424. * @param {string} key The key to check. This key's value must be an array.
  1425. * @param {Array} values The values that will match.
  1426. * @returns {Parse.Query} Returns the query, so you can chain this call.
  1427. */
  1428. }, {
  1429. key: "containsAll",
  1430. value: function (key /*: string*/, values /*: Array<mixed>*/) /*: ParseQuery*/{
  1431. return this._addCondition(key, '$all', values);
  1432. }
  1433. /**
  1434. * Adds a constraint to the query that requires a particular key's value to
  1435. * contain each one of the provided list of values starting with given strings.
  1436. *
  1437. * @param {string} key The key to check. This key's value must be an array.
  1438. * @param {Array<string>} values The string values that will match as starting string.
  1439. * @returns {Parse.Query} Returns the query, so you can chain this call.
  1440. */
  1441. }, {
  1442. key: "containsAllStartingWith",
  1443. value: function (key /*: string*/, values /*: Array<string>*/) /*: ParseQuery*/{
  1444. var _this = this;
  1445. if (!(0, _isArray.default)(values)) {
  1446. values = [values];
  1447. }
  1448. var regexObject = (0, _map2.default)(values).call(values, function (value) {
  1449. return {
  1450. $regex: _this._regexStartWith(value)
  1451. };
  1452. });
  1453. return this.containsAll(key, regexObject);
  1454. }
  1455. /**
  1456. * Adds a constraint for finding objects that contain the given key.
  1457. *
  1458. * @param {string} key The key that should exist.
  1459. * @returns {Parse.Query} Returns the query, so you can chain this call.
  1460. */
  1461. }, {
  1462. key: "exists",
  1463. value: function (key /*: string*/) /*: ParseQuery*/{
  1464. return this._addCondition(key, '$exists', true);
  1465. }
  1466. /**
  1467. * Adds a constraint for finding objects that do not contain a given key.
  1468. *
  1469. * @param {string} key The key that should not exist
  1470. * @returns {Parse.Query} Returns the query, so you can chain this call.
  1471. */
  1472. }, {
  1473. key: "doesNotExist",
  1474. value: function (key /*: string*/) /*: ParseQuery*/{
  1475. return this._addCondition(key, '$exists', false);
  1476. }
  1477. /**
  1478. * Adds a regular expression constraint for finding string values that match
  1479. * the provided regular expression.
  1480. * This may be slow for large datasets.
  1481. *
  1482. * @param {string} key The key that the string to match is stored in.
  1483. * @param {RegExp} regex The regular expression pattern to match.
  1484. * @param {string} modifiers The regular expression mode.
  1485. * @returns {Parse.Query} Returns the query, so you can chain this call.
  1486. */
  1487. }, {
  1488. key: "matches",
  1489. value: function (key /*: string*/, regex /*: RegExp*/, modifiers /*: string*/) /*: ParseQuery*/{
  1490. this._addCondition(key, '$regex', regex);
  1491. if (!modifiers) {
  1492. modifiers = '';
  1493. }
  1494. if (regex.ignoreCase) {
  1495. modifiers += 'i';
  1496. }
  1497. if (regex.multiline) {
  1498. modifiers += 'm';
  1499. }
  1500. if (modifiers.length) {
  1501. this._addCondition(key, '$options', modifiers);
  1502. }
  1503. return this;
  1504. }
  1505. /**
  1506. * Adds a constraint that requires that a key's value matches a Parse.Query
  1507. * constraint.
  1508. *
  1509. * @param {string} key The key that the contains the object to match the
  1510. * query.
  1511. * @param {Parse.Query} query The query that should match.
  1512. * @returns {Parse.Query} Returns the query, so you can chain this call.
  1513. */
  1514. }, {
  1515. key: "matchesQuery",
  1516. value: function (key /*: string*/, query /*: ParseQuery*/) /*: ParseQuery*/{
  1517. var queryJSON = query.toJSON();
  1518. queryJSON.className = query.className;
  1519. return this._addCondition(key, '$inQuery', queryJSON);
  1520. }
  1521. /**
  1522. * Adds a constraint that requires that a key's value not matches a
  1523. * Parse.Query constraint.
  1524. *
  1525. * @param {string} key The key that the contains the object to match the
  1526. * query.
  1527. * @param {Parse.Query} query The query that should not match.
  1528. * @returns {Parse.Query} Returns the query, so you can chain this call.
  1529. */
  1530. }, {
  1531. key: "doesNotMatchQuery",
  1532. value: function (key /*: string*/, query /*: ParseQuery*/) /*: ParseQuery*/{
  1533. var queryJSON = query.toJSON();
  1534. queryJSON.className = query.className;
  1535. return this._addCondition(key, '$notInQuery', queryJSON);
  1536. }
  1537. /**
  1538. * Adds a constraint that requires that a key's value matches a value in
  1539. * an object returned by a different Parse.Query.
  1540. *
  1541. * @param {string} key The key that contains the value that is being
  1542. * matched.
  1543. * @param {string} queryKey The key in the objects returned by the query to
  1544. * match against.
  1545. * @param {Parse.Query} query The query to run.
  1546. * @returns {Parse.Query} Returns the query, so you can chain this call.
  1547. */
  1548. }, {
  1549. key: "matchesKeyInQuery",
  1550. value: function (key /*: string*/, queryKey /*: string*/, query /*: ParseQuery*/) /*: ParseQuery*/{
  1551. var queryJSON = query.toJSON();
  1552. queryJSON.className = query.className;
  1553. return this._addCondition(key, '$select', {
  1554. key: queryKey,
  1555. query: queryJSON
  1556. });
  1557. }
  1558. /**
  1559. * Adds a constraint that requires that a key's value not match a value in
  1560. * an object returned by a different Parse.Query.
  1561. *
  1562. * @param {string} key The key that contains the value that is being
  1563. * excluded.
  1564. * @param {string} queryKey The key in the objects returned by the query to
  1565. * match against.
  1566. * @param {Parse.Query} query The query to run.
  1567. * @returns {Parse.Query} Returns the query, so you can chain this call.
  1568. */
  1569. }, {
  1570. key: "doesNotMatchKeyInQuery",
  1571. value: function (key /*: string*/, queryKey /*: string*/, query /*: ParseQuery*/) /*: ParseQuery*/{
  1572. var queryJSON = query.toJSON();
  1573. queryJSON.className = query.className;
  1574. return this._addCondition(key, '$dontSelect', {
  1575. key: queryKey,
  1576. query: queryJSON
  1577. });
  1578. }
  1579. /**
  1580. * Adds a constraint for finding string values that contain a provided
  1581. * string. This may be slow for large datasets.
  1582. *
  1583. * @param {string} key The key that the string to match is stored in.
  1584. * @param {string} substring The substring that the value must contain.
  1585. * @returns {Parse.Query} Returns the query, so you can chain this call.
  1586. */
  1587. }, {
  1588. key: "contains",
  1589. value: function (key /*: string*/, substring /*: string*/) /*: ParseQuery*/{
  1590. if (typeof substring !== 'string') {
  1591. throw new Error('The value being searched for must be a string.');
  1592. }
  1593. return this._addCondition(key, '$regex', quote(substring));
  1594. }
  1595. /**
  1596. * Adds a constraint for finding string values that contain a provided
  1597. * string. This may be slow for large datasets. Requires Parse-Server > 2.5.0
  1598. *
  1599. * In order to sort you must use select and ascending ($score is required)
  1600. * <pre>
  1601. * query.fullText('field', 'term');
  1602. * query.ascending('$score');
  1603. * query.select('$score');
  1604. * </pre>
  1605. *
  1606. * To retrieve the weight / rank
  1607. * <pre>
  1608. * object->get('score');
  1609. * </pre>
  1610. *
  1611. * You can define optionals by providing an object as a third parameter
  1612. * <pre>
  1613. * query.fullText('field', 'term', { language: 'es', diacriticSensitive: true });
  1614. * </pre>
  1615. *
  1616. * @param {string} key The key that the string to match is stored in.
  1617. * @param {string} value The string to search
  1618. * @param {object} options (Optional)
  1619. * @param {string} options.language The language that determines the list of stop words for the search and the rules for the stemmer and tokenizer.
  1620. * @param {boolean} options.caseSensitive A boolean flag to enable or disable case sensitive search.
  1621. * @param {boolean} options.diacriticSensitive A boolean flag to enable or disable diacritic sensitive search.
  1622. * @returns {Parse.Query} Returns the query, so you can chain this call.
  1623. */
  1624. }, {
  1625. key: "fullText",
  1626. value: function (key /*: string*/, value /*: string*/, options /*: ?Object*/) /*: ParseQuery*/{
  1627. options = options || {};
  1628. if (!key) {
  1629. throw new Error('A key is required.');
  1630. }
  1631. if (!value) {
  1632. throw new Error('A search term is required');
  1633. }
  1634. if (typeof value !== 'string') {
  1635. throw new Error('The value being searched for must be a string.');
  1636. }
  1637. var fullOptions = {};
  1638. fullOptions.$term = value;
  1639. for (var option in options) {
  1640. switch (option) {
  1641. case 'language':
  1642. fullOptions.$language = options[option];
  1643. break;
  1644. case 'caseSensitive':
  1645. fullOptions.$caseSensitive = options[option];
  1646. break;
  1647. case 'diacriticSensitive':
  1648. fullOptions.$diacriticSensitive = options[option];
  1649. break;
  1650. default:
  1651. throw new Error("Unknown option: ".concat(option));
  1652. }
  1653. }
  1654. return this._addCondition(key, '$text', {
  1655. $search: fullOptions
  1656. });
  1657. }
  1658. /**
  1659. * Method to sort the full text search by text score
  1660. *
  1661. * @returns {Parse.Query} Returns the query, so you can chain this call.
  1662. */
  1663. }, {
  1664. key: "sortByTextScore",
  1665. value: function () {
  1666. this.ascending('$score');
  1667. this.select(['$score']);
  1668. return this;
  1669. }
  1670. /**
  1671. * Adds a constraint for finding string values that start with a provided
  1672. * string. This query will use the backend index, so it will be fast even
  1673. * for large datasets.
  1674. *
  1675. * @param {string} key The key that the string to match is stored in.
  1676. * @param {string} prefix The substring that the value must start with.
  1677. * @param {string} modifiers The regular expression mode.
  1678. * @returns {Parse.Query} Returns the query, so you can chain this call.
  1679. */
  1680. }, {
  1681. key: "startsWith",
  1682. value: function (key /*: string*/, prefix /*: string*/, modifiers /*: string*/) /*: ParseQuery*/{
  1683. if (typeof prefix !== 'string') {
  1684. throw new Error('The value being searched for must be a string.');
  1685. }
  1686. return this.matches(key, this._regexStartWith(prefix), modifiers);
  1687. }
  1688. /**
  1689. * Adds a constraint for finding string values that end with a provided
  1690. * string. This will be slow for large datasets.
  1691. *
  1692. * @param {string} key The key that the string to match is stored in.
  1693. * @param {string} suffix The substring that the value must end with.
  1694. * @param {string} modifiers The regular expression mode.
  1695. * @returns {Parse.Query} Returns the query, so you can chain this call.
  1696. */
  1697. }, {
  1698. key: "endsWith",
  1699. value: function (key /*: string*/, suffix /*: string*/, modifiers /*: string*/) /*: ParseQuery*/{
  1700. if (typeof suffix !== 'string') {
  1701. throw new Error('The value being searched for must be a string.');
  1702. }
  1703. return this.matches(key, quote(suffix) + '$', modifiers);
  1704. }
  1705. /**
  1706. * Adds a proximity based constraint for finding objects with key point
  1707. * values near the point given.
  1708. *
  1709. * @param {string} key The key that the Parse.GeoPoint is stored in.
  1710. * @param {Parse.GeoPoint} point The reference Parse.GeoPoint that is used.
  1711. * @returns {Parse.Query} Returns the query, so you can chain this call.
  1712. */
  1713. }, {
  1714. key: "near",
  1715. value: function (key /*: string*/, point /*: ParseGeoPoint*/) /*: ParseQuery*/{
  1716. if (!(point instanceof _ParseGeoPoint.default)) {
  1717. // Try to cast it as a GeoPoint
  1718. point = new _ParseGeoPoint.default(point);
  1719. }
  1720. return this._addCondition(key, '$nearSphere', point);
  1721. }
  1722. /**
  1723. * Adds a proximity based constraint for finding objects with key point
  1724. * values near the point given and within the maximum distance given.
  1725. *
  1726. * @param {string} key The key that the Parse.GeoPoint is stored in.
  1727. * @param {Parse.GeoPoint} point The reference Parse.GeoPoint that is used.
  1728. * @param {number} maxDistance Maximum distance (in radians) of results to return.
  1729. * @param {boolean} sorted A Bool value that is true if results should be
  1730. * sorted by distance ascending, false is no sorting is required,
  1731. * defaults to true.
  1732. * @returns {Parse.Query} Returns the query, so you can chain this call.
  1733. */
  1734. }, {
  1735. key: "withinRadians",
  1736. value: function (key /*: string*/, point /*: ParseGeoPoint*/, maxDistance /*: number*/, sorted /*: boolean*/) /*: ParseQuery*/{
  1737. if (sorted || sorted === undefined) {
  1738. this.near(key, point);
  1739. return this._addCondition(key, '$maxDistance', maxDistance);
  1740. } else {
  1741. return this._addCondition(key, '$geoWithin', {
  1742. $centerSphere: [[point.longitude, point.latitude], maxDistance]
  1743. });
  1744. }
  1745. }
  1746. /**
  1747. * Adds a proximity based constraint for finding objects with key point
  1748. * values near the point given and within the maximum distance given.
  1749. * Radius of earth used is 3958.8 miles.
  1750. *
  1751. * @param {string} key The key that the Parse.GeoPoint is stored in.
  1752. * @param {Parse.GeoPoint} point The reference Parse.GeoPoint that is used.
  1753. * @param {number} maxDistance Maximum distance (in miles) of results to return.
  1754. * @param {boolean} sorted A Bool value that is true if results should be
  1755. * sorted by distance ascending, false is no sorting is required,
  1756. * defaults to true.
  1757. * @returns {Parse.Query} Returns the query, so you can chain this call.
  1758. */
  1759. }, {
  1760. key: "withinMiles",
  1761. value: function (key /*: string*/, point /*: ParseGeoPoint*/, maxDistance /*: number*/, sorted /*: boolean*/) /*: ParseQuery*/{
  1762. return this.withinRadians(key, point, maxDistance / 3958.8, sorted);
  1763. }
  1764. /**
  1765. * Adds a proximity based constraint for finding objects with key point
  1766. * values near the point given and within the maximum distance given.
  1767. * Radius of earth used is 6371.0 kilometers.
  1768. *
  1769. * @param {string} key The key that the Parse.GeoPoint is stored in.
  1770. * @param {Parse.GeoPoint} point The reference Parse.GeoPoint that is used.
  1771. * @param {number} maxDistance Maximum distance (in kilometers) of results to return.
  1772. * @param {boolean} sorted A Bool value that is true if results should be
  1773. * sorted by distance ascending, false is no sorting is required,
  1774. * defaults to true.
  1775. * @returns {Parse.Query} Returns the query, so you can chain this call.
  1776. */
  1777. }, {
  1778. key: "withinKilometers",
  1779. value: function (key /*: string*/, point /*: ParseGeoPoint*/, maxDistance /*: number*/, sorted /*: boolean*/) /*: ParseQuery*/{
  1780. return this.withinRadians(key, point, maxDistance / 6371.0, sorted);
  1781. }
  1782. /**
  1783. * Adds a constraint to the query that requires a particular key's
  1784. * coordinates be contained within a given rectangular geographic bounding
  1785. * box.
  1786. *
  1787. * @param {string} key The key to be constrained.
  1788. * @param {Parse.GeoPoint} southwest
  1789. * The lower-left inclusive corner of the box.
  1790. * @param {Parse.GeoPoint} northeast
  1791. * The upper-right inclusive corner of the box.
  1792. * @returns {Parse.Query} Returns the query, so you can chain this call.
  1793. */
  1794. }, {
  1795. key: "withinGeoBox",
  1796. value: function (key /*: string*/, southwest /*: ParseGeoPoint*/, northeast /*: ParseGeoPoint*/) /*: ParseQuery*/{
  1797. if (!(southwest instanceof _ParseGeoPoint.default)) {
  1798. southwest = new _ParseGeoPoint.default(southwest);
  1799. }
  1800. if (!(northeast instanceof _ParseGeoPoint.default)) {
  1801. northeast = new _ParseGeoPoint.default(northeast);
  1802. }
  1803. this._addCondition(key, '$within', {
  1804. $box: [southwest, northeast]
  1805. });
  1806. return this;
  1807. }
  1808. /**
  1809. * Adds a constraint to the query that requires a particular key's
  1810. * coordinates be contained within and on the bounds of a given polygon.
  1811. * Supports closed and open (last point is connected to first) paths
  1812. *
  1813. * Polygon must have at least 3 points
  1814. *
  1815. * @param {string} key The key to be constrained.
  1816. * @param {Array} points Array of Coordinates / GeoPoints
  1817. * @returns {Parse.Query} Returns the query, so you can chain this call.
  1818. */
  1819. }, {
  1820. key: "withinPolygon",
  1821. value: function (key /*: string*/, points /*: Array<Array<number>>*/) /*: ParseQuery*/{
  1822. return this._addCondition(key, '$geoWithin', {
  1823. $polygon: points
  1824. });
  1825. }
  1826. /**
  1827. * Add a constraint to the query that requires a particular key's
  1828. * coordinates that contains a ParseGeoPoint
  1829. *
  1830. * @param {string} key The key to be constrained.
  1831. * @param {Parse.GeoPoint} point
  1832. * @returns {Parse.Query} Returns the query, so you can chain this call.
  1833. */
  1834. }, {
  1835. key: "polygonContains",
  1836. value: function (key /*: string*/, point /*: ParseGeoPoint*/) /*: ParseQuery*/{
  1837. return this._addCondition(key, '$geoIntersects', {
  1838. $point: point
  1839. });
  1840. }
  1841. /* Query Orderings */
  1842. /**
  1843. * Sorts the results in ascending order by the given key.
  1844. *
  1845. * @param {(string|string[])} keys The key to order by, which is a
  1846. * string of comma separated values, or an Array of keys, or multiple keys.
  1847. * @returns {Parse.Query} Returns the query, so you can chain this call.
  1848. */
  1849. }, {
  1850. key: "ascending",
  1851. value: function () /*: ParseQuery*/{
  1852. this._order = [];
  1853. for (var _len = arguments.length, keys = new Array(_len), _key5 = 0; _key5 < _len; _key5++) {
  1854. keys[_key5] = arguments[_key5];
  1855. }
  1856. return this.addAscending.apply(this, keys);
  1857. }
  1858. /**
  1859. * Sorts the results in ascending order by the given key,
  1860. * but can also add secondary sort descriptors without overwriting _order.
  1861. *
  1862. * @param {(string|string[])} keys The key to order by, which is a
  1863. * string of comma separated values, or an Array of keys, or multiple keys.
  1864. * @returns {Parse.Query} Returns the query, so you can chain this call.
  1865. */
  1866. }, {
  1867. key: "addAscending",
  1868. value: function () /*: ParseQuery*/{
  1869. var _this9 = this;
  1870. if (!this._order) {
  1871. this._order = [];
  1872. }
  1873. for (var _len2 = arguments.length, keys = new Array(_len2), _key6 = 0; _key6 < _len2; _key6++) {
  1874. keys[_key6] = arguments[_key6];
  1875. }
  1876. (0, _forEach.default)(keys).call(keys, function (key) {
  1877. var _context16;
  1878. if ((0, _isArray.default)(key)) {
  1879. key = key.join();
  1880. }
  1881. _this9._order = (0, _concat.default)(_context16 = _this9._order).call(_context16, key.replace(/\s/g, '').split(','));
  1882. });
  1883. return this;
  1884. }
  1885. /**
  1886. * Sorts the results in descending order by the given key.
  1887. *
  1888. * @param {(string|string[])} keys The key to order by, which is a
  1889. * string of comma separated values, or an Array of keys, or multiple keys.
  1890. * @returns {Parse.Query} Returns the query, so you can chain this call.
  1891. */
  1892. }, {
  1893. key: "descending",
  1894. value: function () /*: ParseQuery*/{
  1895. this._order = [];
  1896. for (var _len3 = arguments.length, keys = new Array(_len3), _key7 = 0; _key7 < _len3; _key7++) {
  1897. keys[_key7] = arguments[_key7];
  1898. }
  1899. return this.addDescending.apply(this, keys);
  1900. }
  1901. /**
  1902. * Sorts the results in descending order by the given key,
  1903. * but can also add secondary sort descriptors without overwriting _order.
  1904. *
  1905. * @param {(string|string[])} keys The key to order by, which is a
  1906. * string of comma separated values, or an Array of keys, or multiple keys.
  1907. * @returns {Parse.Query} Returns the query, so you can chain this call.
  1908. */
  1909. }, {
  1910. key: "addDescending",
  1911. value: function () /*: ParseQuery*/{
  1912. var _this10 = this;
  1913. if (!this._order) {
  1914. this._order = [];
  1915. }
  1916. for (var _len4 = arguments.length, keys = new Array(_len4), _key8 = 0; _key8 < _len4; _key8++) {
  1917. keys[_key8] = arguments[_key8];
  1918. }
  1919. (0, _forEach.default)(keys).call(keys, function (key) {
  1920. var _context17, _context18;
  1921. if ((0, _isArray.default)(key)) {
  1922. key = key.join();
  1923. }
  1924. _this10._order = (0, _concat.default)(_context17 = _this10._order).call(_context17, (0, _map2.default)(_context18 = key.replace(/\s/g, '').split(',')).call(_context18, function (k) {
  1925. return '-' + k;
  1926. }));
  1927. });
  1928. return this;
  1929. }
  1930. /* Query Options */
  1931. /**
  1932. * Sets the number of results to skip before returning any results.
  1933. * This is useful for pagination.
  1934. * Default is to skip zero results.
  1935. *
  1936. * @param {number} n the number of results to skip.
  1937. * @returns {Parse.Query} Returns the query, so you can chain this call.
  1938. */
  1939. }, {
  1940. key: "skip",
  1941. value: function (n /*: number*/) /*: ParseQuery*/{
  1942. if (typeof n !== 'number' || n < 0) {
  1943. throw new Error('You can only skip by a positive number');
  1944. }
  1945. this._skip = n;
  1946. return this;
  1947. }
  1948. /**
  1949. * Sets the limit of the number of results to return. The default limit is 100.
  1950. *
  1951. * @param {number} n the number of results to limit to.
  1952. * @returns {Parse.Query} Returns the query, so you can chain this call.
  1953. */
  1954. }, {
  1955. key: "limit",
  1956. value: function (n /*: number*/) /*: ParseQuery*/{
  1957. if (typeof n !== 'number') {
  1958. throw new Error('You can only set the limit to a numeric value');
  1959. }
  1960. this._limit = n;
  1961. return this;
  1962. }
  1963. /**
  1964. * Sets the flag to include with response the total number of objects satisfying this query,
  1965. * despite limits/skip. Might be useful for pagination.
  1966. * Note that result of this query will be wrapped as an object with
  1967. * `results`: holding {ParseObject} array and `count`: integer holding total number
  1968. *
  1969. * @param {boolean} includeCount false - disable, true - enable.
  1970. * @returns {Parse.Query} Returns the query, so you can chain this call.
  1971. */
  1972. }, {
  1973. key: "withCount",
  1974. value: function () /*: ParseQuery*/{
  1975. var includeCount /*: boolean*/ = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : true;
  1976. if (typeof includeCount !== 'boolean') {
  1977. throw new Error('You can only set withCount to a boolean value');
  1978. }
  1979. this._count = includeCount;
  1980. return this;
  1981. }
  1982. /**
  1983. * Includes nested Parse.Objects for the provided key. You can use dot
  1984. * notation to specify which fields in the included object are also fetched.
  1985. *
  1986. * You can include all nested Parse.Objects by passing in '*'.
  1987. * Requires Parse Server 3.0.0+
  1988. * <pre>query.include('*');</pre>
  1989. *
  1990. * @param {...string|Array<string>} keys The name(s) of the key(s) to include.
  1991. * @returns {Parse.Query} Returns the query, so you can chain this call.
  1992. */
  1993. }, {
  1994. key: "include",
  1995. value: function () /*: ParseQuery*/{
  1996. var _this11 = this;
  1997. for (var _len5 = arguments.length, keys = new Array(_len5), _key9 = 0; _key9 < _len5; _key9++) {
  1998. keys[_key9] = arguments[_key9];
  1999. }
  2000. (0, _forEach.default)(keys).call(keys, function (key) {
  2001. if ((0, _isArray.default)(key)) {
  2002. var _context19;
  2003. _this11._include = (0, _concat.default)(_context19 = _this11._include).call(_context19, key);
  2004. } else {
  2005. _this11._include.push(key);
  2006. }
  2007. });
  2008. return this;
  2009. }
  2010. /**
  2011. * Includes all nested Parse.Objects one level deep.
  2012. *
  2013. * Requires Parse Server 3.0.0+
  2014. *
  2015. * @returns {Parse.Query} Returns the query, so you can chain this call.
  2016. */
  2017. }, {
  2018. key: "includeAll",
  2019. value: function () /*: ParseQuery*/{
  2020. return this.include('*');
  2021. }
  2022. /**
  2023. * Restricts the fields of the returned Parse.Objects to include only the
  2024. * provided keys. If this is called multiple times, then all of the keys
  2025. * specified in each of the calls will be included.
  2026. *
  2027. * @param {...string|Array<string>} keys The name(s) of the key(s) to include.
  2028. * @returns {Parse.Query} Returns the query, so you can chain this call.
  2029. */
  2030. }, {
  2031. key: "select",
  2032. value: function () /*: ParseQuery*/{
  2033. var _this12 = this;
  2034. if (!this._select) {
  2035. this._select = [];
  2036. }
  2037. for (var _len6 = arguments.length, keys = new Array(_len6), _key10 = 0; _key10 < _len6; _key10++) {
  2038. keys[_key10] = arguments[_key10];
  2039. }
  2040. (0, _forEach.default)(keys).call(keys, function (key) {
  2041. if ((0, _isArray.default)(key)) {
  2042. var _context20;
  2043. _this12._select = (0, _concat.default)(_context20 = _this12._select).call(_context20, key);
  2044. } else {
  2045. _this12._select.push(key);
  2046. }
  2047. });
  2048. return this;
  2049. }
  2050. /**
  2051. * Restricts the fields of the returned Parse.Objects to all keys except the
  2052. * provided keys. Exclude takes precedence over select and include.
  2053. *
  2054. * Requires Parse Server 3.6.0+
  2055. *
  2056. * @param {...string|Array<string>} keys The name(s) of the key(s) to exclude.
  2057. * @returns {Parse.Query} Returns the query, so you can chain this call.
  2058. */
  2059. }, {
  2060. key: "exclude",
  2061. value: function () /*: ParseQuery*/{
  2062. var _this13 = this;
  2063. for (var _len7 = arguments.length, keys = new Array(_len7), _key11 = 0; _key11 < _len7; _key11++) {
  2064. keys[_key11] = arguments[_key11];
  2065. }
  2066. (0, _forEach.default)(keys).call(keys, function (key) {
  2067. if ((0, _isArray.default)(key)) {
  2068. var _context21;
  2069. _this13._exclude = (0, _concat.default)(_context21 = _this13._exclude).call(_context21, key);
  2070. } else {
  2071. _this13._exclude.push(key);
  2072. }
  2073. });
  2074. return this;
  2075. }
  2076. /**
  2077. * Restricts live query to trigger only for watched fields.
  2078. *
  2079. * Requires Parse Server 6.0.0+
  2080. *
  2081. * @param {...string|Array<string>} keys The name(s) of the key(s) to watch.
  2082. * @returns {Parse.Query} Returns the query, so you can chain this call.
  2083. */
  2084. }, {
  2085. key: "watch",
  2086. value: function () /*: ParseQuery*/{
  2087. var _this14 = this;
  2088. for (var _len8 = arguments.length, keys = new Array(_len8), _key12 = 0; _key12 < _len8; _key12++) {
  2089. keys[_key12] = arguments[_key12];
  2090. }
  2091. (0, _forEach.default)(keys).call(keys, function (key) {
  2092. if ((0, _isArray.default)(key)) {
  2093. var _context22;
  2094. _this14._watch = (0, _concat.default)(_context22 = _this14._watch).call(_context22, key);
  2095. } else {
  2096. _this14._watch.push(key);
  2097. }
  2098. });
  2099. return this;
  2100. }
  2101. /**
  2102. * Changes the read preference that the backend will use when performing the query to the database.
  2103. *
  2104. * @param {string} readPreference The read preference for the main query.
  2105. * @param {string} includeReadPreference The read preference for the queries to include pointers.
  2106. * @param {string} subqueryReadPreference The read preference for the sub queries.
  2107. * @returns {Parse.Query} Returns the query, so you can chain this call.
  2108. */
  2109. }, {
  2110. key: "readPreference",
  2111. value: function (_readPreference /*: string*/, includeReadPreference /*:: ?: string*/, subqueryReadPreference /*:: ?: string*/) /*: ParseQuery*/{
  2112. this._readPreference = _readPreference;
  2113. this._includeReadPreference = includeReadPreference;
  2114. this._subqueryReadPreference = subqueryReadPreference;
  2115. return this;
  2116. }
  2117. /**
  2118. * Subscribe this query to get liveQuery updates
  2119. *
  2120. * @param {string} sessionToken (optional) Defaults to the currentUser
  2121. * @returns {Promise<LiveQuerySubscription>} Returns the liveQuerySubscription, it's an event emitter
  2122. * which can be used to get liveQuery updates.
  2123. */
  2124. }, {
  2125. key: "subscribe",
  2126. value: function () {
  2127. var _subscribe = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee7(sessionToken /*:: ?: string*/) {
  2128. var currentUser, liveQueryClient, subscription;
  2129. return _regenerator.default.wrap(function (_context23) {
  2130. while (1) switch (_context23.prev = _context23.next) {
  2131. case 0:
  2132. _context23.next = 2;
  2133. return _CoreManager.default.getUserController().currentUserAsync();
  2134. case 2:
  2135. currentUser = _context23.sent;
  2136. if (!sessionToken) {
  2137. sessionToken = currentUser ? currentUser.getSessionToken() : undefined;
  2138. }
  2139. _context23.next = 6;
  2140. return _CoreManager.default.getLiveQueryController().getDefaultLiveQueryClient();
  2141. case 6:
  2142. liveQueryClient = _context23.sent;
  2143. if (liveQueryClient.shouldOpen()) {
  2144. liveQueryClient.open();
  2145. }
  2146. subscription = liveQueryClient.subscribe(this, sessionToken);
  2147. return _context23.abrupt("return", subscription.subscribePromise.then(function () {
  2148. return subscription;
  2149. }));
  2150. case 10:
  2151. case "end":
  2152. return _context23.stop();
  2153. }
  2154. }, _callee7, this);
  2155. }));
  2156. function subscribe() {
  2157. return _subscribe.apply(this, arguments);
  2158. }
  2159. return subscribe;
  2160. }()
  2161. /**
  2162. * Constructs a Parse.Query that is the OR of the passed in queries. For
  2163. * example:
  2164. * <pre>var compoundQuery = Parse.Query.or(query1, query2, query3);</pre>
  2165. *
  2166. * will create a compoundQuery that is an or of the query1, query2, and
  2167. * query3.
  2168. *
  2169. * @param {...Parse.Query} queries The list of queries to OR.
  2170. * @static
  2171. * @returns {Parse.Query} The query that is the OR of the passed in queries.
  2172. */
  2173. }, {
  2174. key: "fromNetwork",
  2175. value:
  2176. /**
  2177. * Change the source of this query to the server.
  2178. *
  2179. * @returns {Parse.Query} Returns the query, so you can chain this call.
  2180. */
  2181. function () /*: ParseQuery*/{
  2182. this._queriesLocalDatastore = false;
  2183. this._localDatastorePinName = null;
  2184. return this;
  2185. }
  2186. /**
  2187. * Changes the source of this query to all pinned objects.
  2188. *
  2189. * @returns {Parse.Query} Returns the query, so you can chain this call.
  2190. */
  2191. }, {
  2192. key: "fromLocalDatastore",
  2193. value: function () /*: ParseQuery*/{
  2194. return this.fromPinWithName(null);
  2195. }
  2196. /**
  2197. * Changes the source of this query to the default group of pinned objects.
  2198. *
  2199. * @returns {Parse.Query} Returns the query, so you can chain this call.
  2200. */
  2201. }, {
  2202. key: "fromPin",
  2203. value: function () /*: ParseQuery*/{
  2204. return this.fromPinWithName(_LocalDatastoreUtils.DEFAULT_PIN);
  2205. }
  2206. /**
  2207. * Changes the source of this query to a specific group of pinned objects.
  2208. *
  2209. * @param {string} name The name of query source.
  2210. * @returns {Parse.Query} Returns the query, so you can chain this call.
  2211. */
  2212. }, {
  2213. key: "fromPinWithName",
  2214. value: function (name /*:: ?: string*/) /*: ParseQuery*/{
  2215. var localDatastore = _CoreManager.default.getLocalDatastore();
  2216. if (localDatastore.checkIfEnabled()) {
  2217. this._queriesLocalDatastore = true;
  2218. this._localDatastorePinName = name;
  2219. }
  2220. return this;
  2221. }
  2222. /**
  2223. * Cancels the current network request (if any is running).
  2224. *
  2225. * @returns {Parse.Query} Returns the query, so you can chain this call.
  2226. */
  2227. }, {
  2228. key: "cancel",
  2229. value: function () /*: ParseQuery*/{
  2230. var _this15 = this;
  2231. if (this._xhrRequest.task && typeof this._xhrRequest.task.abort === 'function') {
  2232. this._xhrRequest.task._aborted = true;
  2233. this._xhrRequest.task.abort();
  2234. this._xhrRequest.task = null;
  2235. this._xhrRequest.onchange = function () {};
  2236. return this;
  2237. }
  2238. return this._xhrRequest.onchange = function () {
  2239. return _this15.cancel();
  2240. };
  2241. }
  2242. }, {
  2243. key: "_setRequestTask",
  2244. value: function (options) {
  2245. var _this16 = this;
  2246. options.requestTask = function (task) {
  2247. _this16._xhrRequest.task = task;
  2248. _this16._xhrRequest.onchange();
  2249. };
  2250. }
  2251. }], [{
  2252. key: "fromJSON",
  2253. value: function (className /*: string*/, json /*: QueryJSON*/) /*: ParseQuery*/{
  2254. var query = new ParseQuery(className);
  2255. return query.withJSON(json);
  2256. }
  2257. }, {
  2258. key: "or",
  2259. value: function () /*: ParseQuery*/{
  2260. for (var _len9 = arguments.length, queries = new Array(_len9), _key13 = 0; _key13 < _len9; _key13++) {
  2261. queries[_key13] = arguments[_key13];
  2262. }
  2263. var className = _getClassNameFromQueries(queries);
  2264. var query = new ParseQuery(className);
  2265. query._orQuery(queries);
  2266. return query;
  2267. }
  2268. /**
  2269. * Constructs a Parse.Query that is the AND of the passed in queries. For
  2270. * example:
  2271. * <pre>var compoundQuery = Parse.Query.and(query1, query2, query3);</pre>
  2272. *
  2273. * will create a compoundQuery that is an and of the query1, query2, and
  2274. * query3.
  2275. *
  2276. * @param {...Parse.Query} queries The list of queries to AND.
  2277. * @static
  2278. * @returns {Parse.Query} The query that is the AND of the passed in queries.
  2279. */
  2280. }, {
  2281. key: "and",
  2282. value: function () /*: ParseQuery*/{
  2283. for (var _len10 = arguments.length, queries = new Array(_len10), _key14 = 0; _key14 < _len10; _key14++) {
  2284. queries[_key14] = arguments[_key14];
  2285. }
  2286. var className = _getClassNameFromQueries(queries);
  2287. var query = new ParseQuery(className);
  2288. query._andQuery(queries);
  2289. return query;
  2290. }
  2291. /**
  2292. * Constructs a Parse.Query that is the NOR of the passed in queries. For
  2293. * example:
  2294. * <pre>const compoundQuery = Parse.Query.nor(query1, query2, query3);</pre>
  2295. *
  2296. * will create a compoundQuery that is a nor of the query1, query2, and
  2297. * query3.
  2298. *
  2299. * @param {...Parse.Query} queries The list of queries to NOR.
  2300. * @static
  2301. * @returns {Parse.Query} The query that is the NOR of the passed in queries.
  2302. */
  2303. }, {
  2304. key: "nor",
  2305. value: function () /*: ParseQuery*/{
  2306. for (var _len11 = arguments.length, queries = new Array(_len11), _key15 = 0; _key15 < _len11; _key15++) {
  2307. queries[_key15] = arguments[_key15];
  2308. }
  2309. var className = _getClassNameFromQueries(queries);
  2310. var query = new ParseQuery(className);
  2311. query._norQuery(queries);
  2312. return query;
  2313. }
  2314. }]);
  2315. return ParseQuery;
  2316. }();
  2317. var DefaultController = {
  2318. find: function (className /*: string*/, params /*: QueryJSON*/, options /*: RequestOptions*/) /*: Promise<Array<ParseObject>>*/{
  2319. var RESTController = _CoreManager.default.getRESTController();
  2320. return RESTController.request('GET', 'classes/' + className, params, options);
  2321. },
  2322. aggregate: function (className /*: string*/, params /*: any*/, options /*: RequestOptions*/) /*: Promise<Array<mixed>>*/{
  2323. var RESTController = _CoreManager.default.getRESTController();
  2324. return RESTController.request('GET', 'aggregate/' + className, params, options);
  2325. }
  2326. };
  2327. _CoreManager.default.setQueryController(DefaultController);
  2328. var _default = ParseQuery;
  2329. exports.default = _default;