indexedDB 最高键路径 [英] indexedDB highest keypath

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

问题描述

我在 indexedDB 中有一个使用 Emberjs 适配器填充的数据库.我已经设置了这样的键路径:

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;
});

在所有数据都存储在 indexedDB 中之后,我想添加更多数据,但我需要继续在我的数据中设置一个键值.

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.

如何获得最高的键(在我的情况下它是一个唯一的数字(如 ID)),以便我可以增加当前数据中的键以添加到我的数据库中?

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] 我忘了把 saveProduct fct:

[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");
    };
};

推荐答案

这是最后一个条目的 IDB 模式:

Here's an IDB pattern for the last entry:

  1. 将光标值绑定到第一个 ID 条目.("0" 应该这样做,因为键是按字典顺序排列的.)(原始答案链接到问题 https://stackoverflow.com/a/22812410/317937 现已删除)
  2. 使用反向光标(根据您的需要prev"或prevunique"方向)
  3. 您的下一个条目将是最后一个条目(skip()continue() 都可以)
  1. Bind your cursor value to the first ID entry. ("0" should do it since keys are lexicographically ordered.) (Original answer linked to question https://stackoverflow.com/a/22812410/317937 which has now been removed)
  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天全站免登陆