4

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.)

Tibi
  • 439
  • 7
  • 15
  • Have you followed this thread: https://github.com/louischatriot/nedb/issues/531 ? – Matthew Moran Dec 28 '19 at 04:50
  • 1
    Mathew, thank you for the link. I followed the thread and indeed it made everything clear. NeDB in the render process makes use of the browser version, which means no access to the local file system. I moved access to NeDB in the main process, and voila, the file showed up in my file system. This is because the main process uses the node version of NeDB. I will have to use IPC to transfer data between the main and reder processes, but that's fine, it will force me to create a data access layer, which will be useful if I ever convert my app to a web app. – Tibi Dec 30 '19 at 01:11
  • I used NeDB quite a bit for a project a while back. If I were to do it again, I'd use pouch/couch db. Might be something to consider. – Matthew Moran Dec 30 '19 at 02:07
  • I was not aware of pouch/couch. Reading the docs, I can see how I could use it. Seems more robust than NeDB, which is a quick and simple NoSQL implementation suitable for small amounts of data. Thank you for the hint. – Tibi Jan 02 '20 at 05:48
  • @Tibi no, i still don't see the data file created even in main process (node.js, not browser js) – Dee Jan 03 '21 at 15:24

0 Answers0