0
votes

I am working on IBM Worklight and have question about JSONStore. How can I write a function that remove all documents in a JSONStore collection keeping the reference of the collection?

In other words I want to remove the documents without removing the collection. I can't use removeCollection() in my application because I can't quit the application and call wlCommonInit() again (that calls get and init on JSONStore).

Thanks so much for your help Andrea

2

2 Answers

1
votes

At the moment there is no API to easily achieve this. Your options are:

1.Call remove collection then init for the specific collection you want to clear and re-use. No need to call wlCommonInit again. Some pseudocode:

var collections = {
  people : {...},
  orders: {...},
  greetings: {...}
};

var options = {...};

WL.JSONStore.get('greetings').removeCollection()
.then(function () {
  return WL.JSONStore.init({greetings: collections.greetings}, options);
})

.then(function () {
  //re-use the collection here
});

2.Use the find API to locate documents and the remove API to remove them. There's an example here.

You can open a feature request here.

0
votes

assuming access is an accessor to your collection, you can do this :

access.findAll()
    .then(function(result){ 
    if(result.length>0)
    {
        access.remove(result,{push:false})
    }
    })
    .fail(function(error_msg){
    alert(error_msg);
    });

but keep in mind this will not reset the ids (silly jsonstore !), so they will get shifted by the length of the collection every time you do that.

P.S.: From my experience, the removeCollection API should be avoided in case of encrypted collections because of the time it takes to init an encrypted collection on a low-performance mobile device...