123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850 |
- "use strict";
- Object.defineProperty(exports, "__esModule", {
- value: true
- });
- exports.default = void 0;
- var _CoreManager = _interopRequireDefault(require("./CoreManager"));
- var _canBeSerialized = _interopRequireDefault(require("./canBeSerialized"));
- var _decode = _interopRequireDefault(require("./decode"));
- var _encode = _interopRequireDefault(require("./encode"));
- var _escape = _interopRequireDefault(require("./escape"));
- var _ParseACL = _interopRequireDefault(require("./ParseACL"));
- var _parseDate = _interopRequireDefault(require("./parseDate"));
- var _ParseError = _interopRequireDefault(require("./ParseError"));
- var _ParseFile = _interopRequireDefault(require("./ParseFile"));
- var _promiseUtils = require("./promiseUtils");
- var _LocalDatastoreUtils = require("./LocalDatastoreUtils");
- var _ParseOp = require("./ParseOp");
- var _ParseQuery = _interopRequireDefault(require("./ParseQuery"));
- var _ParseRelation = _interopRequireDefault(require("./ParseRelation"));
- var SingleInstanceStateController = _interopRequireWildcard(require("./SingleInstanceStateController"));
- var _unique = _interopRequireDefault(require("./unique"));
- var UniqueInstanceStateController = _interopRequireWildcard(require("./UniqueInstanceStateController"));
- var _unsavedChildren = _interopRequireDefault(require("./unsavedChildren"));
- function _interopRequireWildcard(obj) {
- if (obj && obj.__esModule) {
- return obj;
- } else {
- var newObj = {};
- if (obj != null) {
- for (var key in obj) {
- if (Object.prototype.hasOwnProperty.call(obj, key)) {
- var desc = Object.defineProperty && Object.getOwnPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : {};
- if (desc.get || desc.set) {
- Object.defineProperty(newObj, key, desc);
- } else {
- newObj[key] = obj[key];
- }
- }
- }
- }
- newObj.default = obj;
- return newObj;
- }
- }
- function _interopRequireDefault(obj) {
- return obj && obj.__esModule ? obj : {
- default: obj
- };
- }
- /**
- * Copyright (c) 2015-present, Parse, LLC.
- * All rights reserved.
- *
- * This source code is licensed under the BSD-style license found in the
- * LICENSE file in the root directory of this source tree. An additional grant
- * of patent rights can be found in the PATENTS file in the same directory.
- *
- * @flow
- */
- const DEFAULT_BATCH_SIZE = 20; // Mapping of class names to constructors, so we can populate objects from the
- // server with appropriate subclasses of ParseObject
- const classMap = {}; // Global counter for generating unique local Ids
- let localCount = 0; // Global counter for generating unique Ids for non-single-instance objects
- let objectCount = 0; // On web clients, objects are single-instance: any two objects with the same Id
- // will have the same attributes. However, this may be dangerous default
- // behavior in a server scenario
- let singleInstance = !_CoreManager.default.get('IS_NODE');
- if (singleInstance) {
- _CoreManager.default.setObjectStateController(SingleInstanceStateController);
- } else {
- _CoreManager.default.setObjectStateController(UniqueInstanceStateController);
- }
- function getServerUrlPath() {
- let serverUrl = _CoreManager.default.get('SERVER_URL');
- if (serverUrl[serverUrl.length - 1] !== '/') {
- serverUrl += '/';
- }
- const url = serverUrl.replace(/https?:\/\//, '');
- return url.substr(url.indexOf('/'));
- }
- /**
- * Creates a new model with defined attributes.
- *
- * <p>You won't normally call this method directly. It is recommended that
- * you use a subclass of <code>Parse.Object</code> instead, created by calling
- * <code>extend</code>.</p>
- *
- * <p>However, if you don't want to use a subclass, or aren't sure which
- * subclass is appropriate, you can use this form:<pre>
- * var object = new Parse.Object("ClassName");
- * </pre>
- * That is basically equivalent to:<pre>
- * var MyClass = Parse.Object.extend("ClassName");
- * var object = new MyClass();
- * </pre></p>
- *
- * @alias Parse.Object
- */
- class ParseObject {
- /**
- * @param {String} className The class name for the object
- * @param {Object} attributes The initial set of data to store in the object.
- * @param {Object} options The options for this object instance.
- */
- constructor(className
- /*: ?string | { className: string, [attr: string]: mixed }*/
- , attributes
- /*:: ?: { [attr: string]: mixed }*/
- , options
- /*:: ?: { ignoreValidation: boolean }*/
- ) {
- // Enable legacy initializers
- if (typeof this.initialize === 'function') {
- this.initialize.apply(this, arguments);
- }
- let toSet = null;
- this._objCount = objectCount++;
- if (typeof className === 'string') {
- this.className = className;
- if (attributes && typeof attributes === 'object') {
- toSet = attributes;
- }
- } else if (className && typeof className === 'object') {
- this.className = className.className;
- toSet = {};
- for (const attr in className) {
- if (attr !== 'className') {
- toSet[attr] = className[attr];
- }
- }
- if (attributes && typeof attributes === 'object') {
- options = attributes;
- }
- }
- if (toSet && !this.set(toSet, options)) {
- throw new Error('Can\'t create an invalid Parse Object');
- }
- }
- /**
- * The ID of this object, unique within its class.
- * @property id
- * @type String
- */
- /*:: id: ?string;*/
- /*:: _localId: ?string;*/
- /*:: _objCount: number;*/
- /*:: className: string;*/
- /** Prototype getters / setters **/
- get attributes()
- /*: AttributeMap*/
- {
- const stateController = _CoreManager.default.getObjectStateController();
- return Object.freeze(stateController.estimateAttributes(this._getStateIdentifier()));
- }
- /**
- * The first time this object was saved on the server.
- * @property createdAt
- * @type Date
- */
- get createdAt()
- /*: ?Date*/
- {
- return this._getServerData().createdAt;
- }
- /**
- * The last time this object was updated on the server.
- * @property updatedAt
- * @type Date
- */
- get updatedAt()
- /*: ?Date*/
- {
- return this._getServerData().updatedAt;
- }
- /** Private methods **/
- /**
- * Returns a local or server Id used uniquely identify this object
- */
- _getId()
- /*: string*/
- {
- if (typeof this.id === 'string') {
- return this.id;
- }
- if (typeof this._localId === 'string') {
- return this._localId;
- }
- const localId = 'local' + String(localCount++);
- this._localId = localId;
- return localId;
- }
- /**
- * Returns a unique identifier used to pull data from the State Controller.
- */
- _getStateIdentifier()
- /*: ParseObject | {id: string, className: string}*/
- {
- if (singleInstance) {
- let id = this.id;
- if (!id) {
- id = this._getId();
- }
- return {
- id: id,
- className: this.className
- };
- } else {
- return this;
- }
- }
- _getServerData()
- /*: AttributeMap*/
- {
- const stateController = _CoreManager.default.getObjectStateController();
- return stateController.getServerData(this._getStateIdentifier());
- }
- _clearServerData() {
- const serverData = this._getServerData();
- const unset = {};
- for (const attr in serverData) {
- unset[attr] = undefined;
- }
- const stateController = _CoreManager.default.getObjectStateController();
- stateController.setServerData(this._getStateIdentifier(), unset);
- }
- _getPendingOps()
- /*: Array<OpsMap>*/
- {
- const stateController = _CoreManager.default.getObjectStateController();
- return stateController.getPendingOps(this._getStateIdentifier());
- }
- /**
- * @param {Array<string>} [keysToClear] - if specified, only ops matching
- * these fields will be cleared
- */
- _clearPendingOps(keysToClear
- /*:: ?: Array<string>*/
- ) {
- const pending = this._getPendingOps();
- const latest = pending[pending.length - 1];
- const keys = keysToClear || Object.keys(latest);
- keys.forEach(key => {
- delete latest[key];
- });
- }
- _getDirtyObjectAttributes()
- /*: AttributeMap*/
- {
- const attributes = this.attributes;
- const stateController = _CoreManager.default.getObjectStateController();
- const objectCache = stateController.getObjectCache(this._getStateIdentifier());
- const dirty = {};
- for (const attr in attributes) {
- const val = attributes[attr];
- if (val && typeof val === 'object' && !(val instanceof ParseObject) && !(val instanceof _ParseFile.default) && !(val instanceof _ParseRelation.default)) {
- // Due to the way browsers construct maps, the key order will not change
- // unless the object is changed
- try {
- const json = (0, _encode.default)(val, false, true);
- const stringified = JSON.stringify(json);
- if (objectCache[attr] !== stringified) {
- dirty[attr] = val;
- }
- } catch (e) {
- // Error occurred, possibly by a nested unsaved pointer in a mutable container
- // No matter how it happened, it indicates a change in the attribute
- dirty[attr] = val;
- }
- }
- }
- return dirty;
- }
- _toFullJSON(seen
- /*:: ?: Array<any>*/
- )
- /*: AttributeMap*/
- {
- const json
- /*: { [key: string]: mixed }*/
- = this.toJSON(seen);
- json.__type = 'Object';
- json.className = this.className;
- return json;
- }
- _getSaveJSON()
- /*: AttributeMap*/
- {
- const pending = this._getPendingOps();
- const dirtyObjects = this._getDirtyObjectAttributes();
- const json = {};
- for (var attr in dirtyObjects) {
- let isDotNotation = false;
- for (let i = 0; i < pending.length; i += 1) {
- for (const field in pending[i]) {
- // Dot notation operations are handled later
- if (field.includes('.')) {
- const fieldName = field.split('.')[0];
- if (fieldName === attr) {
- isDotNotation = true;
- break;
- }
- }
- }
- }
- if (!isDotNotation) {
- json[attr] = new _ParseOp.SetOp(dirtyObjects[attr]).toJSON();
- }
- }
- for (attr in pending[0]) {
- json[attr] = pending[0][attr].toJSON();
- }
- return json;
- }
- _getSaveParams()
- /*: SaveParams*/
- {
- const method = this.id ? 'PUT' : 'POST';
- const body = this._getSaveJSON();
- let path = 'classes/' + this.className;
- if (this.id) {
- path += '/' + this.id;
- } else if (this.className === '_User') {
- path = 'users';
- }
- return {
- method,
- body,
- path
- };
- }
- _finishFetch(serverData
- /*: AttributeMap*/
- ) {
- if (!this.id && serverData.objectId) {
- this.id = serverData.objectId;
- }
- const stateController = _CoreManager.default.getObjectStateController();
- stateController.initializeState(this._getStateIdentifier());
- const decoded = {};
- for (const attr in serverData) {
- if (attr === 'ACL') {
- decoded[attr] = new _ParseACL.default(serverData[attr]);
- } else if (attr !== 'objectId') {
- decoded[attr] = (0, _decode.default)(serverData[attr]);
- if (decoded[attr] instanceof _ParseRelation.default) {
- decoded[attr]._ensureParentAndKey(this, attr);
- }
- }
- }
- if (decoded.createdAt && typeof decoded.createdAt === 'string') {
- decoded.createdAt = (0, _parseDate.default)(decoded.createdAt);
- }
- if (decoded.updatedAt && typeof decoded.updatedAt === 'string') {
- decoded.updatedAt = (0, _parseDate.default)(decoded.updatedAt);
- }
- if (!decoded.updatedAt && decoded.createdAt) {
- decoded.updatedAt = decoded.createdAt;
- }
- stateController.commitServerChanges(this._getStateIdentifier(), decoded);
- }
- _setExisted(existed
- /*: boolean*/
- ) {
- const stateController = _CoreManager.default.getObjectStateController();
- const state = stateController.getState(this._getStateIdentifier());
- if (state) {
- state.existed = existed;
- }
- }
- _migrateId(serverId
- /*: string*/
- ) {
- if (this._localId && serverId) {
- if (singleInstance) {
- const stateController = _CoreManager.default.getObjectStateController();
- const oldState = stateController.removeState(this._getStateIdentifier());
- this.id = serverId;
- delete this._localId;
- if (oldState) {
- stateController.initializeState(this._getStateIdentifier(), oldState);
- }
- } else {
- this.id = serverId;
- delete this._localId;
- }
- }
- }
- _handleSaveResponse(response
- /*: AttributeMap*/
- , status
- /*: number*/
- ) {
- const changes = {};
- const stateController = _CoreManager.default.getObjectStateController();
- const pending = stateController.popPendingState(this._getStateIdentifier());
- for (var attr in pending) {
- if (pending[attr] instanceof _ParseOp.RelationOp) {
- changes[attr] = pending[attr].applyTo(undefined, this, attr);
- } else if (!(attr in response)) {
- // Only SetOps and UnsetOps should not come back with results
- changes[attr] = pending[attr].applyTo(undefined);
- }
- }
- for (attr in response) {
- if ((attr === 'createdAt' || attr === 'updatedAt') && typeof response[attr] === 'string') {
- changes[attr] = (0, _parseDate.default)(response[attr]);
- } else if (attr === 'ACL') {
- changes[attr] = new _ParseACL.default(response[attr]);
- } else if (attr !== 'objectId') {
- changes[attr] = (0, _decode.default)(response[attr]);
- if (changes[attr] instanceof _ParseOp.UnsetOp) {
- changes[attr] = undefined;
- }
- }
- }
- if (changes.createdAt && !changes.updatedAt) {
- changes.updatedAt = changes.createdAt;
- }
- this._migrateId(response.objectId);
- if (status !== 201) {
- this._setExisted(true);
- }
- stateController.commitServerChanges(this._getStateIdentifier(), changes);
- }
- _handleSaveError() {
- const stateController = _CoreManager.default.getObjectStateController();
- stateController.mergeFirstPendingState(this._getStateIdentifier());
- }
- /** Public methods **/
- initialize() {} // NOOP
- /**
- * Returns a JSON version of the object suitable for saving to Parse.
- * @return {Object}
- */
- toJSON(seen
- /*: Array<any> | void*/
- )
- /*: AttributeMap*/
- {
- const seenEntry = this.id ? this.className + ':' + this.id : this;
- seen = seen || [seenEntry];
- const json = {};
- const attrs = this.attributes;
- for (const attr in attrs) {
- if ((attr === 'createdAt' || attr === 'updatedAt') && attrs[attr].toJSON) {
- json[attr] = attrs[attr].toJSON();
- } else {
- json[attr] = (0, _encode.default)(attrs[attr], false, false, seen);
- }
- }
- const pending = this._getPendingOps();
- for (const attr in pending[0]) {
- json[attr] = pending[0][attr].toJSON();
- }
- if (this.id) {
- json.objectId = this.id;
- }
- return json;
- }
- /**
- * Determines whether this ParseObject is equal to another ParseObject
- * @param {Object} other - An other object ot compare
- * @return {Boolean}
- */
- equals(other
- /*: mixed*/
- )
- /*: boolean*/
- {
- if (this === other) {
- return true;
- }
- return other instanceof ParseObject && this.className === other.className && this.id === other.id && typeof this.id !== 'undefined';
- }
- /**
- * Returns true if this object has been modified since its last
- * save/refresh. If an attribute is specified, it returns true only if that
- * particular attribute has been modified since the last save/refresh.
- * @param {String} attr An attribute name (optional).
- * @return {Boolean}
- */
- dirty(attr
- /*:: ?: string*/
- )
- /*: boolean*/
- {
- if (!this.id) {
- return true;
- }
- const pendingOps = this._getPendingOps();
- const dirtyObjects = this._getDirtyObjectAttributes();
- if (attr) {
- if (dirtyObjects.hasOwnProperty(attr)) {
- return true;
- }
- for (let i = 0; i < pendingOps.length; i++) {
- if (pendingOps[i].hasOwnProperty(attr)) {
- return true;
- }
- }
- return false;
- }
- if (Object.keys(pendingOps[0]).length !== 0) {
- return true;
- }
- if (Object.keys(dirtyObjects).length !== 0) {
- return true;
- }
- return false;
- }
- /**
- * Returns an array of keys that have been modified since last save/refresh
- * @return {String[]}
- */
- dirtyKeys()
- /*: Array<string>*/
- {
- const pendingOps = this._getPendingOps();
- const keys = {};
- for (let i = 0; i < pendingOps.length; i++) {
- for (const attr in pendingOps[i]) {
- keys[attr] = true;
- }
- }
- const dirtyObjects = this._getDirtyObjectAttributes();
- for (const attr in dirtyObjects) {
- keys[attr] = true;
- }
- return Object.keys(keys);
- }
- /**
- * Returns true if the object has been fetched.
- * @return {Boolean}
- */
- isDataAvailable()
- /*: boolean*/
- {
- const serverData = this._getServerData();
- return !!Object.keys(serverData).length;
- }
- /**
- * Gets a Pointer referencing this Object.
- * @return {Pointer}
- */
- toPointer()
- /*: Pointer*/
- {
- if (!this.id) {
- throw new Error('Cannot create a pointer to an unsaved ParseObject');
- }
- return {
- __type: 'Pointer',
- className: this.className,
- objectId: this.id
- };
- }
- /**
- * Gets the value of an attribute.
- * @param {String} attr The string name of an attribute.
- */
- get(attr
- /*: string*/
- )
- /*: mixed*/
- {
- return this.attributes[attr];
- }
- /**
- * Gets a relation on the given class for the attribute.
- * @param String attr The attribute to get the relation for.
- * @return {Parse.Relation}
- */
- relation(attr
- /*: string*/
- )
- /*: ParseRelation*/
- {
- const value = this.get(attr);
- if (value) {
- if (!(value instanceof _ParseRelation.default)) {
- throw new Error('Called relation() on non-relation field ' + attr);
- }
- value._ensureParentAndKey(this, attr);
- return value;
- }
- return new _ParseRelation.default(this, attr);
- }
- /**
- * Gets the HTML-escaped value of an attribute.
- * @param {String} attr The string name of an attribute.
- */
- escape(attr
- /*: string*/
- )
- /*: string*/
- {
- let val = this.attributes[attr];
- if (val == null) {
- return '';
- }
- if (typeof val !== 'string') {
- if (typeof val.toString !== 'function') {
- return '';
- }
- val = val.toString();
- }
- return (0, _escape.default)(val);
- }
- /**
- * Returns <code>true</code> if the attribute contains a value that is not
- * null or undefined.
- * @param {String} attr The string name of the attribute.
- * @return {Boolean}
- */
- has(attr
- /*: string*/
- )
- /*: boolean*/
- {
- const attributes = this.attributes;
- if (attributes.hasOwnProperty(attr)) {
- return attributes[attr] != null;
- }
- return false;
- }
- /**
- * Sets a hash of model attributes on the object.
- *
- * <p>You can call it with an object containing keys and values, with one
- * key and value, or dot notation. For example:<pre>
- * gameTurn.set({
- * player: player1,
- * diceRoll: 2
- * }, {
- * error: function(gameTurnAgain, error) {
- * // The set failed validation.
- * }
- * });
- *
- * game.set("currentPlayer", player2, {
- * error: function(gameTurnAgain, error) {
- * // The set failed validation.
- * }
- * });
- *
- * game.set("finished", true);</pre></p>
- *
- * game.set("player.score", 10);</pre></p>
- *
- * @param {String} key The key to set.
- * @param {} value The value to give it.
- * @param {Object} options A set of options for the set.
- * The only supported option is <code>error</code>.
- * @return {(ParseObject|Boolean)} true if the set succeeded.
- */
- set(key
- /*: mixed*/
- , value
- /*: mixed*/
- , options
- /*:: ?: mixed*/
- )
- /*: ParseObject | boolean*/
- {
- let changes = {};
- const newOps = {};
- if (key && typeof key === 'object') {
- changes = key;
- options = value;
- } else if (typeof key === 'string') {
- changes[key] = value;
- } else {
- return this;
- }
- options = options || {};
- let readonly = [];
- if (typeof this.constructor.readOnlyAttributes === 'function') {
- readonly = readonly.concat(this.constructor.readOnlyAttributes());
- }
- for (const k in changes) {
- if (k === 'createdAt' || k === 'updatedAt') {
- // This property is read-only, but for legacy reasons we silently
- // ignore it
- continue;
- }
- if (readonly.indexOf(k) > -1) {
- throw new Error('Cannot modify readonly attribute: ' + k);
- }
- if (options.unset) {
- newOps[k] = new _ParseOp.UnsetOp();
- } else if (changes[k] instanceof _ParseOp.Op) {
- newOps[k] = changes[k];
- } else if (changes[k] && typeof changes[k] === 'object' && typeof changes[k].__op === 'string') {
- newOps[k] = (0, _ParseOp.opFromJSON)(changes[k]);
- } else if (k === 'objectId' || k === 'id') {
- if (typeof changes[k] === 'string') {
- this.id = changes[k];
- }
- } else if (k === 'ACL' && typeof changes[k] === 'object' && !(changes[k] instanceof _ParseACL.default)) {
- newOps[k] = new _ParseOp.SetOp(new _ParseACL.default(changes[k]));
- } else if (changes[k] instanceof _ParseRelation.default) {
- const relation = new _ParseRelation.default(this, k);
- relation.targetClassName = changes[k].targetClassName;
- newOps[k] = new _ParseOp.SetOp(relation);
- } else {
- newOps[k] = new _ParseOp.SetOp(changes[k]);
- }
- }
- const currentAttributes = this.attributes; // Only set nested fields if exists
- const serverData = this._getServerData();
- if (typeof key === 'string' && key.includes('.')) {
- const field = key.split('.')[0];
- if (!serverData[field]) {
- return this;
- }
- } // Calculate new values
- const newValues = {};
- for (const attr in newOps) {
- if (newOps[attr] instanceof _ParseOp.RelationOp) {
- newValues[attr] = newOps[attr].applyTo(currentAttributes[attr], this, attr);
- } else if (!(newOps[attr] instanceof _ParseOp.UnsetOp)) {
- newValues[attr] = newOps[attr].applyTo(currentAttributes[attr]);
- }
- } // Validate changes
- if (!options.ignoreValidation) {
- const validation = this.validate(newValues);
- if (validation) {
- if (typeof options.error === 'function') {
- options.error(this, validation);
- }
- return false;
- }
- } // Consolidate Ops
- const pendingOps = this._getPendingOps();
- const last = pendingOps.length - 1;
- const stateController = _CoreManager.default.getObjectStateController();
- for (const attr in newOps) {
- const nextOp = newOps[attr].mergeWith(pendingOps[last][attr]);
- stateController.setPendingOp(this._getStateIdentifier(), attr, nextOp);
- }
- return this;
- }
- /**
- * Remove an attribute from the model. This is a noop if the attribute doesn't
- * exist.
- * @param {String} attr The string name of an attribute.
- * @return {(ParseObject|Boolean)}
- */
- unset(attr
- /*: string*/
- , options
- /*:: ?: { [opt: string]: mixed }*/
- )
- /*: ParseObject | boolean*/
- {
- options = options || {};
- options.unset = true;
- return this.set(attr, null, options);
- }
- /**
- * Atomically increments the value of the given attribute the next time the
- * object is saved. If no amount is specified, 1 is used by default.
- *
- * @param attr {String} The key.
- * @param amount {Number} The amount to increment by (optional).
- * @return {(ParseObject|Boolean)}
- */
- increment(attr
- /*: string*/
- , amount
- /*:: ?: number*/
- )
- /*: ParseObject | boolean*/
- {
- if (typeof amount === 'undefined') {
- amount = 1;
- }
- if (typeof amount !== 'number') {
- throw new Error('Cannot increment by a non-numeric amount.');
- }
- return this.set(attr, new _ParseOp.IncrementOp(amount));
- }
- /**
- * Atomically add an object to the end of the array associated with a given
- * key.
- * @param attr {String} The key.
- * @param item {} The item to add.
- * @return {(ParseObject|Boolean)}
- */
- add(attr
- /*: string*/
- , item
- /*: mixed*/
- )
- /*: ParseObject | boolean*/
- {
- return this.set(attr, new _ParseOp.AddOp([item]));
- }
- /**
- * Atomically add the objects to the end of the array associated with a given
- * key.
- * @param attr {String} The key.
- * @param items {Object[]} The items to add.
- * @return {(ParseObject|Boolean)}
- */
- addAll(attr
- /*: string*/
- , items
- /*: Array<mixed>*/
- )
- /*: ParseObject | boolean*/
- {
- return this.set(attr, new _ParseOp.AddOp(items));
- }
- /**
- * Atomically add an object to the array associated with a given key, only
- * if it is not already present in the array. The position of the insert is
- * not guaranteed.
- *
- * @param attr {String} The key.
- * @param item {} The object to add.
- * @return {(ParseObject|Boolean)}
- */
- addUnique(attr
- /*: string*/
- , item
- /*: mixed*/
- )
- /*: ParseObject | boolean*/
- {
- return this.set(attr, new _ParseOp.AddUniqueOp([item]));
- }
- /**
- * Atomically add the objects to the array associated with a given key, only
- * if it is not already present in the array. The position of the insert is
- * not guaranteed.
- *
- * @param attr {String} The key.
- * @param items {Object[]} The objects to add.
- * @return {(ParseObject|Boolean)}
- */
- addAllUnique(attr
- /*: string*/
- , items
- /*: Array<mixed>*/
- )
- /*: ParseObject | boolean*/
- {
- return this.set(attr, new _ParseOp.AddUniqueOp(items));
- }
- /**
- * Atomically remove all instances of an object from the array associated
- * with a given key.
- *
- * @param attr {String} The key.
- * @param item {} The object to remove.
- * @return {(ParseObject|Boolean)}
- */
- remove(attr
- /*: string*/
- , item
- /*: mixed*/
- )
- /*: ParseObject | boolean*/
- {
- return this.set(attr, new _ParseOp.RemoveOp([item]));
- }
- /**
- * Atomically remove all instances of the objects from the array associated
- * with a given key.
- *
- * @param attr {String} The key.
- * @param items {Object[]} The object to remove.
- * @return {(ParseObject|Boolean)}
- */
- removeAll(attr
- /*: string*/
- , items
- /*: Array<mixed>*/
- )
- /*: ParseObject | boolean*/
- {
- return this.set(attr, new _ParseOp.RemoveOp(items));
- }
- /**
- * Returns an instance of a subclass of Parse.Op describing what kind of
- * modification has been performed on this field since the last time it was
- * saved. For example, after calling object.increment("x"), calling
- * object.op("x") would return an instance of Parse.Op.Increment.
- *
- * @param attr {String} The key.
- * @returns {Parse.Op} The operation, or undefined if none.
- */
- op(attr
- /*: string*/
- )
- /*: ?Op*/
- {
- const pending = this._getPendingOps();
- for (let i = pending.length; i--;) {
- if (pending[i][attr]) {
- return pending[i][attr];
- }
- }
- }
- /**
- * Creates a new model with identical attributes to this one.
- * @return {Parse.Object}
- */
- clone()
- /*: any*/
- {
- const clone = new this.constructor();
- if (!clone.className) {
- clone.className = this.className;
- }
- let attributes = this.attributes;
- if (typeof this.constructor.readOnlyAttributes === 'function') {
- const readonly = this.constructor.readOnlyAttributes() || []; // Attributes are frozen, so we have to rebuild an object,
- // rather than delete readonly keys
- const copy = {};
- for (const a in attributes) {
- if (readonly.indexOf(a) < 0) {
- copy[a] = attributes[a];
- }
- }
- attributes = copy;
- }
- if (clone.set) {
- clone.set(attributes);
- }
- return clone;
- }
- /**
- * Creates a new instance of this object. Not to be confused with clone()
- * @return {Parse.Object}
- */
- newInstance()
- /*: any*/
- {
- const clone = new this.constructor();
- if (!clone.className) {
- clone.className = this.className;
- }
- clone.id = this.id;
- if (singleInstance) {
- // Just return an object with the right id
- return clone;
- }
- const stateController = _CoreManager.default.getObjectStateController();
- if (stateController) {
- stateController.duplicateState(this._getStateIdentifier(), clone._getStateIdentifier());
- }
- return clone;
- }
- /**
- * Returns true if this object has never been saved to Parse.
- * @return {Boolean}
- */
- isNew()
- /*: boolean*/
- {
- return !this.id;
- }
- /**
- * Returns true if this object was created by the Parse server when the
- * object might have already been there (e.g. in the case of a Facebook
- * login)
- * @return {Boolean}
- */
- existed()
- /*: boolean*/
- {
- if (!this.id) {
- return false;
- }
- const stateController = _CoreManager.default.getObjectStateController();
- const state = stateController.getState(this._getStateIdentifier());
- if (state) {
- return state.existed;
- }
- return false;
- }
- /**
- * Checks if the model is currently in a valid state.
- * @return {Boolean}
- */
- isValid()
- /*: boolean*/
- {
- return !this.validate(this.attributes);
- }
- /**
- * You should not call this function directly unless you subclass
- * <code>Parse.Object</code>, in which case you can override this method
- * to provide additional validation on <code>set</code> and
- * <code>save</code>. Your implementation should return
- *
- * @param {Object} attrs The current data to validate.
- * @return {} False if the data is valid. An error object otherwise.
- * @see Parse.Object#set
- */
- validate(attrs
- /*: AttributeMap*/
- )
- /*: ParseError | boolean*/
- {
- if (attrs.hasOwnProperty('ACL') && !(attrs.ACL instanceof _ParseACL.default)) {
- return new _ParseError.default(_ParseError.default.OTHER_CAUSE, 'ACL must be a Parse ACL.');
- }
- for (const key in attrs) {
- if (!/^[A-Za-z][0-9A-Za-z_.]*$/.test(key)) {
- return new _ParseError.default(_ParseError.default.INVALID_KEY_NAME);
- }
- }
- return false;
- }
- /**
- * Returns the ACL for this object.
- * @returns {Parse.ACL} An instance of Parse.ACL.
- * @see Parse.Object#get
- */
- getACL()
- /*: ?ParseACL*/
- {
- const acl = this.get('ACL');
- if (acl instanceof _ParseACL.default) {
- return acl;
- }
- return null;
- }
- /**
- * Sets the ACL to be used for this object.
- * @param {Parse.ACL} acl An instance of Parse.ACL.
- * @param {Object} options
- * @return {(ParseObject|Boolean)} Whether the set passed validation.
- * @see Parse.Object#set
- */
- setACL(acl
- /*: ParseACL*/
- , options
- /*:: ?: mixed*/
- )
- /*: ParseObject | boolean*/
- {
- return this.set('ACL', acl, options);
- }
- /**
- * Clears any (or specific) changes to this object made since the last call to save()
- * @param {string} [keys] - specify which fields to revert
- */
- revert(...keys)
- /*: void*/
- {
- let keysToRevert;
- if (keys.length) {
- keysToRevert = [];
- for (const key of keys) {
- if (typeof key === "string") {
- keysToRevert.push(key);
- } else {
- throw new Error("Parse.Object#revert expects either no, or a list of string, arguments.");
- }
- }
- }
- this._clearPendingOps(keysToRevert);
- }
- /**
- * Clears all attributes on a model
- * @return {(ParseObject | boolean)}
- */
- clear()
- /*: ParseObject | boolean*/
- {
- const attributes = this.attributes;
- const erasable = {};
- let readonly = ['createdAt', 'updatedAt'];
- if (typeof this.constructor.readOnlyAttributes === 'function') {
- readonly = readonly.concat(this.constructor.readOnlyAttributes());
- }
- for (const attr in attributes) {
- if (readonly.indexOf(attr) < 0) {
- erasable[attr] = true;
- }
- }
- return this.set(erasable, {
- unset: true
- });
- }
- /**
- * Fetch the model from the server. If the server's representation of the
- * model differs from its current attributes, they will be overriden.
- *
- * @param {Object} options
- * Valid options are:<ul>
- * <li>useMasterKey: In Cloud Code and Node only, causes the Master Key to
- * be used for this request.
- * <li>sessionToken: A valid session token, used for making a request on
- * behalf of a specific user.
- * <li>include: The name(s) of the key(s) to include. Can be a string, an array of strings,
- * or an array of array of strings.
- * </ul>
- * @return {Promise} A promise that is fulfilled when the fetch
- * completes.
- */
- fetch(options
- /*: RequestOptions*/
- )
- /*: Promise*/
- {
- options = options || {};
- const fetchOptions = {};
- if (options.hasOwnProperty('useMasterKey')) {
- fetchOptions.useMasterKey = options.useMasterKey;
- }
- if (options.hasOwnProperty('sessionToken')) {
- fetchOptions.sessionToken = options.sessionToken;
- }
- if (options.hasOwnProperty('include')) {
- fetchOptions.include = [];
- if (Array.isArray(options.include)) {
- options.include.forEach(key => {
- if (Array.isArray(key)) {
- fetchOptions.include = fetchOptions.include.concat(key);
- } else {
- fetchOptions.include.push(key);
- }
- });
- } else {
- fetchOptions.include.push(options.include);
- }
- }
- const controller = _CoreManager.default.getObjectController();
- return controller.fetch(this, true, fetchOptions);
- }
- /**
- * Fetch the model from the server. If the server's representation of the
- * model differs from its current attributes, they will be overriden.
- *
- * Includes nested Parse.Objects for the provided key. You can use dot
- * notation to specify which fields in the included object are also fetched.
- *
- * @param {String|Array<string|Array<string>>} keys The name(s) of the key(s) to include.
- * @param {Object} options
- * Valid options are:<ul>
- * <li>useMasterKey: In Cloud Code and Node only, causes the Master Key to
- * be used for this request.
- * <li>sessionToken: A valid session token, used for making a request on
- * behalf of a specific user.
- * </ul>
- * @return {Promise} A promise that is fulfilled when the fetch
- * completes.
- */
- fetchWithInclude(keys
- /*: String|Array<string|Array<string>>*/
- , options
- /*: RequestOptions*/
- )
- /*: Promise*/
- {
- options = options || {};
- options.include = keys;
- return this.fetch(options);
- }
- /**
- * Set a hash of model attributes, and save the model to the server.
- * updatedAt will be updated when the request returns.
- * You can either call it as:<pre>
- * object.save();</pre>
- * or<pre>
- * object.save(attrs);</pre>
- * or<pre>
- * object.save(null, options);</pre>
- * or<pre>
- * object.save(attrs, options);</pre>
- * or<pre>
- * object.save(key, value, options);</pre>
- *
- * For example, <pre>
- * gameTurn.save({
- * player: "Jake Cutter",
- * diceRoll: 2
- * }).then(function(gameTurnAgain) {
- * // The save was successful.
- * }, function(error) {
- * // The save failed. Error is an instance of Parse.Error.
- * });</pre>
- *
- * @param {String|Object|null} [attrs]
- * Valid options are:<ul>
- * <li>`Object` - Key/value pairs to update on the object.</li>
- * <li>`String` Key - Key of attribute to update (requires arg2 to also be string)</li>
- * <li>`null` - Passing null for arg1 allows you to save the object with options passed in arg2.</li>
- * </ul>
- *
- * @param {String|Object} [options]
- * <ul>
- * <li>`String` Value - If arg1 was passed as a key, arg2 is the value that should be set on that key.</li>
- * <li>`Object` Options - Valid options are:
- * <ul>
- * <li>useMasterKey: In Cloud Code and Node only, causes the Master Key to
- * be used for this request.
- * <li>sessionToken: A valid session token, used for making a request on
- * behalf of a specific user.
- * </ul>
- * </li>
- * </ul>
- *
- * @param {Object} [options]
- * Used to pass option parameters to method if arg1 and arg2 were both passed as strings.
- * Valid options are:
- * <ul>
- * <li>useMasterKey: In Cloud Code and Node only, causes the Master Key to
- * be used for this request.
- * <li>sessionToken: A valid session token, used for making a request on
- * behalf of a specific user.
- * </ul>
- *
- * @return {Promise} A promise that is fulfilled when the save
- * completes.
- */
- save(arg1
- /*: ?string | { [attr: string]: mixed }*/
- , arg2
- /*: FullOptions | mixed*/
- , arg3
- /*:: ?: FullOptions*/
- )
- /*: Promise*/
- {
- let attrs;
- let options;
- if (typeof arg1 === 'object' || typeof arg1 === 'undefined') {
- attrs = arg1;
- if (typeof arg2 === 'object') {
- options = arg2;
- }
- } else {
- attrs = {};
- attrs[arg1] = arg2;
- options = arg3;
- } // TODO: safely remove me
- // Support save({ success: function() {}, error: function() {} })
- if (!options && attrs) {
- options = {};
- if (typeof attrs.success === 'function') {
- options.success = attrs.success;
- delete attrs.success;
- }
- if (typeof attrs.error === 'function') {
- options.error = attrs.error;
- delete attrs.error;
- }
- }
- if (attrs) {
- const validation = this.validate(attrs);
- if (validation) {
- if (options && typeof options.error === 'function') {
- options.error(this, validation);
- }
- return Promise.reject(validation);
- }
- this.set(attrs, options);
- }
- options = options || {};
- const saveOptions = {};
- if (options.hasOwnProperty('useMasterKey')) {
- saveOptions.useMasterKey = !!options.useMasterKey;
- }
- if (options.hasOwnProperty('sessionToken') && typeof options.sessionToken === 'string') {
- saveOptions.sessionToken = options.sessionToken;
- }
- const controller = _CoreManager.default.getObjectController();
- const unsaved = (0, _unsavedChildren.default)(this);
- return controller.save(unsaved, saveOptions).then(() => {
- return controller.save(this, saveOptions);
- });
- }
- /**
- * Destroy this model on the server if it was already persisted.
- *
- * @param {Object} options
- * Valid options are:<ul>
- * <li>useMasterKey: In Cloud Code and Node only, causes the Master Key to
- * be used for this request.
- * <li>sessionToken: A valid session token, used for making a request on
- * behalf of a specific user.
- * </ul>
- * @return {Promise} A promise that is fulfilled when the destroy
- * completes.
- */
- destroy(options
- /*: RequestOptions*/
- )
- /*: Promise*/
- {
- options = options || {};
- const destroyOptions = {};
- if (options.hasOwnProperty('useMasterKey')) {
- destroyOptions.useMasterKey = options.useMasterKey;
- }
- if (options.hasOwnProperty('sessionToken')) {
- destroyOptions.sessionToken = options.sessionToken;
- }
- if (!this.id) {
- return Promise.resolve();
- }
- return _CoreManager.default.getObjectController().destroy(this, destroyOptions);
- }
- /**
- * Asynchronously stores the object and every object it points to in the local datastore,
- * recursively, using a default pin name: _default.
- *
- * If those other objects have not been fetched from Parse, they will not be stored.
- * However, if they have changed data, all the changes will be retained.
- *
- * <pre>
- * await object.pin();
- * </pre>
- *
- * To retrieve object:
- * <code>query.fromLocalDatastore()</code> or <code>query.fromPin()</code>
- *
- * @return {Promise} A promise that is fulfilled when the pin completes.
- */
- pin()
- /*: Promise<void>*/
- {
- return ParseObject.pinAllWithName(_LocalDatastoreUtils.DEFAULT_PIN, [this]);
- }
- /**
- * Asynchronously removes the object and every object it points to in the local datastore,
- * recursively, using a default pin name: _default.
- *
- * <pre>
- * await object.unPin();
- * </pre>
- *
- * @return {Promise} A promise that is fulfilled when the unPin completes.
- */
- unPin()
- /*: Promise<void>*/
- {
- return ParseObject.unPinAllWithName(_LocalDatastoreUtils.DEFAULT_PIN, [this]);
- }
- /**
- * Asynchronously returns if the object is pinned
- *
- * <pre>
- * const isPinned = await object.isPinned();
- * </pre>
- *
- * @return {Promise<boolean>} A boolean promise that is fulfilled if object is pinned.
- */
- async isPinned()
- /*: Promise<boolean>*/
- {
- const localDatastore = _CoreManager.default.getLocalDatastore();
- if (!localDatastore.isEnabled) {
- return Promise.reject('Parse.enableLocalDatastore() must be called first');
- }
- const objectKey = localDatastore.getKeyForObject(this);
- const pin = await localDatastore.fromPinWithName(objectKey);
- return pin.length > 0;
- }
- /**
- * Asynchronously stores the objects and every object they point to in the local datastore, recursively.
- *
- * If those other objects have not been fetched from Parse, they will not be stored.
- * However, if they have changed data, all the changes will be retained.
- *
- * <pre>
- * await object.pinWithName(name);
- * </pre>
- *
- * To retrieve object:
- * <code>query.fromLocalDatastore()</code> or <code>query.fromPinWithName(name)</code>
- *
- * @param {String} name Name of Pin.
- * @return {Promise} A promise that is fulfilled when the pin completes.
- */
- pinWithName(name
- /*: string*/
- )
- /*: Promise<void>*/
- {
- return ParseObject.pinAllWithName(name, [this]);
- }
- /**
- * Asynchronously removes the object and every object it points to in the local datastore, recursively.
- *
- * <pre>
- * await object.unPinWithName(name);
- * </pre>
- *
- * @param {String} name Name of Pin.
- * @return {Promise} A promise that is fulfilled when the unPin completes.
- */
- unPinWithName(name
- /*: string*/
- )
- /*: Promise<void>*/
- {
- return ParseObject.unPinAllWithName(name, [this]);
- }
- /**
- * Asynchronously loads data from the local datastore into this object.
- *
- * <pre>
- * await object.fetchFromLocalDatastore();
- * </pre>
- *
- * You can create an unfetched pointer with <code>Parse.Object.createWithoutData()</code>
- * and then call <code>fetchFromLocalDatastore()</code> on it.
- *
- * @return {Promise} A promise that is fulfilled when the fetch completes.
- */
- async fetchFromLocalDatastore()
- /*: Promise<ParseObject>*/
- {
- const localDatastore = _CoreManager.default.getLocalDatastore();
- if (!localDatastore.isEnabled) {
- throw new Error('Parse.enableLocalDatastore() must be called first');
- }
- const objectKey = localDatastore.getKeyForObject(this);
- const pinned = await localDatastore._serializeObject(objectKey);
- if (!pinned) {
- throw new Error('Cannot fetch an unsaved ParseObject');
- }
- const result = ParseObject.fromJSON(pinned);
- this._finishFetch(result.toJSON());
- return this;
- }
- /** Static methods **/
- static _clearAllState() {
- const stateController = _CoreManager.default.getObjectStateController();
- stateController.clearAllState();
- }
- /**
- * Fetches the given list of Parse.Object.
- * If any error is encountered, stops and calls the error handler.
- *
- * <pre>
- * Parse.Object.fetchAll([object1, object2, ...])
- * .then((list) => {
- * // All the objects were fetched.
- * }, (error) => {
- * // An error occurred while fetching one of the objects.
- * });
- * </pre>
- *
- * @param {Array} list A list of <code>Parse.Object</code>.
- * @param {Object} options
- * Valid options are:<ul>
- * <li>useMasterKey: In Cloud Code and Node only, causes the Master Key to
- * be used for this request.
- * <li>sessionToken: A valid session token, used for making a request on
- * behalf of a specific user.
- * <li>include: The name(s) of the key(s) to include. Can be a string, an array of strings,
- * or an array of array of strings.
- * </ul>
- * @static
- */
- static fetchAll(list
- /*: Array<ParseObject>*/
- , options
- /*: RequestOptions*/
- = {}) {
- const queryOptions = {};
- if (options.hasOwnProperty('useMasterKey')) {
- queryOptions.useMasterKey = options.useMasterKey;
- }
- if (options.hasOwnProperty('sessionToken')) {
- queryOptions.sessionToken = options.sessionToken;
- }
- if (options.hasOwnProperty('include')) {
- queryOptions.include = [];
- if (Array.isArray(options.include)) {
- options.include.forEach(key => {
- if (Array.isArray(key)) {
- queryOptions.include = queryOptions.include.concat(key);
- } else {
- queryOptions.include.push(key);
- }
- });
- } else {
- queryOptions.include.push(options.include);
- }
- }
- return _CoreManager.default.getObjectController().fetch(list, true, queryOptions);
- }
- /**
- * Fetches the given list of Parse.Object.
- *
- * Includes nested Parse.Objects for the provided key. You can use dot
- * notation to specify which fields in the included object are also fetched.
- *
- * If any error is encountered, stops and calls the error handler.
- *
- * <pre>
- * Parse.Object.fetchAllWithInclude([object1, object2, ...], [pointer1, pointer2, ...])
- * .then((list) => {
- * // All the objects were fetched.
- * }, (error) => {
- * // An error occurred while fetching one of the objects.
- * });
- * </pre>
- *
- * @param {Array} list A list of <code>Parse.Object</code>.
- * @param {String|Array<string|Array<string>>} keys The name(s) of the key(s) to include.
- * @param {Object} options
- * Valid options are:<ul>
- * <li>useMasterKey: In Cloud Code and Node only, causes the Master Key to
- * be used for this request.
- * <li>sessionToken: A valid session token, used for making a request on
- * behalf of a specific user.
- * </ul>
- * @static
- */
- static fetchAllWithInclude(list
- /*: Array<ParseObject>*/
- , keys
- /*: String|Array<string|Array<string>>*/
- , options
- /*: RequestOptions*/
- ) {
- options = options || {};
- options.include = keys;
- return ParseObject.fetchAll(list, options);
- }
- /**
- * Fetches the given list of Parse.Object if needed.
- * If any error is encountered, stops and calls the error handler.
- *
- * <pre>
- * Parse.Object.fetchAllIfNeeded([object1, ...])
- * .then((list) => {
- * // Objects were fetched and updated.
- * }, (error) => {
- * // An error occurred while fetching one of the objects.
- * });
- * </pre>
- *
- * @param {Array} list A list of <code>Parse.Object</code>.
- * @param {Object} options
- * @static
- */
- static fetchAllIfNeeded(list
- /*: Array<ParseObject>*/
- , options) {
- options = options || {};
- const queryOptions = {};
- if (options.hasOwnProperty('useMasterKey')) {
- queryOptions.useMasterKey = options.useMasterKey;
- }
- if (options.hasOwnProperty('sessionToken')) {
- queryOptions.sessionToken = options.sessionToken;
- }
- return _CoreManager.default.getObjectController().fetch(list, false, queryOptions);
- }
- /**
- * Destroy the given list of models on the server if it was already persisted.
- *
- * <p>Unlike saveAll, if an error occurs while deleting an individual model,
- * this method will continue trying to delete the rest of the models if
- * possible, except in the case of a fatal error like a connection error.
- *
- * <p>In particular, the Parse.Error object returned in the case of error may
- * be one of two types:
- *
- * <ul>
- * <li>A Parse.Error.AGGREGATE_ERROR. This object's "errors" property is an
- * array of other Parse.Error objects. Each error object in this array
- * has an "object" property that references the object that could not be
- * deleted (for instance, because that object could not be found).</li>
- * <li>A non-aggregate Parse.Error. This indicates a serious error that
- * caused the delete operation to be aborted partway through (for
- * instance, a connection failure in the middle of the delete).</li>
- * </ul>
- *
- * <pre>
- * Parse.Object.destroyAll([object1, object2, ...])
- * .then((list) => {
- * // All the objects were deleted.
- * }, (error) => {
- * // An error occurred while deleting one or more of the objects.
- * // If this is an aggregate error, then we can inspect each error
- * // object individually to determine the reason why a particular
- * // object was not deleted.
- * if (error.code === Parse.Error.AGGREGATE_ERROR) {
- * for (var i = 0; i < error.errors.length; i++) {
- * console.log("Couldn't delete " + error.errors[i].object.id +
- * "due to " + error.errors[i].message);
- * }
- * } else {
- * console.log("Delete aborted because of " + error.message);
- * }
- * });
- * </pre>
- *
- * @param {Array} list A list of <code>Parse.Object</code>.
- * @param {Object} options
- * @static
- * Valid options are:<ul>
- * <li>useMasterKey: In Cloud Code and Node only, causes the Master Key to
- * be used for this request.
- * <li>sessionToken: A valid session token, used for making a request on
- * behalf of a specific user.
- * <li>batchSize: Number of objects to process per request
- * </ul>
- * @return {Promise} A promise that is fulfilled when the destroyAll
- * completes.
- */
- static destroyAll(list
- /*: Array<ParseObject>*/
- , options = {}) {
- const destroyOptions = {};
- if (options.hasOwnProperty('useMasterKey')) {
- destroyOptions.useMasterKey = options.useMasterKey;
- }
- if (options.hasOwnProperty('sessionToken')) {
- destroyOptions.sessionToken = options.sessionToken;
- }
- if (options.hasOwnProperty('batchSize') && typeof options.batchSize === 'number') {
- destroyOptions.batchSize = options.batchSize;
- }
- return _CoreManager.default.getObjectController().destroy(list, destroyOptions);
- }
- /**
- * Saves the given list of Parse.Object.
- * If any error is encountered, stops and calls the error handler.
- *
- * <pre>
- * Parse.Object.saveAll([object1, object2, ...])
- * .then((list) => {
- * // All the objects were saved.
- * }, (error) => {
- * // An error occurred while saving one of the objects.
- * });
- * </pre>
- *
- * @param {Array} list A list of <code>Parse.Object</code>.
- * @param {Object} options
- * @static
- * Valid options are:<ul>
- * <li>useMasterKey: In Cloud Code and Node only, causes the Master Key to
- * be used for this request.
- * <li>sessionToken: A valid session token, used for making a request on
- * behalf of a specific user.
- * <li>batchSize: Number of objects to process per request
- * </ul>
- */
- static saveAll(list
- /*: Array<ParseObject>*/
- , options
- /*: RequestOptions*/
- = {}) {
- const saveOptions = {};
- if (options.hasOwnProperty('useMasterKey')) {
- saveOptions.useMasterKey = options.useMasterKey;
- }
- if (options.hasOwnProperty('sessionToken')) {
- saveOptions.sessionToken = options.sessionToken;
- }
- if (options.hasOwnProperty('batchSize') && typeof options.batchSize === 'number') {
- saveOptions.batchSize = options.batchSize;
- }
- return _CoreManager.default.getObjectController().save(list, saveOptions);
- }
- /**
- * Creates a reference to a subclass of Parse.Object with the given id. This
- * does not exist on Parse.Object, only on subclasses.
- *
- * <p>A shortcut for: <pre>
- * var Foo = Parse.Object.extend("Foo");
- * var pointerToFoo = new Foo();
- * pointerToFoo.id = "myObjectId";
- * </pre>
- *
- * @param {String} id The ID of the object to create a reference to.
- * @static
- * @return {Parse.Object} A Parse.Object reference.
- */
- static createWithoutData(id
- /*: string*/
- ) {
- const obj = new this();
- obj.id = id;
- return obj;
- }
- /**
- * Creates a new instance of a Parse Object from a JSON representation.
- * @param {Object} json The JSON map of the Object's data
- * @param {boolean} override In single instance mode, all old server data
- * is overwritten if this is set to true
- * @static
- * @return {Parse.Object} A Parse.Object reference
- */
- static fromJSON(json
- /*: any*/
- , override
- /*:: ?: boolean*/
- ) {
- if (!json.className) {
- throw new Error('Cannot create an object without a className');
- }
- const constructor = classMap[json.className];
- const o = constructor ? new constructor() : new ParseObject(json.className);
- const otherAttributes = {};
- for (const attr in json) {
- if (attr !== 'className' && attr !== '__type') {
- otherAttributes[attr] = json[attr];
- }
- }
- if (override) {
- // id needs to be set before clearServerData can work
- if (otherAttributes.objectId) {
- o.id = otherAttributes.objectId;
- }
- let preserved = null;
- if (typeof o._preserveFieldsOnFetch === 'function') {
- preserved = o._preserveFieldsOnFetch();
- }
- o._clearServerData();
- if (preserved) {
- o._finishFetch(preserved);
- }
- }
- o._finishFetch(otherAttributes);
- if (json.objectId) {
- o._setExisted(true);
- }
- return o;
- }
- /**
- * Registers a subclass of Parse.Object with a specific class name.
- * When objects of that class are retrieved from a query, they will be
- * instantiated with this subclass.
- * This is only necessary when using ES6 subclassing.
- * @param {String} className The class name of the subclass
- * @param {Class} constructor The subclass
- */
- static registerSubclass(className
- /*: string*/
- , constructor
- /*: any*/
- ) {
- if (typeof className !== 'string') {
- throw new TypeError('The first argument must be a valid class name.');
- }
- if (typeof constructor === 'undefined') {
- throw new TypeError('You must supply a subclass constructor.');
- }
- if (typeof constructor !== 'function') {
- throw new TypeError('You must register the subclass constructor. ' + 'Did you attempt to register an instance of the subclass?');
- }
- classMap[className] = constructor;
- if (!constructor.className) {
- constructor.className = className;
- }
- }
- /**
- * Creates a new subclass of Parse.Object for the given Parse class name.
- *
- * <p>Every extension of a Parse class will inherit from the most recent
- * previous extension of that class. When a Parse.Object is automatically
- * created by parsing JSON, it will use the most recent extension of that
- * class.</p>
- *
- * <p>You should call either:<pre>
- * var MyClass = Parse.Object.extend("MyClass", {
- * <i>Instance methods</i>,
- * initialize: function(attrs, options) {
- * this.someInstanceProperty = [],
- * <i>Other instance properties</i>
- * }
- * }, {
- * <i>Class properties</i>
- * });</pre>
- * or, for Backbone compatibility:<pre>
- * var MyClass = Parse.Object.extend({
- * className: "MyClass",
- * <i>Instance methods</i>,
- * initialize: function(attrs, options) {
- * this.someInstanceProperty = [],
- * <i>Other instance properties</i>
- * }
- * }, {
- * <i>Class properties</i>
- * });</pre></p>
- *
- * @param {String} className The name of the Parse class backing this model.
- * @param {Object} protoProps Instance properties to add to instances of the
- * class returned from this method.
- * @param {Object} classProps Class properties to add the class returned from
- * this method.
- * @return {Class} A new subclass of Parse.Object.
- */
- static extend(className
- /*: any*/
- , protoProps
- /*: any*/
- , classProps
- /*: any*/
- ) {
- if (typeof className !== 'string') {
- if (className && typeof className.className === 'string') {
- return ParseObject.extend(className.className, className, protoProps);
- } else {
- throw new Error('Parse.Object.extend\'s first argument should be the className.');
- }
- }
- let adjustedClassName = className;
- if (adjustedClassName === 'User' && _CoreManager.default.get('PERFORM_USER_REWRITE')) {
- adjustedClassName = '_User';
- }
- let parentProto = ParseObject.prototype;
- if (this.hasOwnProperty('__super__') && this.__super__) {
- parentProto = this.prototype;
- } else if (classMap[adjustedClassName]) {
- parentProto = classMap[adjustedClassName].prototype;
- }
- const ParseObjectSubclass = function (attributes, options) {
- this.className = adjustedClassName;
- this._objCount = objectCount++; // Enable legacy initializers
- if (typeof this.initialize === 'function') {
- this.initialize.apply(this, arguments);
- }
- if (attributes && typeof attributes === 'object') {
- if (!this.set(attributes || {}, options)) {
- throw new Error('Can\'t create an invalid Parse Object');
- }
- }
- };
- ParseObjectSubclass.className = adjustedClassName;
- ParseObjectSubclass.__super__ = parentProto;
- ParseObjectSubclass.prototype = Object.create(parentProto, {
- constructor: {
- value: ParseObjectSubclass,
- enumerable: false,
- writable: true,
- configurable: true
- }
- });
- if (protoProps) {
- for (const prop in protoProps) {
- if (prop !== 'className') {
- Object.defineProperty(ParseObjectSubclass.prototype, prop, {
- value: protoProps[prop],
- enumerable: false,
- writable: true,
- configurable: true
- });
- }
- }
- }
- if (classProps) {
- for (const prop in classProps) {
- if (prop !== 'className') {
- Object.defineProperty(ParseObjectSubclass, prop, {
- value: classProps[prop],
- enumerable: false,
- writable: true,
- configurable: true
- });
- }
- }
- }
- ParseObjectSubclass.extend = function (name, protoProps, classProps) {
- if (typeof name === 'string') {
- return ParseObject.extend.call(ParseObjectSubclass, name, protoProps, classProps);
- }
- return ParseObject.extend.call(ParseObjectSubclass, adjustedClassName, name, protoProps);
- };
- ParseObjectSubclass.createWithoutData = ParseObject.createWithoutData;
- classMap[adjustedClassName] = ParseObjectSubclass;
- return ParseObjectSubclass;
- }
- /**
- * Enable single instance objects, where any local objects with the same Id
- * share the same attributes, and stay synchronized with each other.
- * This is disabled by default in server environments, since it can lead to
- * security issues.
- * @static
- */
- static enableSingleInstance() {
- singleInstance = true;
- _CoreManager.default.setObjectStateController(SingleInstanceStateController);
- }
- /**
- * Disable single instance objects, where any local objects with the same Id
- * share the same attributes, and stay synchronized with each other.
- * When disabled, you can have two instances of the same object in memory
- * without them sharing attributes.
- * @static
- */
- static disableSingleInstance() {
- singleInstance = false;
- _CoreManager.default.setObjectStateController(UniqueInstanceStateController);
- }
- /**
- * Asynchronously stores the objects and every object they point to in the local datastore,
- * recursively, using a default pin name: _default.
- *
- * If those other objects have not been fetched from Parse, they will not be stored.
- * However, if they have changed data, all the changes will be retained.
- *
- * <pre>
- * await Parse.Object.pinAll([...]);
- * </pre>
- *
- * To retrieve object:
- * <code>query.fromLocalDatastore()</code> or <code>query.fromPin()</code>
- *
- * @param {Array} objects A list of <code>Parse.Object</code>.
- * @return {Promise} A promise that is fulfilled when the pin completes.
- * @static
- */
- static pinAll(objects
- /*: Array<ParseObject>*/
- )
- /*: Promise<void>*/
- {
- const localDatastore = _CoreManager.default.getLocalDatastore();
- if (!localDatastore.isEnabled) {
- return Promise.reject('Parse.enableLocalDatastore() must be called first');
- }
- return ParseObject.pinAllWithName(_LocalDatastoreUtils.DEFAULT_PIN, objects);
- }
- /**
- * Asynchronously stores the objects and every object they point to in the local datastore, recursively.
- *
- * If those other objects have not been fetched from Parse, they will not be stored.
- * However, if they have changed data, all the changes will be retained.
- *
- * <pre>
- * await Parse.Object.pinAllWithName(name, [obj1, obj2, ...]);
- * </pre>
- *
- * To retrieve object:
- * <code>query.fromLocalDatastore()</code> or <code>query.fromPinWithName(name)</code>
- *
- * @param {String} name Name of Pin.
- * @param {Array} objects A list of <code>Parse.Object</code>.
- * @return {Promise} A promise that is fulfilled when the pin completes.
- * @static
- */
- static pinAllWithName(name
- /*: string*/
- , objects
- /*: Array<ParseObject>*/
- )
- /*: Promise<void>*/
- {
- const localDatastore = _CoreManager.default.getLocalDatastore();
- if (!localDatastore.isEnabled) {
- return Promise.reject('Parse.enableLocalDatastore() must be called first');
- }
- return localDatastore._handlePinAllWithName(name, objects);
- }
- /**
- * Asynchronously removes the objects and every object they point to in the local datastore,
- * recursively, using a default pin name: _default.
- *
- * <pre>
- * await Parse.Object.unPinAll([...]);
- * </pre>
- *
- * @param {Array} objects A list of <code>Parse.Object</code>.
- * @return {Promise} A promise that is fulfilled when the unPin completes.
- * @static
- */
- static unPinAll(objects
- /*: Array<ParseObject>*/
- )
- /*: Promise<void>*/
- {
- const localDatastore = _CoreManager.default.getLocalDatastore();
- if (!localDatastore.isEnabled) {
- return Promise.reject('Parse.enableLocalDatastore() must be called first');
- }
- return ParseObject.unPinAllWithName(_LocalDatastoreUtils.DEFAULT_PIN, objects);
- }
- /**
- * Asynchronously removes the objects and every object they point to in the local datastore, recursively.
- *
- * <pre>
- * await Parse.Object.unPinAllWithName(name, [obj1, obj2, ...]);
- * </pre>
- *
- * @param {String} name Name of Pin.
- * @param {Array} objects A list of <code>Parse.Object</code>.
- * @return {Promise} A promise that is fulfilled when the unPin completes.
- * @static
- */
- static unPinAllWithName(name
- /*: string*/
- , objects
- /*: Array<ParseObject>*/
- )
- /*: Promise<void>*/
- {
- const localDatastore = _CoreManager.default.getLocalDatastore();
- if (!localDatastore.isEnabled) {
- return Promise.reject('Parse.enableLocalDatastore() must be called first');
- }
- return localDatastore._handleUnPinAllWithName(name, objects);
- }
- /**
- * Asynchronously removes all objects in the local datastore using a default pin name: _default.
- *
- * <pre>
- * await Parse.Object.unPinAllObjects();
- * </pre>
- *
- * @return {Promise} A promise that is fulfilled when the unPin completes.
- * @static
- */
- static unPinAllObjects()
- /*: Promise<void>*/
- {
- const localDatastore = _CoreManager.default.getLocalDatastore();
- if (!localDatastore.isEnabled) {
- return Promise.reject('Parse.enableLocalDatastore() must be called first');
- }
- return localDatastore.unPinWithName(_LocalDatastoreUtils.DEFAULT_PIN);
- }
- /**
- * Asynchronously removes all objects with the specified pin name.
- * Deletes the pin name also.
- *
- * <pre>
- * await Parse.Object.unPinAllObjectsWithName(name);
- * </pre>
- *
- * @param {String} name Name of Pin.
- * @return {Promise} A promise that is fulfilled when the unPin completes.
- * @static
- */
- static unPinAllObjectsWithName(name
- /*: string*/
- )
- /*: Promise<void>*/
- {
- const localDatastore = _CoreManager.default.getLocalDatastore();
- if (!localDatastore.isEnabled) {
- return Promise.reject('Parse.enableLocalDatastore() must be called first');
- }
- return localDatastore.unPinWithName(_LocalDatastoreUtils.PIN_PREFIX + name);
- }
- }
- const DefaultController = {
- fetch(target
- /*: ParseObject | Array<ParseObject>*/
- , forceFetch
- /*: boolean*/
- , options
- /*: RequestOptions*/
- )
- /*: Promise<Array<void> | ParseObject>*/
- {
- const localDatastore = _CoreManager.default.getLocalDatastore();
- if (Array.isArray(target)) {
- if (target.length < 1) {
- return Promise.resolve([]);
- }
- const objs = [];
- const ids = [];
- let className = null;
- const results = [];
- let error = null;
- target.forEach(el => {
- if (error) {
- return;
- }
- if (!className) {
- className = el.className;
- }
- if (className !== el.className) {
- error = new _ParseError.default(_ParseError.default.INVALID_CLASS_NAME, 'All objects should be of the same class');
- }
- if (!el.id) {
- error = new _ParseError.default(_ParseError.default.MISSING_OBJECT_ID, 'All objects must have an ID');
- }
- if (forceFetch || !el.isDataAvailable()) {
- ids.push(el.id);
- objs.push(el);
- }
- results.push(el);
- });
- if (error) {
- return Promise.reject(error);
- }
- const query = new _ParseQuery.default(className);
- query.containedIn('objectId', ids);
- if (options && options.include) {
- query.include(options.include);
- }
- query._limit = ids.length;
- return query.find(options).then(async objects => {
- const idMap = {};
- objects.forEach(o => {
- idMap[o.id] = o;
- });
- for (let i = 0; i < objs.length; i++) {
- const obj = objs[i];
- if (!obj || !obj.id || !idMap[obj.id]) {
- if (forceFetch) {
- return Promise.reject(new _ParseError.default(_ParseError.default.OBJECT_NOT_FOUND, 'All objects must exist on the server.'));
- }
- }
- }
- if (!singleInstance) {
- // If single instance objects are disabled, we need to replace the
- for (let i = 0; i < results.length; i++) {
- const obj = results[i];
- if (obj && obj.id && idMap[obj.id]) {
- const id = obj.id;
- obj._finishFetch(idMap[id].toJSON());
- results[i] = idMap[id];
- }
- }
- }
- for (const object of results) {
- await localDatastore._updateObjectIfPinned(object);
- }
- return Promise.resolve(results);
- });
- } else {
- const RESTController = _CoreManager.default.getRESTController();
- const params = {};
- if (options && options.include) {
- params.include = options.include.join();
- }
- return RESTController.request('GET', 'classes/' + target.className + '/' + target._getId(), params, options).then(async response => {
- if (target instanceof ParseObject) {
- target._clearPendingOps();
- target._clearServerData();
- target._finishFetch(response);
- }
- await localDatastore._updateObjectIfPinned(target);
- return target;
- });
- }
- },
- async destroy(target
- /*: ParseObject | Array<ParseObject>*/
- , options
- /*: RequestOptions*/
- )
- /*: Promise<Array<void> | ParseObject>*/
- {
- const batchSize = options && options.batchSize ? options.batchSize : DEFAULT_BATCH_SIZE;
- const localDatastore = _CoreManager.default.getLocalDatastore();
- const RESTController = _CoreManager.default.getRESTController();
- if (Array.isArray(target)) {
- if (target.length < 1) {
- return Promise.resolve([]);
- }
- const batches = [[]];
- target.forEach(obj => {
- if (!obj.id) {
- return;
- }
- batches[batches.length - 1].push(obj);
- if (batches[batches.length - 1].length >= batchSize) {
- batches.push([]);
- }
- });
- if (batches[batches.length - 1].length === 0) {
- // If the last batch is empty, remove it
- batches.pop();
- }
- let deleteCompleted = Promise.resolve();
- const errors = [];
- batches.forEach(batch => {
- deleteCompleted = deleteCompleted.then(() => {
- return RESTController.request('POST', 'batch', {
- requests: batch.map(obj => {
- return {
- method: 'DELETE',
- path: getServerUrlPath() + 'classes/' + obj.className + '/' + obj._getId(),
- body: {}
- };
- })
- }, options).then(results => {
- for (let i = 0; i < results.length; i++) {
- if (results[i] && results[i].hasOwnProperty('error')) {
- const err = new _ParseError.default(results[i].error.code, results[i].error.error);
- err.object = batch[i];
- errors.push(err);
- }
- }
- });
- });
- });
- return deleteCompleted.then(async () => {
- if (errors.length) {
- const aggregate = new _ParseError.default(_ParseError.default.AGGREGATE_ERROR);
- aggregate.errors = errors;
- return Promise.reject(aggregate);
- }
- for (const object of target) {
- await localDatastore._destroyObjectIfPinned(object);
- }
- return Promise.resolve(target);
- });
- } else if (target instanceof ParseObject) {
- return RESTController.request('DELETE', 'classes/' + target.className + '/' + target._getId(), {}, options).then(async () => {
- await localDatastore._destroyObjectIfPinned(target);
- return Promise.resolve(target);
- });
- }
- await localDatastore._destroyObjectIfPinned(target);
- return Promise.resolve(target);
- },
- save(target
- /*: ParseObject | Array<ParseObject | ParseFile>*/
- , options
- /*: RequestOptions*/
- ) {
- const batchSize = options && options.batchSize ? options.batchSize : DEFAULT_BATCH_SIZE;
- const localDatastore = _CoreManager.default.getLocalDatastore();
- const mapIdForPin = {};
- const RESTController = _CoreManager.default.getRESTController();
- const stateController = _CoreManager.default.getObjectStateController();
- options = options || {};
- options.returnStatus = options.returnStatus || true;
- if (Array.isArray(target)) {
- if (target.length < 1) {
- return Promise.resolve([]);
- }
- let unsaved = target.concat();
- for (let i = 0; i < target.length; i++) {
- if (target[i] instanceof ParseObject) {
- unsaved = unsaved.concat((0, _unsavedChildren.default)(target[i], true));
- }
- }
- unsaved = (0, _unique.default)(unsaved);
- let filesSaved = Promise.resolve();
- let pending
- /*: Array<ParseObject>*/
- = [];
- unsaved.forEach(el => {
- if (el instanceof _ParseFile.default) {
- filesSaved = filesSaved.then(() => {
- return el.save();
- });
- } else if (el instanceof ParseObject) {
- pending.push(el);
- }
- });
- return filesSaved.then(() => {
- let objectError = null;
- return (0, _promiseUtils.continueWhile)(() => {
- return pending.length > 0;
- }, () => {
- const batch = [];
- const nextPending = [];
- pending.forEach(el => {
- if (batch.length < batchSize && (0, _canBeSerialized.default)(el)) {
- batch.push(el);
- } else {
- nextPending.push(el);
- }
- });
- pending = nextPending;
- if (batch.length < 1) {
- return Promise.reject(new _ParseError.default(_ParseError.default.OTHER_CAUSE, 'Tried to save a batch with a cycle.'));
- } // Queue up tasks for each object in the batch.
- // When every task is ready, the API request will execute
- let res, rej;
- const batchReturned = new Promise((resolve, reject) => {
- res = resolve;
- rej = reject;
- });
- batchReturned.resolve = res;
- batchReturned.reject = rej;
- const batchReady = [];
- const batchTasks = [];
- batch.forEach((obj, index) => {
- let res, rej;
- const ready = new Promise((resolve, reject) => {
- res = resolve;
- rej = reject;
- });
- ready.resolve = res;
- ready.reject = rej;
- batchReady.push(ready);
- stateController.pushPendingState(obj._getStateIdentifier());
- batchTasks.push(stateController.enqueueTask(obj._getStateIdentifier(), function () {
- ready.resolve();
- return batchReturned.then(responses => {
- if (responses[index].hasOwnProperty('success')) {
- const objectId = responses[index].success.objectId;
- const status = responses[index]._status;
- delete responses[index]._status;
- mapIdForPin[objectId] = obj._localId;
- obj._handleSaveResponse(responses[index].success, status);
- } else {
- if (!objectError && responses[index].hasOwnProperty('error')) {
- const serverError = responses[index].error;
- objectError = new _ParseError.default(serverError.code, serverError.error); // Cancel the rest of the save
- pending = [];
- }
- obj._handleSaveError();
- }
- });
- }));
- });
- (0, _promiseUtils.when)(batchReady).then(() => {
- // Kick off the batch request
- return RESTController.request('POST', 'batch', {
- requests: batch.map(obj => {
- const params = obj._getSaveParams();
- params.path = getServerUrlPath() + params.path;
- return params;
- })
- }, options);
- }).then(batchReturned.resolve, error => {
- batchReturned.reject(new _ParseError.default(_ParseError.default.INCORRECT_TYPE, error.message));
- });
- return (0, _promiseUtils.when)(batchTasks);
- }).then(async () => {
- if (objectError) {
- return Promise.reject(objectError);
- }
- for (const object of target) {
- await localDatastore._updateLocalIdForObject(mapIdForPin[object.id], object);
- await localDatastore._updateObjectIfPinned(object);
- }
- return Promise.resolve(target);
- });
- });
- } else if (target instanceof ParseObject) {
- // copying target lets Flow guarantee the pointer isn't modified elsewhere
- const localId = target._localId;
- const targetCopy = target;
- const task = function () {
- const params = targetCopy._getSaveParams();
- return RESTController.request(params.method, params.path, params.body, options).then(response => {
- const status = response._status;
- delete response._status;
- targetCopy._handleSaveResponse(response, status);
- }, error => {
- targetCopy._handleSaveError();
- return Promise.reject(error);
- });
- };
- stateController.pushPendingState(target._getStateIdentifier());
- return stateController.enqueueTask(target._getStateIdentifier(), task).then(async () => {
- await localDatastore._updateLocalIdForObject(localId, target);
- await localDatastore._updateObjectIfPinned(target);
- return target;
- }, error => {
- return Promise.reject(error);
- });
- }
- return Promise.resolve();
- }
- };
- _CoreManager.default.setObjectController(DefaultController);
- var _default = ParseObject;
- exports.default = _default;
|