json-settings v1.2.0
#Json-Settings Settings was something I threw together quickly for a project I'm working on. I was unable to quickly find anything that was file-based and allowed for auto flushing/reloading. The key word being quickly, there may be other projects out there much better than this one. But for now, this one works for my needs.
#Usage
var settings = require("json-settings");
var main = new settings("./settings.json");
main.set("oauth", "s02m569hrro2gxxt65w5f5qa2x07d6");
main.get("oauth"); //s02m569hrro2gxxt65w5f5qa2x07d6#API
####Constructor(string, object)
The constructor's signature is function(string, object). The string is the relative path to the json file. The object is an options object, which consists of the following defaults:
- skipReload (
boolean; default:false)- Settings won't be reloaded from the constructor. You'll have to reload them manually.
- reloadSync (
boolean; default:true)- Settings will call
reloadSyncfrom the constructor (instead of regular reload)
- Settings will call
- cb (
function)- The callback for the async reload call if
reloadSyncandskipReloadare both false.
- The callback for the async reload call if
var settings = require("json-settings");
var main = new settings("./settings.json");####reload(callback) Reload is asynchronous. It uses fs.readFile to grab the contents of the settings file and run JSON.parse on it. It then stores the parsed json in the raw_data object.
Reload's signature is function(callback) where the callback has the signature of function(error).
var settings = require("json-settings");
var main = new settings("./settings.json");
main.reload(function(err) {
if (!err) {
console.error(err);
} else {
console.log("Settings reloaded");
}
});####reloadSync()
reloadSync is a synchronous version of reload. No errors are thrown, if there's an exception we reset the raw_data and swallow the exception.
var settings = require("json-settings");
var main = new settings("./settings.json");
main.reloadSync();
console.log("Reloaded!");####save(callback) save will flush the json encoded raw_data object to the file.
var settings = require("json-settings");
var main = new settings("./settings.json");
main.save(function(err) {
if (!err) {
console.log("Settings saved!");
} else {
console.error(err);
}
});####saveSync()
saveSync is the synchronous version of save
var settings = require("json-settings");
var main = new settings("./settings.json");
settings.saveSync();####get(string, string)
get's signature is get(setting_name, default_value)
get will return the value associated with the name provided.
var settings = require("json-settings");
var main = new settings("./settings.json");
console.log(main.get("oauth")); //assuming oauth exists and is set to 's02m569hrro2gxxt65w5f5qa2x07d6', 's02m569hrro2gxxt65w5f5qa2x07d6' will be printed to the console.####set(string, mixed, object)
set's signature is: set(setting_name, setting_value, options)
The options are as follows:
- saveSync (
boolean; default: true)- Will save synchronously if true
- save (
boolean; default: false)- If
saveSyncis false and this is true, we'll save asynchronously
- If
- cb (
function)- If
saveSyncis false andsaveis true, we'll set this function as the callback forsave
- If
####setAll(object, object)
setAll's signature is: setAll(object_containing_keys_and_values, options
The object_containing_keys_and_values is something along the lines of
{
oauth: "abcdefghijklmnop",
secret: "qrstuvwxyz"
}The options here are the same from set above
var settings = require("json-settings");
var main = new settings("./settings.json");
main.setAll({
oauth: "abcdefghijklmnop",
secret: "qrstuvwxyz"
});This will set oauth to abcdefghijklmnop and secret to qrstuvwxyz in the flatfile and save according to your options.
####getAll(array)
getAll's signature is: getAll(array_of_keys_to_get) or getAll(array_of_objects_to_get)
The object array looks like:
[{
key: "setting_name",
defaultValue: "default setting value"
}]var settings = require("json-settings");
var main = new settings("./settings.json");
console.log(main.getAll(["oauth", "secret", "non_existant_key"])); //{oauth: "abcdefghijklmnop", secret: "qrstuvwxyz", non_existant_key: undefined}