ParseSchema.js 8.7 KB

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