LocalDatastore.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539
  1. "use strict";
  2. var _CoreManager = _interopRequireDefault(require("./CoreManager"));
  3. var _ParseQuery = _interopRequireDefault(require("./ParseQuery"));
  4. var _LocalDatastoreUtils = require("./LocalDatastoreUtils");
  5. function _interopRequireDefault(obj) {
  6. return obj && obj.__esModule ? obj : {
  7. default: obj
  8. };
  9. }
  10. /**
  11. * Copyright (c) 2015-present, Parse, LLC.
  12. * All rights reserved.
  13. *
  14. * This source code is licensed under the BSD-style license found in the
  15. * LICENSE file in the root directory of this source tree. An additional grant
  16. * of patent rights can be found in the PATENTS file in the same directory.
  17. *
  18. * @flow
  19. */
  20. /**
  21. * Provides a local datastore which can be used to store and retrieve <code>Parse.Object</code>. <br />
  22. * To enable this functionality, call <code>Parse.enableLocalDatastore()</code>.
  23. *
  24. * Pin object to add to local datastore
  25. *
  26. * <pre>await object.pin();</pre>
  27. * <pre>await object.pinWithName('pinName');</pre>
  28. *
  29. * Query pinned objects
  30. *
  31. * <pre>query.fromLocalDatastore();</pre>
  32. * <pre>query.fromPin();</pre>
  33. * <pre>query.fromPinWithName();</pre>
  34. *
  35. * <pre>const localObjects = await query.find();</pre>
  36. *
  37. * @class Parse.LocalDatastore
  38. * @static
  39. */
  40. const LocalDatastore = {
  41. isEnabled: false,
  42. isSyncing: false,
  43. fromPinWithName(name
  44. /*: string*/
  45. )
  46. /*: Promise<Array<Object>>*/
  47. {
  48. const controller = _CoreManager.default.getLocalDatastoreController();
  49. return controller.fromPinWithName(name);
  50. },
  51. pinWithName(name
  52. /*: string*/
  53. , value
  54. /*: any*/
  55. )
  56. /*: Promise<void>*/
  57. {
  58. const controller = _CoreManager.default.getLocalDatastoreController();
  59. return controller.pinWithName(name, value);
  60. },
  61. unPinWithName(name
  62. /*: string*/
  63. )
  64. /*: Promise<void>*/
  65. {
  66. const controller = _CoreManager.default.getLocalDatastoreController();
  67. return controller.unPinWithName(name);
  68. },
  69. _getAllContents()
  70. /*: Promise<Object>*/
  71. {
  72. const controller = _CoreManager.default.getLocalDatastoreController();
  73. return controller.getAllContents();
  74. },
  75. // Use for testing
  76. _getRawStorage()
  77. /*: Promise<Object>*/
  78. {
  79. const controller = _CoreManager.default.getLocalDatastoreController();
  80. return controller.getRawStorage();
  81. },
  82. _clear()
  83. /*: Promise<void>*/
  84. {
  85. const controller = _CoreManager.default.getLocalDatastoreController();
  86. return controller.clear();
  87. },
  88. // Pin the object and children recursively
  89. // Saves the object and children key to Pin Name
  90. async _handlePinAllWithName(name
  91. /*: string*/
  92. , objects
  93. /*: Array<ParseObject>*/
  94. )
  95. /*: Promise<void>*/
  96. {
  97. const pinName = this.getPinName(name);
  98. const toPinPromises = [];
  99. const objectKeys = [];
  100. for (const parent of objects) {
  101. const children = this._getChildren(parent);
  102. const parentKey = this.getKeyForObject(parent);
  103. const json = parent._toFullJSON(undefined, true);
  104. if (parent._localId) {
  105. json._localId = parent._localId;
  106. }
  107. children[parentKey] = json;
  108. for (const objectKey in children) {
  109. objectKeys.push(objectKey);
  110. toPinPromises.push(this.pinWithName(objectKey, [children[objectKey]]));
  111. }
  112. }
  113. const fromPinPromise = this.fromPinWithName(pinName);
  114. const [pinned] = await Promise.all([fromPinPromise, toPinPromises]);
  115. const toPin = [...new Set([...(pinned || []), ...objectKeys])];
  116. return this.pinWithName(pinName, toPin);
  117. },
  118. // Removes object and children keys from pin name
  119. // Keeps the object and children pinned
  120. async _handleUnPinAllWithName(name
  121. /*: string*/
  122. , objects
  123. /*: Array<ParseObject>*/
  124. ) {
  125. const localDatastore = await this._getAllContents();
  126. const pinName = this.getPinName(name);
  127. const promises = [];
  128. let objectKeys = [];
  129. for (const parent of objects) {
  130. const children = this._getChildren(parent);
  131. const parentKey = this.getKeyForObject(parent);
  132. objectKeys.push(parentKey, ...Object.keys(children));
  133. }
  134. objectKeys = [...new Set(objectKeys)];
  135. let pinned = localDatastore[pinName] || [];
  136. pinned = pinned.filter(item => !objectKeys.includes(item));
  137. if (pinned.length === 0) {
  138. promises.push(this.unPinWithName(pinName));
  139. delete localDatastore[pinName];
  140. } else {
  141. promises.push(this.pinWithName(pinName, pinned));
  142. localDatastore[pinName] = pinned;
  143. }
  144. for (const objectKey of objectKeys) {
  145. let hasReference = false;
  146. for (const key in localDatastore) {
  147. if (key === _LocalDatastoreUtils.DEFAULT_PIN || key.startsWith(_LocalDatastoreUtils.PIN_PREFIX)) {
  148. const pinnedObjects = localDatastore[key] || [];
  149. if (pinnedObjects.includes(objectKey)) {
  150. hasReference = true;
  151. break;
  152. }
  153. }
  154. }
  155. if (!hasReference) {
  156. promises.push(this.unPinWithName(objectKey));
  157. }
  158. }
  159. return Promise.all(promises);
  160. },
  161. // Retrieve all pointer fields from object recursively
  162. _getChildren(object
  163. /*: ParseObject*/
  164. ) {
  165. const encountered = {};
  166. const json = object._toFullJSON(undefined, true);
  167. for (const key in json) {
  168. if (json[key] && json[key].__type && json[key].__type === 'Object') {
  169. this._traverse(json[key], encountered);
  170. }
  171. }
  172. return encountered;
  173. },
  174. _traverse(object
  175. /*: any*/
  176. , encountered
  177. /*: any*/
  178. ) {
  179. if (!object.objectId) {
  180. return;
  181. }
  182. const objectKey = this.getKeyForObject(object);
  183. if (encountered[objectKey]) {
  184. return;
  185. }
  186. encountered[objectKey] = object;
  187. for (const key in object) {
  188. let json = object[key];
  189. if (!object[key]) {
  190. json = object;
  191. }
  192. if (json.__type && json.__type === 'Object') {
  193. this._traverse(json, encountered);
  194. }
  195. }
  196. },
  197. // Transform keys in pin name to objects
  198. async _serializeObjectsFromPinName(name
  199. /*: string*/
  200. ) {
  201. const localDatastore = await this._getAllContents();
  202. const allObjects = [];
  203. for (const key in localDatastore) {
  204. if (key.startsWith(_LocalDatastoreUtils.OBJECT_PREFIX)) {
  205. allObjects.push(localDatastore[key][0]);
  206. }
  207. }
  208. if (!name) {
  209. return allObjects;
  210. }
  211. const pinName = this.getPinName(name);
  212. const pinned = localDatastore[pinName];
  213. if (!Array.isArray(pinned)) {
  214. return [];
  215. }
  216. const promises = pinned.map(objectKey => this.fromPinWithName(objectKey));
  217. let objects = await Promise.all(promises);
  218. objects = [].concat(...objects);
  219. return objects.filter(object => object != null);
  220. },
  221. // Replaces object pointers with pinned pointers
  222. // The object pointers may contain old data
  223. // Uses Breadth First Search Algorithm
  224. async _serializeObject(objectKey
  225. /*: string*/
  226. , localDatastore
  227. /*: any*/
  228. ) {
  229. let LDS = localDatastore;
  230. if (!LDS) {
  231. LDS = await this._getAllContents();
  232. }
  233. if (!LDS[objectKey] || LDS[objectKey].length === 0) {
  234. return null;
  235. }
  236. const root = LDS[objectKey][0];
  237. const queue = [];
  238. const meta = {};
  239. let uniqueId = 0;
  240. meta[uniqueId] = root;
  241. queue.push(uniqueId);
  242. while (queue.length !== 0) {
  243. const nodeId = queue.shift();
  244. const subTreeRoot = meta[nodeId];
  245. for (const field in subTreeRoot) {
  246. const value = subTreeRoot[field];
  247. if (value.__type && value.__type === 'Object') {
  248. const key = this.getKeyForObject(value);
  249. if (LDS[key] && LDS[key].length > 0) {
  250. const pointer = LDS[key][0];
  251. uniqueId++;
  252. meta[uniqueId] = pointer;
  253. subTreeRoot[field] = pointer;
  254. queue.push(uniqueId);
  255. }
  256. }
  257. }
  258. }
  259. return root;
  260. },
  261. // Called when an object is save / fetched
  262. // Update object pin value
  263. async _updateObjectIfPinned(object
  264. /*: ParseObject*/
  265. )
  266. /*: Promise<void>*/
  267. {
  268. if (!this.isEnabled) {
  269. return;
  270. }
  271. const objectKey = this.getKeyForObject(object);
  272. const pinned = await this.fromPinWithName(objectKey);
  273. if (!pinned || pinned.length === 0) {
  274. return;
  275. }
  276. return this.pinWithName(objectKey, [object._toFullJSON()]);
  277. },
  278. // Called when object is destroyed
  279. // Unpin object and remove all references from pin names
  280. // TODO: Destroy children?
  281. async _destroyObjectIfPinned(object
  282. /*: ParseObject*/
  283. ) {
  284. if (!this.isEnabled) {
  285. return;
  286. }
  287. const localDatastore = await this._getAllContents();
  288. const objectKey = this.getKeyForObject(object);
  289. const pin = localDatastore[objectKey];
  290. if (!pin) {
  291. return;
  292. }
  293. const promises = [this.unPinWithName(objectKey)];
  294. delete localDatastore[objectKey];
  295. for (const key in localDatastore) {
  296. if (key === _LocalDatastoreUtils.DEFAULT_PIN || key.startsWith(_LocalDatastoreUtils.PIN_PREFIX)) {
  297. let pinned = localDatastore[key] || [];
  298. if (pinned.includes(objectKey)) {
  299. pinned = pinned.filter(item => item !== objectKey);
  300. if (pinned.length === 0) {
  301. promises.push(this.unPinWithName(key));
  302. delete localDatastore[key];
  303. } else {
  304. promises.push(this.pinWithName(key, pinned));
  305. localDatastore[key] = pinned;
  306. }
  307. }
  308. }
  309. }
  310. return Promise.all(promises);
  311. },
  312. // Update pin and references of the unsaved object
  313. async _updateLocalIdForObject(localId
  314. /*: string*/
  315. , object
  316. /*: ParseObject*/
  317. ) {
  318. if (!this.isEnabled) {
  319. return;
  320. }
  321. const localKey = `${_LocalDatastoreUtils.OBJECT_PREFIX}${object.className}_${localId}`;
  322. const objectKey = this.getKeyForObject(object);
  323. const unsaved = await this.fromPinWithName(localKey);
  324. if (!unsaved || unsaved.length === 0) {
  325. return;
  326. }
  327. const promises = [this.unPinWithName(localKey), this.pinWithName(objectKey, unsaved)];
  328. const localDatastore = await this._getAllContents();
  329. for (const key in localDatastore) {
  330. if (key === _LocalDatastoreUtils.DEFAULT_PIN || key.startsWith(_LocalDatastoreUtils.PIN_PREFIX)) {
  331. let pinned = localDatastore[key] || [];
  332. if (pinned.includes(localKey)) {
  333. pinned = pinned.filter(item => item !== localKey);
  334. pinned.push(objectKey);
  335. promises.push(this.pinWithName(key, pinned));
  336. localDatastore[key] = pinned;
  337. }
  338. }
  339. }
  340. return Promise.all(promises);
  341. },
  342. /**
  343. * Updates Local Datastore from Server
  344. *
  345. * <pre>
  346. * await Parse.LocalDatastore.updateFromServer();
  347. * </pre>
  348. *
  349. * @function updateFromServer
  350. * @name Parse.LocalDatastore.updateFromServer
  351. * @static
  352. */
  353. async updateFromServer() {
  354. if (!this.checkIfEnabled() || this.isSyncing) {
  355. return;
  356. }
  357. const localDatastore = await this._getAllContents();
  358. const keys = [];
  359. for (const key in localDatastore) {
  360. if (key.startsWith(_LocalDatastoreUtils.OBJECT_PREFIX)) {
  361. keys.push(key);
  362. }
  363. }
  364. if (keys.length === 0) {
  365. return;
  366. }
  367. this.isSyncing = true;
  368. const pointersHash = {};
  369. for (const key of keys) {
  370. // Ignore the OBJECT_PREFIX
  371. let [,, className, objectId] = key.split('_'); // User key is split into [ 'Parse', 'LDS', '', 'User', 'objectId' ]
  372. if (key.split('_').length === 5 && key.split('_')[3] === 'User') {
  373. className = '_User';
  374. objectId = key.split('_')[4];
  375. }
  376. if (objectId.startsWith('local')) {
  377. continue;
  378. }
  379. if (!(className in pointersHash)) {
  380. pointersHash[className] = new Set();
  381. }
  382. pointersHash[className].add(objectId);
  383. }
  384. const queryPromises = Object.keys(pointersHash).map(className => {
  385. const objectIds = Array.from(pointersHash[className]);
  386. const query = new _ParseQuery.default(className);
  387. query.limit(objectIds.length);
  388. if (objectIds.length === 1) {
  389. query.equalTo('objectId', objectIds[0]);
  390. } else {
  391. query.containedIn('objectId', objectIds);
  392. }
  393. return query.find();
  394. });
  395. try {
  396. const responses = await Promise.all(queryPromises);
  397. const objects = [].concat.apply([], responses);
  398. const pinPromises = objects.map(object => {
  399. const objectKey = this.getKeyForObject(object);
  400. return this.pinWithName(objectKey, object._toFullJSON());
  401. });
  402. await Promise.all(pinPromises);
  403. this.isSyncing = false;
  404. } catch (error) {
  405. // eslint-disable-next-line no-console
  406. console.error('Error syncing LocalDatastore: ', error);
  407. this.isSyncing = false;
  408. }
  409. },
  410. getKeyForObject(object
  411. /*: any*/
  412. ) {
  413. const objectId = object.objectId || object._getId();
  414. return `${_LocalDatastoreUtils.OBJECT_PREFIX}${object.className}_${objectId}`;
  415. },
  416. getPinName(pinName
  417. /*: ?string*/
  418. ) {
  419. if (!pinName || pinName === _LocalDatastoreUtils.DEFAULT_PIN) {
  420. return _LocalDatastoreUtils.DEFAULT_PIN;
  421. }
  422. return _LocalDatastoreUtils.PIN_PREFIX + pinName;
  423. },
  424. checkIfEnabled() {
  425. if (!this.isEnabled) {
  426. // eslint-disable-next-line no-console
  427. console.error('Parse.enableLocalDatastore() must be called first');
  428. }
  429. return this.isEnabled;
  430. }
  431. };
  432. module.exports = LocalDatastore;
  433. _CoreManager.default.setLocalDatastoreController(require('./LocalDatastoreController'));
  434. _CoreManager.default.setLocalDatastore(LocalDatastore);