While building an offline app using YDN-DB wrapper for IndexedDB, I now need to query any object store for the highest Integer
primary key value in the store. By extension, I can then predict and use the next autoIncrement number, even before running the next INSERT
.
Presently I can think of the following approach:
db.values('store_name').done(function (recordset) {
//run through the key/value pairs in recordset until I arrive at the
highest value.
});
Or perhaps:
db.from('store_name').select('primary_key').order('primary_key', true).list(1)
.done(function(value) {
//use value for some task;
});
Is there a more efficient way?
And while we're on it, the "primary_key"
argument specified in .order('primary_key', true)
seems superfluous to me, since this ordering would normally be by primary key. If that is right, what's the syntax to skip it?
Thanks.
EDIT:
While waiting, I realized that, in fact, with the .order('primary_key', true)
, the second parameter of true
(==reverse) wast totally ignored by the query engine. But the below form came to the rescue:
db.from('store_name').select('primary_key').reverse().list(1)
.done(function(highest){
console.log(highest);
});
While this seems to be a little sleeker, I still crave an endorsement from the 'profs'.
Thank you.
This is very common operation. I will use
db.keys('store_name', null, 1, 0, true)
Using query module db.from
is wrapper to above method. Sorry for regression on order
ignoring direction. I will fix. Thanks for reporting.