dexie / Dexie.js

A Minimalistic Wrapper for IndexedDB
https://dexie.org
Apache License 2.0
11.5k stars 642 forks source link

want to create add new table in existing dexie db #1588

Open hiren-jarvisbitz opened 2 years ago

hiren-jarvisbitz commented 2 years ago

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);
  }
};
dfahlander commented 2 years ago

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](https://dexie.org/docs/Dexie/Dexie.open()#dynamic-schema-manipulation)

heylying commented 2 years ago

@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.

hiren-jarvisbitz commented 2 years ago

@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).

dfahlander commented 2 years ago

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