Search code examples
javascriptindexeddbydn-db

How to get the next element after keypath using YDN-DB


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?


Solution

  • 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.