3

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?

grahan
  • 2,148
  • 5
  • 29
  • 43
  • Try using absolute paths for the filenames (e.g. `path.join(app.getPath('userData'), 'tasks.json')`), relative paths will likely break anyway when the app is packaged. – Vadim Macagon Dec 26 '16 at 03:58
  • The problem is, currently I am using the DB in the renderer process, and `app` is only available in the main process. – grahan Dec 26 '16 at 10:20
  • Then just do `const {remote} = require('electron'); remote.app.getPath('userData')` – Vadim Macagon Dec 26 '16 at 10:24
  • Thanks for the hint. I tried it, but this will also just create an IndexedDB key with the path created with `path.join()`. I think, I do not have filesystem access from my React App inside Electron.. – grahan Dec 26 '16 at 10:28

3 Answers3

3

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)

LRN
  • 191
  • 1
  • 7
  • I also read about this, you are right. Do you think I should consider this and redesign my app, so the main process is handling all database related tasks ? Is this the better design ? – grahan Dec 25 '16 at 19:40
  • 1
    You can access the database in the renderer if it makes sense for your app architecture (i.e. single-window app), Electron doesn't care. – Vadim Macagon Dec 26 '16 at 03:37
1

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.

dorian
  • 51
  • 4
0

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/

JeffProd
  • 3,088
  • 1
  • 19
  • 38