12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084 |
- "use strict";
- var _Object$defineProperty = require("@babel/runtime-corejs3/core-js-stable/object/define-property");
- var _interopRequireDefault = require("@babel/runtime-corejs3/helpers/interopRequireDefault");
- _Object$defineProperty(exports, "__esModule", {
- value: true
- });
- exports.default = void 0;
- var _defineProperty2 = _interopRequireDefault(require("@babel/runtime-corejs3/helpers/defineProperty"));
- var _forEach = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/instance/for-each"));
- var _indexOf = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/instance/index-of"));
- var _keys = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/object/keys"));
- var _slice = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/instance/slice"));
- var _map = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/instance/map"));
- var _filter = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/instance/filter"));
- var _keys2 = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/instance/keys"));
- var _concat = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/instance/concat"));
- var _includes = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/instance/includes"));
- var _sort = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/instance/sort"));
- var _splice = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/instance/splice"));
- var _promise = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/promise"));
- var _find = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/instance/find"));
- var _isArray = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/array/is-array"));
- var _entries = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/object/entries"));
- var _CoreManager = _interopRequireDefault(require("./CoreManager"));
- var _encode = _interopRequireDefault(require("./encode"));
- var _promiseUtils = require("./promiseUtils");
- var _ParseError = _interopRequireDefault(require("./ParseError"));
- var _ParseGeoPoint = _interopRequireDefault(require("./ParseGeoPoint"));
- var _ParseObject = _interopRequireDefault(require("./ParseObject"));
- var _OfflineQuery = _interopRequireDefault(require("./OfflineQuery"));
- var _LocalDatastoreUtils = require("./LocalDatastoreUtils");
- function quote(s) {
- return '\\Q' + s.replace('\\E', '\\E\\\\E\\Q') + '\\E';
- }
- function _getClassNameFromQueries(queries) {
- let className = null;
- (0, _forEach.default)(queries).call(queries, q => {
- if (!className) {
- className = q.className;
- }
- if (className !== q.className) {
- throw new Error('All queries must be for the same class.');
- }
- });
- return className;
- }
- function handleSelectResult(data, select) {
- const serverDataMask = {};
- (0, _forEach.default)(select).call(select, field => {
- const hasSubObjectSelect = (0, _indexOf.default)(field).call(field, '.') !== -1;
- if (!hasSubObjectSelect && !data.hasOwnProperty(field)) {
-
- data[field] = undefined;
- } else if (hasSubObjectSelect) {
-
-
- const pathComponents = field.split('.');
- let obj = data;
- let serverMask = serverDataMask;
- (0, _forEach.default)(pathComponents).call(pathComponents, (component, index, arr) => {
-
- if (obj && !obj.hasOwnProperty(component)) {
- obj[component] = undefined;
- }
- if (obj && typeof obj === 'object') {
- obj = obj[component];
- }
-
- if (index < arr.length - 1) {
- if (!serverMask[component]) {
- serverMask[component] = {};
- }
- serverMask = serverMask[component];
- }
- });
- }
- });
- if ((0, _keys.default)(serverDataMask).length > 0) {
-
-
-
-
- const serverData = _CoreManager.default.getObjectStateController().getServerData({
- id: data.objectId,
- className: data.className
- });
- copyMissingDataWithMask(serverData, data, serverDataMask, false);
- }
- }
- function copyMissingDataWithMask(src, dest, mask, copyThisLevel) {
-
- if (copyThisLevel) {
- for (const key in src) {
- if (src.hasOwnProperty(key) && !dest.hasOwnProperty(key)) {
- dest[key] = src[key];
- }
- }
- }
- for (const key in mask) {
- if (dest[key] !== undefined && dest[key] !== null && src !== undefined && src !== null) {
-
- copyMissingDataWithMask(src[key], dest[key], mask[key], true);
- }
- }
- }
- function handleOfflineSort(a, b, sorts) {
- let order = sorts[0];
- const operator = (0, _slice.default)(order).call(order, 0, 1);
- const isDescending = operator === '-';
- if (isDescending) {
- order = order.substring(1);
- }
- if (order === '_created_at') {
- order = 'createdAt';
- }
- if (order === '_updated_at') {
- order = 'updatedAt';
- }
- if (!/^[A-Za-z][0-9A-Za-z_]*$/.test(order) || order === 'password') {
- throw new _ParseError.default(_ParseError.default.INVALID_KEY_NAME, `Invalid Key: ${order}`);
- }
- const field1 = a.get(order);
- const field2 = b.get(order);
- if (field1 < field2) {
- return isDescending ? 1 : -1;
- }
- if (field1 > field2) {
- return isDescending ? -1 : 1;
- }
- if (sorts.length > 1) {
- const remainingSorts = (0, _slice.default)(sorts).call(sorts, 1);
- return handleOfflineSort(a, b, remainingSorts);
- }
- return 0;
- }
- class ParseQuery {
-
- constructor(objectClass) {
-
- (0, _defineProperty2.default)(this, "className", void 0);
- (0, _defineProperty2.default)(this, "_where", void 0);
- (0, _defineProperty2.default)(this, "_watch", void 0);
- (0, _defineProperty2.default)(this, "_include", void 0);
- (0, _defineProperty2.default)(this, "_exclude", void 0);
- (0, _defineProperty2.default)(this, "_select", void 0);
- (0, _defineProperty2.default)(this, "_limit", void 0);
- (0, _defineProperty2.default)(this, "_skip", void 0);
- (0, _defineProperty2.default)(this, "_count", void 0);
- (0, _defineProperty2.default)(this, "_order", void 0);
- (0, _defineProperty2.default)(this, "_readPreference", void 0);
- (0, _defineProperty2.default)(this, "_includeReadPreference", void 0);
- (0, _defineProperty2.default)(this, "_subqueryReadPreference", void 0);
- (0, _defineProperty2.default)(this, "_queriesLocalDatastore", void 0);
- (0, _defineProperty2.default)(this, "_localDatastorePinName", void 0);
- (0, _defineProperty2.default)(this, "_extraOptions", void 0);
- (0, _defineProperty2.default)(this, "_hint", void 0);
- (0, _defineProperty2.default)(this, "_explain", void 0);
- (0, _defineProperty2.default)(this, "_xhrRequest", void 0);
- (0, _defineProperty2.default)(this, "_comment", void 0);
- if (typeof objectClass === 'string') {
- if (objectClass === 'User' && _CoreManager.default.get('PERFORM_USER_REWRITE')) {
- this.className = '_User';
- } else {
- this.className = objectClass;
- }
- } else if (objectClass instanceof _ParseObject.default) {
- this.className = objectClass.className;
- } else if (typeof objectClass === 'function') {
- const objClass = objectClass;
- if (typeof objClass.className === 'string') {
- this.className = objClass.className;
- } else {
- const obj = new objClass();
- this.className = obj.className;
- }
- } else {
- throw new TypeError('A ParseQuery must be constructed with a ParseObject or class name.');
- }
- this._where = {};
- this._watch = [];
- this._include = [];
- this._exclude = [];
- this._count = false;
- this._limit = -1;
- this._skip = 0;
- this._readPreference = null;
- this._includeReadPreference = null;
- this._subqueryReadPreference = null;
- this._queriesLocalDatastore = false;
- this._localDatastorePinName = null;
- this._extraOptions = {};
- this._xhrRequest = {
- task: null,
- onchange: () => {}
- };
- this._comment = null;
- }
-
- _orQuery(queries) {
- const queryJSON = (0, _map.default)(queries).call(queries, q => {
- return q.toJSON().where;
- });
- this._where.$or = queryJSON;
- return this;
- }
-
- _andQuery(queries) {
- const queryJSON = (0, _map.default)(queries).call(queries, q => {
- return q.toJSON().where;
- });
- this._where.$and = queryJSON;
- return this;
- }
-
- _norQuery(queries) {
- const queryJSON = (0, _map.default)(queries).call(queries, q => {
- return q.toJSON().where;
- });
- this._where.$nor = queryJSON;
- return this;
- }
-
- _addCondition(key, condition, value) {
- if (!this._where[key] || typeof this._where[key] === 'string') {
- this._where[key] = {};
- }
- this._where[key][condition] = (0, _encode.default)(value, false, true);
- return this;
- }
-
- _regexStartWith(string) {
- return '^' + quote(string);
- }
- async _handleOfflineQuery(params) {
- var _context;
- _OfflineQuery.default.validateQuery(this);
- const localDatastore = _CoreManager.default.getLocalDatastore();
- const objects = await localDatastore._serializeObjectsFromPinName(this._localDatastorePinName);
- let results = (0, _filter.default)(_context = (0, _map.default)(objects).call(objects, (json, index, arr) => {
- const object = _ParseObject.default.fromJSON(json, false);
- if (json._localId && !json.objectId) {
- object._localId = json._localId;
- }
- if (!_OfflineQuery.default.matchesQuery(this.className, object, arr, this)) {
- return null;
- }
- return object;
- })).call(_context, object => object !== null);
- if ((0, _keys2.default)(params)) {
- let keys = (0, _keys2.default)(params).split(',');
- keys = (0, _concat.default)(keys).call(keys, ['className', 'objectId', 'createdAt', 'updatedAt', 'ACL']);
- results = (0, _map.default)(results).call(results, object => {
- var _context2;
- const json = object._toFullJSON();
- (0, _forEach.default)(_context2 = (0, _keys.default)(json)).call(_context2, key => {
- if (!(0, _includes.default)(keys).call(keys, key)) {
- delete json[key];
- }
- });
- return _ParseObject.default.fromJSON(json, false);
- });
- }
- if (params.order) {
- const sorts = params.order.split(',');
- (0, _sort.default)(results).call(results, (a, b) => {
- return handleOfflineSort(a, b, sorts);
- });
- }
- let count;
- if (params.count) {
- count = results.length;
- }
- if (params.skip) {
- if (params.skip >= results.length) {
- results = [];
- } else {
- results = (0, _splice.default)(results).call(results, params.skip, results.length);
- }
- }
- let limit = results.length;
- if (params.limit !== 0 && params.limit < results.length) {
- limit = params.limit;
- }
- results = (0, _splice.default)(results).call(results, 0, limit);
- if (typeof count === 'number') {
- return {
- results,
- count
- };
- }
- return results;
- }
-
- toJSON() {
- const params = {
- where: this._where
- };
- if (this._watch.length) {
- params.watch = this._watch.join(',');
- }
- if (this._include.length) {
- params.include = this._include.join(',');
- }
- if (this._exclude.length) {
- params.excludeKeys = this._exclude.join(',');
- }
- if (this._select) {
- params.keys = this._select.join(',');
- }
- if (this._count) {
- params.count = 1;
- }
- if (this._limit >= 0) {
- params.limit = this._limit;
- }
- if (this._skip > 0) {
- params.skip = this._skip;
- }
- if (this._order) {
- params.order = this._order.join(',');
- }
- if (this._readPreference) {
- params.readPreference = this._readPreference;
- }
- if (this._includeReadPreference) {
- params.includeReadPreference = this._includeReadPreference;
- }
- if (this._subqueryReadPreference) {
- params.subqueryReadPreference = this._subqueryReadPreference;
- }
- if (this._hint) {
- params.hint = this._hint;
- }
- if (this._explain) {
- params.explain = true;
- }
- if (this._comment) {
- params.comment = this._comment;
- }
- for (const key in this._extraOptions) {
- params[key] = this._extraOptions[key];
- }
- return params;
- }
-
- withJSON(json) {
- if (json.where) {
- this._where = json.where;
- }
- if (json.watch) {
- this._watch = json.watch.split(',');
- }
- if (json.include) {
- this._include = json.include.split(',');
- }
- if ((0, _keys2.default)(json)) {
- this._select = (0, _keys2.default)(json).split(',');
- }
- if (json.excludeKeys) {
- this._exclude = json.excludeKeys.split(',');
- }
- if (json.count) {
- this._count = json.count === 1;
- }
- if (json.limit) {
- this._limit = json.limit;
- }
- if (json.skip) {
- this._skip = json.skip;
- }
- if (json.order) {
- this._order = json.order.split(',');
- }
- if (json.readPreference) {
- this._readPreference = json.readPreference;
- }
- if (json.includeReadPreference) {
- this._includeReadPreference = json.includeReadPreference;
- }
- if (json.subqueryReadPreference) {
- this._subqueryReadPreference = json.subqueryReadPreference;
- }
- if (json.hint) {
- this._hint = json.hint;
- }
- if (json.explain) {
- this._explain = !!json.explain;
- }
- if (json.comment) {
- this._comment = json.comment;
- }
- for (const key in json) {
- if (json.hasOwnProperty(key)) {
- var _context3;
- if ((0, _indexOf.default)(_context3 = ['where', 'include', 'keys', 'count', 'limit', 'skip', 'order', 'readPreference', 'includeReadPreference', 'subqueryReadPreference', 'hint', 'explain', 'comment']).call(_context3, key) === -1) {
- this._extraOptions[key] = json[key];
- }
- }
- }
- return this;
- }
-
- static fromJSON(className, json) {
- const query = new ParseQuery(className);
- return query.withJSON(json);
- }
-
- get(objectId, options) {
- this.equalTo('objectId', objectId);
- const firstOptions = {};
- if (options && options.hasOwnProperty('useMasterKey')) {
- firstOptions.useMasterKey = options.useMasterKey;
- }
- if (options && options.hasOwnProperty('sessionToken')) {
- firstOptions.sessionToken = options.sessionToken;
- }
- if (options && options.hasOwnProperty('context') && typeof options.context === 'object') {
- firstOptions.context = options.context;
- }
- if (options && options.hasOwnProperty('json')) {
- firstOptions.json = options.json;
- }
- return this.first(firstOptions).then(response => {
- if (response) {
- return response;
- }
- const errorObject = new _ParseError.default(_ParseError.default.OBJECT_NOT_FOUND, 'Object not found.');
- return _promise.default.reject(errorObject);
- });
- }
-
- find(options) {
- options = options || {};
- const findOptions = {};
- if (options.hasOwnProperty('useMasterKey')) {
- findOptions.useMasterKey = options.useMasterKey;
- }
- if (options.hasOwnProperty('sessionToken')) {
- findOptions.sessionToken = options.sessionToken;
- }
- if (options.hasOwnProperty('context') && typeof options.context === 'object') {
- findOptions.context = options.context;
- }
- this._setRequestTask(findOptions);
- const controller = _CoreManager.default.getQueryController();
- const select = this._select;
- if (this._queriesLocalDatastore) {
- return this._handleOfflineQuery(this.toJSON());
- }
- return (0, _find.default)(controller).call(controller, this.className, this.toJSON(), findOptions).then(response => {
-
- if (this._explain) {
- return response.results;
- }
- const results = response.results?.map(data => {
-
-
- const override = response.className || this.className;
- if (!data.className) {
- data.className = override;
- }
-
-
-
- if (select) {
- handleSelectResult(data, select);
- }
- if (options.json) {
- return data;
- } else {
- return _ParseObject.default.fromJSON(data, !select);
- }
- });
- const count = response.count;
- if (typeof count === 'number') {
- return {
- results,
- count
- };
- } else {
- return results;
- }
- });
- }
-
- async findAll(options) {
- let result = [];
- await this.eachBatch(objects => {
- result = [...result, ...objects];
- }, options);
- return result;
- }
-
- count(options) {
- options = options || {};
- const findOptions = {};
- if (options.hasOwnProperty('useMasterKey')) {
- findOptions.useMasterKey = options.useMasterKey;
- }
- if (options.hasOwnProperty('sessionToken')) {
- findOptions.sessionToken = options.sessionToken;
- }
- this._setRequestTask(findOptions);
- const controller = _CoreManager.default.getQueryController();
- const params = this.toJSON();
- params.limit = 0;
- params.count = 1;
- return (0, _find.default)(controller).call(controller, this.className, params, findOptions).then(result => {
- return result.count;
- });
- }
-
- distinct(key, options) {
- options = options || {};
- const distinctOptions = {
- useMasterKey: true
- };
- if (options.hasOwnProperty('sessionToken')) {
- distinctOptions.sessionToken = options.sessionToken;
- }
- this._setRequestTask(distinctOptions);
- const controller = _CoreManager.default.getQueryController();
- const params = {
- distinct: key,
- where: this._where,
- hint: this._hint
- };
- return controller.aggregate(this.className, params, distinctOptions).then(results => {
- return results.results;
- });
- }
-
- aggregate(pipeline, options) {
- options = options || {};
- const aggregateOptions = {
- useMasterKey: true
- };
- if (options.hasOwnProperty('sessionToken')) {
- aggregateOptions.sessionToken = options.sessionToken;
- }
- this._setRequestTask(aggregateOptions);
- const controller = _CoreManager.default.getQueryController();
- if (!(0, _isArray.default)(pipeline) && typeof pipeline !== 'object') {
- throw new Error('Invalid pipeline must be Array or Object');
- }
- if ((0, _keys.default)(this._where || {}).length) {
- if (!(0, _isArray.default)(pipeline)) {
- pipeline = [pipeline];
- }
- pipeline.unshift({
- $match: this._where
- });
- }
- const params = {
- pipeline,
- hint: this._hint,
- explain: this._explain,
- readPreference: this._readPreference
- };
- return controller.aggregate(this.className, params, aggregateOptions).then(results => {
- return results.results;
- });
- }
-
- first() {
- let options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
- const findOptions = {};
- if (options.hasOwnProperty('useMasterKey')) {
- findOptions.useMasterKey = options.useMasterKey;
- }
- if (options.hasOwnProperty('sessionToken')) {
- findOptions.sessionToken = options.sessionToken;
- }
- if (options.hasOwnProperty('context') && typeof options.context === 'object') {
- findOptions.context = options.context;
- }
- this._setRequestTask(findOptions);
- const controller = _CoreManager.default.getQueryController();
- const params = this.toJSON();
- params.limit = 1;
- const select = this._select;
- if (this._queriesLocalDatastore) {
- return this._handleOfflineQuery(params).then(objects => {
- if (!objects[0]) {
- return undefined;
- }
- return objects[0];
- });
- }
- return (0, _find.default)(controller).call(controller, this.className, params, findOptions).then(response => {
- const objects = response.results;
- if (!objects[0]) {
- return undefined;
- }
- if (!objects[0].className) {
- objects[0].className = this.className;
- }
-
-
-
- if (select) {
- handleSelectResult(objects[0], select);
- }
- if (options.json) {
- return objects[0];
- } else {
- return _ParseObject.default.fromJSON(objects[0], !select);
- }
- });
- }
-
- eachBatch(callback, options) {
- options = options || {};
- if (this._order || this._skip || this._limit >= 0) {
- return _promise.default.reject('Cannot iterate on a query with sort, skip, or limit.');
- }
- const query = new ParseQuery(this.className);
- query._limit = options.batchSize || 100;
- query._include = [...this._include];
- query._exclude = [...this._exclude];
- if (this._select) {
- query._select = [...this._select];
- }
- query._hint = this._hint;
- query._where = {};
- for (const attr in this._where) {
- const val = this._where[attr];
- if ((0, _isArray.default)(val)) {
- query._where[attr] = (0, _map.default)(val).call(val, v => {
- return v;
- });
- } else if (val && typeof val === 'object') {
- const conditionMap = {};
- query._where[attr] = conditionMap;
- for (const cond in val) {
- conditionMap[cond] = val[cond];
- }
- } else {
- query._where[attr] = val;
- }
- }
- query.ascending('objectId');
- const findOptions = {};
- if (options.hasOwnProperty('useMasterKey')) {
- findOptions.useMasterKey = options.useMasterKey;
- }
- if (options.hasOwnProperty('sessionToken')) {
- findOptions.sessionToken = options.sessionToken;
- }
- if (options.hasOwnProperty('context') && typeof options.context === 'object') {
- findOptions.context = options.context;
- }
- if (options.hasOwnProperty('json')) {
- findOptions.json = options.json;
- }
- let finished = false;
- let previousResults = [];
- return (0, _promiseUtils.continueWhile)(() => {
- return !finished;
- }, async () => {
- const [results] = await _promise.default.all([(0, _find.default)(query).call(query, findOptions), _promise.default.resolve(previousResults.length > 0 && callback(previousResults))]);
- if (results.length >= query._limit) {
- query.greaterThan('objectId', results[results.length - 1].id);
- previousResults = results;
- } else if (results.length > 0) {
- await _promise.default.resolve(callback(results));
- finished = true;
- } else {
- finished = true;
- }
- });
- }
-
- each(callback, options) {
- return this.eachBatch(results => {
- let callbacksDone = _promise.default.resolve();
- (0, _forEach.default)(results).call(results, result => {
- callbacksDone = callbacksDone.then(() => {
- return callback(result);
- });
- });
- return callbacksDone;
- }, options);
- }
-
- hint(value) {
- if (typeof value === 'undefined') {
- delete this._hint;
- }
- this._hint = value;
- return this;
- }
-
- explain() {
- let explain = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : true;
- if (typeof explain !== 'boolean') {
- throw new Error('You can only set explain to a boolean value');
- }
- this._explain = explain;
- return this;
- }
-
- async map(callback, options) {
- const array = [];
- let index = 0;
- await this.each(object => {
- return _promise.default.resolve(callback(object, index, this)).then(result => {
- array.push(result);
- index += 1;
- });
- }, options);
- return array;
- }
-
- async reduce(callback, initialValue, options) {
- let accumulator = initialValue;
- let index = 0;
- await this.each(object => {
-
-
- if (index === 0 && initialValue === undefined) {
- accumulator = object;
- index += 1;
- return;
- }
- return _promise.default.resolve(callback(accumulator, object, index)).then(result => {
- accumulator = result;
- index += 1;
- });
- }, options);
- if (index === 0 && initialValue === undefined) {
-
-
- throw new TypeError('Reducing empty query result set with no initial value');
- }
- return accumulator;
- }
-
- async filter(callback, options) {
- const array = [];
- let index = 0;
- await this.each(object => {
- return _promise.default.resolve(callback(object, index, this)).then(flag => {
- if (flag) {
- array.push(object);
- }
- index += 1;
- });
- }, options);
- return array;
- }
-
-
- equalTo(key, value) {
- if (key && typeof key === 'object') {
- var _context4;
- (0, _forEach.default)(_context4 = (0, _entries.default)(key)).call(_context4, _ref => {
- let [k, val] = _ref;
- return this.equalTo(k, val);
- });
- return this;
- }
- if (typeof value === 'undefined') {
- return this.doesNotExist(key);
- }
- this._where[key] = (0, _encode.default)(value, false, true);
- return this;
- }
-
- notEqualTo(key, value) {
- if (key && typeof key === 'object') {
- var _context5;
- (0, _forEach.default)(_context5 = (0, _entries.default)(key)).call(_context5, _ref2 => {
- let [k, val] = _ref2;
- return this.notEqualTo(k, val);
- });
- return this;
- }
- return this._addCondition(key, '$ne', value);
- }
-
- lessThan(key, value) {
- return this._addCondition(key, '$lt', value);
- }
-
- greaterThan(key, value) {
- return this._addCondition(key, '$gt', value);
- }
-
- lessThanOrEqualTo(key, value) {
- return this._addCondition(key, '$lte', value);
- }
-
- greaterThanOrEqualTo(key, value) {
- return this._addCondition(key, '$gte', value);
- }
-
- containedIn(key, value) {
- return this._addCondition(key, '$in', value);
- }
-
- notContainedIn(key, value) {
- return this._addCondition(key, '$nin', value);
- }
-
- containedBy(key, values) {
- return this._addCondition(key, '$containedBy', values);
- }
-
- containsAll(key, values) {
- return this._addCondition(key, '$all', values);
- }
-
- containsAllStartingWith(key, values) {
- if (!(0, _isArray.default)(values)) {
- values = [values];
- }
- const regexObject = (0, _map.default)(values).call(values, value => {
- return {
- $regex: this._regexStartWith(value)
- };
- });
- return this.containsAll(key, regexObject);
- }
-
- exists(key) {
- return this._addCondition(key, '$exists', true);
- }
-
- doesNotExist(key) {
- return this._addCondition(key, '$exists', false);
- }
-
- matches(key, regex, modifiers) {
- this._addCondition(key, '$regex', regex);
- if (!modifiers) {
- modifiers = '';
- }
- if (typeof regex !== 'string') {
- if (regex.ignoreCase) {
- modifiers += 'i';
- }
- if (regex.multiline) {
- modifiers += 'm';
- }
- }
- if (modifiers.length) {
- this._addCondition(key, '$options', modifiers);
- }
- return this;
- }
-
- matchesQuery(key, query) {
- const queryJSON = query.toJSON();
- queryJSON.className = query.className;
- return this._addCondition(key, '$inQuery', queryJSON);
- }
-
- doesNotMatchQuery(key, query) {
- const queryJSON = query.toJSON();
- queryJSON.className = query.className;
- return this._addCondition(key, '$notInQuery', queryJSON);
- }
-
- matchesKeyInQuery(key, queryKey, query) {
- const queryJSON = query.toJSON();
- queryJSON.className = query.className;
- return this._addCondition(key, '$select', {
- key: queryKey,
- query: queryJSON
- });
- }
-
- doesNotMatchKeyInQuery(key, queryKey, query) {
- const queryJSON = query.toJSON();
- queryJSON.className = query.className;
- return this._addCondition(key, '$dontSelect', {
- key: queryKey,
- query: queryJSON
- });
- }
-
- contains(key, substring) {
- if (typeof substring !== 'string') {
- throw new Error('The value being searched for must be a string.');
- }
- return this._addCondition(key, '$regex', quote(substring));
- }
-
- fullText(key, value) {
- let options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
- options = options || {};
- if (!key) {
- throw new Error('A key is required.');
- }
- if (!value) {
- throw new Error('A search term is required');
- }
- if (typeof value !== 'string') {
- throw new Error('The value being searched for must be a string.');
- }
- const fullOptions = {};
- fullOptions.$term = value;
- for (const option in options) {
- switch (option) {
- case 'language':
- fullOptions.$language = options[option];
- break;
- case 'caseSensitive':
- fullOptions.$caseSensitive = options[option];
- break;
- case 'diacriticSensitive':
- fullOptions.$diacriticSensitive = options[option];
- break;
- default:
- throw new Error(`Unknown option: ${option}`);
- }
- }
- return this._addCondition(key, '$text', {
- $search: fullOptions
- });
- }
-
- sortByTextScore() {
- this.ascending('$score');
- this.select(['$score']);
- return this;
- }
-
- startsWith(key, prefix, modifiers) {
- if (typeof prefix !== 'string') {
- throw new Error('The value being searched for must be a string.');
- }
- return this.matches(key, this._regexStartWith(prefix), modifiers);
- }
-
- endsWith(key, suffix, modifiers) {
- if (typeof suffix !== 'string') {
- throw new Error('The value being searched for must be a string.');
- }
- return this.matches(key, quote(suffix) + '$', modifiers);
- }
-
- near(key, point) {
- if (!(point instanceof _ParseGeoPoint.default)) {
-
- point = new _ParseGeoPoint.default(point);
- }
- return this._addCondition(key, '$nearSphere', point);
- }
-
- withinRadians(key, point, maxDistance, sorted) {
- if (sorted || sorted === undefined) {
- this.near(key, point);
- return this._addCondition(key, '$maxDistance', maxDistance);
- } else {
- return this._addCondition(key, '$geoWithin', {
- $centerSphere: [[point.longitude, point.latitude], maxDistance]
- });
- }
- }
-
- withinMiles(key, point, maxDistance, sorted) {
- return this.withinRadians(key, point, maxDistance / 3958.8, sorted);
- }
-
- withinKilometers(key, point, maxDistance, sorted) {
- return this.withinRadians(key, point, maxDistance / 6371.0, sorted);
- }
-
- withinGeoBox(key, southwest, northeast) {
- if (!(southwest instanceof _ParseGeoPoint.default)) {
- southwest = new _ParseGeoPoint.default(southwest);
- }
- if (!(northeast instanceof _ParseGeoPoint.default)) {
- northeast = new _ParseGeoPoint.default(northeast);
- }
- this._addCondition(key, '$within', {
- $box: [southwest, northeast]
- });
- return this;
- }
-
- withinPolygon(key, points) {
- return this._addCondition(key, '$geoWithin', {
- $polygon: points
- });
- }
-
- polygonContains(key, point) {
- return this._addCondition(key, '$geoIntersects', {
- $point: point
- });
- }
-
-
- ascending() {
- this._order = [];
- for (var _len = arguments.length, keys = new Array(_len), _key = 0; _key < _len; _key++) {
- keys[_key] = arguments[_key];
- }
- return this.addAscending.apply(this, keys);
- }
-
- addAscending() {
- if (!this._order) {
- this._order = [];
- }
- for (var _len2 = arguments.length, keys = new Array(_len2), _key2 = 0; _key2 < _len2; _key2++) {
- keys[_key2] = arguments[_key2];
- }
- (0, _forEach.default)(keys).call(keys, key => {
- var _context6;
- if ((0, _isArray.default)(key)) {
- key = key.join();
- }
- this._order = (0, _concat.default)(_context6 = this._order).call(_context6, key.replace(/\s/g, '').split(','));
- });
- return this;
- }
-
- descending() {
- this._order = [];
- for (var _len3 = arguments.length, keys = new Array(_len3), _key3 = 0; _key3 < _len3; _key3++) {
- keys[_key3] = arguments[_key3];
- }
- return this.addDescending.apply(this, keys);
- }
-
- addDescending() {
- if (!this._order) {
- this._order = [];
- }
- for (var _len4 = arguments.length, keys = new Array(_len4), _key4 = 0; _key4 < _len4; _key4++) {
- keys[_key4] = arguments[_key4];
- }
- (0, _forEach.default)(keys).call(keys, key => {
- var _context7, _context8;
- if ((0, _isArray.default)(key)) {
- key = key.join();
- }
- this._order = (0, _concat.default)(_context7 = this._order).call(_context7, (0, _map.default)(_context8 = key.replace(/\s/g, '').split(',')).call(_context8, k => {
- return '-' + k;
- }));
- });
- return this;
- }
-
-
- skip(n) {
- if (typeof n !== 'number' || n < 0) {
- throw new Error('You can only skip by a positive number');
- }
- this._skip = n;
- return this;
- }
-
- limit(n) {
- if (typeof n !== 'number') {
- throw new Error('You can only set the limit to a numeric value');
- }
- this._limit = n;
- return this;
- }
-
- withCount() {
- let includeCount = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : true;
- if (typeof includeCount !== 'boolean') {
- throw new Error('You can only set withCount to a boolean value');
- }
- this._count = includeCount;
- return this;
- }
-
- include() {
- for (var _len5 = arguments.length, keys = new Array(_len5), _key5 = 0; _key5 < _len5; _key5++) {
- keys[_key5] = arguments[_key5];
- }
- (0, _forEach.default)(keys).call(keys, key => {
- if ((0, _isArray.default)(key)) {
- var _context9;
- this._include = (0, _concat.default)(_context9 = this._include).call(_context9, key);
- } else {
- this._include.push(key);
- }
- });
- return this;
- }
-
- includeAll() {
- return this.include('*');
- }
-
- select() {
- if (!this._select) {
- this._select = [];
- }
- for (var _len6 = arguments.length, keys = new Array(_len6), _key6 = 0; _key6 < _len6; _key6++) {
- keys[_key6] = arguments[_key6];
- }
- (0, _forEach.default)(keys).call(keys, key => {
- if ((0, _isArray.default)(key)) {
- var _context10;
- this._select = (0, _concat.default)(_context10 = this._select).call(_context10, key);
- } else {
- this._select.push(key);
- }
- });
- return this;
- }
-
- exclude() {
- for (var _len7 = arguments.length, keys = new Array(_len7), _key7 = 0; _key7 < _len7; _key7++) {
- keys[_key7] = arguments[_key7];
- }
- (0, _forEach.default)(keys).call(keys, key => {
- if ((0, _isArray.default)(key)) {
- var _context11;
- this._exclude = (0, _concat.default)(_context11 = this._exclude).call(_context11, key);
- } else {
- this._exclude.push(key);
- }
- });
- return this;
- }
-
- watch() {
- for (var _len8 = arguments.length, keys = new Array(_len8), _key8 = 0; _key8 < _len8; _key8++) {
- keys[_key8] = arguments[_key8];
- }
- (0, _forEach.default)(keys).call(keys, key => {
- if ((0, _isArray.default)(key)) {
- var _context12;
- this._watch = (0, _concat.default)(_context12 = this._watch).call(_context12, key);
- } else {
- this._watch.push(key);
- }
- });
- return this;
- }
-
- readPreference(readPreference, includeReadPreference, subqueryReadPreference) {
- this._readPreference = readPreference;
- this._includeReadPreference = includeReadPreference || null;
- this._subqueryReadPreference = subqueryReadPreference || null;
- return this;
- }
-
- async subscribe(sessionToken) {
- const currentUser = await _CoreManager.default.getUserController().currentUserAsync();
- if (!sessionToken) {
- sessionToken = currentUser ? currentUser.getSessionToken() || undefined : undefined;
- }
- const liveQueryClient = await _CoreManager.default.getLiveQueryController().getDefaultLiveQueryClient();
- if (liveQueryClient.shouldOpen()) {
- liveQueryClient.open();
- }
- const subscription = liveQueryClient.subscribe(this, sessionToken);
- return subscription.subscribePromise.then(() => {
- return subscription;
- });
- }
-
- static or() {
- for (var _len9 = arguments.length, queries = new Array(_len9), _key9 = 0; _key9 < _len9; _key9++) {
- queries[_key9] = arguments[_key9];
- }
- const className = _getClassNameFromQueries(queries);
- const query = new ParseQuery(className);
- query._orQuery(queries);
- return query;
- }
-
- static and() {
- for (var _len10 = arguments.length, queries = new Array(_len10), _key10 = 0; _key10 < _len10; _key10++) {
- queries[_key10] = arguments[_key10];
- }
- const className = _getClassNameFromQueries(queries);
- const query = new ParseQuery(className);
- query._andQuery(queries);
- return query;
- }
-
- static nor() {
- for (var _len11 = arguments.length, queries = new Array(_len11), _key11 = 0; _key11 < _len11; _key11++) {
- queries[_key11] = arguments[_key11];
- }
- const className = _getClassNameFromQueries(queries);
- const query = new ParseQuery(className);
- query._norQuery(queries);
- return query;
- }
-
- fromNetwork() {
- this._queriesLocalDatastore = false;
- this._localDatastorePinName = null;
- return this;
- }
-
- fromLocalDatastore() {
- return this.fromPinWithName(null);
- }
-
- fromPin() {
- return this.fromPinWithName(_LocalDatastoreUtils.DEFAULT_PIN);
- }
-
- fromPinWithName(name) {
- const localDatastore = _CoreManager.default.getLocalDatastore();
- if (localDatastore.checkIfEnabled()) {
- this._queriesLocalDatastore = true;
- this._localDatastorePinName = name;
- }
- return this;
- }
-
- cancel() {
- if (this._xhrRequest.task && typeof this._xhrRequest.task.abort === 'function') {
- this._xhrRequest.task._aborted = true;
- this._xhrRequest.task.abort();
- this._xhrRequest.task = null;
- this._xhrRequest.onchange = () => {};
- return this;
- }
- this._xhrRequest.onchange = () => this.cancel();
- return this;
- }
- _setRequestTask(options) {
- options.requestTask = task => {
- this._xhrRequest.task = task;
- this._xhrRequest.onchange();
- };
- }
-
- comment(value) {
- if (value == null) {
- delete this._comment;
- return this;
- }
- if (typeof value !== 'string') {
- throw new Error('The value of a comment to be sent with this query must be a string.');
- }
- this._comment = value;
- return this;
- }
- }
- const DefaultController = {
- find(className, params, options) {
- const RESTController = _CoreManager.default.getRESTController();
- return RESTController.request('GET', 'classes/' + className, params, options);
- },
- aggregate(className, params, options) {
- const RESTController = _CoreManager.default.getRESTController();
- return RESTController.request('GET', 'aggregate/' + className, params, options);
- }
- };
- _CoreManager.default.setParseQuery(ParseQuery);
- _CoreManager.default.setQueryController(DefaultController);
- var _default = exports.default = ParseQuery;
|