I have an Electron app and I chose NeDB to store its data. I need to have access to the NeDB data files for reasons I am not willing to discuss. The problem I have is that even though I provide a file name and location, the data file does not get created. I even searched my entire harddrive for the file and could not find it. However, my code accesses the data stored in the database with no problem, even if I stop the app and start it again. (And I am not talking about the default entries that I create in my code below, but entries that I add show up after I restart the app, and entries that I remove stay removed after I restart the app. This is behavior consistent with a database saved on disk rather than an in-memory database.)
Here is the relevant code. Please note that this is running in the render process. I am running this app on Windows 10 only (for now).
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)}`);
Bottom line is that I cannot find the actual location of the database file ('label.db') anywhere on my harddrive, and it certainly is not at the expected location ('C:\Users\\AppData\Roaming\Electron\MyApp'). What am I missing? Where does the database file get stored?
One final note: I found a similar question answered: Where is NEDB file stored?. The answer was that the file is kept in memory because of using the webpack dev server. If that is the case, I have a problem. I am using the dev server as well, but I still need to be able to see the file while under development. The file showing up only in production is just not good enough. (I have not tried to run the code in production mode, to see whether the data file appears, so I cannot confirm that is indeed true.)