2

In my web app, which is based on Electron, I use IndexedDB. If I copy the entire project over to a separate directory and run it while the original copy is running, the IndexedDB API complains that it cannot open the database. Only if I close the first app will the second app be able to run without problems.

This raises the question as to how IndexedDB treats files. Even if I give each database their own name, the problem still exists. Electron uses the file protocol for accessing urls. How does IndexedDB handle normal web apps that don't use the file protocol but rather http/https? Clearly each app has its own database and can access it without conflicts with other databases. So why does a file based web app behave differently? Is there anything I can do to make both copies of my app work with separate indexedDB databases?

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Johann
  • 27,536
  • 39
  • 165
  • 279

4 Answers4

0

This thread mentions a possible solution for HTML5 localStorage:

BTW, the way I was able to launch 2 instances of the application where each loads different settings stored in localStorage, was to launch each with a different domain, e.g. a different port on localhost - thus each receives a different localStorage.

This may be the way to go for IndexedDB too...

0

The proper solution for Electron is to use:

setPath("userData", [some path]);

This needs to be run in the main process and should be one of the very first things done before any windows are created or anything else is done for that matter. See:

https://github.com/electron/electron/blob/master/docs/api/app.md

Any files or cached data will get stored under the path you define with setPath.

Johann
  • 27,536
  • 39
  • 165
  • 279
0

This issue does not happen when you run your application in a browser. The issue is with Electron, not with the browser. Also, it is possible to run two different Exlectorn apps simultaneously, as long as they use a different IndexedDB database name.

Running two Electron apps simultaneously against the same IndexedDB is not possible, as the IndexedDb will be locked by the first app/instance.

From the documentation, it seems this can be circumvented by calling "app.setPath(name, path)" when initializing the app. However, this results in two different IndexedDB databases, containing different data. Also, this will result in the LocalStorage begin duplicated. If you only store your data temporarily, then this solution may work for you.

0

I found a solution,you need use attribute partition in webPerferences,eg:

  const mainWindow = new BrowserWindow({
    width: 800,
    height: 600,
    webPreferences: {
      preload: path.join(__dirname, 'preload.js'),
      partition: `persist:${new Date().getTime()}`
    }
  })
Max
  • 21
  • 1
  • 8