ParseSchema.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662
  1. "use strict";
  2. var _Object$defineProperty = require("@babel/runtime-corejs3/core-js-stable/object/define-property");
  3. var _interopRequireDefault = require("@babel/runtime-corejs3/helpers/interopRequireDefault");
  4. _Object$defineProperty(exports, "__esModule", {
  5. value: true
  6. });
  7. exports.default = void 0;
  8. var _indexOf = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/instance/index-of"));
  9. var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime-corejs3/helpers/classCallCheck"));
  10. var _createClass2 = _interopRequireDefault(require("@babel/runtime-corejs3/helpers/createClass"));
  11. var _defineProperty2 = _interopRequireDefault(require("@babel/runtime-corejs3/helpers/defineProperty"));
  12. var _CoreManager = _interopRequireDefault(require("./CoreManager"));
  13. var _ParseObject = _interopRequireDefault(require("./ParseObject"));
  14. var _ParseCLP = _interopRequireDefault(require("./ParseCLP"));
  15. /**
  16. * Copyright (c) 2015-present, Parse, LLC.
  17. * All rights reserved.
  18. *
  19. * This source code is licensed under the BSD-style license found in the
  20. * LICENSE file in the root directory of this source tree. An additional grant
  21. * of patent rights can be found in the PATENTS file in the same directory.
  22. *
  23. * @flow
  24. */
  25. var FIELD_TYPES = ['String', 'Number', 'Boolean', 'Date', 'File', 'GeoPoint', 'Polygon', 'Array', 'Object', 'Pointer', 'Relation'];
  26. /*:: type FieldOptions = {
  27. required: boolean,
  28. defaultValue: mixed,
  29. };*/
  30. /**
  31. * A Parse.Schema object is for handling schema data from Parse.
  32. * <p>All the schemas methods require MasterKey.
  33. *
  34. * When adding fields, you may set required and default values. (Requires Parse Server 3.7.0+)
  35. *
  36. * <pre>
  37. * const options = { required: true, defaultValue: 'hello world' };
  38. * const schema = new Parse.Schema('MyClass');
  39. * schema.addString('field', options);
  40. * schema.addIndex('index_name', { 'field': 1 });
  41. * schema.save();
  42. * </pre>
  43. * </p>
  44. *
  45. * @alias Parse.Schema
  46. */
  47. var ParseSchema = /*#__PURE__*/function () {
  48. /**
  49. * @param {string} className Parse Class string.
  50. */
  51. function ParseSchema(className
  52. /*: string*/
  53. ) {
  54. (0, _classCallCheck2.default)(this, ParseSchema);
  55. (0, _defineProperty2.default)(this, "className", void 0);
  56. (0, _defineProperty2.default)(this, "_fields", void 0);
  57. (0, _defineProperty2.default)(this, "_indexes", void 0);
  58. (0, _defineProperty2.default)(this, "_clp", void 0);
  59. if (typeof className === 'string') {
  60. if (className === 'User' && _CoreManager.default.get('PERFORM_USER_REWRITE')) {
  61. this.className = '_User';
  62. } else {
  63. this.className = className;
  64. }
  65. }
  66. this._fields = {};
  67. this._indexes = {};
  68. }
  69. /**
  70. * Static method to get all schemas
  71. *
  72. * @returns {Promise} A promise that is resolved with the result when
  73. * the query completes.
  74. */
  75. (0, _createClass2.default)(ParseSchema, [{
  76. key: "get",
  77. value:
  78. /**
  79. * Get the Schema from Parse
  80. *
  81. * @returns {Promise} A promise that is resolved with the result when
  82. * the query completes.
  83. */
  84. function () {
  85. this.assertClassName();
  86. var controller = _CoreManager.default.getSchemaController();
  87. return controller.get(this.className).then(function (response) {
  88. if (!response) {
  89. throw new Error('Schema not found.');
  90. }
  91. return response;
  92. });
  93. }
  94. /**
  95. * Create a new Schema on Parse
  96. *
  97. * @returns {Promise} A promise that is resolved with the result when
  98. * the query completes.
  99. */
  100. }, {
  101. key: "save",
  102. value: function () {
  103. this.assertClassName();
  104. var controller = _CoreManager.default.getSchemaController();
  105. var params = {
  106. className: this.className,
  107. fields: this._fields,
  108. indexes: this._indexes,
  109. classLevelPermissions: this._clp
  110. };
  111. return controller.create(this.className, params);
  112. }
  113. /**
  114. * Update a Schema on Parse
  115. *
  116. * @returns {Promise} A promise that is resolved with the result when
  117. * the query completes.
  118. */
  119. }, {
  120. key: "update",
  121. value: function () {
  122. this.assertClassName();
  123. var controller = _CoreManager.default.getSchemaController();
  124. var params = {
  125. className: this.className,
  126. fields: this._fields,
  127. indexes: this._indexes,
  128. classLevelPermissions: this._clp
  129. };
  130. this._fields = {};
  131. this._indexes = {};
  132. return controller.update(this.className, params);
  133. }
  134. /**
  135. * Removing a Schema from Parse
  136. * Can only be used on Schema without objects
  137. *
  138. * @returns {Promise} A promise that is resolved with the result when
  139. * the query completes.
  140. */
  141. }, {
  142. key: "delete",
  143. value: function () {
  144. this.assertClassName();
  145. var controller = _CoreManager.default.getSchemaController();
  146. return controller.delete(this.className);
  147. }
  148. /**
  149. * Removes all objects from a Schema (class) in Parse.
  150. * EXERCISE CAUTION, running this will delete all objects for this schema and cannot be reversed
  151. *
  152. * @returns {Promise} A promise that is resolved with the result when
  153. * the query completes.
  154. */
  155. }, {
  156. key: "purge",
  157. value: function () {
  158. this.assertClassName();
  159. var controller = _CoreManager.default.getSchemaController();
  160. return controller.purge(this.className);
  161. }
  162. /**
  163. * Assert if ClassName has been filled
  164. *
  165. * @private
  166. */
  167. }, {
  168. key: "assertClassName",
  169. value: function () {
  170. if (!this.className) {
  171. throw new Error('You must set a Class Name before making any request.');
  172. }
  173. }
  174. /**
  175. * Sets Class Level Permissions when creating / updating a Schema.
  176. * EXERCISE CAUTION, running this may override CLP for this schema and cannot be reversed
  177. *
  178. * @param {object | Parse.CLP} clp Class Level Permissions
  179. * @returns {Parse.Schema} Returns the schema, so you can chain this call.
  180. */
  181. }, {
  182. key: "setCLP",
  183. value: function (clp
  184. /*: PermissionsMap | ParseCLP*/
  185. ) {
  186. if (clp instanceof _ParseCLP.default) {
  187. this._clp = clp.toJSON();
  188. } else {
  189. this._clp = clp;
  190. }
  191. return this;
  192. }
  193. /**
  194. * Adding a Field to Create / Update a Schema
  195. *
  196. * @param {string} name Name of the field that will be created on Parse
  197. * @param {string} type Can be a (String|Number|Boolean|Date|Parse.File|Parse.GeoPoint|Array|Object|Pointer|Parse.Relation)
  198. * @param {object} options
  199. * Valid options are:<ul>
  200. * <li>required: If field is not set, save operation fails (Requires Parse Server 3.7.0+)
  201. * <li>defaultValue: If field is not set, a default value is selected (Requires Parse Server 3.7.0+)
  202. * </ul>
  203. * @returns {Parse.Schema} Returns the schema, so you can chain this call.
  204. */
  205. }, {
  206. key: "addField",
  207. value: function (name
  208. /*: string*/
  209. , type
  210. /*: string*/
  211. ) {
  212. var options
  213. /*: FieldOptions*/
  214. = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
  215. type = type || 'String';
  216. if (!name) {
  217. throw new Error('field name may not be null.');
  218. }
  219. if ((0, _indexOf.default)(FIELD_TYPES).call(FIELD_TYPES, type) === -1) {
  220. throw new Error("".concat(type, " is not a valid type."));
  221. }
  222. var fieldOptions = {
  223. type: type
  224. };
  225. if (typeof options.required === 'boolean') {
  226. fieldOptions.required = options.required;
  227. }
  228. if (options.defaultValue !== undefined) {
  229. fieldOptions.defaultValue = options.defaultValue;
  230. }
  231. this._fields[name] = fieldOptions;
  232. return this;
  233. }
  234. /**
  235. * Adding an Index to Create / Update a Schema
  236. *
  237. * @param {string} name Name of the index
  238. * @param {object} index { field: value }
  239. * @returns {Parse.Schema} Returns the schema, so you can chain this call.
  240. *
  241. * <pre>
  242. * schema.addIndex('index_name', { 'field': 1 });
  243. * </pre>
  244. */
  245. }, {
  246. key: "addIndex",
  247. value: function (name
  248. /*: string*/
  249. , index
  250. /*: any*/
  251. ) {
  252. if (!name) {
  253. throw new Error('index name may not be null.');
  254. }
  255. if (!index) {
  256. throw new Error('index may not be null.');
  257. }
  258. this._indexes[name] = index;
  259. return this;
  260. }
  261. /**
  262. * Adding String Field
  263. *
  264. * @param {string} name Name of the field that will be created on Parse
  265. * @param {object} options See {@link https://parseplatform.org/Parse-SDK-JS/api/master/Parse.Schema.html#addField addField}
  266. * @returns {Parse.Schema} Returns the schema, so you can chain this call.
  267. */
  268. }, {
  269. key: "addString",
  270. value: function (name
  271. /*: string*/
  272. , options
  273. /*: FieldOptions*/
  274. ) {
  275. return this.addField(name, 'String', options);
  276. }
  277. /**
  278. * Adding Number Field
  279. *
  280. * @param {string} name Name of the field that will be created on Parse
  281. * @param {object} options See {@link https://parseplatform.org/Parse-SDK-JS/api/master/Parse.Schema.html#addField addField}
  282. * @returns {Parse.Schema} Returns the schema, so you can chain this call.
  283. */
  284. }, {
  285. key: "addNumber",
  286. value: function (name
  287. /*: string*/
  288. , options
  289. /*: FieldOptions*/
  290. ) {
  291. return this.addField(name, 'Number', options);
  292. }
  293. /**
  294. * Adding Boolean Field
  295. *
  296. * @param {string} name Name of the field that will be created on Parse
  297. * @param {object} options See {@link https://parseplatform.org/Parse-SDK-JS/api/master/Parse.Schema.html#addField addField}
  298. * @returns {Parse.Schema} Returns the schema, so you can chain this call.
  299. */
  300. }, {
  301. key: "addBoolean",
  302. value: function (name
  303. /*: string*/
  304. , options
  305. /*: FieldOptions*/
  306. ) {
  307. return this.addField(name, 'Boolean', options);
  308. }
  309. /**
  310. * Adding Date Field
  311. *
  312. * @param {string} name Name of the field that will be created on Parse
  313. * @param {object} options See {@link https://parseplatform.org/Parse-SDK-JS/api/master/Parse.Schema.html#addField addField}
  314. * @returns {Parse.Schema} Returns the schema, so you can chain this call.
  315. */
  316. }, {
  317. key: "addDate",
  318. value: function (name
  319. /*: string*/
  320. , options
  321. /*: FieldOptions*/
  322. ) {
  323. if (options && options.defaultValue) {
  324. options.defaultValue = {
  325. __type: 'Date',
  326. iso: new Date(options.defaultValue)
  327. };
  328. }
  329. return this.addField(name, 'Date', options);
  330. }
  331. /**
  332. * Adding File Field
  333. *
  334. * @param {string} name Name of the field that will be created on Parse
  335. * @param {object} options See {@link https://parseplatform.org/Parse-SDK-JS/api/master/Parse.Schema.html#addField addField}
  336. * @returns {Parse.Schema} Returns the schema, so you can chain this call.
  337. */
  338. }, {
  339. key: "addFile",
  340. value: function (name
  341. /*: string*/
  342. , options
  343. /*: FieldOptions*/
  344. ) {
  345. return this.addField(name, 'File', options);
  346. }
  347. /**
  348. * Adding GeoPoint Field
  349. *
  350. * @param {string} name Name of the field that will be created on Parse
  351. * @param {object} options See {@link https://parseplatform.org/Parse-SDK-JS/api/master/Parse.Schema.html#addField addField}
  352. * @returns {Parse.Schema} Returns the schema, so you can chain this call.
  353. */
  354. }, {
  355. key: "addGeoPoint",
  356. value: function (name
  357. /*: string*/
  358. , options
  359. /*: FieldOptions*/
  360. ) {
  361. return this.addField(name, 'GeoPoint', options);
  362. }
  363. /**
  364. * Adding Polygon Field
  365. *
  366. * @param {string} name Name of the field that will be created on Parse
  367. * @param {object} options See {@link https://parseplatform.org/Parse-SDK-JS/api/master/Parse.Schema.html#addField addField}
  368. * @returns {Parse.Schema} Returns the schema, so you can chain this call.
  369. */
  370. }, {
  371. key: "addPolygon",
  372. value: function (name
  373. /*: string*/
  374. , options
  375. /*: FieldOptions*/
  376. ) {
  377. return this.addField(name, 'Polygon', options);
  378. }
  379. /**
  380. * Adding Array Field
  381. *
  382. * @param {string} name Name of the field that will be created on Parse
  383. * @param {object} options See {@link https://parseplatform.org/Parse-SDK-JS/api/master/Parse.Schema.html#addField addField}
  384. * @returns {Parse.Schema} Returns the schema, so you can chain this call.
  385. */
  386. }, {
  387. key: "addArray",
  388. value: function (name
  389. /*: string*/
  390. , options
  391. /*: FieldOptions*/
  392. ) {
  393. return this.addField(name, 'Array', options);
  394. }
  395. /**
  396. * Adding Object Field
  397. *
  398. * @param {string} name Name of the field that will be created on Parse
  399. * @param {object} options See {@link https://parseplatform.org/Parse-SDK-JS/api/master/Parse.Schema.html#addField addField}
  400. * @returns {Parse.Schema} Returns the schema, so you can chain this call.
  401. */
  402. }, {
  403. key: "addObject",
  404. value: function (name
  405. /*: string*/
  406. , options
  407. /*: FieldOptions*/
  408. ) {
  409. return this.addField(name, 'Object', options);
  410. }
  411. /**
  412. * Adding Pointer Field
  413. *
  414. * @param {string} name Name of the field that will be created on Parse
  415. * @param {string} targetClass Name of the target Pointer Class
  416. * @param {object} options See {@link https://parseplatform.org/Parse-SDK-JS/api/master/Parse.Schema.html#addField addField}
  417. * @returns {Parse.Schema} Returns the schema, so you can chain this call.
  418. */
  419. }, {
  420. key: "addPointer",
  421. value: function (name
  422. /*: string*/
  423. , targetClass
  424. /*: string*/
  425. ) {
  426. var options
  427. /*: FieldOptions*/
  428. = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
  429. if (!name) {
  430. throw new Error('field name may not be null.');
  431. }
  432. if (!targetClass) {
  433. throw new Error('You need to set the targetClass of the Pointer.');
  434. }
  435. var fieldOptions = {
  436. type: 'Pointer',
  437. targetClass: targetClass
  438. };
  439. if (typeof options.required === 'boolean') {
  440. fieldOptions.required = options.required;
  441. }
  442. if (options.defaultValue !== undefined) {
  443. fieldOptions.defaultValue = options.defaultValue;
  444. if (options.defaultValue instanceof _ParseObject.default) {
  445. fieldOptions.defaultValue = options.defaultValue.toPointer();
  446. }
  447. }
  448. this._fields[name] = fieldOptions;
  449. return this;
  450. }
  451. /**
  452. * Adding Relation Field
  453. *
  454. * @param {string} name Name of the field that will be created on Parse
  455. * @param {string} targetClass Name of the target Pointer Class
  456. * @returns {Parse.Schema} Returns the schema, so you can chain this call.
  457. */
  458. }, {
  459. key: "addRelation",
  460. value: function (name
  461. /*: string*/
  462. , targetClass
  463. /*: string*/
  464. ) {
  465. if (!name) {
  466. throw new Error('field name may not be null.');
  467. }
  468. if (!targetClass) {
  469. throw new Error('You need to set the targetClass of the Relation.');
  470. }
  471. this._fields[name] = {
  472. type: 'Relation',
  473. targetClass: targetClass
  474. };
  475. return this;
  476. }
  477. /**
  478. * Deleting a Field to Update on a Schema
  479. *
  480. * @param {string} name Name of the field
  481. * @returns {Parse.Schema} Returns the schema, so you can chain this call.
  482. */
  483. }, {
  484. key: "deleteField",
  485. value: function (name
  486. /*: string*/
  487. ) {
  488. this._fields[name] = {
  489. __op: 'Delete'
  490. };
  491. return this;
  492. }
  493. /**
  494. * Deleting an Index to Update on a Schema
  495. *
  496. * @param {string} name Name of the field
  497. * @returns {Parse.Schema} Returns the schema, so you can chain this call.
  498. */
  499. }, {
  500. key: "deleteIndex",
  501. value: function (name
  502. /*: string*/
  503. ) {
  504. this._indexes[name] = {
  505. __op: 'Delete'
  506. };
  507. return this;
  508. }
  509. }], [{
  510. key: "all",
  511. value: function () {
  512. var controller = _CoreManager.default.getSchemaController();
  513. return controller.get('').then(function (response) {
  514. if (response.results.length === 0) {
  515. throw new Error('Schema not found.');
  516. }
  517. return response.results;
  518. });
  519. }
  520. }]);
  521. return ParseSchema;
  522. }();
  523. var DefaultController = {
  524. send: function (className
  525. /*: string*/
  526. , method
  527. /*: string*/
  528. )
  529. /*: Promise*/
  530. {
  531. var params
  532. /*: any*/
  533. = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
  534. var RESTController = _CoreManager.default.getRESTController();
  535. return RESTController.request(method, "schemas/".concat(className), params, {
  536. useMasterKey: true
  537. });
  538. },
  539. get: function (className
  540. /*: string*/
  541. )
  542. /*: Promise*/
  543. {
  544. return this.send(className, 'GET');
  545. },
  546. create: function (className
  547. /*: string*/
  548. , params
  549. /*: any*/
  550. )
  551. /*: Promise*/
  552. {
  553. return this.send(className, 'POST', params);
  554. },
  555. update: function (className
  556. /*: string*/
  557. , params
  558. /*: any*/
  559. )
  560. /*: Promise*/
  561. {
  562. return this.send(className, 'PUT', params);
  563. },
  564. delete: function (className
  565. /*: string*/
  566. )
  567. /*: Promise*/
  568. {
  569. return this.send(className, 'DELETE');
  570. },
  571. purge: function (className
  572. /*: string*/
  573. )
  574. /*: Promise*/
  575. {
  576. var RESTController = _CoreManager.default.getRESTController();
  577. return RESTController.request('DELETE', "purge/".concat(className), {}, {
  578. useMasterKey: true
  579. });
  580. }
  581. };
  582. _CoreManager.default.setSchemaController(DefaultController);
  583. var _default = ParseSchema;
  584. exports.default = _default;