dexie / Dexie.js

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

Renaming tables #1853

Open Stretsh opened 9 months ago

Stretsh commented 9 months ago

Prior to IndexedDB 2.0 (2018), the process of renaming an object store involved creating a new store, copying the data from the old store and then deleting the old store. From what I could see, it seems like that is also the process used in Dexie.

Starting from 2.0, the IDBObjectStore interface included a setter for the name of the object store, making is super easy to change the store name:

return new Promise((resolve, reject) => {
    const request = indexedDB.open(db.name, db.version + 1)
    request.onupgradeneeded = event => {
      const objectStore = event.target.transaction.objectStore(storeName)
      objectStore.name = newName
    }
    request.onerror = event => reject(event.target.error)
    request.onsuccess = event => resolve(event.target.result)
  })

I tried to see if it would be possible with Dexie as is, but the table name is used in a number of places. I was thinking of attempting to work towards a PR, but I am too new to Dexie to jump in. I'm not enough aware of what might break or what is involved.

I believe this would be a welcome change.

Tagging #713 in here, which seems to be the basis for the tests in the code.

dfahlander commented 9 months ago

The question is how the dexie syntax would be to rename a table. Any suggestion is welcome.

One suggestion would be:

db.version(2).rename({friends: "friends2"});

Or together with the full history and other changes:


db.version(1).stores({
  friends: '++i, name, age'
});

db.version(2).rename({
  friends: "friends2"
}).stores({
  friends2: '++id, name, age, someOtherIndex'
});

Feedback?

Stretsh commented 9 months ago

That looks great!

To add another suggestion (not necessarily preferred):

db.version(1).stores({
  friends: '++id, name, age'
});

db.version(2).stores({
  'friends=>friends2': '++id, name, age, someOtherIndex'
});

then add a check for => when processing the schema. If present, rename friends to friends2. Feels more in line with the design of the schema definition in stores().

Either looks fine to me, it will depend on what's easier to implement, with the least issues. Which is the part where I'm not able to help much with at the moment.

genezys commented 1 month ago

I like the .rename syntax better as it allows to support many renames in the history of the schema:

db.version(3).rename({
  friends: "friends3",
  friends2: "friends3",
}).stores({
  friends3: '++id, name, age, someOtherIndex'
});

This syntax allows to migrate the name from v1 to v3 directly.

dfahlander commented 1 month ago

Todo: update road map https://dexie.org/roadmap/dexie5.0#new-migration-methods-for-rename.