如何将现有的Dexie数据库迁移到新的Dexie数据库或如何重命名Dexie数据库? [英] How to migrate existing Dexie database to new Dexie database or How to rename Dexie database?

查看:343
本文介绍了如何将现有的Dexie数据库迁移到新的Dexie数据库或如何重命名Dexie数据库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Web应用程序,该Web应用程序将Dexie包装器用于indexedDB,由于某种原因,我需要重命名现有数据库,而不会出现故障,我无法在Dexie文档上找到重命名.

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.

推荐答案

不支持使用Dexie或本机indexedDB重命名数据库.但是您可以使用以下代码(未经测试)克隆数据库:

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

这篇关于如何将现有的Dexie数据库迁移到新的Dexie数据库或如何重命名Dexie数据库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆