ujjwalguptaofficial / JsStore

Simplifying IndexedDB with SQL like syntax and promises
http://jsstore.net/
MIT License
858 stars 110 forks source link

Adding a table to existing database doesn't seem to work #67

Closed edvordo closed 6 years ago

edvordo commented 6 years ago

Originally I had this database definition

    const QOL_DB_NAME        = 'RQDB';
    const TRACKER_TBL_NAME   = 'tracker';
     const TRACKER_DB_SCHEMA = {
        name  : QOL_DB_NAME,
        tables: [
             {
                name   : TRACKER_TBL_NAME,
                columns: [
                    new JsStore.Column('id').options([JsStore.COL_OPTION.PrimaryKey, JsStore.COL_OPTION.AutoIncrement]).setDataType(JsStore.DATA_TYPE.Number),
                    new JsStore.Column('ts').setDataType(JsStore.DATA_TYPE.String),
                    new JsStore.Column('d').setDataType(JsStore.DATA_TYPE.String),
                    new JsStore.Column('v').setDataType(JsStore.DATA_TYPE.Number),
                    new JsStore.Column('g').setDataType(JsStore.DATA_TYPE.Number).setDefault(0).disableSearch(),
                    new JsStore.Column('t').setDataType(JsStore.DATA_TYPE.String),
                ]
            },
        ]
    };

The database and table were created successfully and everything worked fine.

But then I had the need to add a table to the database. I changed the design to this


    const QOL_DB_NAME        = 'RQDB';
    const TRACKER_TBL_NAME   = 'tracker';
    const AVGDMGSTR_TBL_NAME = 'average_damage_to_strength';

    const TRACKER_DB_SCHEMA = {
       name  : QOL_DB_NAME,
       tables: [
           {
                name   : TRACKER_TBL_NAME,
                columns: [
                    new JsStore.Column('id').options([JsStore.COL_OPTION.PrimaryKey, JsStore.COL_OPTION.AutoIncrement]).setDataType(JsStore.DATA_TYPE.Number),
                    new JsStore.Column('ts').setDataType(JsStore.DATA_TYPE.String),
                    new JsStore.Column('d').setDataType(JsStore.DATA_TYPE.String),
                    new JsStore.Column('v').setDataType(JsStore.DATA_TYPE.Number),
                    new JsStore.Column('g').setDataType(JsStore.DATA_TYPE.Number).setDefault(0).disableSearch(),
                    new JsStore.Column('t').setDataType(JsStore.DATA_TYPE.String),
                ]
           },
           {
                name   : AVGDMGSTR_TBL_NAME,
                columns: [
                    new JsStore.Column('id').options([JsStore.COL_OPTION.PrimaryKey, JsStore.COL_OPTION.AutoIncrement]).setDataType(JsStore.DATA_TYPE.Number),
                    new JsStore.Column('ts').setDataType(JsStore.DATA_TYPE.String),
                    new JsStore.Column('s').setDataType(JsStore.DATA_TYPE.Number).disableSearch(),
                    new JsStore.Column('a').setDataType(JsStore.DATA_TYPE.Number).disableSearch(),
                    new JsStore.Column('d').setDataType(JsStore.DATA_TYPE.Number).disableSearch(),
                    new JsStore.Column('t').setDataType(JsStore.DATA_TYPE.String),
                ]
            }
        ]
    };

and as per-manual I attempted to change the database via

    VARIABLES.jsstore.tracker.db.isDbExist({
        dbName: QOL_DB_NAME,
        table : {
            name: AVGDMGSTR_TBL_NAME,
            version: 1
        }
    }).then(exists => {
        console.log(exists);
        if (exists) {
            VARIABLES.jsstore.tracker.db.openDb(QOL_DB_NAME).then(() => {
                log('opened IDB');
                fn.__.getLatestTrackerValues();
            }).catch(e => console.error(e));
        } else {
            VARIABLES.jsstore.tracker.db.createDb(TRACKER_DB_SCHEMA).then(() => {
                log('updated db chema');
                fn.__.getLatestTrackerValues();
            }).catch(e => console.error(e));
        }
    });

the connection is initialized sooner in code as VARIABLES.jsstore.tracker.db = new JsStore.Instance()

However this seems to not have worked. The table was added to KeyStore->LocalStore->[JsStore_RQDB_Schema] but the table itself was not created under RQDB database.

Have I missed something? Should I have attempted the table addition in a different way?

Edit: On first refresh the console said updated db chema and on second refresh opened IDB

Edit2: created JSFiddle, comment the intiFirst and comment out the addTable function in the setTimetout callback

ujjwalguptaofficial commented 6 years ago

could you try with version : 2.

const TRACKER_DB_SCHEMA = {
       name  : QOL_DB_NAME,
       tables: [
           {
                name   : TRACKER_TBL_NAME,
                columns: [
                    new JsStore.Column('id').options([JsStore.COL_OPTION.PrimaryKey, JsStore.COL_OPTION.AutoIncrement]).setDataType(JsStore.DATA_TYPE.Number),
                    new JsStore.Column('ts').setDataType(JsStore.DATA_TYPE.String),
                    new JsStore.Column('d').setDataType(JsStore.DATA_TYPE.String),
                    new JsStore.Column('v').setDataType(JsStore.DATA_TYPE.Number),
                    new JsStore.Column('g').setDataType(JsStore.DATA_TYPE.Number).setDefault(0).disableSearch(),
                    new JsStore.Column('t').setDataType(JsStore.DATA_TYPE.String),
                ]
           },
           {
                name   : AVGDMGSTR_TBL_NAME,
                version: 2,
                columns: [
                    new JsStore.Column('id').options([JsStore.COL_OPTION.PrimaryKey, JsStore.COL_OPTION.AutoIncrement]).setDataType(JsStore.DATA_TYPE.Number),
                    new JsStore.Column('ts').setDataType(JsStore.DATA_TYPE.String),
                    new JsStore.Column('s').setDataType(JsStore.DATA_TYPE.Number).disableSearch(),
                    new JsStore.Column('a').setDataType(JsStore.DATA_TYPE.Number).disableSearch(),
                    new JsStore.Column('d').setDataType(JsStore.DATA_TYPE.Number).disableSearch(),
                    new JsStore.Column('t').setDataType(JsStore.DATA_TYPE.String),
                ]
            }
        ]
    };

VARIABLES.jsstore.tracker.db.isDbExist({
        dbName: QOL_DB_NAME,
        table : {
            name: AVGDMGSTR_TBL_NAME,
            version: 2
        }
    }).then(exists => {
        console.log(exists);
        if (exists) {
            VARIABLES.jsstore.tracker.db.openDb(QOL_DB_NAME).then(() => {
                log('opened IDB');
                fn.__.getLatestTrackerValues();
            }).catch(e => console.error(e));
        } else {
            VARIABLES.jsstore.tracker.db.createDb(TRACKER_DB_SCHEMA).then(() => {
                log('updated db chema');
                fn.__.getLatestTrackerValues();
            }).catch(e => console.error(e));
        }
    });

Actually be default db version is 1, and when you specify 1 again for a table which does not exist - it tells you that it does not exist which is ok .

JsStore calculate the db version from supplied db schema and it find that db version is 1, but indexeddb version is already 1 and specifying 1 again wont fire the create db. In order to update the db schema, version should be greater than current version.

Hope, I am clear. Let me know.

edvordo commented 6 years ago

With setting the table and check to version 2 it worked. I don't like it, but I get the logic once I saw what happened in the browsers' inspector. Oh well, I guess I'll have to be fine with that :D

Thanks for your help :)