0

I am trying to build a function that retrieves the next element in a store after the passing keypath. My getItem looks something like that.

req = smartpigsdb.get(store, keypath);
req.done(function(record) { 
    if(record!==undefined || typeof record=='object'){
    // do something with the record
}
else console.log('error getItem: ' + e);
});
req.fail(function(e) {
    console.log(e);
});

How can I achieve that using YDN-DB?

bboydflo
  • 907
  • 1
  • 15
  • 24

2 Answers2

1

If you don't know the key, you cannot use get. You have to use values, for iterating record. Limit to 1 so that you get only one result. You can use lowerBound keyRange of your know key, so that you will get next object after the key.

Kyaw Tun
  • 12,447
  • 10
  • 56
  • 83
  • I am almost there. I created a ValueIterator: var iter = new ydn.db.ValueIterator(store, '>', idx); to get the next item in the db I do: db.values(iter, 1).done(function(item) { console.log('JSON.stringify(item));}); but my keypath could be like "1", "3", "3a", "3b",...,"10", "11", "11a"... etc. I want to retrieve the item after "1" which is the item keypath = "2" but I get the one with keypath = "10". How can I retrieve the desired item? In my case "2" Thanks for help. Florin – bboydflo Aug 29 '14 at 08:24
  • 1
    Indexeddb order keys according to ICU collation, so "10" is before "2". you may want to use number type key instead. – Kyaw Tun Aug 29 '14 at 09:21
  • BTW, you mean `key`, not `keyPath`. These two are different. – Kyaw Tun Aug 29 '14 at 09:24
0

In the end I did something like this, it works but I am not sure is the best way to do it. 'idx' is an index for store object store.

var range = new ydn.db.KeyRange.lowerBound(index+1);
var iter = new ydn.db.Iterator(store, 'idx', range); // 'idx' is an index for store object store
db.values(iter, 1).done(function(item) {
    if(item.length > 0){
        var req = db.get(store, item[0]);
        req.done(function(record) {
            if(record!==undefined || typeof record=='object'){
                // do soemthing with the record 
            }
            else {
                console.log('record could not be found');
            }
        });
        req.fail(function(e) {
            console.log(e);
        });
    }
});

I tried the same thing with range = new ydn.db.KeyRange.upperBound(index); if I want to retrieve a previous record, but I always get the first record from the store, so I used instead range = new ydn.db.KeyRange.lowerBound(index-1);

Please give me some feedback. Thanks again for the library. Regards, Florin.

bboydflo
  • 907
  • 1
  • 15
  • 24
  • Have your try `db.from('store').where('idx', '>', index).get().done(...` ? – Kyaw Tun Aug 29 '14 at 13:31
  • yes, I just tried. but instead of .get() from above I used list(1) like this: smartpigsdb.from(store).where('idx', '>', index).list(1).done(function(item){ if(item!==null || item!==undefined) doSomething(item[0]); else console.log('no item found'); }); When I try to use the same syntax to get the previous item, it jumps directly to the first item in the database. I am not sure why. – bboydflo Sep 02 '14 at 06:22