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 = ;var main = "./settings.json";main;main; //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
reloadSync
from the constructor (instead of regular reload)
- Settings will call
- cb (
function
)- The callback for the async reload call if
reloadSync
andskipReload
are both false.
- The callback for the async reload call if
var settings = ;var main = "./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 = ;var main = "./settings.json";main;
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 = ;var main = "./settings.json";main;console;
save(callback)
save will flush the json encoded raw_data object to the file.
var settings = ;var main = "./settings.json";main;
saveSync()
saveSync is the synchronous version of save
var settings = ;var main = "./settings.json";settings;
get(string, string)
get
's signature is get(setting_name, default_value)
get
will return the value associated with the name provided.
var settings = ;var main = "./settings.json";console; //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
saveSync
is false and this is true, we'll save asynchronously
- If
- cb (
function
)- If
saveSync
is false andsave
is 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 = ;var main = "./settings.json";main;
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 = ;var main = "./settings.json";console; //{oauth: "abcdefghijklmnop", secret: "qrstuvwxyz", non_existant_key: undefined}