ParseSchema.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673
  1. "use strict";
  2. var _interopRequireDefault = require("@babel/runtime-corejs3/helpers/interopRequireDefault");
  3. var _Object$defineProperty = require("@babel/runtime-corejs3/core-js-stable/object/define-property");
  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. * <li>targetClass: Required if type is Pointer or Parse.Relation
  203. * </ul>
  204. * @returns {Parse.Schema} Returns the schema, so you can chain this call.
  205. */
  206. }, {
  207. key: "addField",
  208. value: function (name
  209. /*: string*/
  210. , type
  211. /*: string*/
  212. ) {
  213. var options
  214. /*: FieldOptions*/
  215. = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
  216. type = type || 'String';
  217. if (!name) {
  218. throw new Error('field name may not be null.');
  219. }
  220. if ((0, _indexOf.default)(FIELD_TYPES).call(FIELD_TYPES, type) === -1) {
  221. throw new Error("".concat(type, " is not a valid type."));
  222. }
  223. if (type === 'Pointer') {
  224. return this.addPointer(name, options.targetClass, options);
  225. }
  226. if (type === 'Relation') {
  227. return this.addRelation(name, options.targetClass, options);
  228. }
  229. var fieldOptions = {
  230. type: type
  231. };
  232. if (typeof options.required === 'boolean') {
  233. fieldOptions.required = options.required;
  234. }
  235. if (options.defaultValue !== undefined) {
  236. fieldOptions.defaultValue = options.defaultValue;
  237. }
  238. if (type === 'Date') {
  239. if (options && options.defaultValue) {
  240. fieldOptions.defaultValue = {
  241. __type: 'Date',
  242. iso: new Date(options.defaultValue)
  243. };
  244. }
  245. }
  246. this._fields[name] = fieldOptions;
  247. return this;
  248. }
  249. /**
  250. * Adding an Index to Create / Update a Schema
  251. *
  252. * @param {string} name Name of the index
  253. * @param {object} index { field: value }
  254. * @returns {Parse.Schema} Returns the schema, so you can chain this call.
  255. *
  256. * <pre>
  257. * schema.addIndex('index_name', { 'field': 1 });
  258. * </pre>
  259. */
  260. }, {
  261. key: "addIndex",
  262. value: function (name
  263. /*: string*/
  264. , index
  265. /*: any*/
  266. ) {
  267. if (!name) {
  268. throw new Error('index name may not be null.');
  269. }
  270. if (!index) {
  271. throw new Error('index may not be null.');
  272. }
  273. this._indexes[name] = index;
  274. return this;
  275. }
  276. /**
  277. * Adding String Field
  278. *
  279. * @param {string} name Name of the field that will be created on Parse
  280. * @param {object} options See {@link https://parseplatform.org/Parse-SDK-JS/api/master/Parse.Schema.html#addField addField}
  281. * @returns {Parse.Schema} Returns the schema, so you can chain this call.
  282. */
  283. }, {
  284. key: "addString",
  285. value: function (name
  286. /*: string*/
  287. , options
  288. /*: FieldOptions*/
  289. ) {
  290. return this.addField(name, 'String', options);
  291. }
  292. /**
  293. * Adding Number Field
  294. *
  295. * @param {string} name Name of the field that will be created on Parse
  296. * @param {object} options See {@link https://parseplatform.org/Parse-SDK-JS/api/master/Parse.Schema.html#addField addField}
  297. * @returns {Parse.Schema} Returns the schema, so you can chain this call.
  298. */
  299. }, {
  300. key: "addNumber",
  301. value: function (name
  302. /*: string*/
  303. , options
  304. /*: FieldOptions*/
  305. ) {
  306. return this.addField(name, 'Number', options);
  307. }
  308. /**
  309. * Adding Boolean Field
  310. *
  311. * @param {string} name Name of the field that will be created on Parse
  312. * @param {object} options See {@link https://parseplatform.org/Parse-SDK-JS/api/master/Parse.Schema.html#addField addField}
  313. * @returns {Parse.Schema} Returns the schema, so you can chain this call.
  314. */
  315. }, {
  316. key: "addBoolean",
  317. value: function (name
  318. /*: string*/
  319. , options
  320. /*: FieldOptions*/
  321. ) {
  322. return this.addField(name, 'Boolean', options);
  323. }
  324. /**
  325. * Adding Date Field
  326. *
  327. * @param {string} name Name of the field that will be created on Parse
  328. * @param {object} options See {@link https://parseplatform.org/Parse-SDK-JS/api/master/Parse.Schema.html#addField addField}
  329. * @returns {Parse.Schema} Returns the schema, so you can chain this call.
  330. */
  331. }, {
  332. key: "addDate",
  333. value: function (name
  334. /*: string*/
  335. , options
  336. /*: FieldOptions*/
  337. ) {
  338. return this.addField(name, 'Date', options);
  339. }
  340. /**
  341. * Adding File Field
  342. *
  343. * @param {string} name Name of the field that will be created on Parse
  344. * @param {object} options See {@link https://parseplatform.org/Parse-SDK-JS/api/master/Parse.Schema.html#addField addField}
  345. * @returns {Parse.Schema} Returns the schema, so you can chain this call.
  346. */
  347. }, {
  348. key: "addFile",
  349. value: function (name
  350. /*: string*/
  351. , options
  352. /*: FieldOptions*/
  353. ) {
  354. return this.addField(name, 'File', options);
  355. }
  356. /**
  357. * Adding GeoPoint Field
  358. *
  359. * @param {string} name Name of the field that will be created on Parse
  360. * @param {object} options See {@link https://parseplatform.org/Parse-SDK-JS/api/master/Parse.Schema.html#addField addField}
  361. * @returns {Parse.Schema} Returns the schema, so you can chain this call.
  362. */
  363. }, {
  364. key: "addGeoPoint",
  365. value: function (name
  366. /*: string*/
  367. , options
  368. /*: FieldOptions*/
  369. ) {
  370. return this.addField(name, 'GeoPoint', options);
  371. }
  372. /**
  373. * Adding Polygon Field
  374. *
  375. * @param {string} name Name of the field that will be created on Parse
  376. * @param {object} options See {@link https://parseplatform.org/Parse-SDK-JS/api/master/Parse.Schema.html#addField addField}
  377. * @returns {Parse.Schema} Returns the schema, so you can chain this call.
  378. */
  379. }, {
  380. key: "addPolygon",
  381. value: function (name
  382. /*: string*/
  383. , options
  384. /*: FieldOptions*/
  385. ) {
  386. return this.addField(name, 'Polygon', options);
  387. }
  388. /**
  389. * Adding Array Field
  390. *
  391. * @param {string} name Name of the field that will be created on Parse
  392. * @param {object} options See {@link https://parseplatform.org/Parse-SDK-JS/api/master/Parse.Schema.html#addField addField}
  393. * @returns {Parse.Schema} Returns the schema, so you can chain this call.
  394. */
  395. }, {
  396. key: "addArray",
  397. value: function (name
  398. /*: string*/
  399. , options
  400. /*: FieldOptions*/
  401. ) {
  402. return this.addField(name, 'Array', options);
  403. }
  404. /**
  405. * Adding Object Field
  406. *
  407. * @param {string} name Name of the field that will be created on Parse
  408. * @param {object} options See {@link https://parseplatform.org/Parse-SDK-JS/api/master/Parse.Schema.html#addField addField}
  409. * @returns {Parse.Schema} Returns the schema, so you can chain this call.
  410. */
  411. }, {
  412. key: "addObject",
  413. value: function (name
  414. /*: string*/
  415. , options
  416. /*: FieldOptions*/
  417. ) {
  418. return this.addField(name, 'Object', options);
  419. }
  420. /**
  421. * Adding Pointer Field
  422. *
  423. * @param {string} name Name of the field that will be created on Parse
  424. * @param {string} targetClass Name of the target Pointer Class
  425. * @param {object} options See {@link https://parseplatform.org/Parse-SDK-JS/api/master/Parse.Schema.html#addField addField}
  426. * @returns {Parse.Schema} Returns the schema, so you can chain this call.
  427. */
  428. }, {
  429. key: "addPointer",
  430. value: function (name
  431. /*: string*/
  432. , targetClass
  433. /*: string*/
  434. ) {
  435. var options
  436. /*: FieldOptions*/
  437. = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
  438. if (!name) {
  439. throw new Error('field name may not be null.');
  440. }
  441. if (!targetClass) {
  442. throw new Error('You need to set the targetClass of the Pointer.');
  443. }
  444. var fieldOptions = {
  445. type: 'Pointer',
  446. targetClass: targetClass
  447. };
  448. if (typeof options.required === 'boolean') {
  449. fieldOptions.required = options.required;
  450. }
  451. if (options.defaultValue !== undefined) {
  452. fieldOptions.defaultValue = options.defaultValue;
  453. if (options.defaultValue instanceof _ParseObject.default) {
  454. fieldOptions.defaultValue = options.defaultValue.toPointer();
  455. }
  456. }
  457. this._fields[name] = fieldOptions;
  458. return this;
  459. }
  460. /**
  461. * Adding Relation Field
  462. *
  463. * @param {string} name Name of the field that will be created on Parse
  464. * @param {string} targetClass Name of the target Pointer Class
  465. * @returns {Parse.Schema} Returns the schema, so you can chain this call.
  466. */
  467. }, {
  468. key: "addRelation",
  469. value: function (name
  470. /*: string*/
  471. , targetClass
  472. /*: string*/
  473. ) {
  474. if (!name) {
  475. throw new Error('field name may not be null.');
  476. }
  477. if (!targetClass) {
  478. throw new Error('You need to set the targetClass of the Relation.');
  479. }
  480. this._fields[name] = {
  481. type: 'Relation',
  482. targetClass: targetClass
  483. };
  484. return this;
  485. }
  486. /**
  487. * Deleting a Field to Update on a Schema
  488. *
  489. * @param {string} name Name of the field
  490. * @returns {Parse.Schema} Returns the schema, so you can chain this call.
  491. */
  492. }, {
  493. key: "deleteField",
  494. value: function (name
  495. /*: string*/
  496. ) {
  497. this._fields[name] = {
  498. __op: 'Delete'
  499. };
  500. return this;
  501. }
  502. /**
  503. * Deleting an Index to Update on a Schema
  504. *
  505. * @param {string} name Name of the field
  506. * @returns {Parse.Schema} Returns the schema, so you can chain this call.
  507. */
  508. }, {
  509. key: "deleteIndex",
  510. value: function (name
  511. /*: string*/
  512. ) {
  513. this._indexes[name] = {
  514. __op: 'Delete'
  515. };
  516. return this;
  517. }
  518. }], [{
  519. key: "all",
  520. value: function () {
  521. var controller = _CoreManager.default.getSchemaController();
  522. return controller.get('').then(function (response) {
  523. if (response.results.length === 0) {
  524. throw new Error('Schema not found.');
  525. }
  526. return response.results;
  527. });
  528. }
  529. }]);
  530. return ParseSchema;
  531. }();
  532. var DefaultController = {
  533. send: function (className
  534. /*: string*/
  535. , method
  536. /*: string*/
  537. )
  538. /*: Promise*/
  539. {
  540. var params
  541. /*: any*/
  542. = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
  543. var RESTController = _CoreManager.default.getRESTController();
  544. return RESTController.request(method, "schemas/".concat(className), params, {
  545. useMasterKey: true
  546. });
  547. },
  548. get: function (className
  549. /*: string*/
  550. )
  551. /*: Promise*/
  552. {
  553. return this.send(className, 'GET');
  554. },
  555. create: function (className
  556. /*: string*/
  557. , params
  558. /*: any*/
  559. )
  560. /*: Promise*/
  561. {
  562. return this.send(className, 'POST', params);
  563. },
  564. update: function (className
  565. /*: string*/
  566. , params
  567. /*: any*/
  568. )
  569. /*: Promise*/
  570. {
  571. return this.send(className, 'PUT', params);
  572. },
  573. delete: function (className
  574. /*: string*/
  575. )
  576. /*: Promise*/
  577. {
  578. return this.send(className, 'DELETE');
  579. },
  580. purge: function (className
  581. /*: string*/
  582. )
  583. /*: Promise*/
  584. {
  585. var RESTController = _CoreManager.default.getRESTController();
  586. return RESTController.request('DELETE', "purge/".concat(className), {}, {
  587. useMasterKey: true
  588. });
  589. }
  590. };
  591. _CoreManager.default.setSchemaController(DefaultController);
  592. var _default = ParseSchema;
  593. exports.default = _default;