Lawnchair-IndexedDB不支持多条记录

时间:2013-07-30 13:21:23

标签: javascript indexeddb lawnchair

我尝试在indexed-db中创建多个记录,但它不允许我,例如,nike和adidas objectStores

var nike = Lawnchair({adapter:'indexed-db', name:'stores', record:'nike'},function(e){
    console.log("nike store open");
    this.save({id:1}, function(data){
        console.log('nike data: ', data);
    });
});

var adidas = Lawnchair({adapter:'indexed-db', name:'stores', record:'adidas'},function(e){
    console.log("adidas store open");
    this.save({id:1}, function(data){
        console.log('adidas data: ', data);
    });
});

我认为这是如何在indexed-db中创建多个记录。它实际上发生在request.onupgradeneeded上。请参阅下面的代码。

// Handle datastore upgrades.
request.onupgradeneeded = function(e) {
    var db = e.target.result;

    var nike = db.createObjectStore('nike');
    var adidas = db.createObjectStore('adidas');
};

如果我无法创建adidas记录,这实际上是访问它时引发的错误。

[Exception... "The operation failed because the requested database object could not be found. For example, an object store did not exist but was being opened." code: "8" ...]

2 个答案:

答案 0 :(得分:1)

Lawnchair专为无模式用例而设计。为每个Lawnchair实例使用单独的数据库。

如果你真的需要数据库中的多个表,你可以使用其他库,比如我自己的,ydn-db。

答案 1 :(得分:1)

找到了解决问题的方法。我通过在选项对象上添加records属性添加了补丁。请参阅下面的{adapter ...,records:[...]}。

<script>

    var nike = Lawnchair({adapter:'indexed-db', name:'stores', record:'nike', records:['nike','adidas']},function(e){
        console.log("nike store open", this);
        this.save({id:1}, function(data){
            console.log('nike data: ', data);
        });
    });


    var adidas = Lawnchair({adapter:'indexed-db', name:'stores', record:'adidas', records:['nike','adidas']},function(e){
        console.log("adidas store open");
        this.save({id:1}, function(data){
            console.log('adidas data: ', data);
        });
    });

</script>

请在此处查看我的拉取请求:https://github.com/brianleroux/lawnchair/pull/175