Parse.js 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. /**
  2. * Copyright (c) 2015-present, Parse, LLC.
  3. * All rights reserved.
  4. *
  5. * This source code is licensed under the BSD-style license found in the
  6. * LICENSE file in the root directory of this source tree. An additional grant
  7. * of patent rights can be found in the PATENTS file in the same directory.
  8. */
  9. import decode from './decode';
  10. import encode from './encode';
  11. import CoreManager from './CoreManager';
  12. import InstallationController from './InstallationController';
  13. import * as ParseOp from './ParseOp';
  14. import RESTController from './RESTController';
  15. /**
  16. * Contains all Parse API classes and functions.
  17. * @static
  18. * @global
  19. * @class
  20. * @hideconstructor
  21. */
  22. const Parse = {
  23. /**
  24. * Call this method first to set up your authentication tokens for Parse.
  25. * You can get your keys from the Data Browser on parse.com.
  26. * @param {String} applicationId Your Parse Application ID.
  27. * @param {String} javaScriptKey (optional) Your Parse JavaScript Key (Not needed for parse-server)
  28. * @param {String} masterKey (optional) Your Parse Master Key. (Node.js only!)
  29. * @static
  30. */
  31. initialize(applicationId
  32. /*: string*/
  33. , javaScriptKey
  34. /*: string*/
  35. ) {
  36. Parse._initialize(applicationId, javaScriptKey);
  37. },
  38. _initialize(applicationId
  39. /*: string*/
  40. , javaScriptKey
  41. /*: string*/
  42. , masterKey
  43. /*: string*/
  44. ) {
  45. CoreManager.set('APPLICATION_ID', applicationId);
  46. CoreManager.set('JAVASCRIPT_KEY', javaScriptKey);
  47. CoreManager.set('MASTER_KEY', masterKey);
  48. CoreManager.set('USE_MASTER_KEY', false);
  49. },
  50. /**
  51. * Call this method to set your AsyncStorage engine
  52. * Starting Parse@1.11, the ParseSDK do not provide a React AsyncStorage as the ReactNative module
  53. * is not provided at a stable path and changes over versions.
  54. * @param {AsyncStorage} storage a react native async storage.
  55. * @static
  56. */
  57. setAsyncStorage(storage
  58. /*: any*/
  59. ) {
  60. CoreManager.setAsyncStorage(storage);
  61. },
  62. /**
  63. * Call this method to set your LocalDatastoreStorage engine
  64. * If using React-Native use {@link Parse.setAsyncStorage Parse.setAsyncStorage()}
  65. * @param {LocalDatastoreController} controller a data storage.
  66. * @static
  67. */
  68. setLocalDatastoreController(controller
  69. /*: any*/
  70. ) {
  71. CoreManager.setLocalDatastoreController(controller);
  72. }
  73. };
  74. /** These legacy setters may eventually be deprecated **/
  75. /**
  76. * @member Parse.applicationId
  77. * @type string
  78. * @static
  79. */
  80. Object.defineProperty(Parse, 'applicationId', {
  81. get() {
  82. return CoreManager.get('APPLICATION_ID');
  83. },
  84. set(value) {
  85. CoreManager.set('APPLICATION_ID', value);
  86. }
  87. });
  88. /**
  89. * @member Parse.javaScriptKey
  90. * @type string
  91. * @static
  92. */
  93. Object.defineProperty(Parse, 'javaScriptKey', {
  94. get() {
  95. return CoreManager.get('JAVASCRIPT_KEY');
  96. },
  97. set(value) {
  98. CoreManager.set('JAVASCRIPT_KEY', value);
  99. }
  100. });
  101. /**
  102. * @member Parse.masterKey
  103. * @type string
  104. * @static
  105. */
  106. Object.defineProperty(Parse, 'masterKey', {
  107. get() {
  108. return CoreManager.get('MASTER_KEY');
  109. },
  110. set(value) {
  111. CoreManager.set('MASTER_KEY', value);
  112. }
  113. });
  114. /**
  115. * @member Parse.serverURL
  116. * @type string
  117. * @static
  118. */
  119. Object.defineProperty(Parse, 'serverURL', {
  120. get() {
  121. return CoreManager.get('SERVER_URL');
  122. },
  123. set(value) {
  124. CoreManager.set('SERVER_URL', value);
  125. }
  126. });
  127. /**
  128. * @member Parse.serverAuthToken
  129. * @type string
  130. * @static
  131. */
  132. Object.defineProperty(Parse, 'serverAuthToken', {
  133. get() {
  134. return CoreManager.get('SERVER_AUTH_TOKEN');
  135. },
  136. set(value) {
  137. CoreManager.set('SERVER_AUTH_TOKEN', value);
  138. }
  139. });
  140. /**
  141. * @member Parse.serverAuthType
  142. * @type string
  143. * @static
  144. */
  145. Object.defineProperty(Parse, 'serverAuthType', {
  146. get() {
  147. return CoreManager.get('SERVER_AUTH_TYPE');
  148. },
  149. set(value) {
  150. CoreManager.set('SERVER_AUTH_TYPE', value);
  151. }
  152. });
  153. /**
  154. * @member Parse.liveQueryServerURL
  155. * @type string
  156. * @static
  157. */
  158. Object.defineProperty(Parse, 'liveQueryServerURL', {
  159. get() {
  160. return CoreManager.get('LIVEQUERY_SERVER_URL');
  161. },
  162. set(value) {
  163. CoreManager.set('LIVEQUERY_SERVER_URL', value);
  164. }
  165. });
  166. /* End setters */
  167. Parse.ACL = require('./ParseACL').default;
  168. Parse.Analytics = require('./Analytics');
  169. Parse.AnonymousUtils = require('./AnonymousUtils').default;
  170. Parse.Cloud = require('./Cloud');
  171. Parse.CoreManager = require('./CoreManager');
  172. Parse.Config = require('./ParseConfig').default;
  173. Parse.Error = require('./ParseError').default;
  174. Parse.FacebookUtils = require('./FacebookUtils').default;
  175. Parse.File = require('./ParseFile').default;
  176. Parse.GeoPoint = require('./ParseGeoPoint').default;
  177. Parse.Polygon = require('./ParsePolygon').default;
  178. Parse.Installation = require('./ParseInstallation').default;
  179. Parse.LocalDatastore = require('./LocalDatastore');
  180. Parse.Object = require('./ParseObject').default;
  181. Parse.Op = {
  182. Set: ParseOp.SetOp,
  183. Unset: ParseOp.UnsetOp,
  184. Increment: ParseOp.IncrementOp,
  185. Add: ParseOp.AddOp,
  186. Remove: ParseOp.RemoveOp,
  187. AddUnique: ParseOp.AddUniqueOp,
  188. Relation: ParseOp.RelationOp
  189. };
  190. Parse.Push = require('./Push');
  191. Parse.Query = require('./ParseQuery').default;
  192. Parse.Relation = require('./ParseRelation').default;
  193. Parse.Role = require('./ParseRole').default;
  194. Parse.Schema = require('./ParseSchema').default;
  195. Parse.Session = require('./ParseSession').default;
  196. Parse.Storage = require('./Storage');
  197. Parse.User = require('./ParseUser').default;
  198. Parse.LiveQuery = require('./ParseLiveQuery').default;
  199. Parse.LiveQueryClient = require('./LiveQueryClient').default;
  200. Parse._request = function (...args) {
  201. return CoreManager.getRESTController().request.apply(null, args);
  202. };
  203. Parse._ajax = function (...args) {
  204. return CoreManager.getRESTController().ajax.apply(null, args);
  205. }; // We attempt to match the signatures of the legacy versions of these methods
  206. Parse._decode = function (_, value) {
  207. return decode(value);
  208. };
  209. Parse._encode = function (value, _, disallowObjects) {
  210. return encode(value, disallowObjects);
  211. };
  212. Parse._getInstallationId = function () {
  213. return CoreManager.getInstallationController().currentInstallationId();
  214. };
  215. /**
  216. * Enable pinning in your application.
  217. * This must be called before your application can use pinning.
  218. *
  219. * @static
  220. */
  221. Parse.enableLocalDatastore = function () {
  222. Parse.LocalDatastore.isEnabled = true;
  223. };
  224. /**
  225. * Flag that indicates whether Local Datastore is enabled.
  226. *
  227. * @static
  228. */
  229. Parse.isLocalDatastoreEnabled = function () {
  230. return Parse.LocalDatastore.isEnabled;
  231. };
  232. /**
  233. * Gets all contents from Local Datastore
  234. *
  235. * <pre>
  236. * await Parse.dumpLocalDatastore();
  237. * </pre>
  238. *
  239. * @static
  240. */
  241. Parse.dumpLocalDatastore = function () {
  242. if (!Parse.LocalDatastore.isEnabled) {
  243. console.log('Parse.enableLocalDatastore() must be called first'); // eslint-disable-line no-console
  244. return Promise.resolve({});
  245. } else {
  246. return Parse.LocalDatastore._getAllContents();
  247. }
  248. };
  249. CoreManager.setInstallationController(InstallationController);
  250. CoreManager.setRESTController(RESTController);
  251. // For legacy requires, of the form `var Parse = require('parse').Parse`
  252. Parse.Parse = Parse;
  253. module.exports = Parse;