ParseSchema.js 15 KB

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