Lawnchair-IndexedDB doesn't support multiple records

我与影子孤独终老i 提交于 2020-01-15 05:16:06

问题


I tried creating multiple records in indexed-db but it won't allow me, example, nike and 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);
    });
});

I think this is how to create multiple records in indexed-db. It actually happens on request.onupgradeneeded. See code below.

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

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

If I can't create an adidas record this is actually the error that is thrown when accessing it.

[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" ...]

回答1:


Lawnchair is designed for schemaless use case. Use separate database for each Lawnchair instance.

If you really need multiple tables in an database, you other libraries, like my own, ydn-db.




回答2:


Found a way to fix it. I've added a patch by adding a records property on option object. See {adapter..., records:[...]} below.

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

See my pull request here: https://github.com/brianleroux/lawnchair/pull/175



来源:https://stackoverflow.com/questions/17948230/lawnchair-indexeddb-doesnt-support-multiple-records

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!