In order to change your table schema, you need to add an extra property 'version' with a value greater than current Db Version.


Change the version in your table schema

var table1 = {
    name: "table_name",
     columns: {
        column1: { dataType: 'datatype', primaryKey: true },
        column2 : { dataType: 'datatype'},
        ..... ,
        columnN: { dataType: 'datatype' }
    version: 2 //Default version is 1.

How to get current db version

You can get Db version by using below code.

connection.getDbVersion(db_name).then(function(version) {

The above code is for only development purpose. Dont execute this code to increase Db Version. You will have to specify the value manually.

or you can also find your current db version in indexedDb section of development tools.

What is the need of db version

IndexedDb is a database technology for browser which means if you do some changes in your web application , any one who use your web app should get latest changes including database changes.

Browser decides to change db schema when indexedb is initiated with db version greater than current db version.

What happens to data when schema is changed

All table is recreated when there is database schema change but JsStore deletes only those tables which version is changed.

So table which does not have schema changes will have no effect but table which have schema changes - means table will be recreated and all data inside it will be lost.

How do i preserve my data for table which has schema changes

Before calling initDb api with new db schema changes, select all data from a table and then insert it after the connection is initiated.

e.g -

async function changeDbSchema() {
    var allData = await{

    var isDbCreated = await connection.initDb(newDbSchema);
        await connection.insert({

// should be called after connection is initiated with old schema