1

I'm developing Vue-Electron with NeDB. On using NeDB, I've encountered problem that NeDB don't save local file though I set option filename and autoload: true.

I tried output log of db object when load NeDB, it correct path set.

Datastore {inMemoryOnly: false,  
  autoload: true,  
  timestampData: false,  
  filename: "./db/nedb.db", 
  compareStrings: undefined, …}  
  autoload: (...)compareStrings: (...)  
  executor: Executorfilename: "./db/nedb.db"  
  inMemoryOnly: falseindexes: Objectpersistence: PersistencetimestampData: ...

I saw other posts. but I couldn't figure out how to do Then in the renderer process get the datastore via Electron.Remote NEDB persistance in Electron app

I thought may it happen because NeDB needs file exist before run script. So I tried touch nedb.db but that didn't work.

Moreover, other odd thing: I have another Vue Application that is using NeDB, and the data showed. But I don't set a db path in that application. Inserted data by this application doesn't exist in the other application's db file.

Below is my code. If Someone could help me. Thanks.

const remote = require('electron').remote;
const app = remote.app;
const path = require('path');
var db = new nedb({
        //filename: path.join(app.getPath('userData'), 'library.db'),
    filename: './db/nedb.db',
    autoload: true
});
let doc = {
    dev: true,
    message: 'test'
}
db.insert(doc);

db.find({}, function (err, docs) {
    console.log(docs)
    console.log(err)
})
Panda
  • 6,955
  • 6
  • 40
  • 55
John Smith
  • 11
  • 1
  • 7
  • What's the output of `path.join(app.getPath('userData'), 'library.db')` . Please try – Kashif Siddiqui Oct 20 '18 at 08:24
  • Also instead of `const path = require('path');` try `const path = remote.require('path');` – Kashif Siddiqui Oct 20 '18 at 08:25
  • thanks. I tried running script after change `const path = remote.require('path')` and filename set `path.join(app.getPath('userData'), 'library.db')` . DB Object shows filename `/Users/***/Library/Application Support/Electron/library.db` but there is not such a file.... – John Smith Oct 20 '18 at 17:48
  • I think after using `const path = remote.require('path');` your NeDB should work. – Kashif Siddiqui Oct 21 '18 at 00:25

1 Answers1

1

NeDB will default to browser storage (IndexedDB) if called from the renderer process. If created in the main process it will create the file. See this post on github.

From the post:

Nedb let's you create a new auto-loading Datastore using this call:

let db = new Datastore({ filename: 'path/to/datafile', autoload: true });

It appears, however, that this command is only accurate when performed from the main process (for new Electron developers, this is usually your main.ts or main.js file).

If you perform the Datastore creation command from a class which is executed in the renderer process (any class which is executed in the BrowserWindow), nedb ignores the datafile you provided and creates your database in IndexedDB instead. You'll find your database in your application's "userData" directory (see documentation for your os) If you really want nedb to create and use the database file that you provide during Datastore creation, you must create AND access the data file (add, remove,... documents) from the main process.

  1. Creating the data file from the main process (in main.ts):

let collectionsDb: Datastore = new Datastore({ filename: path.join(app.getPath("userData"), "Collections.db"), autoload: true });

  1. Putting the db variable in a global variable in the main process (in main.ts):

const globalAny:any = global;

globalAny.collectionsDb = collectionsDb;

Accessing the global db variable from the renderer process by calling the global db variable: import { remote } from 'electron';

private db = remote.getGlobal('collectionDb');

See also this answer to the question referenced by the OP.

bjbk
  • 388
  • 6
  • 16