1.1.1 • Published 5 years ago
@sub0709/json-config v1.1.1
json-config
control json configuration file
Installation
npm install @sub0709/json-config
API
load
const Configure = require('@sub0709/json-config');
let conf = Configure.load('test-setting.json');
test-setting.json sample
{
"dbInfo": {
"db": {
"host": "localhost",
"port": 3781,
"dbName": "testapp"
},
"option": {
"order": "asc",
"count": 1
}
}
}
load option
This is same let conf = Configure.load('test-setting.json');
let conf = Configure.load('test-setting.json', {
forceSet : false,
ignoreDataType : false,
immediateFileSave : false
});
get
console.log(conf.get('dbInfo.db.host')); // localhost
console.log(conf.dbInfo.db.host); // localhost
set
conf.set('dbInfo.db.host', 'local.app.com');
console.log(conf.dbInfo.db.host); // local.app.com
conf.set('dbInfo.db.abc', false); // Error: abc is not exist
if forceSet is true
conf.set('dbInfo.db.abc', false); // ok, dbInfo.db.abc maked
console.log(conf.dbInfo.db.abc); // false
Data type of json value have to same.
conf.set('dbInfo.db.dbName', 999); // Error: data type is different
if ignoreDataType is true
conf.set('dbInfo.db.dbName', 999); // ok, dbInfo.db.dbName value changed to number
console.log(conf.dbInfo.db.dbName); // 999
save
all json value save to file.
conf.save();
If immediateFileSave is true, save() don't need. When called set(), immediate save to file.
store
store() is overwrite json value.
conf.store({aaa : 1});
console.log(conf.aaa); // 1
console.log(conf.dbInfo); // error, dbInfo is not exist