LocalDatastore.js 13 KB

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