Dexie.js icon indicating copy to clipboard operation
Dexie.js copied to clipboard

want to create add new table in existing dexie db

Open hiren-jarvisbitz opened this issue 2 years ago • 4 comments

Want to create a new table with the existing dexie index DB. But here don't want to delete my previous DB data or not to clear cache.

Currently created DB function as below.

export const productDb = () => {
  try {
    // create database
    db = new Dexie('optimizeDb');

    applyEncryptionMiddleware(db, new Uint8Array(INDEXED_DB_ENC_KEY.split(',')), {
      ...TABLES
    });

    db.version(1).stores(TABLES);
    // Open the database
    db.open().catch((e) => {
      console.error(`Open failed: ${e}`);
    }).then(() => {
      hospitalUpdate(db);
    return db;
  } catch (e) {
    console.log('DB created Error --> ', e);
    Bugsnag.notify(e);
  }
};

I have tried the below code to create a new table with the existing DB.:

export const productDb = () => {
  try {
    // create database
    db = new Dexie('optimizeDb');

    applyEncryptionMiddleware(db, new Uint8Array(INDEXED_DB_ENC_KEY.split(',')), {
      ...TABLES
    });

    db.version(1).stores(TABLES);
    // Open the database
    db.open().catch((e) => {
      console.error(`Open failed: ${e}`);
    }).then(() => {
      hospitalUpdate(db);
// Create new table
db.close();
      db.version(Math.round(db.verno + 2)).stores({ ComplianceApp: '++id, CaseScheduleID, OfficeName, RepCaseNo, UserName, ComplianceType, UserResponse, ResponseDate, Sync' });
      console.log('DB created--> ', db);
      db.open();
    });

    return db;
  } catch (e) {
    console.log('DB created Error --> ', e);
    Bugsnag.notify(e);
  }
};

hiren-jarvisbitz avatar Jun 27 '22 06:06 hiren-jarvisbitz

Don't know what hospitalUpdate() does but else than that, the principle is to create a new Dexie instance, open it using an incremented version and the new schema.

There is sample code for dynamic schema manipulation using a function changeSchema() in the docs of Dexie.open()#dynamic-schema-manipulation

dfahlander avatar Jun 27 '22 12:06 dfahlander

@dfahlander Thanks! Your answer helps me a lot. And I want to confirm a problem. I have my Electron desktop application which use Dexie db. Now I think I must upgrade db version to create a new table in my lastest version of application (e.g. db verison 10 to 20 dynamically) . So when I open my old application, my code must open the db at verison 20 dynamically to read data correctly, is it right?

Because I get an error like this before:

VersionError: The requested version (10) is less than the existing version (20).

My db is on version 20, but my code still tries to open it at version 10.

heylying avatar Jul 01 '22 06:07 heylying

@dfahlander Yes, I'm also getting the same error as well.

Open failed: VersionError: The requested version (20) is less than the existing version (30). VersionError: The requested version (20) is less than the existing version (30).

hiren-jarvisbitz avatar Jul 01 '22 09:07 hiren-jarvisbitz

You need to open the db without specifying a version.

https://dexie.org/docs/Dexie/Dexie.open()#sample-dynamic-mode

The only downside is that your tables need to be accessed via

db.table('myTableName')

instead of

db.myTableName

dfahlander avatar Jul 01 '22 12:07 dfahlander