Home > Software design >  Saving app data to a predetermined path with Angular in Electron
Saving app data to a predetermined path with Angular in Electron

Time:07-28

I am trying to find a way to save files to AppData for persistent storage of application data, and I can't seem to find a good method. Mainly, I've tried using fs and Electron's session object (in an attempt to store the data as cookie and save it in AppData on app close, but I get a "Cannot destructure property 'session' of 'electron' as it is undefined" error) in addition to a handful of libraries and other suggestions from similar posts. Any help on this would be appreciated!

CodePudding user response:

Cookies have maximum size of 4096 bytes. Alternatives could be LocalStroage or SessionStorage. As the name says, SessionStorage is cleared after each session, but LocalStorage persists until you clear cache.

https://stackoverflow.com/a/55090255/15439733

CodePudding user response:

There are a number of ways you can persist data to the filesystem with an Electron app. If you simply want to persist some JSON there's an excellent library electron-json-storage that will allow you to easily save data as JSON. You can also use Electron APIs to easily get cross-platform paths to common directories such as appData, desktop, documents, etc., using the app.getPath(name) API.

In terms of implementing this, you should only use these APIs in the Electron main process. It's generally not a good idea to directly call Nodejs and Electron APIs from within your renderer process, which is where Angular (or whatever framework you decide to use) runs. You can invoke those APIs by using Electron's IPC, or Inter-Process Communication. That is most likely what you're referring to when you say that electron is undefined, because in most instances electron is not available from the renderer process unless you use some method to set it up that way. But again, this is not encouraged.

In general I would recommend you learn more about Electron's process model including context isolation and IPC. Those principles are critical for any electron app, independent of the framework you're using in the renderer process.

  • Related