How to migrate existing Dexie database to new Dexie database or How to rename Dexie database?

五迷三道 提交于 2019-12-11 15:13:47

问题


I have web application which uses Dexie wrapper for indexedDB, for some reason i need to rename existing Database with no glitch, i couldn't find renaming on Dexie documentation.


回答1:


There's no support to rename a database is neither Dexie or native indexedDB. But you can clone a database using the following piece of code (not tested):

function cloneDatabase (sourceName, destinationName) {
  //
  // Open source database
  //
  const origDb = new Dexie(sourceName);
  return origDb.open().then(()=> {
    // Create the destination database
    const destDb = new Dexie(destinationName);

    //
    // Clone Schema
    //
    const schema = origDb.tables.reduce((result,table)=>{
      result[table.name] = [table.schema.primKey]
        .concat(table.schema.indexes)
        .map(indexSpec => indexSpec.src);
      return result;
    }, {});
    destDb.version(origDb.verno).stores(schema);

    //
    // Clone Data
    //
    return origDb.tables.reduce(

      (prev, table) => prev
        .then(() => table.toArray())
        .then(rows => destDb.table(table.name).bulkAdd(rows)),

      Promise.resolve()

    ).then(()=>{
      //
      // Finally close the databases
      //
      origDb.close();
      destDb.close();
    });
  });
}


来源:https://stackoverflow.com/questions/49423263/how-to-migrate-existing-dexie-database-to-new-dexie-database-or-how-to-rename-de

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!