ParseQuery.js 56 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974
  1. var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = void 0;
  6. var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));
  7. var _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray"));
  8. var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));
  9. var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));
  10. var _CoreManager = _interopRequireDefault(require("./CoreManager"));
  11. var _encode = _interopRequireDefault(require("./encode"));
  12. var _promiseUtils = require("./promiseUtils");
  13. var _ParseError = _interopRequireDefault(require("./ParseError"));
  14. var _ParseGeoPoint = _interopRequireDefault(require("./ParseGeoPoint"));
  15. var _ParseObject = _interopRequireDefault(require("./ParseObject"));
  16. var _OfflineQuery = _interopRequireDefault(require("./OfflineQuery"));
  17. var _LocalDatastoreUtils = require("./LocalDatastoreUtils");
  18. function _regeneratorRuntime() {
  19. "use strict";
  20. _regeneratorRuntime = function () {
  21. return exports;
  22. };
  23. var exports = {},
  24. Op = Object.prototype,
  25. hasOwn = Op.hasOwnProperty,
  26. $Symbol = "function" == typeof Symbol ? Symbol : {},
  27. iteratorSymbol = $Symbol.iterator || "@@iterator",
  28. asyncIteratorSymbol = $Symbol.asyncIterator || "@@asyncIterator",
  29. toStringTagSymbol = $Symbol.toStringTag || "@@toStringTag";
  30. function define(obj, key, value) {
  31. return Object.defineProperty(obj, key, {
  32. value: value,
  33. enumerable: !0,
  34. configurable: !0,
  35. writable: !0
  36. }), obj[key];
  37. }
  38. try {
  39. define({}, "");
  40. } catch (err) {
  41. define = function (obj, key, value) {
  42. return obj[key] = value;
  43. };
  44. }
  45. function wrap(innerFn, outerFn, self, tryLocsList) {
  46. var protoGenerator = outerFn && outerFn.prototype instanceof Generator ? outerFn : Generator,
  47. generator = Object.create(protoGenerator.prototype),
  48. context = new Context(tryLocsList || []);
  49. return generator._invoke = function (innerFn, self, context) {
  50. var state = "suspendedStart";
  51. return function (method, arg) {
  52. if ("executing" === state) throw new Error("Generator is already running");
  53. if ("completed" === state) {
  54. if ("throw" === method) throw arg;
  55. return doneResult();
  56. }
  57. for (context.method = method, context.arg = arg;;) {
  58. var delegate = context.delegate;
  59. if (delegate) {
  60. var delegateResult = maybeInvokeDelegate(delegate, context);
  61. if (delegateResult) {
  62. if (delegateResult === ContinueSentinel) continue;
  63. return delegateResult;
  64. }
  65. }
  66. if ("next" === context.method) context.sent = context._sent = context.arg;else if ("throw" === context.method) {
  67. if ("suspendedStart" === state) throw state = "completed", context.arg;
  68. context.dispatchException(context.arg);
  69. } else "return" === context.method && context.abrupt("return", context.arg);
  70. state = "executing";
  71. var record = tryCatch(innerFn, self, context);
  72. if ("normal" === record.type) {
  73. if (state = context.done ? "completed" : "suspendedYield", record.arg === ContinueSentinel) continue;
  74. return {
  75. value: record.arg,
  76. done: context.done
  77. };
  78. }
  79. "throw" === record.type && (state = "completed", context.method = "throw", context.arg = record.arg);
  80. }
  81. };
  82. }(innerFn, self, context), generator;
  83. }
  84. function tryCatch(fn, obj, arg) {
  85. try {
  86. return {
  87. type: "normal",
  88. arg: fn.call(obj, arg)
  89. };
  90. } catch (err) {
  91. return {
  92. type: "throw",
  93. arg: err
  94. };
  95. }
  96. }
  97. exports.wrap = wrap;
  98. var ContinueSentinel = {};
  99. function Generator() {}
  100. function GeneratorFunction() {}
  101. function GeneratorFunctionPrototype() {}
  102. var IteratorPrototype = {};
  103. define(IteratorPrototype, iteratorSymbol, function () {
  104. return this;
  105. });
  106. var getProto = Object.getPrototypeOf,
  107. NativeIteratorPrototype = getProto && getProto(getProto(values([])));
  108. NativeIteratorPrototype && NativeIteratorPrototype !== Op && hasOwn.call(NativeIteratorPrototype, iteratorSymbol) && (IteratorPrototype = NativeIteratorPrototype);
  109. var Gp = GeneratorFunctionPrototype.prototype = Generator.prototype = Object.create(IteratorPrototype);
  110. function defineIteratorMethods(prototype) {
  111. ["next", "throw", "return"].forEach(function (method) {
  112. define(prototype, method, function (arg) {
  113. return this._invoke(method, arg);
  114. });
  115. });
  116. }
  117. function AsyncIterator(generator, PromiseImpl) {
  118. function invoke(method, arg, resolve, reject) {
  119. var record = tryCatch(generator[method], generator, arg);
  120. if ("throw" !== record.type) {
  121. var result = record.arg,
  122. value = result.value;
  123. return value && "object" == typeof value && hasOwn.call(value, "__await") ? PromiseImpl.resolve(value.__await).then(function (value) {
  124. invoke("next", value, resolve, reject);
  125. }, function (err) {
  126. invoke("throw", err, resolve, reject);
  127. }) : PromiseImpl.resolve(value).then(function (unwrapped) {
  128. result.value = unwrapped, resolve(result);
  129. }, function (error) {
  130. return invoke("throw", error, resolve, reject);
  131. });
  132. }
  133. reject(record.arg);
  134. }
  135. var previousPromise;
  136. this._invoke = function (method, arg) {
  137. function callInvokeWithMethodAndArg() {
  138. return new PromiseImpl(function (resolve, reject) {
  139. invoke(method, arg, resolve, reject);
  140. });
  141. }
  142. return previousPromise = previousPromise ? previousPromise.then(callInvokeWithMethodAndArg, callInvokeWithMethodAndArg) : callInvokeWithMethodAndArg();
  143. };
  144. }
  145. function maybeInvokeDelegate(delegate, context) {
  146. var method = delegate.iterator[context.method];
  147. if (undefined === method) {
  148. if (context.delegate = null, "throw" === context.method) {
  149. if (delegate.iterator.return && (context.method = "return", context.arg = undefined, maybeInvokeDelegate(delegate, context), "throw" === context.method)) return ContinueSentinel;
  150. context.method = "throw", context.arg = new TypeError("The iterator does not provide a 'throw' method");
  151. }
  152. return ContinueSentinel;
  153. }
  154. var record = tryCatch(method, delegate.iterator, context.arg);
  155. if ("throw" === record.type) return context.method = "throw", context.arg = record.arg, context.delegate = null, ContinueSentinel;
  156. var info = record.arg;
  157. return info ? info.done ? (context[delegate.resultName] = info.value, context.next = delegate.nextLoc, "return" !== context.method && (context.method = "next", context.arg = undefined), context.delegate = null, ContinueSentinel) : info : (context.method = "throw", context.arg = new TypeError("iterator result is not an object"), context.delegate = null, ContinueSentinel);
  158. }
  159. function pushTryEntry(locs) {
  160. var entry = {
  161. tryLoc: locs[0]
  162. };
  163. 1 in locs && (entry.catchLoc = locs[1]), 2 in locs && (entry.finallyLoc = locs[2], entry.afterLoc = locs[3]), this.tryEntries.push(entry);
  164. }
  165. function resetTryEntry(entry) {
  166. var record = entry.completion || {};
  167. record.type = "normal", delete record.arg, entry.completion = record;
  168. }
  169. function Context(tryLocsList) {
  170. this.tryEntries = [{
  171. tryLoc: "root"
  172. }], tryLocsList.forEach(pushTryEntry, this), this.reset(!0);
  173. }
  174. function values(iterable) {
  175. if (iterable) {
  176. var iteratorMethod = iterable[iteratorSymbol];
  177. if (iteratorMethod) return iteratorMethod.call(iterable);
  178. if ("function" == typeof iterable.next) return iterable;
  179. if (!isNaN(iterable.length)) {
  180. var i = -1,
  181. next = function next() {
  182. for (; ++i < iterable.length;) {
  183. if (hasOwn.call(iterable, i)) return next.value = iterable[i], next.done = !1, next;
  184. }
  185. return next.value = undefined, next.done = !0, next;
  186. };
  187. return next.next = next;
  188. }
  189. }
  190. return {
  191. next: doneResult
  192. };
  193. }
  194. function doneResult() {
  195. return {
  196. value: undefined,
  197. done: !0
  198. };
  199. }
  200. return GeneratorFunction.prototype = GeneratorFunctionPrototype, define(Gp, "constructor", GeneratorFunctionPrototype), define(GeneratorFunctionPrototype, "constructor", GeneratorFunction), GeneratorFunction.displayName = define(GeneratorFunctionPrototype, toStringTagSymbol, "GeneratorFunction"), exports.isGeneratorFunction = function (genFun) {
  201. var ctor = "function" == typeof genFun && genFun.constructor;
  202. return !!ctor && (ctor === GeneratorFunction || "GeneratorFunction" === (ctor.displayName || ctor.name));
  203. }, exports.mark = function (genFun) {
  204. return Object.setPrototypeOf ? Object.setPrototypeOf(genFun, GeneratorFunctionPrototype) : (genFun.__proto__ = GeneratorFunctionPrototype, define(genFun, toStringTagSymbol, "GeneratorFunction")), genFun.prototype = Object.create(Gp), genFun;
  205. }, exports.awrap = function (arg) {
  206. return {
  207. __await: arg
  208. };
  209. }, defineIteratorMethods(AsyncIterator.prototype), define(AsyncIterator.prototype, asyncIteratorSymbol, function () {
  210. return this;
  211. }), exports.AsyncIterator = AsyncIterator, exports.async = function (innerFn, outerFn, self, tryLocsList, PromiseImpl) {
  212. void 0 === PromiseImpl && (PromiseImpl = Promise);
  213. var iter = new AsyncIterator(wrap(innerFn, outerFn, self, tryLocsList), PromiseImpl);
  214. return exports.isGeneratorFunction(outerFn) ? iter : iter.next().then(function (result) {
  215. return result.done ? result.value : iter.next();
  216. });
  217. }, defineIteratorMethods(Gp), define(Gp, toStringTagSymbol, "Generator"), define(Gp, iteratorSymbol, function () {
  218. return this;
  219. }), define(Gp, "toString", function () {
  220. return "[object Generator]";
  221. }), exports.keys = function (object) {
  222. var keys = [];
  223. for (var key in object) {
  224. keys.push(key);
  225. }
  226. return keys.reverse(), function next() {
  227. for (; keys.length;) {
  228. var key = keys.pop();
  229. if (key in object) return next.value = key, next.done = !1, next;
  230. }
  231. return next.done = !0, next;
  232. };
  233. }, exports.values = values, Context.prototype = {
  234. constructor: Context,
  235. reset: function (skipTempReset) {
  236. if (this.prev = 0, this.next = 0, this.sent = this._sent = undefined, this.done = !1, this.delegate = null, this.method = "next", this.arg = undefined, this.tryEntries.forEach(resetTryEntry), !skipTempReset) for (var name in this) {
  237. "t" === name.charAt(0) && hasOwn.call(this, name) && !isNaN(+name.slice(1)) && (this[name] = undefined);
  238. }
  239. },
  240. stop: function () {
  241. this.done = !0;
  242. var rootRecord = this.tryEntries[0].completion;
  243. if ("throw" === rootRecord.type) throw rootRecord.arg;
  244. return this.rval;
  245. },
  246. dispatchException: function (exception) {
  247. if (this.done) throw exception;
  248. var context = this;
  249. function handle(loc, caught) {
  250. return record.type = "throw", record.arg = exception, context.next = loc, caught && (context.method = "next", context.arg = undefined), !!caught;
  251. }
  252. for (var i = this.tryEntries.length - 1; i >= 0; --i) {
  253. var entry = this.tryEntries[i],
  254. record = entry.completion;
  255. if ("root" === entry.tryLoc) return handle("end");
  256. if (entry.tryLoc <= this.prev) {
  257. var hasCatch = hasOwn.call(entry, "catchLoc"),
  258. hasFinally = hasOwn.call(entry, "finallyLoc");
  259. if (hasCatch && hasFinally) {
  260. if (this.prev < entry.catchLoc) return handle(entry.catchLoc, !0);
  261. if (this.prev < entry.finallyLoc) return handle(entry.finallyLoc);
  262. } else if (hasCatch) {
  263. if (this.prev < entry.catchLoc) return handle(entry.catchLoc, !0);
  264. } else {
  265. if (!hasFinally) throw new Error("try statement without catch or finally");
  266. if (this.prev < entry.finallyLoc) return handle(entry.finallyLoc);
  267. }
  268. }
  269. }
  270. },
  271. abrupt: function (type, arg) {
  272. for (var i = this.tryEntries.length - 1; i >= 0; --i) {
  273. var entry = this.tryEntries[i];
  274. if (entry.tryLoc <= this.prev && hasOwn.call(entry, "finallyLoc") && this.prev < entry.finallyLoc) {
  275. var finallyEntry = entry;
  276. break;
  277. }
  278. }
  279. finallyEntry && ("break" === type || "continue" === type) && finallyEntry.tryLoc <= arg && arg <= finallyEntry.finallyLoc && (finallyEntry = null);
  280. var record = finallyEntry ? finallyEntry.completion : {};
  281. return record.type = type, record.arg = arg, finallyEntry ? (this.method = "next", this.next = finallyEntry.finallyLoc, ContinueSentinel) : this.complete(record);
  282. },
  283. complete: function (record, afterLoc) {
  284. if ("throw" === record.type) throw record.arg;
  285. return "break" === record.type || "continue" === record.type ? this.next = record.arg : "return" === record.type ? (this.rval = this.arg = record.arg, this.method = "return", this.next = "end") : "normal" === record.type && afterLoc && (this.next = afterLoc), ContinueSentinel;
  286. },
  287. finish: function (finallyLoc) {
  288. for (var i = this.tryEntries.length - 1; i >= 0; --i) {
  289. var entry = this.tryEntries[i];
  290. if (entry.finallyLoc === finallyLoc) return this.complete(entry.completion, entry.afterLoc), resetTryEntry(entry), ContinueSentinel;
  291. }
  292. },
  293. catch: function (tryLoc) {
  294. for (var i = this.tryEntries.length - 1; i >= 0; --i) {
  295. var entry = this.tryEntries[i];
  296. if (entry.tryLoc === tryLoc) {
  297. var record = entry.completion;
  298. if ("throw" === record.type) {
  299. var thrown = record.arg;
  300. resetTryEntry(entry);
  301. }
  302. return thrown;
  303. }
  304. }
  305. throw new Error("illegal catch attempt");
  306. },
  307. delegateYield: function (iterable, resultName, nextLoc) {
  308. return this.delegate = {
  309. iterator: values(iterable),
  310. resultName: resultName,
  311. nextLoc: nextLoc
  312. }, "next" === this.method && (this.arg = undefined), ContinueSentinel;
  313. }
  314. }, exports;
  315. }
  316. function quote(s) {
  317. return '\\Q' + s.replace('\\E', '\\E\\\\E\\Q') + '\\E';
  318. }
  319. function _getClassNameFromQueries(queries) {
  320. var className = null;
  321. queries.forEach(function (q) {
  322. if (!className) {
  323. className = q.className;
  324. }
  325. if (className !== q.className) {
  326. throw new Error('All queries must be for the same class.');
  327. }
  328. });
  329. return className;
  330. }
  331. function handleSelectResult(data, select) {
  332. var serverDataMask = {};
  333. select.forEach(function (field) {
  334. var hasSubObjectSelect = field.indexOf('.') !== -1;
  335. if (!hasSubObjectSelect && !data.hasOwnProperty(field)) {
  336. data[field] = undefined;
  337. } else if (hasSubObjectSelect) {
  338. var pathComponents = field.split('.');
  339. var _obj = data;
  340. var serverMask = serverDataMask;
  341. pathComponents.forEach(function (component, index, arr) {
  342. if (_obj && !_obj.hasOwnProperty(component)) {
  343. _obj[component] = undefined;
  344. }
  345. if (_obj && typeof _obj === 'object') {
  346. _obj = _obj[component];
  347. }
  348. if (index < arr.length - 1) {
  349. if (!serverMask[component]) {
  350. serverMask[component] = {};
  351. }
  352. serverMask = serverMask[component];
  353. }
  354. });
  355. }
  356. });
  357. if (Object.keys(serverDataMask).length > 0) {
  358. var serverData = _CoreManager.default.getObjectStateController().getServerData({
  359. id: data.objectId,
  360. className: data.className
  361. });
  362. copyMissingDataWithMask(serverData, data, serverDataMask, false);
  363. }
  364. }
  365. function copyMissingDataWithMask(src, dest, mask, copyThisLevel) {
  366. if (copyThisLevel) {
  367. for (var _key in src) {
  368. if (src.hasOwnProperty(_key) && !dest.hasOwnProperty(_key)) {
  369. dest[_key] = src[_key];
  370. }
  371. }
  372. }
  373. for (var _key2 in mask) {
  374. if (dest[_key2] !== undefined && dest[_key2] !== null && src !== undefined && src !== null) {
  375. copyMissingDataWithMask(src[_key2], dest[_key2], mask[_key2], true);
  376. }
  377. }
  378. }
  379. function handleOfflineSort(a, b, sorts) {
  380. var order = sorts[0];
  381. var operator = order.slice(0, 1);
  382. var isDescending = operator === '-';
  383. if (isDescending) {
  384. order = order.substring(1);
  385. }
  386. if (order === '_created_at') {
  387. order = 'createdAt';
  388. }
  389. if (order === '_updated_at') {
  390. order = 'updatedAt';
  391. }
  392. if (!/^[A-Za-z][0-9A-Za-z_]*$/.test(order) || order === 'password') {
  393. throw new _ParseError.default(_ParseError.default.INVALID_KEY_NAME, "Invalid Key: " + order);
  394. }
  395. var field1 = a.get(order);
  396. var field2 = b.get(order);
  397. if (field1 < field2) {
  398. return isDescending ? 1 : -1;
  399. }
  400. if (field1 > field2) {
  401. return isDescending ? -1 : 1;
  402. }
  403. if (sorts.length > 1) {
  404. var remainingSorts = sorts.slice(1);
  405. return handleOfflineSort(a, b, remainingSorts);
  406. }
  407. return 0;
  408. }
  409. var ParseQuery = function () {
  410. function ParseQuery(objectClass) {
  411. (0, _classCallCheck2.default)(this, ParseQuery);
  412. if (typeof objectClass === 'string') {
  413. if (objectClass === 'User' && _CoreManager.default.get('PERFORM_USER_REWRITE')) {
  414. this.className = '_User';
  415. } else {
  416. this.className = objectClass;
  417. }
  418. } else if (objectClass instanceof _ParseObject.default) {
  419. this.className = objectClass.className;
  420. } else if (typeof objectClass === 'function') {
  421. if (typeof objectClass.className === 'string') {
  422. this.className = objectClass.className;
  423. } else {
  424. var _obj2 = new objectClass();
  425. this.className = _obj2.className;
  426. }
  427. } else {
  428. throw new TypeError('A ParseQuery must be constructed with a ParseObject or class name.');
  429. }
  430. this._where = {};
  431. this._include = [];
  432. this._exclude = [];
  433. this._count = false;
  434. this._limit = -1;
  435. this._skip = 0;
  436. this._readPreference = null;
  437. this._includeReadPreference = null;
  438. this._subqueryReadPreference = null;
  439. this._queriesLocalDatastore = false;
  440. this._localDatastorePinName = null;
  441. this._extraOptions = {};
  442. this._xhrRequest = {
  443. task: null,
  444. onchange: function () {}
  445. };
  446. }
  447. (0, _createClass2.default)(ParseQuery, [{
  448. key: "_orQuery",
  449. value: function (queries) {
  450. var queryJSON = queries.map(function (q) {
  451. return q.toJSON().where;
  452. });
  453. this._where.$or = queryJSON;
  454. return this;
  455. }
  456. }, {
  457. key: "_andQuery",
  458. value: function (queries) {
  459. var queryJSON = queries.map(function (q) {
  460. return q.toJSON().where;
  461. });
  462. this._where.$and = queryJSON;
  463. return this;
  464. }
  465. }, {
  466. key: "_norQuery",
  467. value: function (queries) {
  468. var queryJSON = queries.map(function (q) {
  469. return q.toJSON().where;
  470. });
  471. this._where.$nor = queryJSON;
  472. return this;
  473. }
  474. }, {
  475. key: "_addCondition",
  476. value: function (key, condition, value) {
  477. if (!this._where[key] || typeof this._where[key] === 'string') {
  478. this._where[key] = {};
  479. }
  480. this._where[key][condition] = (0, _encode.default)(value, false, true);
  481. return this;
  482. }
  483. }, {
  484. key: "_regexStartWith",
  485. value: function (string) {
  486. return '^' + quote(string);
  487. }
  488. }, {
  489. key: "_handleOfflineQuery",
  490. value: function (params) {
  491. var _this2 = this;
  492. var localDatastore, objects, results, keys, alwaysSelectedKeys, sorts, count, limit;
  493. return _regeneratorRuntime().async(function (_context) {
  494. while (1) {
  495. switch (_context.prev = _context.next) {
  496. case 0:
  497. _OfflineQuery.default.validateQuery(this);
  498. localDatastore = _CoreManager.default.getLocalDatastore();
  499. _context.next = 4;
  500. return _regeneratorRuntime().awrap(localDatastore._serializeObjectsFromPinName(this._localDatastorePinName));
  501. case 4:
  502. objects = _context.sent;
  503. results = objects.map(function (json, index, arr) {
  504. var object = _ParseObject.default.fromJSON(json, false);
  505. if (json._localId && !json.objectId) {
  506. object._localId = json._localId;
  507. }
  508. if (!_OfflineQuery.default.matchesQuery(_this2.className, object, arr, _this2)) {
  509. return null;
  510. }
  511. return object;
  512. }).filter(function (object) {
  513. return object !== null;
  514. });
  515. if (params.keys) {
  516. keys = params.keys.split(',');
  517. alwaysSelectedKeys = ['className', 'objectId', 'createdAt', 'updatedAt', 'ACL'];
  518. keys = keys.concat(alwaysSelectedKeys);
  519. results = results.map(function (object) {
  520. var json = object._toFullJSON();
  521. Object.keys(json).forEach(function (key) {
  522. if (!keys.includes(key)) {
  523. delete json[key];
  524. }
  525. });
  526. return _ParseObject.default.fromJSON(json, false);
  527. });
  528. }
  529. if (params.order) {
  530. sorts = params.order.split(',');
  531. results.sort(function (a, b) {
  532. return handleOfflineSort(a, b, sorts);
  533. });
  534. }
  535. if (params.count) {
  536. count = results.length;
  537. }
  538. if (params.skip) {
  539. if (params.skip >= results.length) {
  540. results = [];
  541. } else {
  542. results = results.splice(params.skip, results.length);
  543. }
  544. }
  545. limit = results.length;
  546. if (params.limit !== 0 && params.limit < results.length) {
  547. limit = params.limit;
  548. }
  549. results = results.splice(0, limit);
  550. if (!(typeof count === 'number')) {
  551. _context.next = 15;
  552. break;
  553. }
  554. return _context.abrupt("return", {
  555. results: results,
  556. count: count
  557. });
  558. case 15:
  559. return _context.abrupt("return", results);
  560. case 16:
  561. case "end":
  562. return _context.stop();
  563. }
  564. }
  565. }, null, this, null, Promise);
  566. }
  567. }, {
  568. key: "toJSON",
  569. value: function () {
  570. var params = {
  571. where: this._where
  572. };
  573. if (this._include.length) {
  574. params.include = this._include.join(',');
  575. }
  576. if (this._exclude.length) {
  577. params.excludeKeys = this._exclude.join(',');
  578. }
  579. if (this._select) {
  580. params.keys = this._select.join(',');
  581. }
  582. if (this._count) {
  583. params.count = 1;
  584. }
  585. if (this._limit >= 0) {
  586. params.limit = this._limit;
  587. }
  588. if (this._skip > 0) {
  589. params.skip = this._skip;
  590. }
  591. if (this._order) {
  592. params.order = this._order.join(',');
  593. }
  594. if (this._readPreference) {
  595. params.readPreference = this._readPreference;
  596. }
  597. if (this._includeReadPreference) {
  598. params.includeReadPreference = this._includeReadPreference;
  599. }
  600. if (this._subqueryReadPreference) {
  601. params.subqueryReadPreference = this._subqueryReadPreference;
  602. }
  603. if (this._hint) {
  604. params.hint = this._hint;
  605. }
  606. if (this._explain) {
  607. params.explain = true;
  608. }
  609. for (var _key3 in this._extraOptions) {
  610. params[_key3] = this._extraOptions[_key3];
  611. }
  612. return params;
  613. }
  614. }, {
  615. key: "withJSON",
  616. value: function (json) {
  617. if (json.where) {
  618. this._where = json.where;
  619. }
  620. if (json.include) {
  621. this._include = json.include.split(',');
  622. }
  623. if (json.keys) {
  624. this._select = json.keys.split(',');
  625. }
  626. if (json.excludeKeys) {
  627. this._exclude = json.excludeKeys.split(',');
  628. }
  629. if (json.count) {
  630. this._count = json.count === 1;
  631. }
  632. if (json.limit) {
  633. this._limit = json.limit;
  634. }
  635. if (json.skip) {
  636. this._skip = json.skip;
  637. }
  638. if (json.order) {
  639. this._order = json.order.split(',');
  640. }
  641. if (json.readPreference) {
  642. this._readPreference = json.readPreference;
  643. }
  644. if (json.includeReadPreference) {
  645. this._includeReadPreference = json.includeReadPreference;
  646. }
  647. if (json.subqueryReadPreference) {
  648. this._subqueryReadPreference = json.subqueryReadPreference;
  649. }
  650. if (json.hint) {
  651. this._hint = json.hint;
  652. }
  653. if (json.explain) {
  654. this._explain = !!json.explain;
  655. }
  656. for (var _key4 in json) {
  657. if (json.hasOwnProperty(_key4)) {
  658. if (['where', 'include', 'keys', 'count', 'limit', 'skip', 'order', 'readPreference', 'includeReadPreference', 'subqueryReadPreference', 'hint', 'explain'].indexOf(_key4) === -1) {
  659. this._extraOptions[_key4] = json[_key4];
  660. }
  661. }
  662. }
  663. return this;
  664. }
  665. }, {
  666. key: "get",
  667. value: function (objectId, options) {
  668. this.equalTo('objectId', objectId);
  669. var firstOptions = {};
  670. if (options && options.hasOwnProperty('useMasterKey')) {
  671. firstOptions.useMasterKey = options.useMasterKey;
  672. }
  673. if (options && options.hasOwnProperty('sessionToken')) {
  674. firstOptions.sessionToken = options.sessionToken;
  675. }
  676. if (options && options.hasOwnProperty('context') && typeof options.context === 'object') {
  677. firstOptions.context = options.context;
  678. }
  679. if (options && options.hasOwnProperty('json')) {
  680. firstOptions.json = options.json;
  681. }
  682. return this.first(firstOptions).then(function (response) {
  683. if (response) {
  684. return response;
  685. }
  686. var errorObject = new _ParseError.default(_ParseError.default.OBJECT_NOT_FOUND, 'Object not found.');
  687. return Promise.reject(errorObject);
  688. });
  689. }
  690. }, {
  691. key: "find",
  692. value: function (options) {
  693. var _this3 = this;
  694. options = options || {};
  695. var findOptions = {};
  696. if (options.hasOwnProperty('useMasterKey')) {
  697. findOptions.useMasterKey = options.useMasterKey;
  698. }
  699. if (options.hasOwnProperty('sessionToken')) {
  700. findOptions.sessionToken = options.sessionToken;
  701. }
  702. if (options.hasOwnProperty('context') && typeof options.context === 'object') {
  703. findOptions.context = options.context;
  704. }
  705. this._setRequestTask(findOptions);
  706. var controller = _CoreManager.default.getQueryController();
  707. var select = this._select;
  708. if (this._queriesLocalDatastore) {
  709. return this._handleOfflineQuery(this.toJSON());
  710. }
  711. return controller.find(this.className, this.toJSON(), findOptions).then(function (response) {
  712. if (_this3._explain) {
  713. return response.results;
  714. }
  715. var results = response.results.map(function (data) {
  716. var override = response.className || _this3.className;
  717. if (!data.className) {
  718. data.className = override;
  719. }
  720. if (select) {
  721. handleSelectResult(data, select);
  722. }
  723. if (options.json) {
  724. return data;
  725. } else {
  726. return _ParseObject.default.fromJSON(data, !select);
  727. }
  728. });
  729. var count = response.count;
  730. if (typeof count === 'number') {
  731. return {
  732. results: results,
  733. count: count
  734. };
  735. } else {
  736. return results;
  737. }
  738. });
  739. }
  740. }, {
  741. key: "findAll",
  742. value: function (options) {
  743. var result;
  744. return _regeneratorRuntime().async(function (_context2) {
  745. while (1) {
  746. switch (_context2.prev = _context2.next) {
  747. case 0:
  748. result = [];
  749. _context2.next = 3;
  750. return _regeneratorRuntime().awrap(this.eachBatch(function (objects) {
  751. result = [].concat((0, _toConsumableArray2.default)(result), (0, _toConsumableArray2.default)(objects));
  752. }, options));
  753. case 3:
  754. return _context2.abrupt("return", result);
  755. case 4:
  756. case "end":
  757. return _context2.stop();
  758. }
  759. }
  760. }, null, this, null, Promise);
  761. }
  762. }, {
  763. key: "count",
  764. value: function (options) {
  765. options = options || {};
  766. var findOptions = {};
  767. if (options.hasOwnProperty('useMasterKey')) {
  768. findOptions.useMasterKey = options.useMasterKey;
  769. }
  770. if (options.hasOwnProperty('sessionToken')) {
  771. findOptions.sessionToken = options.sessionToken;
  772. }
  773. this._setRequestTask(findOptions);
  774. var controller = _CoreManager.default.getQueryController();
  775. var params = this.toJSON();
  776. params.limit = 0;
  777. params.count = 1;
  778. return controller.find(this.className, params, findOptions).then(function (result) {
  779. return result.count;
  780. });
  781. }
  782. }, {
  783. key: "distinct",
  784. value: function (key, options) {
  785. options = options || {};
  786. var distinctOptions = {};
  787. distinctOptions.useMasterKey = true;
  788. if (options.hasOwnProperty('sessionToken')) {
  789. distinctOptions.sessionToken = options.sessionToken;
  790. }
  791. this._setRequestTask(distinctOptions);
  792. var controller = _CoreManager.default.getQueryController();
  793. var params = {
  794. distinct: key,
  795. where: this._where,
  796. hint: this._hint
  797. };
  798. return controller.aggregate(this.className, params, distinctOptions).then(function (results) {
  799. return results.results;
  800. });
  801. }
  802. }, {
  803. key: "aggregate",
  804. value: function (pipeline, options) {
  805. options = options || {};
  806. var aggregateOptions = {};
  807. aggregateOptions.useMasterKey = true;
  808. if (options.hasOwnProperty('sessionToken')) {
  809. aggregateOptions.sessionToken = options.sessionToken;
  810. }
  811. this._setRequestTask(aggregateOptions);
  812. var controller = _CoreManager.default.getQueryController();
  813. if (!Array.isArray(pipeline) && typeof pipeline !== 'object') {
  814. throw new Error('Invalid pipeline must be Array or Object');
  815. }
  816. if (Object.keys(this._where || {}).length) {
  817. if (!Array.isArray(pipeline)) {
  818. pipeline = [pipeline];
  819. }
  820. pipeline.unshift({
  821. match: this._where
  822. });
  823. }
  824. var params = {
  825. pipeline: pipeline,
  826. hint: this._hint,
  827. explain: this._explain,
  828. readPreference: this._readPreference
  829. };
  830. return controller.aggregate(this.className, params, aggregateOptions).then(function (results) {
  831. return results.results;
  832. });
  833. }
  834. }, {
  835. key: "first",
  836. value: function (options) {
  837. var _this4 = this;
  838. options = options || {};
  839. var findOptions = {};
  840. if (options.hasOwnProperty('useMasterKey')) {
  841. findOptions.useMasterKey = options.useMasterKey;
  842. }
  843. if (options.hasOwnProperty('sessionToken')) {
  844. findOptions.sessionToken = options.sessionToken;
  845. }
  846. if (options.hasOwnProperty('context') && typeof options.context === 'object') {
  847. findOptions.context = options.context;
  848. }
  849. this._setRequestTask(findOptions);
  850. var controller = _CoreManager.default.getQueryController();
  851. var params = this.toJSON();
  852. params.limit = 1;
  853. var select = this._select;
  854. if (this._queriesLocalDatastore) {
  855. return this._handleOfflineQuery(params).then(function (objects) {
  856. if (!objects[0]) {
  857. return undefined;
  858. }
  859. return objects[0];
  860. });
  861. }
  862. return controller.find(this.className, params, findOptions).then(function (response) {
  863. var objects = response.results;
  864. if (!objects[0]) {
  865. return undefined;
  866. }
  867. if (!objects[0].className) {
  868. objects[0].className = _this4.className;
  869. }
  870. if (select) {
  871. handleSelectResult(objects[0], select);
  872. }
  873. if (options.json) {
  874. return objects[0];
  875. } else {
  876. return _ParseObject.default.fromJSON(objects[0], !select);
  877. }
  878. });
  879. }
  880. }, {
  881. key: "eachBatch",
  882. value: function (callback, options) {
  883. options = options || {};
  884. if (this._order || this._skip || this._limit >= 0) {
  885. return Promise.reject('Cannot iterate on a query with sort, skip, or limit.');
  886. }
  887. var query = new ParseQuery(this.className);
  888. query._limit = options.batchSize || 100;
  889. query._include = this._include.map(function (i) {
  890. return i;
  891. });
  892. if (this._select) {
  893. query._select = this._select.map(function (s) {
  894. return s;
  895. });
  896. }
  897. query._hint = this._hint;
  898. query._where = {};
  899. for (var _attr in this._where) {
  900. var val = this._where[_attr];
  901. if (Array.isArray(val)) {
  902. query._where[_attr] = val.map(function (v) {
  903. return v;
  904. });
  905. } else if (val && typeof val === 'object') {
  906. var conditionMap = {};
  907. query._where[_attr] = conditionMap;
  908. for (var cond in val) {
  909. conditionMap[cond] = val[cond];
  910. }
  911. } else {
  912. query._where[_attr] = val;
  913. }
  914. }
  915. query.ascending('objectId');
  916. var findOptions = {};
  917. if (options.hasOwnProperty('useMasterKey')) {
  918. findOptions.useMasterKey = options.useMasterKey;
  919. }
  920. if (options.hasOwnProperty('sessionToken')) {
  921. findOptions.sessionToken = options.sessionToken;
  922. }
  923. if (options.hasOwnProperty('context') && typeof options.context === 'object') {
  924. findOptions.context = options.context;
  925. }
  926. if (options.hasOwnProperty('json')) {
  927. findOptions.json = options.json;
  928. }
  929. var finished = false;
  930. var previousResults = [];
  931. return (0, _promiseUtils.continueWhile)(function () {
  932. return !finished;
  933. }, function () {
  934. var _await$Promise$all, _await$Promise$all2, results;
  935. return _regeneratorRuntime().async(function (_context3) {
  936. while (1) {
  937. switch (_context3.prev = _context3.next) {
  938. case 0:
  939. _context3.next = 2;
  940. return _regeneratorRuntime().awrap(Promise.all([query.find(findOptions), Promise.resolve(previousResults.length > 0 && callback(previousResults))]));
  941. case 2:
  942. _await$Promise$all = _context3.sent;
  943. _await$Promise$all2 = (0, _slicedToArray2.default)(_await$Promise$all, 1);
  944. results = _await$Promise$all2[0];
  945. if (!(results.length >= query._limit)) {
  946. _context3.next = 10;
  947. break;
  948. }
  949. query.greaterThan('objectId', results[results.length - 1].id);
  950. previousResults = results;
  951. _context3.next = 17;
  952. break;
  953. case 10:
  954. if (!(results.length > 0)) {
  955. _context3.next = 16;
  956. break;
  957. }
  958. _context3.next = 13;
  959. return _regeneratorRuntime().awrap(Promise.resolve(callback(results)));
  960. case 13:
  961. finished = true;
  962. _context3.next = 17;
  963. break;
  964. case 16:
  965. finished = true;
  966. case 17:
  967. case "end":
  968. return _context3.stop();
  969. }
  970. }
  971. }, null, null, null, Promise);
  972. });
  973. }
  974. }, {
  975. key: "each",
  976. value: function (callback, options) {
  977. return this.eachBatch(function (results) {
  978. var callbacksDone = Promise.resolve();
  979. results.forEach(function (result) {
  980. callbacksDone = callbacksDone.then(function () {
  981. return callback(result);
  982. });
  983. });
  984. return callbacksDone;
  985. }, options);
  986. }
  987. }, {
  988. key: "hint",
  989. value: function (value) {
  990. if (typeof value === 'undefined') {
  991. delete this._hint;
  992. }
  993. this._hint = value;
  994. return this;
  995. }
  996. }, {
  997. key: "explain",
  998. value: function () {
  999. var _explain = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : true;
  1000. if (typeof _explain !== 'boolean') {
  1001. throw new Error('You can only set explain to a boolean value');
  1002. }
  1003. this._explain = _explain;
  1004. return this;
  1005. }
  1006. }, {
  1007. key: "map",
  1008. value: function (callback, options) {
  1009. var _this5 = this;
  1010. var array, index;
  1011. return _regeneratorRuntime().async(function (_context4) {
  1012. while (1) {
  1013. switch (_context4.prev = _context4.next) {
  1014. case 0:
  1015. array = [];
  1016. index = 0;
  1017. _context4.next = 4;
  1018. return _regeneratorRuntime().awrap(this.each(function (object) {
  1019. return Promise.resolve(callback(object, index, _this5)).then(function (result) {
  1020. array.push(result);
  1021. index += 1;
  1022. });
  1023. }, options));
  1024. case 4:
  1025. return _context4.abrupt("return", array);
  1026. case 5:
  1027. case "end":
  1028. return _context4.stop();
  1029. }
  1030. }
  1031. }, null, this, null, Promise);
  1032. }
  1033. }, {
  1034. key: "reduce",
  1035. value: function (callback, initialValue, options) {
  1036. var accumulator, index;
  1037. return _regeneratorRuntime().async(function (_context5) {
  1038. while (1) {
  1039. switch (_context5.prev = _context5.next) {
  1040. case 0:
  1041. accumulator = initialValue;
  1042. index = 0;
  1043. _context5.next = 4;
  1044. return _regeneratorRuntime().awrap(this.each(function (object) {
  1045. if (index === 0 && initialValue === undefined) {
  1046. accumulator = object;
  1047. index += 1;
  1048. return;
  1049. }
  1050. return Promise.resolve(callback(accumulator, object, index)).then(function (result) {
  1051. accumulator = result;
  1052. index += 1;
  1053. });
  1054. }, options));
  1055. case 4:
  1056. if (!(index === 0 && initialValue === undefined)) {
  1057. _context5.next = 6;
  1058. break;
  1059. }
  1060. throw new TypeError('Reducing empty query result set with no initial value');
  1061. case 6:
  1062. return _context5.abrupt("return", accumulator);
  1063. case 7:
  1064. case "end":
  1065. return _context5.stop();
  1066. }
  1067. }
  1068. }, null, this, null, Promise);
  1069. }
  1070. }, {
  1071. key: "filter",
  1072. value: function (callback, options) {
  1073. var _this6 = this;
  1074. var array, index;
  1075. return _regeneratorRuntime().async(function (_context6) {
  1076. while (1) {
  1077. switch (_context6.prev = _context6.next) {
  1078. case 0:
  1079. array = [];
  1080. index = 0;
  1081. _context6.next = 4;
  1082. return _regeneratorRuntime().awrap(this.each(function (object) {
  1083. return Promise.resolve(callback(object, index, _this6)).then(function (flag) {
  1084. if (flag) {
  1085. array.push(object);
  1086. }
  1087. index += 1;
  1088. });
  1089. }, options));
  1090. case 4:
  1091. return _context6.abrupt("return", array);
  1092. case 5:
  1093. case "end":
  1094. return _context6.stop();
  1095. }
  1096. }
  1097. }, null, this, null, Promise);
  1098. }
  1099. }, {
  1100. key: "equalTo",
  1101. value: function (key, value) {
  1102. var _this7 = this;
  1103. if (key && typeof key === 'object') {
  1104. Object.entries(key).forEach(function (_ref) {
  1105. var _ref2 = (0, _slicedToArray2.default)(_ref, 2),
  1106. k = _ref2[0],
  1107. val = _ref2[1];
  1108. return _this7.equalTo(k, val);
  1109. });
  1110. return this;
  1111. }
  1112. if (typeof value === 'undefined') {
  1113. return this.doesNotExist(key);
  1114. }
  1115. this._where[key] = (0, _encode.default)(value, false, true);
  1116. return this;
  1117. }
  1118. }, {
  1119. key: "notEqualTo",
  1120. value: function (key, value) {
  1121. var _this8 = this;
  1122. if (key && typeof key === 'object') {
  1123. Object.entries(key).forEach(function (_ref3) {
  1124. var _ref4 = (0, _slicedToArray2.default)(_ref3, 2),
  1125. k = _ref4[0],
  1126. val = _ref4[1];
  1127. return _this8.notEqualTo(k, val);
  1128. });
  1129. return this;
  1130. }
  1131. return this._addCondition(key, '$ne', value);
  1132. }
  1133. }, {
  1134. key: "lessThan",
  1135. value: function (key, value) {
  1136. return this._addCondition(key, '$lt', value);
  1137. }
  1138. }, {
  1139. key: "greaterThan",
  1140. value: function (key, value) {
  1141. return this._addCondition(key, '$gt', value);
  1142. }
  1143. }, {
  1144. key: "lessThanOrEqualTo",
  1145. value: function (key, value) {
  1146. return this._addCondition(key, '$lte', value);
  1147. }
  1148. }, {
  1149. key: "greaterThanOrEqualTo",
  1150. value: function (key, value) {
  1151. return this._addCondition(key, '$gte', value);
  1152. }
  1153. }, {
  1154. key: "containedIn",
  1155. value: function (key, value) {
  1156. return this._addCondition(key, '$in', value);
  1157. }
  1158. }, {
  1159. key: "notContainedIn",
  1160. value: function (key, value) {
  1161. return this._addCondition(key, '$nin', value);
  1162. }
  1163. }, {
  1164. key: "containedBy",
  1165. value: function (key, values) {
  1166. return this._addCondition(key, '$containedBy', values);
  1167. }
  1168. }, {
  1169. key: "containsAll",
  1170. value: function (key, values) {
  1171. return this._addCondition(key, '$all', values);
  1172. }
  1173. }, {
  1174. key: "containsAllStartingWith",
  1175. value: function (key, values) {
  1176. var _this = this;
  1177. if (!Array.isArray(values)) {
  1178. values = [values];
  1179. }
  1180. var regexObject = values.map(function (value) {
  1181. return {
  1182. $regex: _this._regexStartWith(value)
  1183. };
  1184. });
  1185. return this.containsAll(key, regexObject);
  1186. }
  1187. }, {
  1188. key: "exists",
  1189. value: function (key) {
  1190. return this._addCondition(key, '$exists', true);
  1191. }
  1192. }, {
  1193. key: "doesNotExist",
  1194. value: function (key) {
  1195. return this._addCondition(key, '$exists', false);
  1196. }
  1197. }, {
  1198. key: "matches",
  1199. value: function (key, regex, modifiers) {
  1200. this._addCondition(key, '$regex', regex);
  1201. if (!modifiers) {
  1202. modifiers = '';
  1203. }
  1204. if (regex.ignoreCase) {
  1205. modifiers += 'i';
  1206. }
  1207. if (regex.multiline) {
  1208. modifiers += 'm';
  1209. }
  1210. if (modifiers.length) {
  1211. this._addCondition(key, '$options', modifiers);
  1212. }
  1213. return this;
  1214. }
  1215. }, {
  1216. key: "matchesQuery",
  1217. value: function (key, query) {
  1218. var queryJSON = query.toJSON();
  1219. queryJSON.className = query.className;
  1220. return this._addCondition(key, '$inQuery', queryJSON);
  1221. }
  1222. }, {
  1223. key: "doesNotMatchQuery",
  1224. value: function (key, query) {
  1225. var queryJSON = query.toJSON();
  1226. queryJSON.className = query.className;
  1227. return this._addCondition(key, '$notInQuery', queryJSON);
  1228. }
  1229. }, {
  1230. key: "matchesKeyInQuery",
  1231. value: function (key, queryKey, query) {
  1232. var queryJSON = query.toJSON();
  1233. queryJSON.className = query.className;
  1234. return this._addCondition(key, '$select', {
  1235. key: queryKey,
  1236. query: queryJSON
  1237. });
  1238. }
  1239. }, {
  1240. key: "doesNotMatchKeyInQuery",
  1241. value: function (key, queryKey, query) {
  1242. var queryJSON = query.toJSON();
  1243. queryJSON.className = query.className;
  1244. return this._addCondition(key, '$dontSelect', {
  1245. key: queryKey,
  1246. query: queryJSON
  1247. });
  1248. }
  1249. }, {
  1250. key: "contains",
  1251. value: function (key, substring) {
  1252. if (typeof substring !== 'string') {
  1253. throw new Error('The value being searched for must be a string.');
  1254. }
  1255. return this._addCondition(key, '$regex', quote(substring));
  1256. }
  1257. }, {
  1258. key: "fullText",
  1259. value: function (key, value, options) {
  1260. options = options || {};
  1261. if (!key) {
  1262. throw new Error('A key is required.');
  1263. }
  1264. if (!value) {
  1265. throw new Error('A search term is required');
  1266. }
  1267. if (typeof value !== 'string') {
  1268. throw new Error('The value being searched for must be a string.');
  1269. }
  1270. var fullOptions = {};
  1271. fullOptions.$term = value;
  1272. for (var option in options) {
  1273. switch (option) {
  1274. case 'language':
  1275. fullOptions.$language = options[option];
  1276. break;
  1277. case 'caseSensitive':
  1278. fullOptions.$caseSensitive = options[option];
  1279. break;
  1280. case 'diacriticSensitive':
  1281. fullOptions.$diacriticSensitive = options[option];
  1282. break;
  1283. default:
  1284. throw new Error("Unknown option: " + option);
  1285. }
  1286. }
  1287. return this._addCondition(key, '$text', {
  1288. $search: fullOptions
  1289. });
  1290. }
  1291. }, {
  1292. key: "sortByTextScore",
  1293. value: function () {
  1294. this.ascending('$score');
  1295. this.select(['$score']);
  1296. return this;
  1297. }
  1298. }, {
  1299. key: "startsWith",
  1300. value: function (key, prefix, modifiers) {
  1301. if (typeof prefix !== 'string') {
  1302. throw new Error('The value being searched for must be a string.');
  1303. }
  1304. return this.matches(key, this._regexStartWith(prefix), modifiers);
  1305. }
  1306. }, {
  1307. key: "endsWith",
  1308. value: function (key, suffix, modifiers) {
  1309. if (typeof suffix !== 'string') {
  1310. throw new Error('The value being searched for must be a string.');
  1311. }
  1312. return this.matches(key, quote(suffix) + '$', modifiers);
  1313. }
  1314. }, {
  1315. key: "near",
  1316. value: function (key, point) {
  1317. if (!(point instanceof _ParseGeoPoint.default)) {
  1318. point = new _ParseGeoPoint.default(point);
  1319. }
  1320. return this._addCondition(key, '$nearSphere', point);
  1321. }
  1322. }, {
  1323. key: "withinRadians",
  1324. value: function (key, point, maxDistance, sorted) {
  1325. if (sorted || sorted === undefined) {
  1326. this.near(key, point);
  1327. return this._addCondition(key, '$maxDistance', maxDistance);
  1328. } else {
  1329. return this._addCondition(key, '$geoWithin', {
  1330. $centerSphere: [[point.longitude, point.latitude], maxDistance]
  1331. });
  1332. }
  1333. }
  1334. }, {
  1335. key: "withinMiles",
  1336. value: function (key, point, maxDistance, sorted) {
  1337. return this.withinRadians(key, point, maxDistance / 3958.8, sorted);
  1338. }
  1339. }, {
  1340. key: "withinKilometers",
  1341. value: function (key, point, maxDistance, sorted) {
  1342. return this.withinRadians(key, point, maxDistance / 6371.0, sorted);
  1343. }
  1344. }, {
  1345. key: "withinGeoBox",
  1346. value: function (key, southwest, northeast) {
  1347. if (!(southwest instanceof _ParseGeoPoint.default)) {
  1348. southwest = new _ParseGeoPoint.default(southwest);
  1349. }
  1350. if (!(northeast instanceof _ParseGeoPoint.default)) {
  1351. northeast = new _ParseGeoPoint.default(northeast);
  1352. }
  1353. this._addCondition(key, '$within', {
  1354. $box: [southwest, northeast]
  1355. });
  1356. return this;
  1357. }
  1358. }, {
  1359. key: "withinPolygon",
  1360. value: function (key, points) {
  1361. return this._addCondition(key, '$geoWithin', {
  1362. $polygon: points
  1363. });
  1364. }
  1365. }, {
  1366. key: "polygonContains",
  1367. value: function (key, point) {
  1368. return this._addCondition(key, '$geoIntersects', {
  1369. $point: point
  1370. });
  1371. }
  1372. }, {
  1373. key: "ascending",
  1374. value: function () {
  1375. this._order = [];
  1376. for (var _len = arguments.length, keys = new Array(_len), _key5 = 0; _key5 < _len; _key5++) {
  1377. keys[_key5] = arguments[_key5];
  1378. }
  1379. return this.addAscending.apply(this, keys);
  1380. }
  1381. }, {
  1382. key: "addAscending",
  1383. value: function () {
  1384. var _this9 = this;
  1385. if (!this._order) {
  1386. this._order = [];
  1387. }
  1388. for (var _len2 = arguments.length, keys = new Array(_len2), _key6 = 0; _key6 < _len2; _key6++) {
  1389. keys[_key6] = arguments[_key6];
  1390. }
  1391. keys.forEach(function (key) {
  1392. if (Array.isArray(key)) {
  1393. key = key.join();
  1394. }
  1395. _this9._order = _this9._order.concat(key.replace(/\s/g, '').split(','));
  1396. });
  1397. return this;
  1398. }
  1399. }, {
  1400. key: "descending",
  1401. value: function () {
  1402. this._order = [];
  1403. for (var _len3 = arguments.length, keys = new Array(_len3), _key7 = 0; _key7 < _len3; _key7++) {
  1404. keys[_key7] = arguments[_key7];
  1405. }
  1406. return this.addDescending.apply(this, keys);
  1407. }
  1408. }, {
  1409. key: "addDescending",
  1410. value: function () {
  1411. var _this10 = this;
  1412. if (!this._order) {
  1413. this._order = [];
  1414. }
  1415. for (var _len4 = arguments.length, keys = new Array(_len4), _key8 = 0; _key8 < _len4; _key8++) {
  1416. keys[_key8] = arguments[_key8];
  1417. }
  1418. keys.forEach(function (key) {
  1419. if (Array.isArray(key)) {
  1420. key = key.join();
  1421. }
  1422. _this10._order = _this10._order.concat(key.replace(/\s/g, '').split(',').map(function (k) {
  1423. return '-' + k;
  1424. }));
  1425. });
  1426. return this;
  1427. }
  1428. }, {
  1429. key: "skip",
  1430. value: function (n) {
  1431. if (typeof n !== 'number' || n < 0) {
  1432. throw new Error('You can only skip by a positive number');
  1433. }
  1434. this._skip = n;
  1435. return this;
  1436. }
  1437. }, {
  1438. key: "limit",
  1439. value: function (n) {
  1440. if (typeof n !== 'number') {
  1441. throw new Error('You can only set the limit to a numeric value');
  1442. }
  1443. this._limit = n;
  1444. return this;
  1445. }
  1446. }, {
  1447. key: "withCount",
  1448. value: function () {
  1449. var includeCount = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : true;
  1450. if (typeof includeCount !== 'boolean') {
  1451. throw new Error('You can only set withCount to a boolean value');
  1452. }
  1453. this._count = includeCount;
  1454. return this;
  1455. }
  1456. }, {
  1457. key: "include",
  1458. value: function () {
  1459. var _this11 = this;
  1460. for (var _len5 = arguments.length, keys = new Array(_len5), _key9 = 0; _key9 < _len5; _key9++) {
  1461. keys[_key9] = arguments[_key9];
  1462. }
  1463. keys.forEach(function (key) {
  1464. if (Array.isArray(key)) {
  1465. _this11._include = _this11._include.concat(key);
  1466. } else {
  1467. _this11._include.push(key);
  1468. }
  1469. });
  1470. return this;
  1471. }
  1472. }, {
  1473. key: "includeAll",
  1474. value: function () {
  1475. return this.include('*');
  1476. }
  1477. }, {
  1478. key: "select",
  1479. value: function () {
  1480. var _this12 = this;
  1481. if (!this._select) {
  1482. this._select = [];
  1483. }
  1484. for (var _len6 = arguments.length, keys = new Array(_len6), _key10 = 0; _key10 < _len6; _key10++) {
  1485. keys[_key10] = arguments[_key10];
  1486. }
  1487. keys.forEach(function (key) {
  1488. if (Array.isArray(key)) {
  1489. _this12._select = _this12._select.concat(key);
  1490. } else {
  1491. _this12._select.push(key);
  1492. }
  1493. });
  1494. return this;
  1495. }
  1496. }, {
  1497. key: "exclude",
  1498. value: function () {
  1499. var _this13 = this;
  1500. for (var _len7 = arguments.length, keys = new Array(_len7), _key11 = 0; _key11 < _len7; _key11++) {
  1501. keys[_key11] = arguments[_key11];
  1502. }
  1503. keys.forEach(function (key) {
  1504. if (Array.isArray(key)) {
  1505. _this13._exclude = _this13._exclude.concat(key);
  1506. } else {
  1507. _this13._exclude.push(key);
  1508. }
  1509. });
  1510. return this;
  1511. }
  1512. }, {
  1513. key: "readPreference",
  1514. value: function (_readPreference, includeReadPreference, subqueryReadPreference) {
  1515. this._readPreference = _readPreference;
  1516. this._includeReadPreference = includeReadPreference;
  1517. this._subqueryReadPreference = subqueryReadPreference;
  1518. return this;
  1519. }
  1520. }, {
  1521. key: "subscribe",
  1522. value: function (sessionToken) {
  1523. var currentUser, liveQueryClient, subscription;
  1524. return _regeneratorRuntime().async(function (_context7) {
  1525. while (1) {
  1526. switch (_context7.prev = _context7.next) {
  1527. case 0:
  1528. _context7.next = 2;
  1529. return _regeneratorRuntime().awrap(_CoreManager.default.getUserController().currentUserAsync());
  1530. case 2:
  1531. currentUser = _context7.sent;
  1532. if (!sessionToken) {
  1533. sessionToken = currentUser ? currentUser.getSessionToken() : undefined;
  1534. }
  1535. _context7.next = 6;
  1536. return _regeneratorRuntime().awrap(_CoreManager.default.getLiveQueryController().getDefaultLiveQueryClient());
  1537. case 6:
  1538. liveQueryClient = _context7.sent;
  1539. if (liveQueryClient.shouldOpen()) {
  1540. liveQueryClient.open();
  1541. }
  1542. subscription = liveQueryClient.subscribe(this, sessionToken);
  1543. return _context7.abrupt("return", subscription.subscribePromise.then(function () {
  1544. return subscription;
  1545. }));
  1546. case 10:
  1547. case "end":
  1548. return _context7.stop();
  1549. }
  1550. }
  1551. }, null, this, null, Promise);
  1552. }
  1553. }, {
  1554. key: "fromNetwork",
  1555. value: function () {
  1556. this._queriesLocalDatastore = false;
  1557. this._localDatastorePinName = null;
  1558. return this;
  1559. }
  1560. }, {
  1561. key: "fromLocalDatastore",
  1562. value: function () {
  1563. return this.fromPinWithName(null);
  1564. }
  1565. }, {
  1566. key: "fromPin",
  1567. value: function () {
  1568. return this.fromPinWithName(_LocalDatastoreUtils.DEFAULT_PIN);
  1569. }
  1570. }, {
  1571. key: "fromPinWithName",
  1572. value: function (name) {
  1573. var localDatastore = _CoreManager.default.getLocalDatastore();
  1574. if (localDatastore.checkIfEnabled()) {
  1575. this._queriesLocalDatastore = true;
  1576. this._localDatastorePinName = name;
  1577. }
  1578. return this;
  1579. }
  1580. }, {
  1581. key: "cancel",
  1582. value: function () {
  1583. var _this14 = this;
  1584. if (this._xhrRequest.task && typeof this._xhrRequest.task.abort === 'function') {
  1585. this._xhrRequest.task._aborted = true;
  1586. this._xhrRequest.task.abort();
  1587. this._xhrRequest.task = null;
  1588. this._xhrRequest.onchange = function () {};
  1589. return this;
  1590. }
  1591. return this._xhrRequest.onchange = function () {
  1592. return _this14.cancel();
  1593. };
  1594. }
  1595. }, {
  1596. key: "_setRequestTask",
  1597. value: function (options) {
  1598. var _this15 = this;
  1599. options.requestTask = function (task) {
  1600. _this15._xhrRequest.task = task;
  1601. _this15._xhrRequest.onchange();
  1602. };
  1603. }
  1604. }], [{
  1605. key: "fromJSON",
  1606. value: function (className, json) {
  1607. var query = new ParseQuery(className);
  1608. return query.withJSON(json);
  1609. }
  1610. }, {
  1611. key: "or",
  1612. value: function () {
  1613. for (var _len8 = arguments.length, queries = new Array(_len8), _key12 = 0; _key12 < _len8; _key12++) {
  1614. queries[_key12] = arguments[_key12];
  1615. }
  1616. var className = _getClassNameFromQueries(queries);
  1617. var query = new ParseQuery(className);
  1618. query._orQuery(queries);
  1619. return query;
  1620. }
  1621. }, {
  1622. key: "and",
  1623. value: function () {
  1624. for (var _len9 = arguments.length, queries = new Array(_len9), _key13 = 0; _key13 < _len9; _key13++) {
  1625. queries[_key13] = arguments[_key13];
  1626. }
  1627. var className = _getClassNameFromQueries(queries);
  1628. var query = new ParseQuery(className);
  1629. query._andQuery(queries);
  1630. return query;
  1631. }
  1632. }, {
  1633. key: "nor",
  1634. value: function () {
  1635. for (var _len10 = arguments.length, queries = new Array(_len10), _key14 = 0; _key14 < _len10; _key14++) {
  1636. queries[_key14] = arguments[_key14];
  1637. }
  1638. var className = _getClassNameFromQueries(queries);
  1639. var query = new ParseQuery(className);
  1640. query._norQuery(queries);
  1641. return query;
  1642. }
  1643. }]);
  1644. return ParseQuery;
  1645. }();
  1646. var DefaultController = {
  1647. find: function (className, params, options) {
  1648. var RESTController = _CoreManager.default.getRESTController();
  1649. return RESTController.request('GET', 'classes/' + className, params, options);
  1650. },
  1651. aggregate: function (className, params, options) {
  1652. var RESTController = _CoreManager.default.getRESTController();
  1653. return RESTController.request('GET', 'aggregate/' + className, params, options);
  1654. }
  1655. };
  1656. _CoreManager.default.setQueryController(DefaultController);
  1657. var _default = ParseQuery;
  1658. exports.default = _default;