Skip to content

Commit 629a550

Browse files
authored
Local Datastore (#612)
* initial setup * object pinning * initial setup * object pinning * finished object pinning * fetchfromlocaldatastore * Add Query Functionality * remove circular dependencies * fix unpinallobjects * ungodly amount of jest test * added on destroy remove pin * coverage improvement * run tests against local storage * lint and remove pinName if empty * lint n nits * Revert "lint n nits" This reverts commit 0e06b14. * test for enabled and disabled datastore * getLocalDatastore rename * add react and promises * rename object key to ${className]_${id} * add documentation * Offline Query Tests and Documentation * test fix * Update ParseLocalDatastoreTest.js * tests for fetch * auto remove reference if not pinned to name * fetch object from LDS with children * add containedBy, withinPolygon, polygonContains * reduce number of async calls * lint * test fix * jest tests * prevent fetch from overriding server data improve coverage and clean up unused code * lint * use bfs to avoid recursion
1 parent 2b6f363 commit 629a550

27 files changed

+6083
-31
lines changed

integration/cloud/main.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,13 @@ Parse.Cloud.define("bar", function(request) {
77
}
88
});
99

10+
Parse.Cloud.define('TestFetchFromLocalDatastore', function (request) {
11+
const object = new Parse.Object('Item');
12+
object.id = request.params.id;
13+
object.set('foo', 'changed');
14+
return object.save();
15+
});
16+
1017
Parse.Cloud.define('CloudFunctionUndefined', function() {
1118
return undefined;
1219
});

integration/test/ParseLocalDatastoreTest.js

Lines changed: 2406 additions & 0 deletions
Large diffs are not rendered by default.

integration/test/mockLocalStorage.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
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+
10+
let mockStorage = {};
11+
const mockLocalStorage = {
12+
13+
getItem(path) {
14+
return mockStorage[path] || null;
15+
},
16+
17+
setItem(path, value) {
18+
mockStorage[path] = value;
19+
},
20+
21+
removeItem(path) {
22+
delete mockStorage[path];
23+
},
24+
25+
get length() {
26+
return Object.keys(mockStorage).length;
27+
},
28+
29+
key: (i) => {
30+
const keys = Object.keys(mockStorage);
31+
return keys[i] || null;
32+
},
33+
34+
clear() {
35+
mockStorage = {};
36+
},
37+
};
38+
39+
module.exports = mockLocalStorage;

integration/test/mockRNStorage.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
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+
10+
let mockStorage = {};
11+
const mockRNStorage = {
12+
getItem(path, cb) {
13+
cb(undefined, mockStorage[path] || null);
14+
},
15+
16+
setItem(path, value, cb) {
17+
mockStorage[path] = value;
18+
cb();
19+
},
20+
21+
removeItem(path, cb) {
22+
delete mockStorage[path];
23+
cb();
24+
},
25+
26+
getAllKeys(cb) {
27+
cb(undefined, Object.keys(mockStorage));
28+
},
29+
30+
clear() {
31+
mockStorage = {};
32+
},
33+
};
34+
35+
module.exports = mockRNStorage;

src/CoreManager.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,13 @@ type StorageController = {
108108
removeItemAsync: (path: string) => Promise;
109109
clear: () => void;
110110
};
111+
type LocalDatastoreController = {
112+
fromPinWithName: (name: string) => ?any;
113+
pinWithName: (name: string, objects: any) => void;
114+
unPinWithName: (name: string) => void;
115+
getAllContents: () => ?any;
116+
clear: () => void;
117+
};
111118
type UserController = {
112119
setCurrentUser: (user: ParseUser) => Promise;
113120
currentUser: () => ?ParseUser;
@@ -144,6 +151,7 @@ type Config = {
144151
SchemaController?: SchemaController,
145152
SessionController?: SessionController,
146153
StorageController?: StorageController,
154+
LocalDatastoreController?: LocalDatastoreController,
147155
UserController?: UserController,
148156
HooksController?: HooksController,
149157
};
@@ -331,6 +339,23 @@ module.exports = {
331339
config['StorageController'] = controller;
332340
},
333341

342+
setLocalDatastoreController(controller: LocalDatastoreController) {
343+
requireMethods('LocalDatastoreController', ['pinWithName', 'fromPinWithName', 'unPinWithName', 'getAllContents', 'clear'], controller);
344+
config['LocalDatastoreController'] = controller;
345+
},
346+
347+
getLocalDatastoreController(): LocalDatastoreController {
348+
return config['LocalDatastoreController'];
349+
},
350+
351+
setLocalDatastore(store: any) {
352+
config['LocalDatastore'] = store;
353+
},
354+
355+
getLocalDatastore() {
356+
return config['LocalDatastore'];
357+
},
358+
334359
getStorageController(): StorageController {
335360
return config['StorageController'];
336361
},

0 commit comments

Comments
 (0)