ParseSchema.js 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = void 0;
  6. var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));
  7. var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));
  8. var _CoreManager = _interopRequireDefault(require("./CoreManager"));
  9. var _ParseObject = _interopRequireDefault(require("./ParseObject"));
  10. var _ParseCLP = _interopRequireDefault(require("./ParseCLP"));
  11. var FIELD_TYPES = ['String', 'Number', 'Boolean', 'Date', 'File', 'GeoPoint', 'Polygon', 'Array', 'Object', 'Pointer', 'Relation'];
  12. var ParseSchema = function () {
  13. function ParseSchema(className) {
  14. (0, _classCallCheck2.default)(this, ParseSchema);
  15. if (typeof className === 'string') {
  16. if (className === 'User' && _CoreManager.default.get('PERFORM_USER_REWRITE')) {
  17. this.className = '_User';
  18. } else {
  19. this.className = className;
  20. }
  21. }
  22. this._fields = {};
  23. this._indexes = {};
  24. }
  25. (0, _createClass2.default)(ParseSchema, [{
  26. key: "get",
  27. value: function () {
  28. this.assertClassName();
  29. var controller = _CoreManager.default.getSchemaController();
  30. return controller.get(this.className).then(function (response) {
  31. if (!response) {
  32. throw new Error('Schema not found.');
  33. }
  34. return response;
  35. });
  36. }
  37. }, {
  38. key: "save",
  39. value: function () {
  40. this.assertClassName();
  41. var controller = _CoreManager.default.getSchemaController();
  42. var params = {
  43. className: this.className,
  44. fields: this._fields,
  45. indexes: this._indexes,
  46. classLevelPermissions: this._clp
  47. };
  48. return controller.create(this.className, params);
  49. }
  50. }, {
  51. key: "update",
  52. value: function () {
  53. this.assertClassName();
  54. var controller = _CoreManager.default.getSchemaController();
  55. var params = {
  56. className: this.className,
  57. fields: this._fields,
  58. indexes: this._indexes,
  59. classLevelPermissions: this._clp
  60. };
  61. this._fields = {};
  62. this._indexes = {};
  63. return controller.update(this.className, params);
  64. }
  65. }, {
  66. key: "delete",
  67. value: function () {
  68. this.assertClassName();
  69. var controller = _CoreManager.default.getSchemaController();
  70. return controller.delete(this.className);
  71. }
  72. }, {
  73. key: "purge",
  74. value: function () {
  75. this.assertClassName();
  76. var controller = _CoreManager.default.getSchemaController();
  77. return controller.purge(this.className);
  78. }
  79. }, {
  80. key: "assertClassName",
  81. value: function () {
  82. if (!this.className) {
  83. throw new Error('You must set a Class Name before making any request.');
  84. }
  85. }
  86. }, {
  87. key: "setCLP",
  88. value: function (clp) {
  89. if (clp instanceof _ParseCLP.default) {
  90. this._clp = clp.toJSON();
  91. } else {
  92. this._clp = clp;
  93. }
  94. return this;
  95. }
  96. }, {
  97. key: "addField",
  98. value: function (name, type) {
  99. var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
  100. type = type || 'String';
  101. if (!name) {
  102. throw new Error('field name may not be null.');
  103. }
  104. if (FIELD_TYPES.indexOf(type) === -1) {
  105. throw new Error(`${type} is not a valid type.`);
  106. }
  107. if (type === 'Pointer') {
  108. return this.addPointer(name, options.targetClass, options);
  109. }
  110. if (type === 'Relation') {
  111. return this.addRelation(name, options.targetClass, options);
  112. }
  113. var fieldOptions = {
  114. type: type
  115. };
  116. if (typeof options.required === 'boolean') {
  117. fieldOptions.required = options.required;
  118. }
  119. if (options.defaultValue !== undefined) {
  120. fieldOptions.defaultValue = options.defaultValue;
  121. }
  122. if (type === 'Date') {
  123. if (options && options.defaultValue) {
  124. fieldOptions.defaultValue = {
  125. __type: 'Date',
  126. iso: new Date(options.defaultValue)
  127. };
  128. }
  129. }
  130. this._fields[name] = fieldOptions;
  131. return this;
  132. }
  133. }, {
  134. key: "addIndex",
  135. value: function (name, index) {
  136. if (!name) {
  137. throw new Error('index name may not be null.');
  138. }
  139. if (!index) {
  140. throw new Error('index may not be null.');
  141. }
  142. this._indexes[name] = index;
  143. return this;
  144. }
  145. }, {
  146. key: "addString",
  147. value: function (name, options) {
  148. return this.addField(name, 'String', options);
  149. }
  150. }, {
  151. key: "addNumber",
  152. value: function (name, options) {
  153. return this.addField(name, 'Number', options);
  154. }
  155. }, {
  156. key: "addBoolean",
  157. value: function (name, options) {
  158. return this.addField(name, 'Boolean', options);
  159. }
  160. }, {
  161. key: "addDate",
  162. value: function (name, options) {
  163. return this.addField(name, 'Date', options);
  164. }
  165. }, {
  166. key: "addFile",
  167. value: function (name, options) {
  168. return this.addField(name, 'File', options);
  169. }
  170. }, {
  171. key: "addGeoPoint",
  172. value: function (name, options) {
  173. return this.addField(name, 'GeoPoint', options);
  174. }
  175. }, {
  176. key: "addPolygon",
  177. value: function (name, options) {
  178. return this.addField(name, 'Polygon', options);
  179. }
  180. }, {
  181. key: "addArray",
  182. value: function (name, options) {
  183. return this.addField(name, 'Array', options);
  184. }
  185. }, {
  186. key: "addObject",
  187. value: function (name, options) {
  188. return this.addField(name, 'Object', options);
  189. }
  190. }, {
  191. key: "addPointer",
  192. value: function (name, targetClass) {
  193. var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
  194. if (!name) {
  195. throw new Error('field name may not be null.');
  196. }
  197. if (!targetClass) {
  198. throw new Error('You need to set the targetClass of the Pointer.');
  199. }
  200. var fieldOptions = {
  201. type: 'Pointer',
  202. targetClass: targetClass
  203. };
  204. if (typeof options.required === 'boolean') {
  205. fieldOptions.required = options.required;
  206. }
  207. if (options.defaultValue !== undefined) {
  208. fieldOptions.defaultValue = options.defaultValue;
  209. if (options.defaultValue instanceof _ParseObject.default) {
  210. fieldOptions.defaultValue = options.defaultValue.toPointer();
  211. }
  212. }
  213. this._fields[name] = fieldOptions;
  214. return this;
  215. }
  216. }, {
  217. key: "addRelation",
  218. value: function (name, targetClass) {
  219. if (!name) {
  220. throw new Error('field name may not be null.');
  221. }
  222. if (!targetClass) {
  223. throw new Error('You need to set the targetClass of the Relation.');
  224. }
  225. this._fields[name] = {
  226. type: 'Relation',
  227. targetClass: targetClass
  228. };
  229. return this;
  230. }
  231. }, {
  232. key: "deleteField",
  233. value: function (name) {
  234. this._fields[name] = {
  235. __op: 'Delete'
  236. };
  237. return this;
  238. }
  239. }, {
  240. key: "deleteIndex",
  241. value: function (name) {
  242. this._indexes[name] = {
  243. __op: 'Delete'
  244. };
  245. return this;
  246. }
  247. }], [{
  248. key: "all",
  249. value: function () {
  250. var controller = _CoreManager.default.getSchemaController();
  251. return controller.get('').then(function (response) {
  252. if (response.results.length === 0) {
  253. throw new Error('Schema not found.');
  254. }
  255. return response.results;
  256. });
  257. }
  258. }]);
  259. return ParseSchema;
  260. }();
  261. var DefaultController = {
  262. send: function (className, method) {
  263. var params = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
  264. var RESTController = _CoreManager.default.getRESTController();
  265. return RESTController.request(method, `schemas/${className}`, params, {
  266. useMasterKey: true
  267. });
  268. },
  269. get: function (className) {
  270. return this.send(className, 'GET');
  271. },
  272. create: function (className, params) {
  273. return this.send(className, 'POST', params);
  274. },
  275. update: function (className, params) {
  276. return this.send(className, 'PUT', params);
  277. },
  278. delete: function (className) {
  279. return this.send(className, 'DELETE');
  280. },
  281. purge: function (className) {
  282. var RESTController = _CoreManager.default.getRESTController();
  283. return RESTController.request('DELETE', `purge/${className}`, {}, {
  284. useMasterKey: true
  285. });
  286. }
  287. };
  288. _CoreManager.default.setSchemaController(DefaultController);
  289. var _default = ParseSchema;
  290. exports.default = _default;