ParseSchema.js 15 KB

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