ParseSchema.js 13 KB

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