Remzo
Remzo

Reputation: 103

parse is still alive, but how to query by 'index'

I found that Parse is now supported by the Buddy platform, but all the forum/help files from the old Parse website are gone...

What i would like to do is query for one row in a table by row number/index.

I now do this, but it seems inefficient to me to get all data and then select the row (although it works fine):

var thisRow = Parse.Object.extend(GlobTable);
var query= new Parse.Query(thisRow);
query.descending('updatedAt');
query.find({
  success: function(results) {

  var object = results[RowNumberThatINeed];
  //etc using object

@Ran, thanks! Somehow it doesn't work, Parse will store '1' for every row: 1,1,1,1. But it saves all other data ok.

var Object = Parse.Object.extend(LocTable);
var obj = new Object();

    obj.set("User", LocUser);
    obj.set("Happened", d);
    obj.set("Happy", b);
    obj.set("Learned",a);
    obj.set("ConnectedTo",c);
    obj.increment("rowIndex");
    obj.save({
success: function(obj) {

 updateDatabase();
 alert("Your data is saved");

},
error: function(obj, err) {
alert("Your data was not saved. Sorry..." + err);
}
});

Upvotes: 0

Views: 188

Answers (1)

Ran Hassid
Ran Hassid

Reputation: 2788

you solution will work but you will experience a lot performance issues when your table will growth.

What i suggest to you is the following solution:

  1. Add another field to your GlobTable of type integer. This field will be incremental field and will store your row index (let's call this field rowIndex)
  2. Each time you save a new object to this table make sure you increment this field by using the following command:

object.increment("rowIndex");

  1. Now your query should look like the following:

var thisRow = Parse.Object.extend(GlobTable);
var query= new Parse.Query(thisRow);
query.equalTo("rowIndex",RowNumberThatINeed);
query.first().then(function(result){
  // do something with the result
},function(error){
  // error handling
});

If you want event better performance you can create index (in MongoDb) on your rowIndex.

In my code snippets i use Promises according to the best practices.

I use first and not find because there will always be one object for this index

Upvotes: 1

Related Questions