Page 1 of 1

How do delete rows automatically

Posted: Tue Sep 09, 2014 10:42 am
by Eric Leroyer

I'm trying to delete rows in one of my collections in the database, I have setup a input test box where I load all of the _ID's first then bind them and store then in localstorage. Then run delete service, my issue is that when I click a button kick of the service it does nothing. I have read various article's that say you cannot delete rows via the REST service.

http://devcenter.appery.io/tutorials/...

https://getsatisfaction.com/apperyio/...

Is there a way to delete rows in collections every hour perhaps using Server Code to delete all rows in table or is there another way.

Image Image Image


How do delete rows automatically

Posted: Tue Sep 09, 2014 2:13 pm
by Kateryna Grynko

Hi Eric,

As we can see in your screenshots you do not save all _id to localStorage.

We'd recommend that you use server code in order to reduce traffic:
http://devcenter.appery.io/documentat...

You could also clear records each hour:
http://devcenter.appery.io/documentat...


How do delete rows automatically

Posted: Wed Sep 10, 2014 3:20 am
by Eric Leroyer

Thanks for the reply,

I mapped the list of _id to a localstoragevariable called rowstodelete and set this to run on page load which it did.

I then mapped the local storage item rowstodelete to the delete_service and invoked this service on button click.

It is working but not for all rows, it is only removing 1 row for each button click?


How do delete rows automatically

Posted: Wed Sep 10, 2014 11:20 pm
by Yurii Orishchuk

Hi Eric,

Appery.io DB API can delete only one item per request.

Thus you have two ways to do it:

1 Create server script in which you can pass multiple id's and delete them in this server script.

pre

Links:
1.1. Tutorial: http://devcenter.appery.io/tutorials/creating-app-logic-with-server-code/
1.2 Work with db from server script: http://devcenter.appery.io/documentation/backendservices/server-code/#Database

/pre

2 Iterate through your items and invoke delete service for each of them.

pre

2.1 save all items to "listItems" lsv. http://prntscr.com/4lnc5m/direct
2.2 On "Kick" button click you need to invoke following code:

pre

var listItems = JSON.parse(localStorage.getItem("listItems"));

//Iterate through saved items.
for(var i = 0; i < listItems&#46;length; i++){
&#47;&#47;Delete current item&#46;
&#47;&#47;Where "deleteServiceDatasource" is delete service datasource name&#46;
deleteServiceDatasource&#46;execute({data: {"id": listItems&#46;id} });
};

/pre

/pre

That's all.

Regards.


How do delete rows automatically

Posted: Thu Sep 11, 2014 10:29 am
by Eric Leroyer

Hi Yurri,

I have tried to create a server script but get the following error:

Script getradarrowsanddelete: Error: java.lang.ClassCastException@3caa84d9 ( @ 73 : 18 ) - throw e;

Image

Option 2 that you provided above worked perfectly thankyou for that. But if I can get it working on the server it would be more ideal as I plan on setting the script to run on schedule i.e every hour.


How do delete rows automatically

Posted: Fri Sep 12, 2014 12:32 am
by Yurii Orishchuk

Hi Eric,

You have following wrong line of code(see on screen shot): http://prntscr.com/4lyxlq/direct

So here is worked code(to delete all items in collection from which you can start):

pre

var dbId = "52fd3d06e4b0a25c11cxxx17";
var collectionName = "tempCollection";
var query = [] ;

try
{

var params = {};

params&#46;criteria =
{
&#47;&#47;here you can add query paremeters&#46;
};

queryResult = Collection&#46;query(dbId, collectionName, params);

for(var i = 0; i < queryResult&#46;length; i++){
Collection&#46;deleteObject(dbId, collectionName, queryResult&#46;_id);
queryResult
};

response&#46;success({message: (queryResult&#46;length + " items deleted")});
}
catch (e)
{
response&#46;success("message: " + e&#46;message + "ncode: " + e&#46;code);
}

/pre

Regards.