【问题标题】:How to migrate existing Dexie database to new Dexie database or How to rename Dexie database?如何将现有的 Dexie 数据库迁移到新的 Dexie 数据库或如何重命名 Dexie 数据库?
【发布时间】:2018-08-31 13:44:09
【问题描述】:

我有一个使用 Dexie 包装器进行 indexedDB 的 Web 应用程序,由于某种原因,我需要重命名现有数据库而不会出现故障,我在 Dexie 文档中找不到重命名。

【问题讨论】:

    标签: javascript html database migration dexie


    【解决方案1】:

    不支持重命名数据库既不是 Dexie 也不是原生 indexedDB。但是您可以使用以下代码(未经测试)克隆数据库:

    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();
        });
      });
    }
    

    【讨论】:

    • 你好大卫,谢谢你的好例子,我希望没关系我改变它并使用它here。您的示例不起作用,因为需要对键路径数组进行字符串化,请添加 toString() ,就像我在方案部分的示例中一样。如果您对改进我的示例有任何建议,也欢迎您。问候
    猜你喜欢
    • 2017-11-17
    • 2018-02-03
    • 2020-02-10
    • 2017-10-19
    • 2021-05-20
    • 2017-07-17
    • 2016-06-23
    • 2018-05-07
    • 2017-03-15
    相关资源
    最近更新 更多