Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allows multiple object stores per database #69

Closed
wants to merge 5 commits into from

Conversation

goofballLogic
Copy link

For #31

If an object store is missing from a database, re-opens the database as the next version in order to allow upgrade to add the missing object store.

@goofballLogic
Copy link
Author

goofballLogic commented Jun 29, 2019

Ok, this now contains a fix for IE11. Because IE11 doesn't allow multiple connections to the same database, the script needs to explicitly close the first open connection before reopening to upgrade.

IE11 test script:

<!DOCTYPE html>
<html>
    <head>

        <script src="//cdn.jsdelivr.net/npm/promise-polyfill@8/dist/polyfill.min.js"></script>
        <script>window.exports = {};</script>
        <script src="/idb-keyval-cjs-compat.min.js"></script>

    </head>
    <body>

        <div id="main"></div>
        <script>

            var Store = window.exports.MultiStore;
            var set = window.exports.set;
            var get = window.exports.get;

            var databaseName = "__test_multistore";
            //indexedDB.deleteDatabase( databaseName );

            var storeName = "__test_" + Date.now();
            console.log( "Test object store:", storeName, "( in database", databaseName, ")" );
            var store = new Store( databaseName, storeName );
            var db, stores = [];

            store._dbp.then( function( promised ) {

                db = promised;
                console.log( "Database version", db.version );
                for(var i = 0; i < db.objectStoreNames.length; i++ ) stores.push( db.objectStoreNames[ i ] );
                console.log( "Object stores", stores );

            } ).then( function() {

                console.log( "Set and get values from store" );
                return set( "thanks", "fish", store );

            } ).then( function() {

                return get( "thanks", store );

            } ).then( function( actual ) {

                var expected = "fish";
                console.log( "Expected", expected );
                console.log( "Actual", actual );
                if ( actual !== expected ) throw new Error( "Failed to set and retrieve value" );

            } ).then( function() {

                if ( stores.length === 0 )
                    throw new Error( "No store was created!" );
                else if ( stores.length === 1 )
                    console.warn( "This test is not passed until you run it at least twice (by reloading)" );
                else
                    console.log( "SUCCESS - the database was accessed with new object stores" );

            } );


        </script>

    </body>

</html>

@goofballLogic
Copy link
Author

@jakearchibald N.B. this fix does not change the fact that with IE11 you'll have to close the connection to one object store before opening a connection to another within the same database, but I think these two issues should be handled separately.

@goofballLogic
Copy link
Author

Updated IE11 test above to use MultiStore instead of Store

@DonGissel
Copy link

I'm subscribing to this, because this feature would be so very nice to have. :)

@goofballLogic
Copy link
Author

@DonGissel, I ended up rewriting the same API as jake has here: https://github.com/goofballLogic/idb-lite if it's of any use

@goofballLogic
Copy link
Author

Giving up on this being updated.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants