nathanbuchar / electron-settings

šŸ“ A simple persistent user settings framework for Electron.
https://electron-settings.js.org
MIT License
817 stars 60 forks source link

Multiple settings files? #92

Open stavlocker opened 7 years ago

stavlocker commented 7 years ago

Hi. Great project! Could it be possible for us to use multiple settings files? It'd make this project ideal for me. Thanks in advance!

nathanbuchar commented 7 years ago

Hi @stavlocker,

I'm afraid this capability does not currently exist and there are no current plans to implement it in the near future. So that I can get a sense of how a feature like this would be used, may I ask how multiple settings files would be ideal for your project and how having one does not work for you?

Thanks :)

stavlocker commented 7 years ago

@nathanbuchar Well, an example might be saving data, and not only settings - I could have my config.json file to manage the configuration, and my cupcakes.json to save all the cupcakes a user made on my app (or any other less stupid example). It would be excellent for saving data that I may not want in my config files (sort of a mini-database if you want to think of it that way).

nathanbuchar commented 7 years ago

I can certainly understand wanting to split data into their own files, but with electron-settings you could just scope settings within their own key or namespace, like so:

settings.set('config', {
  isLoggedIn: true,
  username: 'nathanbuchar'
});

settings.set('cupcakes', [
  'Red Velvet',
  'Vanilla',
  'Chocolate'
]);

settings.get('config.isLoggedIn'); // true
settings.get('cupcakes')[0]; // 'Red Velvet'

If you're still keen on having different files for each config, check out electron-json-storage. There, each key is its own JSON fileā€”there's no concept of "key paths". It's kind of like a re-implementation of LocalStorage, where a JSON file is created for each key.

EmadAdly commented 7 years ago

@nathanbuchar Maybe an unexpected error occurred while write, It may cause damage for file, also there are many possible causes for data damage. so which will cause all data to be lost. do not put your eggs in one basket So I think their separation will be logical in this case.

wilburforce83 commented 5 years ago

I know this is super old, but if anyone else needs this, I have gone for creating a backup after and before critical changes are made in the application. I then have a simple function which looks for setting on startup, if they are not there, then copy this file in place of setting and voila, setting are back :) :

function postTradeBackup() {

     var fileName;

     var location = settings.file() //.slice(0, -8) for folder location
     var content;

     fs.readFile(location, 'utf-8', (err, data) => {
         if (err) {
             console.log("An error ocurred reading the file :" + err.message);
             return;
         }

         // console.log("The file content is : " + data);
         fileName = location + '1'; // could be .bkp or whatever
         content = data;
         //  console.log(fileName);
         // fileName is a string that contains the path and filename created in the save file dialog.  
         fs.writeFile(fileName, content, (err) => {
             if (err) {
                 console.log("An error ocurred creating the file " + err.message)
                 return;
             }

             //     console.log("Settings has been succesfully backed up");
         });
     });
 }