-
-
Notifications
You must be signed in to change notification settings - Fork 514
Add JS Doc for Local Datastore #558
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 5 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,6 @@ | ||
| # Generated site | ||
| _site/ | ||
| .jekyll-metadata | ||
|
|
||
| # Logs | ||
| logs/ | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,146 @@ | ||
| # Local Datastore | ||
|
|
||
| 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()`. | ||
|
|
||
| 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. | ||
|
|
||
| ## Pinning | ||
|
|
||
| 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. | ||
|
|
||
| ```javascript | ||
| const GameScore = Parse.Object.extend("GameScore"); | ||
| const gameScore = new GameScore(); | ||
|
|
||
| await gameScore.save(); | ||
| await gameScore.pin(); | ||
| ``` | ||
|
|
||
| If you have multiple objects, you can pin them all at once with the `Parse.Object.pinAll()` convenience method. | ||
|
|
||
| ```javascript | ||
| await Parse.Object.pinAll(listOfObjects); | ||
| ``` | ||
|
|
||
| ## Retrieving | ||
|
|
||
| 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. | ||
|
|
||
| ```javascript | ||
| const GameScore = Parse.Object.extend("GameScore"); | ||
| const query = new Parse.Query(GameScore); | ||
| query.fromLocalDatastore(); | ||
| const result = await query.get('xWMyZ4YE'); | ||
| ``` | ||
|
|
||
| ## Querying the Local Datastore | ||
|
|
||
| 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. | ||
|
|
||
| ```javascript | ||
| const GameScore = Parse.Object.extend("GameScore"); | ||
| const query = new Parse.Query(GameScore); | ||
| query.equalTo('playerName', 'Joe Bob'); | ||
| query.fromLocalDatastore(); | ||
| const results = await query.find(); | ||
| ``` | ||
|
|
||
| ## Unpinning | ||
|
|
||
| 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. | ||
|
|
||
| ```javascript | ||
| await gameScore.unPin(); | ||
| ``` | ||
|
|
||
| There's also a method to unpin several objects at once. | ||
|
|
||
| ```javascript | ||
| await Parse.Object.unPinAll(listOfObjects); | ||
| ``` | ||
|
|
||
| There's also a method to remove all objects from default pin | ||
|
|
||
| ```javascript | ||
| await Parse.Object.unPinAllObjects(); | ||
| ``` | ||
|
|
||
| ## Pinning with Labels | ||
|
|
||
| Labels indicate a group of objects that should be stored together. | ||
|
|
||
| ```javascript | ||
| // Add several objects with a label. | ||
| await Parse.Object.pinAllWithName('MyScores', listOfObjects); | ||
|
|
||
| // Add another object with the same label. | ||
| await anotherGameScore.pinWithName('MyScores'); | ||
| ``` | ||
|
|
||
| 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 you care about. | ||
|
|
||
| ```javascript | ||
| await Parse.Object.unPinAllWithName('MyScores', listOfObjects); | ||
| ``` | ||
|
|
||
| There's also a method to remove all objects from a label. | ||
|
|
||
| ```javascript | ||
| await Parse.Object.unPinAllObjectsWithName('MyScores'); | ||
| ``` | ||
|
|
||
| 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. | ||
|
|
||
| ## Caching Query Results | ||
|
|
||
| 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. | ||
|
|
||
| ```javascript | ||
| const GameScore = Parse.Object.extend("GameScore"); | ||
| const query = new Parse.Query(GameScore); | ||
| query.equalTo("playerName", "foo"); | ||
| const results = await query.find(); | ||
| await Parse.Object.unPinAllObjectsWithName('HighScores'); | ||
| await Parse.Object.pinAllWithName('HighScores', results); | ||
| ``` | ||
|
|
||
| When you want to get the cached results for the query, you can then run the same query against the local datastore. | ||
|
|
||
| ```javascript | ||
| const GameScore = Parse.Object.extend("GameScore"); | ||
| const query = new Parse.Query(GameScore); | ||
| query.equalTo("playerName", "foo"); | ||
| query.fromLocalDatastore(); | ||
| const results = await query.find(); | ||
| ``` | ||
|
|
||
| ## Dumping Contents | ||
|
|
||
| For testing purposes, you can use `Parse.dumpLocalDatastore()` to view the contents of your local datastore. | ||
|
|
||
| ```javascript | ||
| const LDS = await Parse.dumpLocalDatastore(); | ||
| ``` | ||
|
|
||
| The local datastore is a dictionary of key / values. Each object has a key (className_objectId) and serves as a reference to this object. | ||
|
|
||
| `pin()` will save this reference under the default pin `_default` | ||
| `pinWithName('YourPinName')` will save this reference under `parsePin_YourPinName` | ||
|
|
||
| Unpinning will have the opposite effect. | ||
|
|
||
| ## LiveQuery | ||
|
|
||
| If you are subscribed to a LiveQuery Update Event, the updated object will be stored in the LocalDatastore if pinned. | ||
|
|
||
| ```javascript | ||
| const gameScore = new GameScore(); | ||
| await gameScore.save(); | ||
| await gameScore.pin(); | ||
| const query = new Parse.Query(GameScore); | ||
| query.equalTo('objectId', gameScore.id) | ||
| const subscription = query.subscribe(); | ||
| subscription.on('update', (object) => { | ||
| // Since object (gameScore) is pinned, LDS will update automatically | ||
| }); | ||
| ``` | ||
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
remove 'you care about'