ParseSchema.js 15 KB

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