Saving everyday scripts data

What would you consider the best way of saving data - variables - for your scripts to get and edit ? I am currently using a somewhat dashed two-file setup, (within the app in itself to make it more practical) and I was was wondering if I missed an easy way to do it

Both files are on the scriptable folder, “Data.js” storing the data and “DataHandler.js” moving the data around.
Here is DataHandler.js’s code.


module.exports.set = function (uuid, obj) {
  var f = FileManager.iCloud();
  var dir = f.documentsDirectory();
  var d = f.read("/private/var/mobile/Library/Mobile Documents/iCloud~dk~simonbs~Scriptable/Documents/Data.js");
  d = d.toRawString();
  var p0 = d.indexOf(uuid);
  if (p0 == -1) {
//  Declare new value.
    d += uuid + "#" + JSON.stringify(obj) + "#";
  } else {
    var p1 = d.indexOf("#", p0 + uuid.length);
    var p2 = d.indexOf("#", p1 + 1);
    if (p1 == -1 || p2 == -1) {
      console.error("The requested item could not be modified. Reason: bad formatting.");
      return false;
    }
    d = d.substring(0, p1 + 1) + JSON.stringify(obj) + d.substring(p2, d.length);
  }
  f.write("/private/var/mobile/Library/Mobile Documents/iCloud~dk~simonbs~Scriptable/Documents/Data.js",Data.fromString(d));
  return uuid;
};
module.exports.get = function (uuid) {
  var f = FileManager.iCloud();
  var dir = f.documentsDirectory();
  var d = f.read("/private/var/mobile/Library/Mobile Documents/iCloud~dk~simonbs~Scriptable/Documents/Data.js");
  d = d.toRawString();
  var p0 = d.indexOf(uuid);
  if (p0 == -1) {
    console.error(uuid + " could not be retrieved. Reason: uuid not found.");
    return undefined;
  }
  var p1 = d.indexOf("#", p0 + uuid.length);
  var p2 = d.indexOf("#", p1 + 1);
  if (p1 == -1 || p2 == -1) {
    console.error(uuid + " could not be retrieved. Reason: bad formatting.");
    return undefined;
  }
  var r = d.substring(p1 + 1, p2);
  return JSON.parse(r);
};