ParseSchema.js 14 KB

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