Force NeDB to create local file with Electron

2k Views Asked by At

I am using NeDB in my Electron Application with React.js to store some tasks and projects persistently. I am initializing two DataStores in a file called Database.js.

this.taskCollection = new Datastore({
        filename:'./tasks.json',
        autoload: true,
        timestampData: true,
    });

    this.projectCollection = new Datastore({
        filename:'./projects.json',
        autoload: true,
        timestampData: true,
    });

Then I am importing the file in my React-App. This happens in the renderer process of Electron. I used the filename property to force NeDB to create a two local file called tasks.json and projects.json. Assuming the docs of NeDB this should create the two mentioned files in the current directory, but they are not created. NeDB is only creating IndexedDB datastores and I really do not know why this is the case. Does anyone have a suggestion why this is the case ?

Thanks in advance :)

Edit

When I create the datastores in the main process the files are created. Could it be, that I do not have access to the file System in the renderer process?

3

There are 3 best solutions below

2
On BEST ANSWER

I'm pretty new to Electron too, but as far as I gather from the docs & tutorials, interactions with your database should be done in the main process. You should then use ipc or remote to do the communication between your renderer process and your main. That's how I have it set up anyway.

I use ipc to send events with queries etc from my renderer to my main and then send the results back in the same way. That also allows you to listen for the same data change in multiple places in your app. (eg when i remove the 'favourite' status from a contact, that component gets updated but i can also listen for the update in my favourite list at the same time to reload that so it's up to date)

0
On

I did it by creating the datastore in the main.js and share it as a global object. So I can use it in the renderer process but it's still creating a json file as store instead of a indexedDb or so.

0
On

If using vue-cli3, add this webpack option in vue.config.js :

configureWebpack: {
  target: 'electron-main'
}

Then a local file will be created instead of using the browser's indexdb.

See : https://webpack.js.org/configuration/target/