ParseQuery.js 71 KB

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