ParseSchema.js 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  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. var fieldOptions = {
  108. type: type
  109. };
  110. if (typeof options.required === 'boolean') {
  111. fieldOptions.required = options.required;
  112. }
  113. if (options.defaultValue !== undefined) {
  114. fieldOptions.defaultValue = options.defaultValue;
  115. }
  116. this._fields[name] = fieldOptions;
  117. return this;
  118. }
  119. }, {
  120. key: "addIndex",
  121. value: function (name, index) {
  122. if (!name) {
  123. throw new Error('index name may not be null.');
  124. }
  125. if (!index) {
  126. throw new Error('index may not be null.');
  127. }
  128. this._indexes[name] = index;
  129. return this;
  130. }
  131. }, {
  132. key: "addString",
  133. value: function (name, options) {
  134. return this.addField(name, 'String', options);
  135. }
  136. }, {
  137. key: "addNumber",
  138. value: function (name, options) {
  139. return this.addField(name, 'Number', options);
  140. }
  141. }, {
  142. key: "addBoolean",
  143. value: function (name, options) {
  144. return this.addField(name, 'Boolean', options);
  145. }
  146. }, {
  147. key: "addDate",
  148. value: function (name, options) {
  149. if (options && options.defaultValue) {
  150. options.defaultValue = {
  151. __type: 'Date',
  152. iso: new Date(options.defaultValue)
  153. };
  154. }
  155. return this.addField(name, 'Date', options);
  156. }
  157. }, {
  158. key: "addFile",
  159. value: function (name, options) {
  160. return this.addField(name, 'File', options);
  161. }
  162. }, {
  163. key: "addGeoPoint",
  164. value: function (name, options) {
  165. return this.addField(name, 'GeoPoint', options);
  166. }
  167. }, {
  168. key: "addPolygon",
  169. value: function (name, options) {
  170. return this.addField(name, 'Polygon', options);
  171. }
  172. }, {
  173. key: "addArray",
  174. value: function (name, options) {
  175. return this.addField(name, 'Array', options);
  176. }
  177. }, {
  178. key: "addObject",
  179. value: function (name, options) {
  180. return this.addField(name, 'Object', options);
  181. }
  182. }, {
  183. key: "addPointer",
  184. value: function (name, targetClass) {
  185. var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
  186. if (!name) {
  187. throw new Error('field name may not be null.');
  188. }
  189. if (!targetClass) {
  190. throw new Error('You need to set the targetClass of the Pointer.');
  191. }
  192. var fieldOptions = {
  193. type: 'Pointer',
  194. targetClass: targetClass
  195. };
  196. if (typeof options.required === 'boolean') {
  197. fieldOptions.required = options.required;
  198. }
  199. if (options.defaultValue !== undefined) {
  200. fieldOptions.defaultValue = options.defaultValue;
  201. if (options.defaultValue instanceof _ParseObject.default) {
  202. fieldOptions.defaultValue = options.defaultValue.toPointer();
  203. }
  204. }
  205. this._fields[name] = fieldOptions;
  206. return this;
  207. }
  208. }, {
  209. key: "addRelation",
  210. value: function (name, targetClass) {
  211. if (!name) {
  212. throw new Error('field name may not be null.');
  213. }
  214. if (!targetClass) {
  215. throw new Error('You need to set the targetClass of the Relation.');
  216. }
  217. this._fields[name] = {
  218. type: 'Relation',
  219. targetClass: targetClass
  220. };
  221. return this;
  222. }
  223. }, {
  224. key: "deleteField",
  225. value: function (name) {
  226. this._fields[name] = {
  227. __op: 'Delete'
  228. };
  229. return this;
  230. }
  231. }, {
  232. key: "deleteIndex",
  233. value: function (name) {
  234. this._indexes[name] = {
  235. __op: 'Delete'
  236. };
  237. return this;
  238. }
  239. }], [{
  240. key: "all",
  241. value: function () {
  242. var controller = _CoreManager.default.getSchemaController();
  243. return controller.get('').then(function (response) {
  244. if (response.results.length === 0) {
  245. throw new Error('Schema not found.');
  246. }
  247. return response.results;
  248. });
  249. }
  250. }]);
  251. return ParseSchema;
  252. }();
  253. var DefaultController = {
  254. send: function (className, method) {
  255. var params = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
  256. var RESTController = _CoreManager.default.getRESTController();
  257. return RESTController.request(method, "schemas/" + className, params, {
  258. useMasterKey: true
  259. });
  260. },
  261. get: function (className) {
  262. return this.send(className, 'GET');
  263. },
  264. create: function (className, params) {
  265. return this.send(className, 'POST', params);
  266. },
  267. update: function (className, params) {
  268. return this.send(className, 'PUT', params);
  269. },
  270. delete: function (className) {
  271. return this.send(className, 'DELETE');
  272. },
  273. purge: function (className) {
  274. var RESTController = _CoreManager.default.getRESTController();
  275. return RESTController.request('DELETE', "purge/" + className, {}, {
  276. useMasterKey: true
  277. });
  278. }
  279. };
  280. _CoreManager.default.setSchemaController(DefaultController);
  281. var _default = ParseSchema;
  282. exports.default = _default;