indexedDB最高的关键字 [英] indexedDB highest keypath

查看:96
本文介绍了indexedDB最高的关键字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用Emberjs适配器填充的indexedDB内的数据库。我设置了这样的关键字:



this.addModel(App.Device,{keyPath:'key'});



当我第一次看到我的数据时,我的密钥自动增加:

  data = data.map(function(item,idx){
item.key = idx;
return item;
});

所有的数据都存储在indexedDB中,我想添加一些更多的数据,但是我需要以继续在我的数据中设置一个关键值。



如何获得最高的密钥(在我的情况下,它是一个唯一的号码(如id)),所以我可以增加我当前数据中的一个密钥添加到我的数据库?



以下是我要用于添加更多数据的代码示例:

  var request = indexedDB.open('products'); 
request.onsuccess = function(e){
console.log('Success!');
that.setProperties({db:e.target.result});
var db = e.target.result;
var reader = new window.FileReader();
reader.readAsDataURL(blob_);
var img64;
reader.onloadend = function(){
base64data = reader.result;
var dataImage = {
key:42,
url:url_,
base64:base64data
}
that.storage.saveProduct(db,dataImage) ;
}
};

request.onerror = function(e){
console.log('Error!');
console.dir(e);
};

[edit]我忘了把saveProduct fct:

  this.saveProduct = function(db,data){
var transaction = db.transaction([App.Device],readwrite);
var store = transaction.objectStore(App.Device);

var request = store.add(data);

request.onerror = function(e){
console.log(Error,e.target.error.name);
//某些类型的错误处理程序
};

request.onsuccess = function(e){
console.log(设备保存在db);
};
};


解决方案

以下是最后一个条目的IDB模式: p>


  1. 将您的游标值绑定到第一个ID条目。 (0应该这样做,因为键是按字典排序的)

  2. 使用反向光标( prev prevunique 取决于您的需要)

  3. 您的下一个条目将是最后一个条目( skip() continue( )应该工作)


I have a database inside indexedDB filled using an Emberjs adapter. I have set the keypath like this :

this.addModel(App.Device, { keyPath: 'key' });

And my key is autoincremented when I parse my data the first time I got them like this :

data = data.map(function( item, idx ) {
                item.key = idx;
                return item;
            });

After all of the data are store inside indexedDB, I want to add some more data, but I need to get to continue to set a key value inside my data.

How can I get the highest key (in my case it's a unique number (like an id)), so I can increment the one the key in my current data to add to my database ?

Here is the sample of my code that I want to use to add more data :

var request = indexedDB.open( 'products' );
            request.onsuccess = function( e ) {
                console.log('Success!');
                that.setProperties({ db: e.target.result });
                var db = e.target.result;
                var reader = new window.FileReader();
                reader.readAsDataURL(blob_);
                var img64;
                reader.onloadend = function() {
                    base64data = reader.result;
                    var dataImage = {
                        key: 42,
                        url: url_,
                        base64: base64data
                    }
                    that.storage.saveProduct( db, dataImage );
                }
            };

            request.onerror = function( e ) {
                console.log('Error!');
                console.dir( e );
            };

[edit] I forgot to put the saveProduct fct :

this.saveProduct = function( db, data ) {
        var transaction = db.transaction( ["App.Device"], "readwrite" );
        var store = transaction.objectStore( "App.Device" );

        var request = store.add( data );

        request.onerror = function( e ) {
            console.log("Error",e.target.error.name);
            //some type of error handler
        };

        request.onsuccess = function( e ) {
            console.log("Device saved in db");
        };
    };

解决方案

Here's an IDB pattern for the last entry:

  1. Bind your cursor value to the first ID entry. ("0" should do it since keys are lexicographically ordered)
  2. Use a reverse cursor ("prev" or "prevunique" direction depending on your needs)
  3. Your next entry will be the last entry (skip() or continue() either should work)

这篇关于indexedDB最高的关键字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆