ParseSchema.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461
  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. * @flow
  16. */
  17. /*:: import type { PermissionsMap } from './ParseCLP';*/
  18. const FIELD_TYPES = ['String', 'Number', 'Boolean', 'Date', 'File', 'GeoPoint', 'Polygon', 'Array', 'Object', 'Pointer', 'Relation'];
  19. /*:: type FieldOptions = {
  20. required: boolean,
  21. defaultValue: mixed,
  22. };*/
  23. /**
  24. * A Parse.Schema object is for handling schema data from Parse.
  25. * <p>All the schemas methods require MasterKey.
  26. *
  27. * When adding fields, you may set required and default values. (Requires Parse Server 3.7.0+)
  28. *
  29. * <pre>
  30. * const options = { required: true, defaultValue: 'hello world' };
  31. * const schema = new Parse.Schema('MyClass');
  32. * schema.addString('field', options);
  33. * schema.addIndex('index_name', { 'field': 1 });
  34. * schema.save();
  35. * </pre>
  36. * </p>
  37. *
  38. * @alias Parse.Schema
  39. */
  40. class ParseSchema {
  41. /*:: className: string;*/
  42. /*:: _fields: { [key: string]: mixed };*/
  43. /*:: _indexes: { [key: string]: mixed };*/
  44. /*:: _clp: { [key: string]: mixed };*/
  45. /**
  46. * @param {string} className Parse Class string.
  47. */
  48. constructor(className /*: string*/) {
  49. if (typeof className === 'string') {
  50. if (className === 'User' && _CoreManager.default.get('PERFORM_USER_REWRITE')) {
  51. this.className = '_User';
  52. } else {
  53. this.className = className;
  54. }
  55. }
  56. this._fields = {};
  57. this._indexes = {};
  58. }
  59. /**
  60. * Static method to get all schemas
  61. *
  62. * @returns {Promise} A promise that is resolved with the result when
  63. * the query completes.
  64. */
  65. static all() {
  66. const controller = _CoreManager.default.getSchemaController();
  67. return controller.get('').then(response => {
  68. if (response.results.length === 0) {
  69. throw new Error('Schema not found.');
  70. }
  71. return response.results;
  72. });
  73. }
  74. /**
  75. * Get the Schema from Parse
  76. *
  77. * @returns {Promise} A promise that is resolved with the result when
  78. * the query completes.
  79. */
  80. get() {
  81. this.assertClassName();
  82. const controller = _CoreManager.default.getSchemaController();
  83. return controller.get(this.className).then(response => {
  84. if (!response) {
  85. throw new Error('Schema not found.');
  86. }
  87. return response;
  88. });
  89. }
  90. /**
  91. * Create a new Schema on Parse
  92. *
  93. * @returns {Promise} A promise that is resolved with the result when
  94. * the query completes.
  95. */
  96. save() {
  97. this.assertClassName();
  98. const controller = _CoreManager.default.getSchemaController();
  99. const params = {
  100. className: this.className,
  101. fields: this._fields,
  102. indexes: this._indexes,
  103. classLevelPermissions: this._clp
  104. };
  105. return controller.create(this.className, params);
  106. }
  107. /**
  108. * Update a Schema on Parse
  109. *
  110. * @returns {Promise} A promise that is resolved with the result when
  111. * the query completes.
  112. */
  113. update() {
  114. this.assertClassName();
  115. const controller = _CoreManager.default.getSchemaController();
  116. const 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. delete() {
  134. this.assertClassName();
  135. const controller = _CoreManager.default.getSchemaController();
  136. return controller.delete(this.className);
  137. }
  138. /**
  139. * Removes all objects from a Schema (class) in Parse.
  140. * EXERCISE CAUTION, running this will delete all objects for this schema and cannot be reversed
  141. *
  142. * @returns {Promise} A promise that is resolved with the result when
  143. * the query completes.
  144. */
  145. purge() {
  146. this.assertClassName();
  147. const controller = _CoreManager.default.getSchemaController();
  148. return controller.purge(this.className);
  149. }
  150. /**
  151. * Assert if ClassName has been filled
  152. *
  153. * @private
  154. */
  155. assertClassName() {
  156. if (!this.className) {
  157. throw new Error('You must set a Class Name before making any request.');
  158. }
  159. }
  160. /**
  161. * Sets Class Level Permissions when creating / updating a Schema.
  162. * EXERCISE CAUTION, running this may override CLP for this schema and cannot be reversed
  163. *
  164. * @param {object | Parse.CLP} clp Class Level Permissions
  165. * @returns {Parse.Schema} Returns the schema, so you can chain this call.
  166. */
  167. setCLP(clp /*: PermissionsMap | ParseCLP*/) {
  168. if (clp instanceof _ParseCLP.default) {
  169. this._clp = clp.toJSON();
  170. } else {
  171. this._clp = clp;
  172. }
  173. return this;
  174. }
  175. /**
  176. * Adding a Field to Create / Update a Schema
  177. *
  178. * @param {string} name Name of the field that will be created on Parse
  179. * @param {string} type Can be a (String|Number|Boolean|Date|Parse.File|Parse.GeoPoint|Array|Object|Pointer|Parse.Relation)
  180. * @param {object} options
  181. * Valid options are:<ul>
  182. * <li>required: If field is not set, save operation fails (Requires Parse Server 3.7.0+)
  183. * <li>defaultValue: If field is not set, a default value is selected (Requires Parse Server 3.7.0+)
  184. * <li>targetClass: Required if type is Pointer or Parse.Relation
  185. * </ul>
  186. * @returns {Parse.Schema} Returns the schema, so you can chain this call.
  187. */
  188. addField(name /*: string*/, type /*: string*/, options /*: FieldOptions*/ = {}) {
  189. type = type || 'String';
  190. if (!name) {
  191. throw new Error('field name may not be null.');
  192. }
  193. if (FIELD_TYPES.indexOf(type) === -1) {
  194. throw new Error(`${type} is not a valid type.`);
  195. }
  196. if (type === 'Pointer') {
  197. return this.addPointer(name, options.targetClass, options);
  198. }
  199. if (type === 'Relation') {
  200. return this.addRelation(name, options.targetClass, options);
  201. }
  202. const fieldOptions = {
  203. type
  204. };
  205. if (typeof options.required === 'boolean') {
  206. fieldOptions.required = options.required;
  207. }
  208. if (options.defaultValue !== undefined) {
  209. fieldOptions.defaultValue = options.defaultValue;
  210. }
  211. if (type === 'Date') {
  212. if (options && options.defaultValue) {
  213. fieldOptions.defaultValue = {
  214. __type: 'Date',
  215. iso: new Date(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 /*: string*/, index /*: any*/) {
  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 /*: string*/, options /*: FieldOptions*/) {
  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 /*: string*/, options /*: FieldOptions*/) {
  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 /*: string*/, options /*: FieldOptions*/) {
  271. return this.addField(name, 'Boolean', options);
  272. }
  273. /**
  274. * Adding Date 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. addDate(name /*: string*/, options /*: FieldOptions*/) {
  281. return this.addField(name, 'Date', options);
  282. }
  283. /**
  284. * Adding File 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. addFile(name /*: string*/, options /*: FieldOptions*/) {
  291. return this.addField(name, 'File', options);
  292. }
  293. /**
  294. * Adding GeoPoint 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. addGeoPoint(name /*: string*/, options /*: FieldOptions*/) {
  301. return this.addField(name, 'GeoPoint', options);
  302. }
  303. /**
  304. * Adding Polygon 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. addPolygon(name /*: string*/, options /*: FieldOptions*/) {
  311. return this.addField(name, 'Polygon', options);
  312. }
  313. /**
  314. * Adding Array 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. addArray(name /*: string*/, options /*: FieldOptions*/) {
  321. return this.addField(name, 'Array', options);
  322. }
  323. /**
  324. * Adding Object 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. addObject(name /*: string*/, options /*: FieldOptions*/) {
  331. return this.addField(name, 'Object', options);
  332. }
  333. /**
  334. * Adding Pointer Field
  335. *
  336. * @param {string} name Name of the field that will be created on Parse
  337. * @param {string} targetClass Name of the target Pointer Class
  338. * @param {object} options See {@link https://parseplatform.org/Parse-SDK-JS/api/master/Parse.Schema.html#addField addField}
  339. * @returns {Parse.Schema} Returns the schema, so you can chain this call.
  340. */
  341. addPointer(name /*: string*/, targetClass /*: string*/, options /*: FieldOptions*/ = {}) {
  342. if (!name) {
  343. throw new Error('field name may not be null.');
  344. }
  345. if (!targetClass) {
  346. throw new Error('You need to set the targetClass of the Pointer.');
  347. }
  348. const fieldOptions = {
  349. type: 'Pointer',
  350. targetClass
  351. };
  352. if (typeof options.required === 'boolean') {
  353. fieldOptions.required = options.required;
  354. }
  355. if (options.defaultValue !== undefined) {
  356. fieldOptions.defaultValue = options.defaultValue;
  357. if (options.defaultValue instanceof _ParseObject.default) {
  358. fieldOptions.defaultValue = options.defaultValue.toPointer();
  359. }
  360. }
  361. this._fields[name] = fieldOptions;
  362. return this;
  363. }
  364. /**
  365. * Adding Relation 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. * @returns {Parse.Schema} Returns the schema, so you can chain this call.
  370. */
  371. addRelation(name /*: string*/, targetClass /*: string*/) {
  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 Relation.');
  377. }
  378. this._fields[name] = {
  379. type: 'Relation',
  380. targetClass
  381. };
  382. return this;
  383. }
  384. /**
  385. * Deleting a Field to Update on a Schema
  386. *
  387. * @param {string} name Name of the field
  388. * @returns {Parse.Schema} Returns the schema, so you can chain this call.
  389. */
  390. deleteField(name /*: string*/) {
  391. this._fields[name] = {
  392. __op: 'Delete'
  393. };
  394. return this;
  395. }
  396. /**
  397. * Deleting an Index to Update on a Schema
  398. *
  399. * @param {string} name Name of the field
  400. * @returns {Parse.Schema} Returns the schema, so you can chain this call.
  401. */
  402. deleteIndex(name /*: string*/) {
  403. this._indexes[name] = {
  404. __op: 'Delete'
  405. };
  406. return this;
  407. }
  408. }
  409. const DefaultController = {
  410. send(className /*: string*/, method /*: string*/, params /*: any*/ = {}) /*: Promise*/{
  411. const RESTController = _CoreManager.default.getRESTController();
  412. return RESTController.request(method, `schemas/${className}`, params, {
  413. useMasterKey: true
  414. });
  415. },
  416. get(className /*: string*/) /*: Promise*/{
  417. return this.send(className, 'GET');
  418. },
  419. create(className /*: string*/, params /*: any*/) /*: Promise*/{
  420. return this.send(className, 'POST', params);
  421. },
  422. update(className /*: string*/, params /*: any*/) /*: Promise*/{
  423. return this.send(className, 'PUT', params);
  424. },
  425. delete(className /*: string*/) /*: Promise*/{
  426. return this.send(className, 'DELETE');
  427. },
  428. purge(className /*: string*/) /*: Promise*/{
  429. const RESTController = _CoreManager.default.getRESTController();
  430. return RESTController.request('DELETE', `purge/${className}`, {}, {
  431. useMasterKey: true
  432. });
  433. }
  434. };
  435. _CoreManager.default.setSchemaController(DefaultController);
  436. var _default = ParseSchema;
  437. exports.default = _default;