Lawnchair-IndexedDB doesn't support multiple records

1.1k Views Asked by At

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

There are 2 best solutions below

6
On BEST ANSWER

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.

0
On

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