| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461 | "use strict";Object.defineProperty(exports, "__esModule", {  value: true});exports.default = void 0;var _CoreManager = _interopRequireDefault(require("./CoreManager"));var _ParseObject = _interopRequireDefault(require("./ParseObject"));var _ParseCLP = _interopRequireDefault(require("./ParseCLP"));function _interopRequireDefault(obj) {  return obj && obj.__esModule ? obj : {    default: obj  };}/** * @flow *//*:: import type { PermissionsMap } from './ParseCLP';*/const FIELD_TYPES = ['String', 'Number', 'Boolean', 'Date', 'File', 'GeoPoint', 'Polygon', 'Array', 'Object', 'Pointer', 'Relation'];/*:: type FieldOptions = {  required: boolean,  defaultValue: mixed,};*//** * A Parse.Schema object is for handling schema data from Parse. * <p>All the schemas methods require MasterKey. * * When adding fields, you may set required and default values. (Requires Parse Server 3.7.0+) * * <pre> * const options = { required: true, defaultValue: 'hello world' }; * const schema = new Parse.Schema('MyClass'); * schema.addString('field', options); * schema.addIndex('index_name', { 'field': 1 }); * schema.save(); * </pre> * </p> * * @alias Parse.Schema */class ParseSchema {  /*:: className: string;*/  /*:: _fields: { [key: string]: mixed };*/  /*:: _indexes: { [key: string]: mixed };*/  /*:: _clp: { [key: string]: mixed };*/  /**   * @param {string} className Parse Class string.   */  constructor(className /*: string*/) {    if (typeof className === 'string') {      if (className === 'User' && _CoreManager.default.get('PERFORM_USER_REWRITE')) {        this.className = '_User';      } else {        this.className = className;      }    }    this._fields = {};    this._indexes = {};  }  /**   * Static method to get all schemas   *   * @returns {Promise} A promise that is resolved with the result when   * the query completes.   */  static all() {    const controller = _CoreManager.default.getSchemaController();    return controller.get('').then(response => {      if (response.results.length === 0) {        throw new Error('Schema not found.');      }      return response.results;    });  }  /**   * Get the Schema from Parse   *   * @returns {Promise} A promise that is resolved with the result when   * the query completes.   */  get() {    this.assertClassName();    const controller = _CoreManager.default.getSchemaController();    return controller.get(this.className).then(response => {      if (!response) {        throw new Error('Schema not found.');      }      return response;    });  }  /**   * Create a new Schema on Parse   *   * @returns {Promise} A promise that is resolved with the result when   * the query completes.   */  save() {    this.assertClassName();    const controller = _CoreManager.default.getSchemaController();    const params = {      className: this.className,      fields: this._fields,      indexes: this._indexes,      classLevelPermissions: this._clp    };    return controller.create(this.className, params);  }  /**   * Update a Schema on Parse   *   * @returns {Promise} A promise that is resolved with the result when   * the query completes.   */  update() {    this.assertClassName();    const controller = _CoreManager.default.getSchemaController();    const params = {      className: this.className,      fields: this._fields,      indexes: this._indexes,      classLevelPermissions: this._clp    };    this._fields = {};    this._indexes = {};    return controller.update(this.className, params);  }  /**   * Removing a Schema from Parse   * Can only be used on Schema without objects   *   * @returns {Promise} A promise that is resolved with the result when   * the query completes.   */  delete() {    this.assertClassName();    const controller = _CoreManager.default.getSchemaController();    return controller.delete(this.className);  }  /**   * Removes all objects from a Schema (class) in Parse.   * EXERCISE CAUTION, running this will delete all objects for this schema and cannot be reversed   *   * @returns {Promise} A promise that is resolved with the result when   * the query completes.   */  purge() {    this.assertClassName();    const controller = _CoreManager.default.getSchemaController();    return controller.purge(this.className);  }  /**   * Assert if ClassName has been filled   *   * @private   */  assertClassName() {    if (!this.className) {      throw new Error('You must set a Class Name before making any request.');    }  }  /**   * Sets Class Level Permissions when creating / updating a Schema.   * EXERCISE CAUTION, running this may override CLP for this schema and cannot be reversed   *   * @param {object | Parse.CLP} clp Class Level Permissions   * @returns {Parse.Schema} Returns the schema, so you can chain this call.   */  setCLP(clp /*: PermissionsMap | ParseCLP*/) {    if (clp instanceof _ParseCLP.default) {      this._clp = clp.toJSON();    } else {      this._clp = clp;    }    return this;  }  /**   * Adding a Field to Create / Update a Schema   *   * @param {string} name Name of the field that will be created on Parse   * @param {string} type Can be a (String|Number|Boolean|Date|Parse.File|Parse.GeoPoint|Array|Object|Pointer|Parse.Relation)   * @param {object} options   * Valid options are:<ul>   *   <li>required: If field is not set, save operation fails (Requires Parse Server 3.7.0+)   *   <li>defaultValue: If field is not set, a default value is selected (Requires Parse Server 3.7.0+)   *   <li>targetClass: Required if type is Pointer or Parse.Relation   * </ul>   * @returns {Parse.Schema} Returns the schema, so you can chain this call.   */  addField(name /*: string*/, type /*: string*/, options /*: FieldOptions*/ = {}) {    type = type || 'String';    if (!name) {      throw new Error('field name may not be null.');    }    if (FIELD_TYPES.indexOf(type) === -1) {      throw new Error(`${type} is not a valid type.`);    }    if (type === 'Pointer') {      return this.addPointer(name, options.targetClass, options);    }    if (type === 'Relation') {      return this.addRelation(name, options.targetClass, options);    }    const fieldOptions = {      type    };    if (typeof options.required === 'boolean') {      fieldOptions.required = options.required;    }    if (options.defaultValue !== undefined) {      fieldOptions.defaultValue = options.defaultValue;    }    if (type === 'Date') {      if (options && options.defaultValue) {        fieldOptions.defaultValue = {          __type: 'Date',          iso: new Date(options.defaultValue)        };      }    }    this._fields[name] = fieldOptions;    return this;  }  /**   * Adding an Index to Create / Update a Schema   *   * @param {string} name Name of the index   * @param {object} index { field: value }   * @returns {Parse.Schema} Returns the schema, so you can chain this call.   *   * <pre>   * schema.addIndex('index_name', { 'field': 1 });   * </pre>   */  addIndex(name /*: string*/, index /*: any*/) {    if (!name) {      throw new Error('index name may not be null.');    }    if (!index) {      throw new Error('index may not be null.');    }    this._indexes[name] = index;    return this;  }  /**   * Adding String Field   *   * @param {string} name Name of the field that will be created on Parse   * @param {object} options See {@link https://parseplatform.org/Parse-SDK-JS/api/master/Parse.Schema.html#addField addField}   * @returns {Parse.Schema} Returns the schema, so you can chain this call.   */  addString(name /*: string*/, options /*: FieldOptions*/) {    return this.addField(name, 'String', options);  }  /**   * Adding Number Field   *   * @param {string} name Name of the field that will be created on Parse   * @param {object} options See {@link https://parseplatform.org/Parse-SDK-JS/api/master/Parse.Schema.html#addField addField}   * @returns {Parse.Schema} Returns the schema, so you can chain this call.   */  addNumber(name /*: string*/, options /*: FieldOptions*/) {    return this.addField(name, 'Number', options);  }  /**   * Adding Boolean Field   *   * @param {string} name Name of the field that will be created on Parse   * @param {object} options See {@link https://parseplatform.org/Parse-SDK-JS/api/master/Parse.Schema.html#addField addField}   * @returns {Parse.Schema} Returns the schema, so you can chain this call.   */  addBoolean(name /*: string*/, options /*: FieldOptions*/) {    return this.addField(name, 'Boolean', options);  }  /**   * Adding Date Field   *   * @param {string} name Name of the field that will be created on Parse   * @param {object} options See {@link https://parseplatform.org/Parse-SDK-JS/api/master/Parse.Schema.html#addField addField}   * @returns {Parse.Schema} Returns the schema, so you can chain this call.   */  addDate(name /*: string*/, options /*: FieldOptions*/) {    return this.addField(name, 'Date', options);  }  /**   * Adding File Field   *   * @param {string} name Name of the field that will be created on Parse   * @param {object} options See {@link https://parseplatform.org/Parse-SDK-JS/api/master/Parse.Schema.html#addField addField}   * @returns {Parse.Schema} Returns the schema, so you can chain this call.   */  addFile(name /*: string*/, options /*: FieldOptions*/) {    return this.addField(name, 'File', options);  }  /**   * Adding GeoPoint Field   *   * @param {string} name Name of the field that will be created on Parse   * @param {object} options See {@link https://parseplatform.org/Parse-SDK-JS/api/master/Parse.Schema.html#addField addField}   * @returns {Parse.Schema} Returns the schema, so you can chain this call.   */  addGeoPoint(name /*: string*/, options /*: FieldOptions*/) {    return this.addField(name, 'GeoPoint', options);  }  /**   * Adding Polygon Field   *   * @param {string} name Name of the field that will be created on Parse   * @param {object} options See {@link https://parseplatform.org/Parse-SDK-JS/api/master/Parse.Schema.html#addField addField}   * @returns {Parse.Schema} Returns the schema, so you can chain this call.   */  addPolygon(name /*: string*/, options /*: FieldOptions*/) {    return this.addField(name, 'Polygon', options);  }  /**   * Adding Array Field   *   * @param {string} name Name of the field that will be created on Parse   * @param {object} options See {@link https://parseplatform.org/Parse-SDK-JS/api/master/Parse.Schema.html#addField addField}   * @returns {Parse.Schema} Returns the schema, so you can chain this call.   */  addArray(name /*: string*/, options /*: FieldOptions*/) {    return this.addField(name, 'Array', options);  }  /**   * Adding Object Field   *   * @param {string} name Name of the field that will be created on Parse   * @param {object} options See {@link https://parseplatform.org/Parse-SDK-JS/api/master/Parse.Schema.html#addField addField}   * @returns {Parse.Schema} Returns the schema, so you can chain this call.   */  addObject(name /*: string*/, options /*: FieldOptions*/) {    return this.addField(name, 'Object', options);  }  /**   * Adding Pointer Field   *   * @param {string} name Name of the field that will be created on Parse   * @param {string} targetClass Name of the target Pointer Class   * @param {object} options See {@link https://parseplatform.org/Parse-SDK-JS/api/master/Parse.Schema.html#addField addField}   * @returns {Parse.Schema} Returns the schema, so you can chain this call.   */  addPointer(name /*: string*/, targetClass /*: string*/, options /*: FieldOptions*/ = {}) {    if (!name) {      throw new Error('field name may not be null.');    }    if (!targetClass) {      throw new Error('You need to set the targetClass of the Pointer.');    }    const fieldOptions = {      type: 'Pointer',      targetClass    };    if (typeof options.required === 'boolean') {      fieldOptions.required = options.required;    }    if (options.defaultValue !== undefined) {      fieldOptions.defaultValue = options.defaultValue;      if (options.defaultValue instanceof _ParseObject.default) {        fieldOptions.defaultValue = options.defaultValue.toPointer();      }    }    this._fields[name] = fieldOptions;    return this;  }  /**   * Adding Relation Field   *   * @param {string} name Name of the field that will be created on Parse   * @param {string} targetClass Name of the target Pointer Class   * @returns {Parse.Schema} Returns the schema, so you can chain this call.   */  addRelation(name /*: string*/, targetClass /*: string*/) {    if (!name) {      throw new Error('field name may not be null.');    }    if (!targetClass) {      throw new Error('You need to set the targetClass of the Relation.');    }    this._fields[name] = {      type: 'Relation',      targetClass    };    return this;  }  /**   * Deleting a Field to Update on a Schema   *   * @param {string} name Name of the field   * @returns {Parse.Schema} Returns the schema, so you can chain this call.   */  deleteField(name /*: string*/) {    this._fields[name] = {      __op: 'Delete'    };    return this;  }  /**   * Deleting an Index to Update on a Schema   *   * @param {string} name Name of the field   * @returns {Parse.Schema} Returns the schema, so you can chain this call.   */  deleteIndex(name /*: string*/) {    this._indexes[name] = {      __op: 'Delete'    };    return this;  }}const DefaultController = {  send(className /*: string*/, method /*: string*/, params /*: any*/ = {}) /*: Promise*/{    const RESTController = _CoreManager.default.getRESTController();    return RESTController.request(method, `schemas/${className}`, params, {      useMasterKey: true    });  },  get(className /*: string*/) /*: Promise*/{    return this.send(className, 'GET');  },  create(className /*: string*/, params /*: any*/) /*: Promise*/{    return this.send(className, 'POST', params);  },  update(className /*: string*/, params /*: any*/) /*: Promise*/{    return this.send(className, 'PUT', params);  },  delete(className /*: string*/) /*: Promise*/{    return this.send(className, 'DELETE');  },  purge(className /*: string*/) /*: Promise*/{    const RESTController = _CoreManager.default.getRESTController();    return RESTController.request('DELETE', `purge/${className}`, {}, {      useMasterKey: true    });  }};_CoreManager.default.setSchemaController(DefaultController);var _default = ParseSchema;exports.default = _default;
 |