ParseSchema.js 17 KB

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