objfile
v0.1.2
Published
Read from + write to + update INI and JSON files via a simple asynchronous API
Downloads
3
Maintainers
Readme
objfile
Read from + write to + update INI and JSON files via a simple asynchronous API.
Install for your app
cd /path/to/your/app
npm install objfile --save
Usage
Example
var objfile = require('objfile');
var my_file = objfile('/path/to/my/file.ini'); // auto-identified as INI file
// read
my_file.get('my', 'hierarchy', 'in', 'the', 'object', function (err, value) {
if (err) {
console.error(err);
} else {
console.log('Value:', value);
}
});
// write
my_file.set('some', 'other', 'path', 'new_value', function (err) {
if (err) {
console.error(err);
} else {
console.log('Value set');
}
});
API
// factory: returns instantiated IniFile or JsonFile
objfile(path[, options][, callback]);
// INI file
new objfile.IniFile(path[, options][, callback]);
// JSON file
new objfile.JsonFile(path[, options][, callback]);
Argument: path
- type: string
- value: /path/to/data/file
- required
Argument: options
- type: object
- possible keys/values:
- type: 'ini' or 'json' forces a type, no path-parsing (factory only)
- dataRoot: data hierarchy root for get/set/del methods (default: top-level)
- optional
Argument: callback
- type: function
- call: function (err) { /* err has value if file is not readable */ }
- optional
Return value: IniFile instance or JsonFile instance
Read (get)
my_file.get(key1[, key2, ...], callback);
Argument: key (any number)
- type: string or number
- value: key on object
- required
Argument: callback
- type: function
- call: function (err, value) { /* err if key-path does not exist */ }
- required
Return value: undefined
Write (set)
my_file.get(key1[, key2, ...], value, callback);
Argument: key (any number)
- type: string or number
- value: key on object
- required
Argument: value (required, any type)
Argument: callback
- type: function
- call: function (err) { /* err if sync fails */ }
- optional
Return value: undefined
Delete
my_file.del(key1[, key2, ...], callback);
Argument: key (any number)
- type: string or number
- value: key on object
- required
Argument: callback
- type: function
- call: function (err) { /* err if sync fails or key-path does not exist */ }
- optional
Return value: undefined