ParseSchema.js 15 KB

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