NeDB将其文件存储在Electron应用程序中的什么位置?

时间:2019-12-28 04:10:32

标签: node.js electron nedb

我有一个Electron应用程序,我选择了NeDB来存储其数据。由于我不愿讨论的原因,我需要访问NeDB数据文件。我的问题是,即使我提供了文件名和位置,也不会创建数据文件。我什至在整个硬盘驱动器中搜索该文件,但找不到。但是,即使我停止了该应用程序并再次启动它,我的代码也可以毫无问题地访问存储在数据库中的数据。 (并且我不是在谈论我在下面的代码中创建的默认条目,但是重新启动应用程序后会显示添加的条目,而在重新启动应用程序后删除的条目将保持删除状态。此行为与数据库保存在磁盘上,而不是内存数据库中。)

这是相关的代码。请注意,它正在渲染过程中运行。我目前仅在Windows 10上运行此应用程序。

import Datastore from 'nedb';
import path from 'path';
import { remote } from 'electron';

const fs = require('fs-extra');


const basePath = path.join(remote.app.getPath('userData'), 'MyApp');
const dbFile = path.join(basePath, 'label.db');

// the following line prints:
// C:\Users\<my account name>\AppData\Roaming\Electron\MyApp
console.log(`dbFile = ${dbFile}`);

// The entire database will eventually have more than one Datastore
const db = {
  labels: new Datastore({
    autoload: true,
    filename: dbFile,
  }),
};

// create database content if it does not exist
db.labels.count({}, (err, count) => {
  if (!err && !count) {
    db.labels.insert([
      { name: 'Insurance', canDelete: true },
      { name: 'Work', canDelete: true },
      { name: 'Social media', canDelete: true },
      { name: 'All', canDelete: false },
    ]);

    // The NeDB docs state the compactDatafile will also save it to the disk.
    // It does not, at least not to the expected location
    db.labels.persistence.compactDatafile();
  }
});

// the following prints:
// database file exists: false
console.log(`database file exists: ${fs.pathExistsSync(dbFile)}`);

最重要的是,我无法在硬盘上的任何位置找到数据库文件('label.db')的实际位置,并且肯定不在预期的位置('C:\ Users \\ AppData \ Roaming \ Electron \ MyApp')。我想念什么?数据库文件存储在哪里?

最后一点说明:我发现了一个类似的问题:Where is NEDB file stored?。答案是由于使用了webpack开发服务器,文件被保存在内存中。如果是这样,我有问题。我也正在使用开发服务器,但是在开发过程中我仍然需要能够看到文件。仅在生产中显示的文件不够好。 (我没有尝试在生产模式下运行代码,以查看数据文件是否出现,因此我无法确认这确实是真的。)

0 个答案:

没有答案