Skip to content

Commit 5e9f3fd

Browse files
authored
Merge pull request parse-community#558 from dplewis/lds
Add JS Doc for Local Datastore
2 parents 3a4a4d7 + 7623838 commit 5e9f3fd

File tree

5 files changed

+151
-2
lines changed

5 files changed

+151
-2
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# Generated site
22
_site/
3+
.jekyll-metadata
34

45
# Logs
56
logs/

_includes/ios/local-datastore.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,7 @@ gameScore.unpinInBackground()
171171

172172
There's also a method to unpin several objects at once.
173173

174+
<div class="language-toggle" markdown="1">
174175
```objective_c
175176
[PFObject unpinAllInBackground:listOfObjects];
176177
```

_includes/js/local-datastore.md

Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
# Local Datastore
2+
3+
The Parse JS SDK (Version 2.2.0+) provides a local datastore which can be used to store and retrieve `Parse.Object`s. To enable this functionality, call `Parse.enableLocalDatastore()`.
4+
5+
There are a couple of side effects of enabling the local datastore that you should be aware of. When enabled, there will only be one instance of any given `Parse.Object`. For example, imagine you have an instance of the `"GameScore"` class with an `objectId` of `"xWMyZ4YEGZ"`, and then you issue a `Parse.Query` for all instances of `"GameScore"` with that `objectId`. The result will be the same instance of the object you already have in memory.
6+
7+
## Pinning
8+
9+
You can store a `Parse.Object` in the local datastore by pinning it. Pinning a `Parse.Object` is recursive, just like saving, so any objects that are pointed to by the one you are pinning will also be pinned. When an object is pinned, every time you update it by fetching or saving new data, the copy in the local datastore will be updated automatically. You don't need to worry about it at all.
10+
11+
```javascript
12+
const GameScore = Parse.Object.extend("GameScore");
13+
const gameScore = new GameScore();
14+
15+
await gameScore.save();
16+
await gameScore.pin();
17+
```
18+
19+
If you have multiple objects, you can pin them all at once with the `Parse.Object.pinAll()` convenience method.
20+
21+
```javascript
22+
await Parse.Object.pinAll(listOfObjects);
23+
```
24+
25+
## Retrieving
26+
27+
Storing objects is great, but it's only useful if you can then get the objects back out later. Retrieving an object from the local datastore works just like retrieving one over the network. The only difference is calling the `fromLocalDatastore` method to tell the `Parse.Query` where to look for its results.
28+
29+
```javascript
30+
const GameScore = Parse.Object.extend("GameScore");
31+
const query = new Parse.Query(GameScore);
32+
query.fromLocalDatastore();
33+
const result = await query.get('xWMyZ4YE');
34+
```
35+
36+
## Querying the Local Datastore
37+
38+
Often, you'll want to find a whole list of objects that match certain criteria, instead of getting a single object by id. To do that, you can use a [Parse.Query](#queries). Any `Parse.Query` can be used with the local datastore just as with the network. The results will include any object you have pinned that matches the query. Any unsaved changes you have made to the object will be considered when evaluating the query. So you can find a local object that matches, even if it was never returned from the server for this particular query. All query methods are supported except aggregate and distinct queries.
39+
40+
```javascript
41+
const GameScore = Parse.Object.extend("GameScore");
42+
const query = new Parse.Query(GameScore);
43+
query.equalTo('playerName', 'Joe Bob');
44+
query.fromLocalDatastore();
45+
const results = await query.find();
46+
```
47+
48+
## Unpinning
49+
50+
When you are done with an object and no longer need it to be in the local datastore, you can simply unpin it. This will free up disk space and keep your queries on the local datastore running quickly. This will unpin from the default pin.
51+
52+
```javascript
53+
await gameScore.unPin();
54+
```
55+
56+
There's also a method to unpin several objects at once.
57+
58+
```javascript
59+
await Parse.Object.unPinAll(listOfObjects);
60+
```
61+
62+
There's also a method to remove all objects from default pin
63+
64+
```javascript
65+
await Parse.Object.unPinAllObjects();
66+
```
67+
68+
## Pinning with Labels
69+
70+
Labels indicate a group of objects that should be stored together.
71+
72+
```javascript
73+
// Add several objects with a label.
74+
await Parse.Object.pinAllWithName('MyScores', listOfObjects);
75+
76+
// Add another object with the same label.
77+
await anotherGameScore.pinWithName('MyScores');
78+
```
79+
80+
To unpin all of the objects with the same label at the same time, you can pass a label to the unpin methods. This saves you from having to manually track which objects are in each group.
81+
82+
```javascript
83+
await Parse.Object.unPinAllWithName('MyScores', listOfObjects);
84+
```
85+
86+
There's also a method to remove all objects from a label.
87+
88+
```javascript
89+
await Parse.Object.unPinAllObjectsWithName('MyScores');
90+
```
91+
92+
An Object will be kept in the datastore as long as it is pinned by at least one label. So an object pinned with two labels will stay in the datastore if one of the two labels is unpinned.
93+
94+
## Caching Query Results
95+
96+
Pinning with labels makes it easy to cache the results of queries. You can use one label to pin the results of each different query. To get new results from the network, just do a query and update the pinned objects.
97+
98+
```javascript
99+
const GameScore = Parse.Object.extend("GameScore");
100+
const query = new Parse.Query(GameScore);
101+
query.equalTo("playerName", "foo");
102+
const results = await query.find();
103+
await Parse.Object.unPinAllObjectsWithName('HighScores');
104+
await Parse.Object.pinAllWithName('HighScores', results);
105+
```
106+
107+
When you want to get the cached results for the query, you can then run the same query against the local datastore.
108+
109+
```javascript
110+
const GameScore = Parse.Object.extend("GameScore");
111+
const query = new Parse.Query(GameScore);
112+
query.equalTo("playerName", "foo");
113+
query.fromLocalDatastore();
114+
const results = await query.find();
115+
```
116+
117+
## Dumping Contents
118+
119+
For testing purposes, you can use `Parse.dumpLocalDatastore()` to view the contents of your local datastore.
120+
121+
```javascript
122+
const LDS = await Parse.dumpLocalDatastore();
123+
```
124+
125+
The local datastore is a dictionary of key / values. Each object has a key (className_objectId) and serves as a reference to this object.
126+
127+
`pin()` will save this reference under the default pin `_default`
128+
`pinWithName('YourPinName')` will save this reference under `parsePin_YourPinName`
129+
130+
Unpinning will have the opposite effect.
131+
132+
## LiveQuery
133+
134+
If you are subscribed to a LiveQuery Update Event, the updated object will be stored in the LocalDatastore if pinned.
135+
136+
```javascript
137+
const gameScore = new GameScore();
138+
await gameScore.save();
139+
await gameScore.pin();
140+
const query = new Parse.Query(GameScore);
141+
query.equalTo('objectId', gameScore.id)
142+
const subscription = query.subscribe();
143+
subscription.on('update', (object) => {
144+
// Since object (gameScore) is pinned, LDS will update automatically
145+
});
146+
```

assets/js/bundle.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

js.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ sections:
1818
- "js/files.md"
1919
- "js/promises.md"
2020
- "js/geopoints.md"
21+
- "js/local-datastore.md"
2122
- "js/push-notifications.md"
2223
- "js/config.md"
2324
- "js/analytics.md"

0 commit comments

Comments
 (0)