Skip to content
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

how to export to a file and import? #2

Closed
ChangePlaces opened this issue May 20, 2017 · 1 comment
Closed

how to export to a file and import? #2

ChangePlaces opened this issue May 20, 2017 · 1 comment

Comments

@ChangePlaces
Copy link

I'm wanting to allow the user to backup the data to a file, and import it. The backing up is not so much an issue as I can JSON.stringify the pocket object I presume. how would I go about importing it? I'm not sure what keys to use to set on localStorage. I see the keys are: . but would I have to manually cycle through all the stores and set the keys in the localstorage? Or, is there a nicer way that you have?

@vincentracine
Copy link
Owner

vincentracine commented May 22, 2017

@ChangePlaces Just to confirm my understanding; you are trying to save the pocket to a JSON file by using JSON.stringify(...) and then at a later time, restore the data in the JSON file to the pocket? Please correct me if I did not understand your objective correctly.

Currently, there is no functionality to achieve this by default but it can easily be done. You can iterate through the JSON from the file and override whatever is in the localStorage and then call pocket.restore() which will load the data from the localStorage into the pocket. Alternatively, you can create a pocket, load the JSON from the file, iterate through the JSON keys and add the documents to the pocket e.g.

var jsonFileContent = '{"test": {"name":"test","documents":[{"forename":"Foo","surname":"Bar","_id":"914b2c31-4385-4089-8871-45a779d1202e"}],"options":{"autoCommit":true,"dbname":"pocket"},"length":1}, "user": {"name":"session","documents":[],"options":{"autoCommit":true,"dbname":"pocket"},"length":0}}';
var collections = JSON.parse(jsonFileContent);
var pocket = new Pocket(); // Or an existing pocket

Object.keys(collections).forEach(function(collection){
	pocket.collection(collection).remove().insert(collections[collection].documents)
});

To save the data from a pocket to a JSON file I would do the following:

var dataToSaveToFile = JSON.stringify(pocket.collections);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants